xref: /titanic_44/usr/src/uts/common/io/ppp/spppcomp/zlib.c (revision 843e19887f64dde75055cf8842fc4db2171eff45)
17c478bd9Sstevel@tonic-gate /*
2*c9431fa1Sahl  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate /*
77c478bd9Sstevel@tonic-gate  * Updated from zlib-1.0.4 to zlib-1.1.3 by James Carlson.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * This file is derived from various .h and .c files from the zlib-1.0.4
107c478bd9Sstevel@tonic-gate  * distribution by Jean-loup Gailly and Mark Adler, with some additions
117c478bd9Sstevel@tonic-gate  * by Paul Mackerras to aid in implementing Deflate compression and
127c478bd9Sstevel@tonic-gate  * decompression for PPP packets.  See zlib.h for conditions of
137c478bd9Sstevel@tonic-gate  * distribution and use.
147c478bd9Sstevel@tonic-gate  *
157c478bd9Sstevel@tonic-gate  * Changes that have been made include:
167c478bd9Sstevel@tonic-gate  * - added Z_PACKET_FLUSH (see zlib.h for details)
177c478bd9Sstevel@tonic-gate  * - added inflateIncomp and deflateOutputPending
187c478bd9Sstevel@tonic-gate  * - allow strm->next_out to be NULL, meaning discard the output
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * $Id: zlib.c,v 1.11 1998/09/13 23:37:12 paulus Exp $
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate 
237c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate /*
267c478bd9Sstevel@tonic-gate  *  ==FILEVERSION 971210==
277c478bd9Sstevel@tonic-gate  *
287c478bd9Sstevel@tonic-gate  * This marker is used by the Linux installation script to determine
297c478bd9Sstevel@tonic-gate  * whether an up-to-date version of this file is already installed.
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #define	NO_DUMMY_DECL
337c478bd9Sstevel@tonic-gate #define	NO_ZCFUNCS
347c478bd9Sstevel@tonic-gate #define	MY_ZCALLOC
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #if defined(__FreeBSD__) && (defined(KERNEL) || defined(_KERNEL))
377c478bd9Sstevel@tonic-gate #define	inflate	inflate_ppp	/* FreeBSD already has an inflate :-( */
387c478bd9Sstevel@tonic-gate #endif
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /* +++ zutil.h */
427c478bd9Sstevel@tonic-gate /*
437c478bd9Sstevel@tonic-gate  *
447c478bd9Sstevel@tonic-gate  * zutil.h -- internal interface and configuration of the compression library
457c478bd9Sstevel@tonic-gate  * Copyright (C) 1995-1998 Jean-loup Gailly.
467c478bd9Sstevel@tonic-gate  * For conditions of distribution and use, see copyright notice in zlib.h
477c478bd9Sstevel@tonic-gate  */
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate /*
507c478bd9Sstevel@tonic-gate  * WARNING: this file should *not* be used by applications. It is part
517c478bd9Sstevel@tonic-gate  * of the implementation of the compression library and is subject to
527c478bd9Sstevel@tonic-gate  * change. Applications should only use zlib.h.
537c478bd9Sstevel@tonic-gate  */
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate /* From: zutil.h,v 1.16 1996/07/24 13:41:13 me Exp $ */
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #ifndef _Z_UTIL_H
587c478bd9Sstevel@tonic-gate #define	_Z_UTIL_H
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate #include "zlib.h"
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate #if defined(KERNEL) || defined(_KERNEL)
637c478bd9Sstevel@tonic-gate /* Assume this is a *BSD or SVR4 kernel */
647c478bd9Sstevel@tonic-gate #include <sys/types.h>
657c478bd9Sstevel@tonic-gate #include <sys/time.h>
667c478bd9Sstevel@tonic-gate #include <sys/systm.h>
677c478bd9Sstevel@tonic-gate #ifdef SOL2
687c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
697c478bd9Sstevel@tonic-gate #endif
707c478bd9Sstevel@tonic-gate #define	HAVE_MEMCPY
717c478bd9Sstevel@tonic-gate #define	memcmp		bcmp
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate #else
747c478bd9Sstevel@tonic-gate #if defined(__KERNEL__)
757c478bd9Sstevel@tonic-gate /* Assume this is a Linux kernel */
767c478bd9Sstevel@tonic-gate #include <linux/string.h>
777c478bd9Sstevel@tonic-gate #define	HAVE_MEMCPY
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate #else /* not kernel */
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate #include <stddef.h>
827c478bd9Sstevel@tonic-gate #ifdef NO_ERRNO_H
837c478bd9Sstevel@tonic-gate extern int errno;
847c478bd9Sstevel@tonic-gate #else
857c478bd9Sstevel@tonic-gate #include <errno.h>
867c478bd9Sstevel@tonic-gate #endif
877c478bd9Sstevel@tonic-gate #ifdef STDC
887c478bd9Sstevel@tonic-gate #include <string.h>
897c478bd9Sstevel@tonic-gate #include <stdlib.h>
907c478bd9Sstevel@tonic-gate #endif
917c478bd9Sstevel@tonic-gate #endif /* __KERNEL__ */
927c478bd9Sstevel@tonic-gate #endif /* _KERNEL || KERNEL */
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate #ifndef local
957c478bd9Sstevel@tonic-gate #define	local static
967c478bd9Sstevel@tonic-gate #endif
977c478bd9Sstevel@tonic-gate /* compile with -Dlocal if your debugger can't find static symbols */
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate typedef unsigned char  uch;
1007c478bd9Sstevel@tonic-gate typedef uch FAR uchf;
1017c478bd9Sstevel@tonic-gate typedef unsigned short ush;
1027c478bd9Sstevel@tonic-gate typedef ush FAR ushf;
1037c478bd9Sstevel@tonic-gate typedef unsigned long  ulg;
1047c478bd9Sstevel@tonic-gate 
105*c9431fa1Sahl static const char *z_errmsg[10]; /* indexed by 2-zlib_error */
1067c478bd9Sstevel@tonic-gate /* (size given to avoid silly warnings with Visual C++) */
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate #define	ERR_MSG(err) z_errmsg[Z_NEED_DICT-(err)]
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate #define	ERR_RETURN(strm, err) \
1117c478bd9Sstevel@tonic-gate 	return (strm->msg = ERR_MSG(err), (err))
1127c478bd9Sstevel@tonic-gate /* To be used only when the state is known to be valid */
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	/* common constants */
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate #ifndef DEF_WBITS
1177c478bd9Sstevel@tonic-gate #define	DEF_WBITS MAX_WBITS
1187c478bd9Sstevel@tonic-gate #endif
1197c478bd9Sstevel@tonic-gate /* default windowBits for decompression. MAX_WBITS is for compression only */
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate #if MAX_MEM_LEVEL >= 8
1227c478bd9Sstevel@tonic-gate #define	DEF_MEM_LEVEL 8
1237c478bd9Sstevel@tonic-gate #else
1247c478bd9Sstevel@tonic-gate #define	DEF_MEM_LEVEL  MAX_MEM_LEVEL
1257c478bd9Sstevel@tonic-gate #endif
1267c478bd9Sstevel@tonic-gate /* default memLevel */
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate #define	STORED_BLOCK 0
1297c478bd9Sstevel@tonic-gate #define	STATIC_TREES 1
1307c478bd9Sstevel@tonic-gate #define	DYN_TREES    2
1317c478bd9Sstevel@tonic-gate /* The three kinds of block type */
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate #define	MIN_MATCH  3
1347c478bd9Sstevel@tonic-gate #define	MAX_MATCH  258
1357c478bd9Sstevel@tonic-gate /* The minimum and maximum match lengths */
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate #define	PRESET_DICT 0x20 /* preset dictionary flag in zlib header */
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	/* target dependencies */
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate #ifdef MSDOS
1427c478bd9Sstevel@tonic-gate #define	OS_CODE  0x00
1437c478bd9Sstevel@tonic-gate #ifdef __TURBOC__
1447c478bd9Sstevel@tonic-gate #include <alloc.h>
1457c478bd9Sstevel@tonic-gate #else /* MSC or DJGPP */
1467c478bd9Sstevel@tonic-gate #include <malloc.h>
1477c478bd9Sstevel@tonic-gate #endif
1487c478bd9Sstevel@tonic-gate #endif
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate #ifdef OS2
1517c478bd9Sstevel@tonic-gate #define	OS_CODE  0x06
1527c478bd9Sstevel@tonic-gate #endif
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate #ifdef WIN32 /* Window 95 & Windows NT */
1557c478bd9Sstevel@tonic-gate #define	OS_CODE  0x0b
1567c478bd9Sstevel@tonic-gate #endif
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate #if defined(VAXC) || defined(VMS)
1597c478bd9Sstevel@tonic-gate #define	OS_CODE  0x02
1607c478bd9Sstevel@tonic-gate #define	F_OPEN(name, mode) \
1617c478bd9Sstevel@tonic-gate 	fopen((name), (mode), "mbc=60", "ctx=stm", "rfm=fix", "mrs=512")
1627c478bd9Sstevel@tonic-gate #endif
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate #ifdef AMIGA
1657c478bd9Sstevel@tonic-gate #define	OS_CODE  0x01
1667c478bd9Sstevel@tonic-gate #endif
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate #if defined(ATARI) || defined(atarist)
1697c478bd9Sstevel@tonic-gate #define	OS_CODE  0x05
1707c478bd9Sstevel@tonic-gate #endif
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate #ifdef MACOS
1737c478bd9Sstevel@tonic-gate #define	OS_CODE  0x07
1747c478bd9Sstevel@tonic-gate #endif
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate #ifdef __50SERIES /* Prime/PRIMOS */
1777c478bd9Sstevel@tonic-gate #define	OS_CODE  0x0F
1787c478bd9Sstevel@tonic-gate #endif
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate #ifdef TOPS20
1817c478bd9Sstevel@tonic-gate #define	OS_CODE  0x0a
1827c478bd9Sstevel@tonic-gate #endif
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate #if defined(_BEOS_) || defined(RISCOS)
1857c478bd9Sstevel@tonic-gate #define	fdopen(fd, mode) NULL /* No fdopen() */
1867c478bd9Sstevel@tonic-gate #endif
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	/* Common defaults */
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate #ifndef OS_CODE
1917c478bd9Sstevel@tonic-gate #define	OS_CODE  0x03  /* assume Unix */
1927c478bd9Sstevel@tonic-gate #endif
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate #ifndef F_OPEN
1957c478bd9Sstevel@tonic-gate #define	F_OPEN(name, mode) fopen((name), (mode))
1967c478bd9Sstevel@tonic-gate #endif
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	/* functions */
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate #ifdef HAVE_STRERROR
2017c478bd9Sstevel@tonic-gate extern char *strerror OF((int));
2027c478bd9Sstevel@tonic-gate #define	zstrerror(errnum) strerror(errnum)
2037c478bd9Sstevel@tonic-gate #else
2047c478bd9Sstevel@tonic-gate #define	zstrerror(errnum) ""
2057c478bd9Sstevel@tonic-gate #endif
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate #if defined(pyr)
2087c478bd9Sstevel@tonic-gate #define	NO_MEMCPY
2097c478bd9Sstevel@tonic-gate #endif
2107c478bd9Sstevel@tonic-gate #if (defined(M_I86SM) || defined(M_I86MM)) && !defined(_MSC_VER)
2117c478bd9Sstevel@tonic-gate /*
2127c478bd9Sstevel@tonic-gate  * Use our own functions for small and medium model with MSC <= 5.0.
2137c478bd9Sstevel@tonic-gate  * You may have to use the same strategy for Borland C (untested).
2147c478bd9Sstevel@tonic-gate  */
2157c478bd9Sstevel@tonic-gate #define	NO_MEMCPY
2167c478bd9Sstevel@tonic-gate #endif
2177c478bd9Sstevel@tonic-gate #if defined(STDC) && !defined(HAVE_MEMCPY) && !defined(NO_MEMCPY)
2187c478bd9Sstevel@tonic-gate #define	HAVE_MEMCPY
2197c478bd9Sstevel@tonic-gate #endif
2207c478bd9Sstevel@tonic-gate #ifdef HAVE_MEMCPY
2217c478bd9Sstevel@tonic-gate #ifdef SMALL_MEDIUM /* MSDOS small or medium model */
2227c478bd9Sstevel@tonic-gate #define	zmemcpy _fmemcpy
2237c478bd9Sstevel@tonic-gate #define	zmemcmp _fmemcmp
2247c478bd9Sstevel@tonic-gate #define	zmemzero(dest, len) _fmemset(dest, 0, len)
2257c478bd9Sstevel@tonic-gate #else
2267c478bd9Sstevel@tonic-gate #define	zmemcpy (void) memcpy
2277c478bd9Sstevel@tonic-gate #define	zmemcmp memcmp
2287c478bd9Sstevel@tonic-gate #define	zmemzero(dest, len) (void) memset(dest, 0, len)
2297c478bd9Sstevel@tonic-gate #endif
2307c478bd9Sstevel@tonic-gate #else
2317c478bd9Sstevel@tonic-gate extern void zmemcpy  OF((Bytef* dest, const Bytef* source, uInt len));
2327c478bd9Sstevel@tonic-gate extern int  zmemcmp  OF((const Bytef* s1, const Bytef* s2, uInt len));
2337c478bd9Sstevel@tonic-gate extern void zmemzero OF((Bytef* dest, uInt len));
2347c478bd9Sstevel@tonic-gate #endif
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate /* Diagnostic functions */
2377c478bd9Sstevel@tonic-gate #ifdef DEBUG_ZLIB
2387c478bd9Sstevel@tonic-gate #include <stdio.h>
2397c478bd9Sstevel@tonic-gate #ifndef verbose
2407c478bd9Sstevel@tonic-gate #define	verbose 0
2417c478bd9Sstevel@tonic-gate #endif
2427c478bd9Sstevel@tonic-gate extern void z_error    OF((char *m));
2437c478bd9Sstevel@tonic-gate #define	Assert(cond, msg) { if (!(cond)) z_error(msg); }
2447c478bd9Sstevel@tonic-gate #define	Trace(x) {if (z_verbose >= 0) fprintf x; }
2457c478bd9Sstevel@tonic-gate #define	Tracev(x) {if (z_verbose > 0) fprintf x; }
2467c478bd9Sstevel@tonic-gate #define	Tracevv(x) {if (z_verbose > 1) fprintf x; }
2477c478bd9Sstevel@tonic-gate #define	Tracec(c, x) {if (z_verbose > 0 && (c)) fprintf x; }
2487c478bd9Sstevel@tonic-gate #define	Tracecv(c, x) {if (z_verbose > 1 && (c)) fprintf x; }
2497c478bd9Sstevel@tonic-gate #else
2507c478bd9Sstevel@tonic-gate #if defined(SOL2) && defined(DEBUG)
2517c478bd9Sstevel@tonic-gate #define	Assert(cond, msg)	((cond) ? ((void)0) : panic(msg))
2527c478bd9Sstevel@tonic-gate #else
2537c478bd9Sstevel@tonic-gate #define	Assert(cond, msg)	((void)0)
2547c478bd9Sstevel@tonic-gate #endif
2557c478bd9Sstevel@tonic-gate #define	Trace(x)	((void)0)
2567c478bd9Sstevel@tonic-gate #define	Tracev(x)	((void)0)
2577c478bd9Sstevel@tonic-gate #define	Tracevv(x)	((void)0)
2587c478bd9Sstevel@tonic-gate #define	Tracec(c, x)	((void)0)
2597c478bd9Sstevel@tonic-gate #define	Tracecv(c, x)	((void)0)
2607c478bd9Sstevel@tonic-gate #endif
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate typedef uLong (*check_func) OF((uLong check, const Bytef *buf, uInt len));
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate /* voidpf zcalloc OF((voidpf opaque, unsigned items, unsigned size)); */
2667c478bd9Sstevel@tonic-gate /* void   zcfree  OF((voidpf opaque, voidpf ptr)); */
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate #define	ZALLOC(strm, items, size) \
2697c478bd9Sstevel@tonic-gate 	(*((strm)->zalloc))((strm)->opaque, (items), (size))
2707c478bd9Sstevel@tonic-gate #define	ZFREE(strm, addr)  (*((strm)->zfree))((strm)->opaque, (voidpf)(addr))
2717c478bd9Sstevel@tonic-gate #define	TRY_FREE(s, p) {if (p) ZFREE(s, p); }
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate #endif /* _Z_UTIL_H */
2747c478bd9Sstevel@tonic-gate /* --- zutil.h */
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate /* +++ deflate.h */
2777c478bd9Sstevel@tonic-gate /*
2787c478bd9Sstevel@tonic-gate  * deflate.h -- internal compression state
2797c478bd9Sstevel@tonic-gate  * Copyright (C) 1995-1998 Jean-loup Gailly
2807c478bd9Sstevel@tonic-gate  * For conditions of distribution and use, see copyright notice in zlib.h
2817c478bd9Sstevel@tonic-gate  */
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate /*
2847c478bd9Sstevel@tonic-gate  * WARNING: this file should *not* be used by applications. It is part
2857c478bd9Sstevel@tonic-gate  * of the implementation of the compression library and is subject to
2867c478bd9Sstevel@tonic-gate  * change. Applications should only use zlib.h.
2877c478bd9Sstevel@tonic-gate  */
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate /* From: deflate.h,v 1.10 1996/07/02 12:41:00 me Exp $ */
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate #ifndef _DEFLATE_H
2927c478bd9Sstevel@tonic-gate #define	_DEFLATE_H
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate /* #include "zutil.h" */
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate /*
2977c478bd9Sstevel@tonic-gate  * ===========================================================================
2987c478bd9Sstevel@tonic-gate  * Internal compression state.
2997c478bd9Sstevel@tonic-gate  */
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate #define	LENGTH_CODES 29
3027c478bd9Sstevel@tonic-gate /* number of length codes, not counting the special END_BLOCK code */
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate #define	LITERALS  256
3057c478bd9Sstevel@tonic-gate /* number of literal bytes 0..255 */
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate #define	L_CODES (LITERALS+1+LENGTH_CODES)
3087c478bd9Sstevel@tonic-gate /* number of Literal or Length codes, including the END_BLOCK code */
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate #define	D_CODES   30
3117c478bd9Sstevel@tonic-gate /* number of distance codes */
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate #define	BL_CODES  19
3147c478bd9Sstevel@tonic-gate /* number of codes used to transfer the bit lengths */
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate #define	HEAP_SIZE (2*L_CODES+1)
3177c478bd9Sstevel@tonic-gate /* maximum heap size */
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate #define	MAX_BITS 15
3207c478bd9Sstevel@tonic-gate /* All codes must not exceed MAX_BITS bits */
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate #define	INIT_STATE    42
3237c478bd9Sstevel@tonic-gate #define	BUSY_STATE   113
3247c478bd9Sstevel@tonic-gate #define	FINISH_STATE 666
3257c478bd9Sstevel@tonic-gate /* Stream status */
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate /* Data structure describing a single value and its code string. */
3297c478bd9Sstevel@tonic-gate typedef struct ct_data_s {
3307c478bd9Sstevel@tonic-gate 	union {
3317c478bd9Sstevel@tonic-gate 		ush freq;	/* frequency count */
3327c478bd9Sstevel@tonic-gate 		ush code;	/* bit string */
3337c478bd9Sstevel@tonic-gate 	} fc;
3347c478bd9Sstevel@tonic-gate 	union {
3357c478bd9Sstevel@tonic-gate 		ush dad;	/* father node in Huffman tree */
3367c478bd9Sstevel@tonic-gate 		ush len;	/* length of bit string */
3377c478bd9Sstevel@tonic-gate 	} dl;
3387c478bd9Sstevel@tonic-gate } FAR ct_data;
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate #define	Freq fc.freq
3417c478bd9Sstevel@tonic-gate #define	Code fc.code
3427c478bd9Sstevel@tonic-gate #define	Dad  dl.dad
3437c478bd9Sstevel@tonic-gate #define	Len  dl.len
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate typedef struct static_tree_desc_s  static_tree_desc;
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate typedef struct tree_desc_s {
3487c478bd9Sstevel@tonic-gate 	ct_data *dyn_tree;	/* the dynamic tree */
3497c478bd9Sstevel@tonic-gate 	int	max_code;	/* largest code with non zero frequency */
3507c478bd9Sstevel@tonic-gate 	static_tree_desc *stat_desc;	/* the corresponding static tree */
3517c478bd9Sstevel@tonic-gate } FAR tree_desc;
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate typedef ush Pos;
3547c478bd9Sstevel@tonic-gate typedef Pos FAR Posf;
3557c478bd9Sstevel@tonic-gate typedef unsigned IPos;
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate /*
3587c478bd9Sstevel@tonic-gate  * A Pos is an index in the character window. We use short instead of
3597c478bd9Sstevel@tonic-gate  * int to save space in the various tables. IPos is used only for
3607c478bd9Sstevel@tonic-gate  * parameter passing.
3617c478bd9Sstevel@tonic-gate  */
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate typedef struct deflate_state {
3647c478bd9Sstevel@tonic-gate 	z_streamp strm;	/* pointer back to this zlib stream */
3657c478bd9Sstevel@tonic-gate 	int   status;	/* as the name implies */
3667c478bd9Sstevel@tonic-gate 	Bytef *pending_buf;	/* output still pending */
3677c478bd9Sstevel@tonic-gate 	ulg   pending_buf_size;	/* size of pending_buf */
3687c478bd9Sstevel@tonic-gate 	Bytef *pending_out;	/* next pending byte to output to the stream */
3697c478bd9Sstevel@tonic-gate 	int   pending;	/* nb of bytes in the pending buffer */
3707c478bd9Sstevel@tonic-gate 	int   noheader;	/* suppress zlib header and adler32 */
3717c478bd9Sstevel@tonic-gate 	Byte  data_type;	/* UNKNOWN, BINARY or ASCII */
3727c478bd9Sstevel@tonic-gate 	Byte  method;	/* STORED (for zip only) or DEFLATED */
3737c478bd9Sstevel@tonic-gate 	/* value of flush param for previous deflate call */
3747c478bd9Sstevel@tonic-gate 	int   last_flush;
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	/* used by deflate.c: */
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	uInt  w_size;	/* LZ77 window size (32K by default) */
3797c478bd9Sstevel@tonic-gate 	uInt  w_bits;	/* log2(w_size)  (8..16) */
3807c478bd9Sstevel@tonic-gate 	uInt  w_mask;	/* w_size - 1 */
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	Bytef *window;
3837c478bd9Sstevel@tonic-gate 	/*
3847c478bd9Sstevel@tonic-gate 	 * Sliding window. Input bytes are read into the second half
3857c478bd9Sstevel@tonic-gate 	 * of the window, and move to the first half later to keep a
3867c478bd9Sstevel@tonic-gate 	 * dictionary of at least wSize bytes. With this organization,
3877c478bd9Sstevel@tonic-gate 	 * matches are limited to a distance of wSize-MAX_MATCH bytes,
3887c478bd9Sstevel@tonic-gate 	 * but this ensures that IO is always performed with a length
3897c478bd9Sstevel@tonic-gate 	 * multiple of the block size. Also, it limits the window size
3907c478bd9Sstevel@tonic-gate 	 * to 64K, which is quite useful on MSDOS.  To do: use the
3917c478bd9Sstevel@tonic-gate 	 * user input buffer as sliding window.
3927c478bd9Sstevel@tonic-gate 	 */
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	ulg window_size;
3957c478bd9Sstevel@tonic-gate 	/*
3967c478bd9Sstevel@tonic-gate 	 * Actual size of window: 2*wSize, except when the user input
3977c478bd9Sstevel@tonic-gate 	 * buffer is directly used as sliding window.
3987c478bd9Sstevel@tonic-gate 	 */
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	Posf *prev;
4017c478bd9Sstevel@tonic-gate 	/*
4027c478bd9Sstevel@tonic-gate 	 * Link to older string with same hash index. To limit the
4037c478bd9Sstevel@tonic-gate 	 * size of this array to 64K, this link is maintained only for
4047c478bd9Sstevel@tonic-gate 	 * the last 32K strings.  An index in this array is thus a
4057c478bd9Sstevel@tonic-gate 	 * window index modulo 32K.
4067c478bd9Sstevel@tonic-gate 	 */
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 	Posf *head;	/* Heads of the hash chains or NIL. */
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	uInt  ins_h;	/* hash index of string to be inserted */
4117c478bd9Sstevel@tonic-gate 	uInt  hash_size;	/* number of elements in hash table */
4127c478bd9Sstevel@tonic-gate 	uInt  hash_bits;	/* log2(hash_size) */
4137c478bd9Sstevel@tonic-gate 	uInt  hash_mask;	/* hash_size-1 */
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	uInt  hash_shift;
4167c478bd9Sstevel@tonic-gate 	/*
4177c478bd9Sstevel@tonic-gate 	 * Number of bits by which ins_h must be shifted at each input
4187c478bd9Sstevel@tonic-gate 	 * step. It must be such that after MIN_MATCH steps, the
4197c478bd9Sstevel@tonic-gate 	 * oldest byte no longer takes part in the hash key, that is:
4207c478bd9Sstevel@tonic-gate 	 * hash_shift * MIN_MATCH >= hash_bits
4217c478bd9Sstevel@tonic-gate 	 */
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	long block_start;
4247c478bd9Sstevel@tonic-gate 	/*
4257c478bd9Sstevel@tonic-gate 	 * Window position at the beginning of the current output
4267c478bd9Sstevel@tonic-gate 	 * block. Gets negative when the window is moved backwards.
4277c478bd9Sstevel@tonic-gate 	 */
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	uInt match_length;	/* length of best match */
4307c478bd9Sstevel@tonic-gate 	IPos prev_match;	/* previous match */
4317c478bd9Sstevel@tonic-gate 	int match_available;	/* set if previous match exists */
4327c478bd9Sstevel@tonic-gate 	uInt strstart;	/* start of string to insert */
4337c478bd9Sstevel@tonic-gate 	uInt match_start;	/* start of matching string */
4347c478bd9Sstevel@tonic-gate 	uInt lookahead;	/* number of valid bytes ahead in window */
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	uInt prev_length;
4377c478bd9Sstevel@tonic-gate 	/*
4387c478bd9Sstevel@tonic-gate 	 * Length of the best match at previous step. Matches not
4397c478bd9Sstevel@tonic-gate 	 * greater than this are discarded. This is used in the lazy
4407c478bd9Sstevel@tonic-gate 	 * match evaluation.
4417c478bd9Sstevel@tonic-gate 	 */
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 	uInt max_chain_length;
4447c478bd9Sstevel@tonic-gate 	/*
4457c478bd9Sstevel@tonic-gate 	 * To speed up deflation, hash chains are never searched
4467c478bd9Sstevel@tonic-gate 	 * beyond *this length.  A higher limit improves compression
4477c478bd9Sstevel@tonic-gate 	 * ratio but *degrades the speed.
4487c478bd9Sstevel@tonic-gate 	 */
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	uInt max_lazy_match;
4517c478bd9Sstevel@tonic-gate 	/*
4527c478bd9Sstevel@tonic-gate 	 * Attempt to find a better match only when the current match
4537c478bd9Sstevel@tonic-gate 	 * is strictly smaller than this value. This mechanism is used
4547c478bd9Sstevel@tonic-gate 	 * only for compression levels >= 4.
4557c478bd9Sstevel@tonic-gate 	 */
4567c478bd9Sstevel@tonic-gate #define	max_insert_length  max_lazy_match
4577c478bd9Sstevel@tonic-gate 	/*
4587c478bd9Sstevel@tonic-gate 	 * Insert new strings in the hash table only if the match
4597c478bd9Sstevel@tonic-gate 	 * length is not greater than this length. This saves time but
4607c478bd9Sstevel@tonic-gate 	 * degrades compression.  max_insert_length is used only for
4617c478bd9Sstevel@tonic-gate 	 * compression levels <= 3.
4627c478bd9Sstevel@tonic-gate 	 */
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	int level;	/* compression level (1..9) */
4657c478bd9Sstevel@tonic-gate 	int strategy;	/* favor or force Huffman coding */
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 	uInt good_match;
4687c478bd9Sstevel@tonic-gate 	/* Use a faster search when the previous match is longer than this */
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	int nice_match;	/* Stop searching when current match exceeds this */
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	/* used by trees.c: */
4737c478bd9Sstevel@tonic-gate 	/* Didn't use ct_data typedef below to supress compiler warning */
4747c478bd9Sstevel@tonic-gate 	struct ct_data_s dyn_ltree[HEAP_SIZE];	/* literal and length tree */
4757c478bd9Sstevel@tonic-gate 	struct ct_data_s dyn_dtree[2*D_CODES+1];	/* distance tree */
4767c478bd9Sstevel@tonic-gate 	/* Huffman tree for bit lengths */
4777c478bd9Sstevel@tonic-gate 	struct ct_data_s bl_tree[2*BL_CODES+1];
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 	struct tree_desc_s l_desc;	/* desc. for literal tree */
4807c478bd9Sstevel@tonic-gate 	struct tree_desc_s d_desc;	/* desc. for distance tree */
4817c478bd9Sstevel@tonic-gate 	struct tree_desc_s bl_desc;	/* desc. for bit length tree */
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	ush bl_count[MAX_BITS+1];
4847c478bd9Sstevel@tonic-gate 	/* number of codes at each bit length for an optimal tree */
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 	int heap[2*L_CODES+1];	/* heap used to build the Huffman trees */
4877c478bd9Sstevel@tonic-gate 	int heap_len;	/* number of elements in the heap */
4887c478bd9Sstevel@tonic-gate 	int heap_max;	/* element of largest frequency */
4897c478bd9Sstevel@tonic-gate 	/*
4907c478bd9Sstevel@tonic-gate 	 * The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0]
4917c478bd9Sstevel@tonic-gate 	 * is not used.  The same heap array is used to build all
4927c478bd9Sstevel@tonic-gate 	 * trees.
4937c478bd9Sstevel@tonic-gate 	 */
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 	uch depth[2*L_CODES+1];
4967c478bd9Sstevel@tonic-gate 	/*
4977c478bd9Sstevel@tonic-gate 	 * Depth of each subtree used as tie breaker for trees of
4987c478bd9Sstevel@tonic-gate 	 * equal frequency
4997c478bd9Sstevel@tonic-gate 	 */
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 	uchf *l_buf;	/* buffer for literals or lengths */
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 	uInt lit_bufsize;
5047c478bd9Sstevel@tonic-gate 	/*
5057c478bd9Sstevel@tonic-gate 	 * Size of match buffer for literals/lengths.  There are 4
5067c478bd9Sstevel@tonic-gate 	 * reasons for limiting lit_bufsize to 64K:
5077c478bd9Sstevel@tonic-gate 	 *
5087c478bd9Sstevel@tonic-gate 	 *   - frequencies can be kept in 16 bit counters
5097c478bd9Sstevel@tonic-gate 	 *
5107c478bd9Sstevel@tonic-gate 	 *   - if compression is not successful for the first block,
5117c478bd9Sstevel@tonic-gate 	 *   all input data is still in the window so we can still
5127c478bd9Sstevel@tonic-gate 	 *   emit a stored block even when input comes from standard
5137c478bd9Sstevel@tonic-gate 	 *   input.  (This can also be done for all blocks if
5147c478bd9Sstevel@tonic-gate 	 *   lit_bufsize is not greater than 32K.)
5157c478bd9Sstevel@tonic-gate 	 *
5167c478bd9Sstevel@tonic-gate 	 *   - if compression is not successful for a file smaller
5177c478bd9Sstevel@tonic-gate 	 *   than 64K, we can even emit a stored file instead of a
5187c478bd9Sstevel@tonic-gate 	 *   stored block (saving 5 bytes).  This is applicable only
5197c478bd9Sstevel@tonic-gate 	 *   for zip (not gzip or zlib).
5207c478bd9Sstevel@tonic-gate 	 *
5217c478bd9Sstevel@tonic-gate 	 *   - creating new Huffman trees less frequently may not
5227c478bd9Sstevel@tonic-gate 	 *   provide fast adaptation to changes in the input data
5237c478bd9Sstevel@tonic-gate 	 *   statistics. (Take for example a binary file with poorly
5247c478bd9Sstevel@tonic-gate 	 *   compressible code followed by a highly compressible
5257c478bd9Sstevel@tonic-gate 	 *   string table.) Smaller buffer sizes give fast adaptation
5267c478bd9Sstevel@tonic-gate 	 *   but have of course the overhead of transmitting trees
5277c478bd9Sstevel@tonic-gate 	 *   more frequently.
5287c478bd9Sstevel@tonic-gate 	 *
5297c478bd9Sstevel@tonic-gate 	 *   - I can't count above 4
5307c478bd9Sstevel@tonic-gate 	 */
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 	uInt last_lit;	/* running index in l_buf */
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate 	ushf *d_buf;
5357c478bd9Sstevel@tonic-gate 	/*
5367c478bd9Sstevel@tonic-gate 	 * Buffer for distances. To simplify the code, d_buf and l_buf
5377c478bd9Sstevel@tonic-gate 	 * have the same number of elements. To use different lengths,
5387c478bd9Sstevel@tonic-gate 	 * an extra flag array would be necessary.
5397c478bd9Sstevel@tonic-gate 	 */
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	ulg opt_len;	/* bit length of current block with optimal trees */
5427c478bd9Sstevel@tonic-gate 	ulg static_len;	/* bit length of current block with static trees */
5437c478bd9Sstevel@tonic-gate 	uInt matches;	/* number of string matches in current block */
5447c478bd9Sstevel@tonic-gate 	int last_eob_len;	/* bit length of EOB code for last block */
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	ulg compressed_len;	/* total bit length of compressed file PPP */
5477c478bd9Sstevel@tonic-gate #ifdef DEBUG_ZLIB
5487c478bd9Sstevel@tonic-gate 	ulg bits_sent;	/* bit length of the compressed data */
5497c478bd9Sstevel@tonic-gate #endif
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate 	ush bi_buf;
5527c478bd9Sstevel@tonic-gate 	/*
5537c478bd9Sstevel@tonic-gate 	 * Output buffer. bits are inserted starting at the bottom
5547c478bd9Sstevel@tonic-gate 	 * (least significant bits).
5557c478bd9Sstevel@tonic-gate 	 */
5567c478bd9Sstevel@tonic-gate 	int bi_valid;
5577c478bd9Sstevel@tonic-gate 	/*
5587c478bd9Sstevel@tonic-gate 	 * Number of valid bits in bi_buf.  All bits above the last
5597c478bd9Sstevel@tonic-gate 	 * valid bit are always zero.
5607c478bd9Sstevel@tonic-gate 	 */
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate } FAR deflate_state;
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate /*
5657c478bd9Sstevel@tonic-gate  * Output a byte on the stream.  IN assertion: there is enough room in
5667c478bd9Sstevel@tonic-gate  * pending_buf.
5677c478bd9Sstevel@tonic-gate  */
5687c478bd9Sstevel@tonic-gate #define	put_byte(s, c) {s->pending_buf[s->pending++] = (c); }
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate #define	MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
5727c478bd9Sstevel@tonic-gate /*
5737c478bd9Sstevel@tonic-gate  * Minimum amount of lookahead, except at the end of the input file.
5747c478bd9Sstevel@tonic-gate  * See deflate.c for comments about the MIN_MATCH+1.
5757c478bd9Sstevel@tonic-gate  */
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate #define	MAX_DIST(s)  ((s)->w_size-MIN_LOOKAHEAD)
5787c478bd9Sstevel@tonic-gate /*
5797c478bd9Sstevel@tonic-gate  * In order to simplify the code, particularly on 16 bit machines,
5807c478bd9Sstevel@tonic-gate  * match distances are limited to MAX_DIST instead of WSIZE.
5817c478bd9Sstevel@tonic-gate  */
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 	/* in trees.c */
5847c478bd9Sstevel@tonic-gate void _tr_init		OF((deflate_state *s));
5857c478bd9Sstevel@tonic-gate int  _tr_tally		OF((deflate_state *s, unsigned dist, unsigned lc));
5867c478bd9Sstevel@tonic-gate void  _tr_flush_block	OF((deflate_state *s, charf *buf, ulg stored_len,
5877c478bd9Sstevel@tonic-gate     int eof));
5887c478bd9Sstevel@tonic-gate void _tr_align		OF((deflate_state *s));
5897c478bd9Sstevel@tonic-gate void _tr_stored_block	OF((deflate_state *s, charf *buf, ulg stored_len,
5907c478bd9Sstevel@tonic-gate     int eof));
5917c478bd9Sstevel@tonic-gate void _tr_stored_type_only OF((deflate_state *));	/* PPP */
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate #define	d_code(dist) \
5947c478bd9Sstevel@tonic-gate 	((dist) < 256 ? _dist_code[dist] : _dist_code[256+((dist)>>7)])
5957c478bd9Sstevel@tonic-gate /*
5967c478bd9Sstevel@tonic-gate  * Mapping from a distance to a distance code. dist is the distance - 1 and
5977c478bd9Sstevel@tonic-gate  * must not have side effects. _dist_code[256] and _dist_code[257] are never
5987c478bd9Sstevel@tonic-gate  * used.
5997c478bd9Sstevel@tonic-gate  */
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate #ifndef DEBUG_ZLIB
6027c478bd9Sstevel@tonic-gate /* Inline versions of _tr_tally for speed: */
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate local uch _length_code[];
6057c478bd9Sstevel@tonic-gate local uch _dist_code[];
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate #define	_tr_tally_lit(s, c, flush) \
6087c478bd9Sstevel@tonic-gate 	{	uch cc = (c); \
6097c478bd9Sstevel@tonic-gate 		s->d_buf[s->last_lit] = 0; \
6107c478bd9Sstevel@tonic-gate 		s->l_buf[s->last_lit++] = cc; \
6117c478bd9Sstevel@tonic-gate 		s->dyn_ltree[cc].Freq++; \
6127c478bd9Sstevel@tonic-gate 		flush = (s->last_lit == s->lit_bufsize-1); \
6137c478bd9Sstevel@tonic-gate 	}
6147c478bd9Sstevel@tonic-gate #define	_tr_tally_dist(s, distance, length, flush) \
6157c478bd9Sstevel@tonic-gate 	{	uch len = (length); \
6167c478bd9Sstevel@tonic-gate 		ush dist = (distance); \
6177c478bd9Sstevel@tonic-gate 		s->d_buf[s->last_lit] = dist; \
6187c478bd9Sstevel@tonic-gate 		s->l_buf[s->last_lit++] = len; \
6197c478bd9Sstevel@tonic-gate 		dist--; \
6207c478bd9Sstevel@tonic-gate 		s->dyn_ltree[_length_code[len]+LITERALS+1].Freq++; \
6217c478bd9Sstevel@tonic-gate 		s->dyn_dtree[d_code(dist)].Freq++; \
6227c478bd9Sstevel@tonic-gate 		flush = (s->last_lit == s->lit_bufsize-1); \
6237c478bd9Sstevel@tonic-gate 	}
6247c478bd9Sstevel@tonic-gate #else
6257c478bd9Sstevel@tonic-gate #define	_tr_tally_lit(s, c, flush) flush = _tr_tally(s, 0, c)
6267c478bd9Sstevel@tonic-gate #define	_tr_tally_dist(s, distance, length, flush) \
6277c478bd9Sstevel@tonic-gate 		flush = _tr_tally(s, distance, length)
6287c478bd9Sstevel@tonic-gate #endif
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate #endif
6317c478bd9Sstevel@tonic-gate /* --- deflate.h */
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate /* +++ deflate.c */
6347c478bd9Sstevel@tonic-gate /*
6357c478bd9Sstevel@tonic-gate  * deflate.c -- compress data using the deflation algorithm
6367c478bd9Sstevel@tonic-gate  * Copyright (C) 1995-1998 Jean-loup Gailly.
6377c478bd9Sstevel@tonic-gate  * For conditions of distribution and use, see copyright notice in zlib.h
6387c478bd9Sstevel@tonic-gate  */
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate /*
6417c478bd9Sstevel@tonic-gate  *  ALGORITHM
6427c478bd9Sstevel@tonic-gate  *
6437c478bd9Sstevel@tonic-gate  *      The "deflation" process depends on being able to identify portions
6447c478bd9Sstevel@tonic-gate  *      of the input text which are identical to earlier input (within a
6457c478bd9Sstevel@tonic-gate  *      sliding window trailing behind the input currently being processed).
6467c478bd9Sstevel@tonic-gate  *
6477c478bd9Sstevel@tonic-gate  *      The most straightforward technique turns out to be the fastest for
6487c478bd9Sstevel@tonic-gate  *      most input files: try all possible matches and select the longest.
6497c478bd9Sstevel@tonic-gate  *      The key feature of this algorithm is that insertions into the string
6507c478bd9Sstevel@tonic-gate  *      dictionary are very simple and thus fast, and deletions are avoided
6517c478bd9Sstevel@tonic-gate  *      completely. Insertions are performed at each input character, whereas
6527c478bd9Sstevel@tonic-gate  *      string matches are performed only when the previous match ends. So it
6537c478bd9Sstevel@tonic-gate  *      is preferable to spend more time in matches to allow very fast string
6547c478bd9Sstevel@tonic-gate  *      insertions and avoid deletions. The matching algorithm for small
6557c478bd9Sstevel@tonic-gate  *      strings is inspired from that of Rabin & Karp. A brute force approach
6567c478bd9Sstevel@tonic-gate  *      is used to find longer strings when a small match has been found.
6577c478bd9Sstevel@tonic-gate  *      A similar algorithm is used in comic (by Jan-Mark Wams) and freeze
6587c478bd9Sstevel@tonic-gate  *      (by Leonid Broukhis).
6597c478bd9Sstevel@tonic-gate  *         A previous version of this file used a more sophisticated algorithm
6607c478bd9Sstevel@tonic-gate  *      (by Fiala and Greene) which is guaranteed to run in linear amortized
6617c478bd9Sstevel@tonic-gate  *      time, but has a larger average cost, uses more memory and is patented.
6627c478bd9Sstevel@tonic-gate  *      However the F&G algorithm may be faster for some highly redundant
6637c478bd9Sstevel@tonic-gate  *      files if the parameter max_chain_length (described below) is too large.
6647c478bd9Sstevel@tonic-gate  *
6657c478bd9Sstevel@tonic-gate  *  ACKNOWLEDGEMENTS
6667c478bd9Sstevel@tonic-gate  *
6677c478bd9Sstevel@tonic-gate  *      The idea of lazy evaluation of matches is due to Jan-Mark Wams, and
6687c478bd9Sstevel@tonic-gate  *      I found it in 'freeze' written by Leonid Broukhis.
6697c478bd9Sstevel@tonic-gate  *      Thanks to many people for bug reports and testing.
6707c478bd9Sstevel@tonic-gate  *
6717c478bd9Sstevel@tonic-gate  *  REFERENCES
6727c478bd9Sstevel@tonic-gate  *
6737c478bd9Sstevel@tonic-gate  *      Deutsch, L.P.,"DEFLATE Compressed Data Format Specification".
6747c478bd9Sstevel@tonic-gate  *      Available in ftp://ds.internic.net/rfc/rfc1951.txt
6757c478bd9Sstevel@tonic-gate  *
6767c478bd9Sstevel@tonic-gate  *      A description of the Rabin and Karp algorithm is given in the book
6777c478bd9Sstevel@tonic-gate  *         "Algorithms" by R. Sedgewick, Addison-Wesley, p252.
6787c478bd9Sstevel@tonic-gate  *
6797c478bd9Sstevel@tonic-gate  *      Fiala,E.R., and Greene,D.H.
6807c478bd9Sstevel@tonic-gate  *         Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595
6817c478bd9Sstevel@tonic-gate  *
6827c478bd9Sstevel@tonic-gate  */
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate /* From: deflate.c,v 1.15 1996/07/24 13:40:58 me Exp $ */
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate /* #include "deflate.h" */
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate const char deflate_copyright[] =
6897c478bd9Sstevel@tonic-gate " deflate 1.1.3 Copyright 1995-1998 Jean-loup Gailly ";
6907c478bd9Sstevel@tonic-gate /*
6917c478bd9Sstevel@tonic-gate  * If you use the zlib library in a product, an acknowledgment is
6927c478bd9Sstevel@tonic-gate  * welcome in the documentation of your product. If for some reason
6937c478bd9Sstevel@tonic-gate  * you cannot include such an acknowledgment, I would appreciate that
6947c478bd9Sstevel@tonic-gate  * you keep this copyright string in the executable of your product.
6957c478bd9Sstevel@tonic-gate  */
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate /*
6987c478bd9Sstevel@tonic-gate  * ===========================================================================
6997c478bd9Sstevel@tonic-gate  *  Function prototypes.
7007c478bd9Sstevel@tonic-gate  */
7017c478bd9Sstevel@tonic-gate typedef enum {
7027c478bd9Sstevel@tonic-gate 	/* block not completed, need more input or more output */
7037c478bd9Sstevel@tonic-gate 	need_more,
7047c478bd9Sstevel@tonic-gate 	block_done,	/* block flush performed */
7057c478bd9Sstevel@tonic-gate 	/* finish started, need only more output at next deflate */
7067c478bd9Sstevel@tonic-gate 	finish_started,
7077c478bd9Sstevel@tonic-gate 	finish_done	/* finish done, accept no more input or output */
7087c478bd9Sstevel@tonic-gate } block_state;
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate typedef block_state (*compress_func) OF((deflate_state *s, int flush));
7117c478bd9Sstevel@tonic-gate /* Compression function. Returns the block state after the call. */
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate local void fill_window	OF((deflate_state *s));
7147c478bd9Sstevel@tonic-gate local block_state deflate_stored OF((deflate_state *s, int flush));
7157c478bd9Sstevel@tonic-gate local block_state deflate_fast	OF((deflate_state *s, int flush));
7167c478bd9Sstevel@tonic-gate local block_state deflate_slow	OF((deflate_state *s, int flush));
7177c478bd9Sstevel@tonic-gate local void lm_init	OF((deflate_state *s));
7187c478bd9Sstevel@tonic-gate local void putShortMSB	OF((deflate_state *s, uInt b));
7197c478bd9Sstevel@tonic-gate local void flush_pending	OF((z_streamp strm));
7207c478bd9Sstevel@tonic-gate local int read_buf	OF((z_streamp strm, Bytef *buf, unsigned size));
7217c478bd9Sstevel@tonic-gate #ifdef ASMV
7227c478bd9Sstevel@tonic-gate void match_init	OF((void));	/* asm code initialization */
7237c478bd9Sstevel@tonic-gate uInt longest_match	OF((deflate_state *s, IPos cur_match));
7247c478bd9Sstevel@tonic-gate #else
7257c478bd9Sstevel@tonic-gate local uInt longest_match	OF((deflate_state *s, IPos cur_match));
7267c478bd9Sstevel@tonic-gate #endif
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate #ifdef DEBUG_ZLIB
7297c478bd9Sstevel@tonic-gate local void check_match OF((deflate_state *s, IPos start, IPos match,
7307c478bd9Sstevel@tonic-gate     int length));
7317c478bd9Sstevel@tonic-gate #endif
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate /*
7347c478bd9Sstevel@tonic-gate  * ===========================================================================
7357c478bd9Sstevel@tonic-gate  * Local data
7367c478bd9Sstevel@tonic-gate  */
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate #define	NIL 0
7397c478bd9Sstevel@tonic-gate /* Tail of hash chains */
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate #ifndef TOO_FAR
7427c478bd9Sstevel@tonic-gate #define	TOO_FAR 4096
7437c478bd9Sstevel@tonic-gate #endif
7447c478bd9Sstevel@tonic-gate /* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate #define	MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
7477c478bd9Sstevel@tonic-gate /*
7487c478bd9Sstevel@tonic-gate  * Minimum amount of lookahead, except at the end of the input file.
7497c478bd9Sstevel@tonic-gate  * See deflate.c for comments about the MIN_MATCH+1.
7507c478bd9Sstevel@tonic-gate  */
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate /*
7537c478bd9Sstevel@tonic-gate  * Values for max_lazy_match, good_match and max_chain_length,
7547c478bd9Sstevel@tonic-gate  * depending on the desired pack level (0..9). The values given below
7557c478bd9Sstevel@tonic-gate  * have been tuned to exclude worst case performance for pathological
7567c478bd9Sstevel@tonic-gate  * files. Better values may be found for specific files.
7577c478bd9Sstevel@tonic-gate  */
7587c478bd9Sstevel@tonic-gate typedef struct config_s {
7597c478bd9Sstevel@tonic-gate 	ush good_length;	/* reduce lazy search above this match length */
7607c478bd9Sstevel@tonic-gate 	ush max_lazy;	/* do not perform lazy search above this match length */
7617c478bd9Sstevel@tonic-gate 	ush nice_length;	/* quit search above this match length */
7627c478bd9Sstevel@tonic-gate 	ush max_chain;
7637c478bd9Sstevel@tonic-gate 	compress_func func;
7647c478bd9Sstevel@tonic-gate } config;
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate local const config configuration_table[10] = {
7677c478bd9Sstevel@tonic-gate /*	good lazy nice chain */
7687c478bd9Sstevel@tonic-gate /* 0 */ {0,    0,  0,    0, deflate_stored},  /* store only */
7697c478bd9Sstevel@tonic-gate /* 1 */ {4,    4,  8,    4, deflate_fast}, /* maximum speed, no lazy matches */
7707c478bd9Sstevel@tonic-gate /* 2 */ {4,    5, 16,    8, deflate_fast},
7717c478bd9Sstevel@tonic-gate /* 3 */ {4,    6, 32,   32, deflate_fast},
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate /* 4 */ {4,    4, 16,   16, deflate_slow},  /* lazy matches */
7747c478bd9Sstevel@tonic-gate /* 5 */ {8,   16, 32,   32, deflate_slow},
7757c478bd9Sstevel@tonic-gate /* 6 */ {8,   16, 128, 128, deflate_slow},
7767c478bd9Sstevel@tonic-gate /* 7 */ {8,   32, 128, 256, deflate_slow},
7777c478bd9Sstevel@tonic-gate /* 8 */ {32, 128, 258, 1024, deflate_slow},
7787c478bd9Sstevel@tonic-gate /* 9 */ {32, 258, 258, 4096, deflate_slow}};	/* maximum compression */
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate /*
7817c478bd9Sstevel@tonic-gate  * Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
7827c478bd9Sstevel@tonic-gate  * For deflate_fast() (levels <= 3) good is ignored and lazy has a different
7837c478bd9Sstevel@tonic-gate  * meaning.
7847c478bd9Sstevel@tonic-gate  */
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate #define	EQUAL 0
7877c478bd9Sstevel@tonic-gate /* result of memcmp for equal strings */
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate #ifndef NO_DUMMY_DECL
7907c478bd9Sstevel@tonic-gate struct static_tree_desc_s {int dummy; };	/* for buggy compilers */
7917c478bd9Sstevel@tonic-gate #endif
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate /*
7947c478bd9Sstevel@tonic-gate  * ===========================================================================
7957c478bd9Sstevel@tonic-gate  * Update a hash value with the given input byte
7967c478bd9Sstevel@tonic-gate  * IN  assertion: all calls to to UPDATE_HASH are made with consecutive
7977c478bd9Sstevel@tonic-gate  *    input characters, so that a running hash key can be computed from the
7987c478bd9Sstevel@tonic-gate  *    previous key instead of complete recalculation each time.
7997c478bd9Sstevel@tonic-gate  */
8007c478bd9Sstevel@tonic-gate #define	UPDATE_HASH(s, h, c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask)
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate /*
8047c478bd9Sstevel@tonic-gate  * ===========================================================================
8057c478bd9Sstevel@tonic-gate  * Insert string str in the dictionary and set match_head to the previous head
8067c478bd9Sstevel@tonic-gate  * of the hash chain (the most recent string with same hash key). Return
8077c478bd9Sstevel@tonic-gate  * the previous length of the hash chain.
8087c478bd9Sstevel@tonic-gate  * If this file is compiled with -DFASTEST, the compression level is forced
8097c478bd9Sstevel@tonic-gate  * to 1, and no hash chains are maintained.
8107c478bd9Sstevel@tonic-gate  * IN  assertion: all calls to to INSERT_STRING are made with consecutive
8117c478bd9Sstevel@tonic-gate  *    input characters and the first MIN_MATCH bytes of str are valid
8127c478bd9Sstevel@tonic-gate  *    (except for the last MIN_MATCH-1 bytes of the input file).
8137c478bd9Sstevel@tonic-gate  */
8147c478bd9Sstevel@tonic-gate #ifdef FASTEST
8157c478bd9Sstevel@tonic-gate #define	INSERT_STRING(s, str, match_head) \
8167c478bd9Sstevel@tonic-gate 	(UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
8177c478bd9Sstevel@tonic-gate 	match_head = s->head[s->ins_h], \
8187c478bd9Sstevel@tonic-gate 	s->head[s->ins_h] = (Pos)(str))
8197c478bd9Sstevel@tonic-gate #else
8207c478bd9Sstevel@tonic-gate #define	INSERT_STRING(s, str, match_head) \
8217c478bd9Sstevel@tonic-gate 	(UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
8227c478bd9Sstevel@tonic-gate 	s->prev[(str) & s->w_mask] = match_head = s->head[s->ins_h], \
8237c478bd9Sstevel@tonic-gate 	s->head[s->ins_h] = (Pos)(str))
8247c478bd9Sstevel@tonic-gate #endif
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate /*
8277c478bd9Sstevel@tonic-gate  * ===========================================================================
8287c478bd9Sstevel@tonic-gate  * Initialize the hash table (avoiding 64K overflow for 16 bit systems).
8297c478bd9Sstevel@tonic-gate  * prev[] will be initialized on the fly.
8307c478bd9Sstevel@tonic-gate  */
8317c478bd9Sstevel@tonic-gate #define	CLEAR_HASH(s) \
8327c478bd9Sstevel@tonic-gate     s->head[s->hash_size-1] = NIL; \
8337c478bd9Sstevel@tonic-gate     zmemzero((Bytef *)s->head, (unsigned)(s->hash_size-1)*sizeof (*s->head));
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate /* ========================================================================= */
8367c478bd9Sstevel@tonic-gate int
deflateInit_(strm,level,version,stream_size)8377c478bd9Sstevel@tonic-gate deflateInit_(strm, level, version, stream_size)
8387c478bd9Sstevel@tonic-gate     z_streamp strm;
8397c478bd9Sstevel@tonic-gate     int level;
8407c478bd9Sstevel@tonic-gate     const char *version;
8417c478bd9Sstevel@tonic-gate     int stream_size;
8427c478bd9Sstevel@tonic-gate {
8437c478bd9Sstevel@tonic-gate 	(void) deflate_copyright;
8447c478bd9Sstevel@tonic-gate 	return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL,
8457c478bd9Sstevel@tonic-gate 	    Z_DEFAULT_STRATEGY, version, stream_size);
8467c478bd9Sstevel@tonic-gate 	/* To do: ignore strm->next_in if we use it as window */
8477c478bd9Sstevel@tonic-gate }
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate /* ========================================================================= */
deflateInit2_(strm,level,method,windowBits,memLevel,strategy,version,stream_size)8507c478bd9Sstevel@tonic-gate int deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
8517c478bd9Sstevel@tonic-gate     version, stream_size)
8527c478bd9Sstevel@tonic-gate     z_streamp strm;
8537c478bd9Sstevel@tonic-gate     int  level;
8547c478bd9Sstevel@tonic-gate     int  method;
8557c478bd9Sstevel@tonic-gate     int  windowBits;
8567c478bd9Sstevel@tonic-gate     int  memLevel;
8577c478bd9Sstevel@tonic-gate     int  strategy;
8587c478bd9Sstevel@tonic-gate     const char *version;
8597c478bd9Sstevel@tonic-gate     int stream_size;
8607c478bd9Sstevel@tonic-gate {
8617c478bd9Sstevel@tonic-gate 	deflate_state *s;
8627c478bd9Sstevel@tonic-gate 	int noheader = 0;
8637c478bd9Sstevel@tonic-gate 	static const char *my_version = ZLIB_VERSION;
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 	ushf *overlay;
8667c478bd9Sstevel@tonic-gate 	/*
8677c478bd9Sstevel@tonic-gate 	 * We overlay pending_buf and d_buf+l_buf. This works since
8687c478bd9Sstevel@tonic-gate 	 * the average output size for (length, distance) codes is <=
8697c478bd9Sstevel@tonic-gate 	 * 24 bits.
8707c478bd9Sstevel@tonic-gate 	 */
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate 	if (version == Z_NULL || version[0] != my_version[0] ||
8737c478bd9Sstevel@tonic-gate 	    stream_size != sizeof (z_stream)) {
8747c478bd9Sstevel@tonic-gate 		return (Z_VERSION_ERROR);
8757c478bd9Sstevel@tonic-gate 	}
8767c478bd9Sstevel@tonic-gate 	if (strm == Z_NULL)
8777c478bd9Sstevel@tonic-gate 		return (Z_STREAM_ERROR);
8787c478bd9Sstevel@tonic-gate 
8797c478bd9Sstevel@tonic-gate 	strm->msg = Z_NULL;
8807c478bd9Sstevel@tonic-gate #ifndef NO_ZCFUNCS
8817c478bd9Sstevel@tonic-gate 	if (strm->zalloc == Z_NULL) {
8827c478bd9Sstevel@tonic-gate 		strm->zalloc = zcalloc;
8837c478bd9Sstevel@tonic-gate 		strm->opaque = (voidpf)0;
8847c478bd9Sstevel@tonic-gate 	}
8857c478bd9Sstevel@tonic-gate 	if (strm->zfree == Z_NULL) strm->zfree = zcfree;
8867c478bd9Sstevel@tonic-gate #endif
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 	if (level == Z_DEFAULT_COMPRESSION) level = 6;
8897c478bd9Sstevel@tonic-gate #ifdef FASTEST
8907c478bd9Sstevel@tonic-gate 	level = 1;
8917c478bd9Sstevel@tonic-gate #endif
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate 	if (windowBits < 0) { /* undocumented feature: suppress zlib header */
8947c478bd9Sstevel@tonic-gate 		noheader = 1;
8957c478bd9Sstevel@tonic-gate 		windowBits = -windowBits;
8967c478bd9Sstevel@tonic-gate 	}
8977c478bd9Sstevel@tonic-gate 	if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
8987c478bd9Sstevel@tonic-gate 	    windowBits <= 8 || windowBits > 15 || level < 0 || level > 9 ||
8997c478bd9Sstevel@tonic-gate 	    strategy < 0 || strategy > Z_HUFFMAN_ONLY) {
9007c478bd9Sstevel@tonic-gate 		return (Z_STREAM_ERROR);
9017c478bd9Sstevel@tonic-gate 	}
9027c478bd9Sstevel@tonic-gate 	s = (deflate_state *) ZALLOC(strm, 1, sizeof (deflate_state));
9037c478bd9Sstevel@tonic-gate 	if (s == Z_NULL)
9047c478bd9Sstevel@tonic-gate 		return (Z_MEM_ERROR);
9057c478bd9Sstevel@tonic-gate 	strm->state = (struct internal_state FAR *)s;
9067c478bd9Sstevel@tonic-gate 	s->strm = strm;
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 	s->noheader = noheader;
9097c478bd9Sstevel@tonic-gate 	s->w_bits = windowBits;
9107c478bd9Sstevel@tonic-gate 	s->w_size = 1 << s->w_bits;
9117c478bd9Sstevel@tonic-gate 	s->w_mask = s->w_size - 1;
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate 	s->hash_bits = memLevel + 7;
9147c478bd9Sstevel@tonic-gate 	s->hash_size = 1 << s->hash_bits;
9157c478bd9Sstevel@tonic-gate 	s->hash_mask = s->hash_size - 1;
9167c478bd9Sstevel@tonic-gate 	s->hash_shift =  ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH);
9177c478bd9Sstevel@tonic-gate 
9187c478bd9Sstevel@tonic-gate 	s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof (Byte));
9197c478bd9Sstevel@tonic-gate 	s->prev   = (Posf *)  ZALLOC(strm, s->w_size, sizeof (Pos));
9207c478bd9Sstevel@tonic-gate 	s->head   = (Posf *)  ZALLOC(strm, s->hash_size, sizeof (Pos));
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate 	s->lit_bufsize = 1 << (memLevel + 6);	/* 16K elements by default */
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate 	overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof (ush)+2);
9257c478bd9Sstevel@tonic-gate 	s->pending_buf = (uchf *) overlay;
9267c478bd9Sstevel@tonic-gate 	s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof (ush)+2L);
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate 	if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
9297c478bd9Sstevel@tonic-gate 	    s->pending_buf == Z_NULL) {
9307c478bd9Sstevel@tonic-gate 		strm->msg = ERR_MSG(Z_MEM_ERROR);
9317c478bd9Sstevel@tonic-gate 		s->status = INIT_STATE;
9327c478bd9Sstevel@tonic-gate 		(void) deflateEnd(strm);
9337c478bd9Sstevel@tonic-gate 		return (Z_MEM_ERROR);
9347c478bd9Sstevel@tonic-gate 	}
9357c478bd9Sstevel@tonic-gate 	s->d_buf = overlay + s->lit_bufsize/sizeof (ush);
9367c478bd9Sstevel@tonic-gate 	s->l_buf = s->pending_buf + (1+sizeof (ush))*s->lit_bufsize;
9377c478bd9Sstevel@tonic-gate 
9387c478bd9Sstevel@tonic-gate 	s->level = level;
9397c478bd9Sstevel@tonic-gate 	s->strategy = strategy;
9407c478bd9Sstevel@tonic-gate 	s->method = (Byte)method;
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate 	return (deflateReset(strm));
9437c478bd9Sstevel@tonic-gate }
9447c478bd9Sstevel@tonic-gate 
9457c478bd9Sstevel@tonic-gate /* ========================================================================= */
9467c478bd9Sstevel@tonic-gate int
deflateSetDictionary(strm,dictionary,dictLength)9477c478bd9Sstevel@tonic-gate deflateSetDictionary(strm, dictionary, dictLength)
9487c478bd9Sstevel@tonic-gate     z_streamp strm;
9497c478bd9Sstevel@tonic-gate     const Bytef *dictionary;
9507c478bd9Sstevel@tonic-gate     uInt  dictLength;
9517c478bd9Sstevel@tonic-gate {
9527c478bd9Sstevel@tonic-gate 	deflate_state *s;
9537c478bd9Sstevel@tonic-gate 	uInt length = dictLength;
9547c478bd9Sstevel@tonic-gate 	uInt n;
9557c478bd9Sstevel@tonic-gate 	IPos hash_head = 0;
9567c478bd9Sstevel@tonic-gate 
9577c478bd9Sstevel@tonic-gate 	if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL)
9587c478bd9Sstevel@tonic-gate 		return (Z_STREAM_ERROR);
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 	s = (deflate_state *) strm->state;
9617c478bd9Sstevel@tonic-gate 	if (s->status != INIT_STATE)
9627c478bd9Sstevel@tonic-gate 		return (Z_STREAM_ERROR);
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate 	strm->adler = adler32(strm->adler, dictionary, dictLength);
9657c478bd9Sstevel@tonic-gate 
9667c478bd9Sstevel@tonic-gate 	if (length < MIN_MATCH)
9677c478bd9Sstevel@tonic-gate 		return (Z_OK);
9687c478bd9Sstevel@tonic-gate 	if (length > MAX_DIST(s)) {
9697c478bd9Sstevel@tonic-gate 		length = MAX_DIST(s);
9707c478bd9Sstevel@tonic-gate #ifndef USE_DICT_HEAD
9717c478bd9Sstevel@tonic-gate 		/* use the tail of the dictionary */
9727c478bd9Sstevel@tonic-gate 		dictionary += dictLength - length;
9737c478bd9Sstevel@tonic-gate #endif
9747c478bd9Sstevel@tonic-gate 	}
9757c478bd9Sstevel@tonic-gate 	Assert(length <= s->window_size, "dict copy");
9767c478bd9Sstevel@tonic-gate 	zmemcpy(s->window, dictionary, length);
9777c478bd9Sstevel@tonic-gate 	s->strstart = length;
9787c478bd9Sstevel@tonic-gate 	s->block_start = (long)length;
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate 	/*
9817c478bd9Sstevel@tonic-gate 	 * Insert all strings in the hash table (except for the last
9827c478bd9Sstevel@tonic-gate 	 * two bytes).  s->lookahead stays null, so s->ins_h will be
9837c478bd9Sstevel@tonic-gate 	 * recomputed at the next call of fill_window.
9847c478bd9Sstevel@tonic-gate 	 */
9857c478bd9Sstevel@tonic-gate 	s->ins_h = s->window[0];
9867c478bd9Sstevel@tonic-gate 	UPDATE_HASH(s, s->ins_h, s->window[1]);
9877c478bd9Sstevel@tonic-gate 	for (n = 0; n <= length - MIN_MATCH; n++) {
9887c478bd9Sstevel@tonic-gate 		INSERT_STRING(s, n, hash_head);
9897c478bd9Sstevel@tonic-gate 	}
9907c478bd9Sstevel@tonic-gate 	if (hash_head) hash_head = 0;	/* to make compiler happy */
9917c478bd9Sstevel@tonic-gate 	return (Z_OK);
9927c478bd9Sstevel@tonic-gate }
9937c478bd9Sstevel@tonic-gate 
9947c478bd9Sstevel@tonic-gate /* ========================================================================= */
9957c478bd9Sstevel@tonic-gate int
deflateReset(strm)9967c478bd9Sstevel@tonic-gate deflateReset(strm)
9977c478bd9Sstevel@tonic-gate     z_streamp strm;
9987c478bd9Sstevel@tonic-gate {
9997c478bd9Sstevel@tonic-gate 	deflate_state *s;
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate 	if (strm == Z_NULL || strm->state == Z_NULL ||
10027c478bd9Sstevel@tonic-gate 	    strm->zalloc == Z_NULL || strm->zfree == Z_NULL)
10037c478bd9Sstevel@tonic-gate 		return (Z_STREAM_ERROR);
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 	strm->total_in = strm->total_out = 0;
10067c478bd9Sstevel@tonic-gate 	/* use zfree if we ever allocate msg dynamically */
10077c478bd9Sstevel@tonic-gate 	strm->msg = Z_NULL;
10087c478bd9Sstevel@tonic-gate 	strm->data_type = Z_UNKNOWN;
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate 	s = (deflate_state *)strm->state;
10117c478bd9Sstevel@tonic-gate 	s->pending = 0;
10127c478bd9Sstevel@tonic-gate 	s->pending_out = s->pending_buf;
10137c478bd9Sstevel@tonic-gate 
10147c478bd9Sstevel@tonic-gate 	if (s->noheader < 0) {
10157c478bd9Sstevel@tonic-gate 		/* was set to -1 by deflate(..., Z_FINISH); */
10167c478bd9Sstevel@tonic-gate 		s->noheader = 0;
10177c478bd9Sstevel@tonic-gate 	}
10187c478bd9Sstevel@tonic-gate 	s->status = s->noheader ? BUSY_STATE : INIT_STATE;
10197c478bd9Sstevel@tonic-gate 	strm->adler = 1;
10207c478bd9Sstevel@tonic-gate 	s->last_flush = Z_NO_FLUSH;
10217c478bd9Sstevel@tonic-gate 
10227c478bd9Sstevel@tonic-gate 	_tr_init(s);
10237c478bd9Sstevel@tonic-gate 	lm_init(s);
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate 	return (Z_OK);
10267c478bd9Sstevel@tonic-gate }
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate /* ========================================================================= */
10297c478bd9Sstevel@tonic-gate int
deflateParams(strm,level,strategy)10307c478bd9Sstevel@tonic-gate deflateParams(strm, level, strategy)
10317c478bd9Sstevel@tonic-gate     z_streamp strm;
10327c478bd9Sstevel@tonic-gate     int level;
10337c478bd9Sstevel@tonic-gate     int strategy;
10347c478bd9Sstevel@tonic-gate {
10357c478bd9Sstevel@tonic-gate 	deflate_state *s;
10367c478bd9Sstevel@tonic-gate 	compress_func func;
10377c478bd9Sstevel@tonic-gate 	int err = Z_OK;
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate 	if (strm == Z_NULL || strm->state == Z_NULL)
10407c478bd9Sstevel@tonic-gate 		return (Z_STREAM_ERROR);
10417c478bd9Sstevel@tonic-gate 	s = (deflate_state *) strm->state;
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate 	if (level == Z_DEFAULT_COMPRESSION) {
10447c478bd9Sstevel@tonic-gate 		level = 6;
10457c478bd9Sstevel@tonic-gate 	}
10467c478bd9Sstevel@tonic-gate 	if (level < 0 || level > 9 || strategy < 0 ||
10477c478bd9Sstevel@tonic-gate 	    strategy > Z_HUFFMAN_ONLY) {
10487c478bd9Sstevel@tonic-gate 		return (Z_STREAM_ERROR);
10497c478bd9Sstevel@tonic-gate 	}
10507c478bd9Sstevel@tonic-gate 	func = configuration_table[s->level].func;
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate 	if (func != configuration_table[level].func && strm->total_in != 0) {
10537c478bd9Sstevel@tonic-gate 		/* Flush the last buffer: */
10547c478bd9Sstevel@tonic-gate 		err = deflate(strm, Z_PARTIAL_FLUSH);
10557c478bd9Sstevel@tonic-gate 	}
10567c478bd9Sstevel@tonic-gate 	if (s->level != level) {
10577c478bd9Sstevel@tonic-gate 		s->level = level;
10587c478bd9Sstevel@tonic-gate 		s->max_lazy_match   = configuration_table[level].max_lazy;
10597c478bd9Sstevel@tonic-gate 		s->good_match	= configuration_table[level].good_length;
10607c478bd9Sstevel@tonic-gate 		s->nice_match	= configuration_table[level].nice_length;
10617c478bd9Sstevel@tonic-gate 		s->max_chain_length = configuration_table[level].max_chain;
10627c478bd9Sstevel@tonic-gate 	}
10637c478bd9Sstevel@tonic-gate 	s->strategy = strategy;
10647c478bd9Sstevel@tonic-gate 	return (err);
10657c478bd9Sstevel@tonic-gate }
10667c478bd9Sstevel@tonic-gate 
10677c478bd9Sstevel@tonic-gate /*
10687c478bd9Sstevel@tonic-gate  * =========================================================================
10697c478bd9Sstevel@tonic-gate  * Put a short in the pending buffer. The 16-bit value is put in MSB order.
10707c478bd9Sstevel@tonic-gate  * IN assertion: the stream state is correct and there is enough room in
10717c478bd9Sstevel@tonic-gate  * pending_buf.
10727c478bd9Sstevel@tonic-gate  */
10737c478bd9Sstevel@tonic-gate local void
putShortMSB(s,b)10747c478bd9Sstevel@tonic-gate putShortMSB(s, b)
10757c478bd9Sstevel@tonic-gate     deflate_state *s;
10767c478bd9Sstevel@tonic-gate     uInt b;
10777c478bd9Sstevel@tonic-gate {
10787c478bd9Sstevel@tonic-gate 	put_byte(s, (Byte)(b >> 8));
10797c478bd9Sstevel@tonic-gate 	put_byte(s, (Byte)(b & 0xff));
10807c478bd9Sstevel@tonic-gate }
10817c478bd9Sstevel@tonic-gate 
10827c478bd9Sstevel@tonic-gate /*
10837c478bd9Sstevel@tonic-gate  * =========================================================================
10847c478bd9Sstevel@tonic-gate  * Flush as much pending output as possible. All deflate() output goes
10857c478bd9Sstevel@tonic-gate  * through this function so some applications may wish to modify it
10867c478bd9Sstevel@tonic-gate  * to avoid allocating a large strm->next_out buffer and copying into it.
10877c478bd9Sstevel@tonic-gate  * (See also read_buf()).
10887c478bd9Sstevel@tonic-gate  */
10897c478bd9Sstevel@tonic-gate local void
flush_pending(strm)10907c478bd9Sstevel@tonic-gate flush_pending(strm)
10917c478bd9Sstevel@tonic-gate     z_streamp strm;
10927c478bd9Sstevel@tonic-gate {
10937c478bd9Sstevel@tonic-gate 	deflate_state *s = (deflate_state *) strm->state;
10947c478bd9Sstevel@tonic-gate 	unsigned len = s->pending;
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate 	if (len > strm->avail_out) len = strm->avail_out;
10977c478bd9Sstevel@tonic-gate 	if (len == 0)
10987c478bd9Sstevel@tonic-gate 		return;
10997c478bd9Sstevel@tonic-gate 
11007c478bd9Sstevel@tonic-gate 	if (strm->next_out != Z_NULL) {		/* PPP */
11017c478bd9Sstevel@tonic-gate 		zmemcpy(strm->next_out, s->pending_out, len);
11027c478bd9Sstevel@tonic-gate 		strm->next_out += len;
11037c478bd9Sstevel@tonic-gate 	}					/* PPP */
11047c478bd9Sstevel@tonic-gate 	s->pending_out += len;
11057c478bd9Sstevel@tonic-gate 	strm->total_out += len;
11067c478bd9Sstevel@tonic-gate 	strm->avail_out  -= len;
11077c478bd9Sstevel@tonic-gate 	s->pending -= len;
11087c478bd9Sstevel@tonic-gate 	if (s->pending == 0) {
11097c478bd9Sstevel@tonic-gate 		s->pending_out = s->pending_buf;
11107c478bd9Sstevel@tonic-gate 	}
11117c478bd9Sstevel@tonic-gate }
11127c478bd9Sstevel@tonic-gate 
11137c478bd9Sstevel@tonic-gate /* ========================================================================= */
11147c478bd9Sstevel@tonic-gate int
deflate(strm,flush)11157c478bd9Sstevel@tonic-gate deflate(strm, flush)
11167c478bd9Sstevel@tonic-gate     z_streamp strm;
11177c478bd9Sstevel@tonic-gate     int flush;
11187c478bd9Sstevel@tonic-gate {
11197c478bd9Sstevel@tonic-gate 	int old_flush;	/* value of flush param for previous deflate call */
11207c478bd9Sstevel@tonic-gate 	deflate_state *s;
11217c478bd9Sstevel@tonic-gate 
11227c478bd9Sstevel@tonic-gate 	if (strm == Z_NULL || strm->state == Z_NULL ||
11237c478bd9Sstevel@tonic-gate 	    flush > Z_FINISH || flush < 0) {
11247c478bd9Sstevel@tonic-gate 		return (Z_STREAM_ERROR);
11257c478bd9Sstevel@tonic-gate 	}
11267c478bd9Sstevel@tonic-gate 	s = (deflate_state *) strm->state;
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate 	if (/* strm->next_out == Z_NULL || --- we allow null --- PPP */
11297c478bd9Sstevel@tonic-gate 		(strm->next_in == Z_NULL && strm->avail_in != 0) ||
11307c478bd9Sstevel@tonic-gate 	    (s->status == FINISH_STATE && flush != Z_FINISH)) {
11317c478bd9Sstevel@tonic-gate 		ERR_RETURN(strm, Z_STREAM_ERROR);
11327c478bd9Sstevel@tonic-gate 	}
11337c478bd9Sstevel@tonic-gate 	if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR);
11347c478bd9Sstevel@tonic-gate 
11357c478bd9Sstevel@tonic-gate 	s->strm = strm;	/* just in case */
11367c478bd9Sstevel@tonic-gate 	old_flush = s->last_flush;
11377c478bd9Sstevel@tonic-gate 	s->last_flush = flush;
11387c478bd9Sstevel@tonic-gate 
11397c478bd9Sstevel@tonic-gate 	/* Write the zlib header */
11407c478bd9Sstevel@tonic-gate 	if (s->status == INIT_STATE) {
11417c478bd9Sstevel@tonic-gate 
11427c478bd9Sstevel@tonic-gate 		uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
11437c478bd9Sstevel@tonic-gate 		uInt level_flags = (s->level-1) >> 1;
11447c478bd9Sstevel@tonic-gate 
11457c478bd9Sstevel@tonic-gate 		if (level_flags > 3) level_flags = 3;
11467c478bd9Sstevel@tonic-gate 		header |= (level_flags << 6);
11477c478bd9Sstevel@tonic-gate 		if (s->strstart != 0) header |= PRESET_DICT;
11487c478bd9Sstevel@tonic-gate 		header += 31 - (header % 31);
11497c478bd9Sstevel@tonic-gate 
11507c478bd9Sstevel@tonic-gate 		s->status = BUSY_STATE;
11517c478bd9Sstevel@tonic-gate 		putShortMSB(s, header);
11527c478bd9Sstevel@tonic-gate 
11537c478bd9Sstevel@tonic-gate 		/* Save the adler32 of the preset dictionary: */
11547c478bd9Sstevel@tonic-gate 		if (s->strstart != 0) {
11557c478bd9Sstevel@tonic-gate 			putShortMSB(s, (uInt)(strm->adler >> 16));
11567c478bd9Sstevel@tonic-gate 			putShortMSB(s, (uInt)(strm->adler & 0xffff));
11577c478bd9Sstevel@tonic-gate 		}
11587c478bd9Sstevel@tonic-gate 		strm->adler = 1L;
11597c478bd9Sstevel@tonic-gate 	}
11607c478bd9Sstevel@tonic-gate 
11617c478bd9Sstevel@tonic-gate 	/* Flush as much pending output as possible */
11627c478bd9Sstevel@tonic-gate 	if (s->pending != 0) {
11637c478bd9Sstevel@tonic-gate 		flush_pending(strm);
11647c478bd9Sstevel@tonic-gate 		if (strm->avail_out == 0) {
11657c478bd9Sstevel@tonic-gate 			/*
11667c478bd9Sstevel@tonic-gate 			 * Since avail_out is 0, deflate will be
11677c478bd9Sstevel@tonic-gate 			 * called again with more output space, but
11687c478bd9Sstevel@tonic-gate 			 * possibly with both pending and avail_in
11697c478bd9Sstevel@tonic-gate 			 * equal to zero. There won't be anything to
11707c478bd9Sstevel@tonic-gate 			 * do, but this is not an error situation so
11717c478bd9Sstevel@tonic-gate 			 * make sure we return OK instead of BUF_ERROR
11727c478bd9Sstevel@tonic-gate 			 * at next call of deflate:
11737c478bd9Sstevel@tonic-gate 			 */
11747c478bd9Sstevel@tonic-gate 			s->last_flush = -1;
11757c478bd9Sstevel@tonic-gate 			return (Z_OK);
11767c478bd9Sstevel@tonic-gate 		}
11777c478bd9Sstevel@tonic-gate 
11787c478bd9Sstevel@tonic-gate 		/*
11797c478bd9Sstevel@tonic-gate 		 * Make sure there is something to do and avoid
11807c478bd9Sstevel@tonic-gate 		 * duplicate consecutive flushes. For repeated and
11817c478bd9Sstevel@tonic-gate 		 * useless calls with Z_FINISH, we keep returning
11827c478bd9Sstevel@tonic-gate 		 * Z_STREAM_END instead of Z_BUFF_ERROR.
11837c478bd9Sstevel@tonic-gate 		 */
11847c478bd9Sstevel@tonic-gate 	} else if (strm->avail_in == 0 && flush <= old_flush &&
11857c478bd9Sstevel@tonic-gate 	    flush != Z_FINISH) {
11867c478bd9Sstevel@tonic-gate 		ERR_RETURN(strm, Z_BUF_ERROR);
11877c478bd9Sstevel@tonic-gate 	}
11887c478bd9Sstevel@tonic-gate 
11897c478bd9Sstevel@tonic-gate 	/* User must not provide more input after the first FINISH: */
11907c478bd9Sstevel@tonic-gate 	if (s->status == FINISH_STATE && strm->avail_in != 0) {
11917c478bd9Sstevel@tonic-gate 		ERR_RETURN(strm, Z_BUF_ERROR);
11927c478bd9Sstevel@tonic-gate 	}
11937c478bd9Sstevel@tonic-gate 
11947c478bd9Sstevel@tonic-gate 	/* Start a new block or continue the current one. */
11957c478bd9Sstevel@tonic-gate 	if (strm->avail_in != 0 || s->lookahead != 0 ||
11967c478bd9Sstevel@tonic-gate 	    (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
11977c478bd9Sstevel@tonic-gate 		block_state bstate;
11987c478bd9Sstevel@tonic-gate 
11997c478bd9Sstevel@tonic-gate 		bstate = (*(configuration_table[s->level].func))(s, flush);
12007c478bd9Sstevel@tonic-gate 
12017c478bd9Sstevel@tonic-gate 		if (bstate == finish_started || bstate == finish_done) {
12027c478bd9Sstevel@tonic-gate 			s->status = FINISH_STATE;
12037c478bd9Sstevel@tonic-gate 		}
12047c478bd9Sstevel@tonic-gate 		if (bstate == need_more || bstate == finish_started) {
12057c478bd9Sstevel@tonic-gate 			if (strm->avail_out == 0) {
12067c478bd9Sstevel@tonic-gate 				/* avoid BUF_ERROR next call, see above */
12077c478bd9Sstevel@tonic-gate 				s->last_flush = -1;
12087c478bd9Sstevel@tonic-gate 			}
12097c478bd9Sstevel@tonic-gate 			return (Z_OK);
12107c478bd9Sstevel@tonic-gate 			/*
12117c478bd9Sstevel@tonic-gate 			 * If flush != Z_NO_FLUSH && avail_out == 0,
12127c478bd9Sstevel@tonic-gate 			 * the next call of deflate should use the
12137c478bd9Sstevel@tonic-gate 			 * same flush parameter to make sure that the
12147c478bd9Sstevel@tonic-gate 			 * flush is complete. So we don't have to
12157c478bd9Sstevel@tonic-gate 			 * output an empty block here, this will be
12167c478bd9Sstevel@tonic-gate 			 * done at next call. This also ensures that
12177c478bd9Sstevel@tonic-gate 			 * for a very small output buffer, we emit at
12187c478bd9Sstevel@tonic-gate 			 * most one empty block.
12197c478bd9Sstevel@tonic-gate 			 */
12207c478bd9Sstevel@tonic-gate 		}
12217c478bd9Sstevel@tonic-gate 		if (bstate == block_done) {
12227c478bd9Sstevel@tonic-gate 			if (flush == Z_PARTIAL_FLUSH) {
12237c478bd9Sstevel@tonic-gate 				_tr_align(s);
12247c478bd9Sstevel@tonic-gate 			} else if (flush == Z_PACKET_FLUSH) {	/* PPP */
12257c478bd9Sstevel@tonic-gate 				/*
12267c478bd9Sstevel@tonic-gate 				 * Output just the 3-bit `stored'
12277c478bd9Sstevel@tonic-gate 				 * block type value, but not a zero
12287c478bd9Sstevel@tonic-gate 				 * length.  Added for PPP.
12297c478bd9Sstevel@tonic-gate 				 */
12307c478bd9Sstevel@tonic-gate 				_tr_stored_type_only(s);	/* PPP */
12317c478bd9Sstevel@tonic-gate 			} else { /* FULL_FLUSH or SYNC_FLUSH */
12327c478bd9Sstevel@tonic-gate 				_tr_stored_block(s, (char *)0, 0L, 0);
12337c478bd9Sstevel@tonic-gate 				/*
12347c478bd9Sstevel@tonic-gate 				 * For a full flush, this empty block
12357c478bd9Sstevel@tonic-gate 				 * will be recognized as a special
12367c478bd9Sstevel@tonic-gate 				 * marker by inflate_sync().
12377c478bd9Sstevel@tonic-gate 				 */
12387c478bd9Sstevel@tonic-gate 				if (flush == Z_FULL_FLUSH) {
12397c478bd9Sstevel@tonic-gate 					CLEAR_HASH(s);	/* forget history */
12407c478bd9Sstevel@tonic-gate 				}
12417c478bd9Sstevel@tonic-gate 			}
12427c478bd9Sstevel@tonic-gate 			flush_pending(strm);
12437c478bd9Sstevel@tonic-gate 			if (strm->avail_out == 0) {
12447c478bd9Sstevel@tonic-gate 				/* avoid BUF_ERROR at next call, see above */
12457c478bd9Sstevel@tonic-gate 				s->last_flush = -1;
12467c478bd9Sstevel@tonic-gate 				return (Z_OK);
12477c478bd9Sstevel@tonic-gate 			}
12487c478bd9Sstevel@tonic-gate 		}
12497c478bd9Sstevel@tonic-gate 	}
12507c478bd9Sstevel@tonic-gate 	Assert(strm->avail_out > 0, "bug2");
12517c478bd9Sstevel@tonic-gate 
12527c478bd9Sstevel@tonic-gate 	if (flush != Z_FINISH)
12537c478bd9Sstevel@tonic-gate 		return (Z_OK);
12547c478bd9Sstevel@tonic-gate 	if (s->noheader)
12557c478bd9Sstevel@tonic-gate 		return (Z_STREAM_END);
12567c478bd9Sstevel@tonic-gate 
12577c478bd9Sstevel@tonic-gate 	/* Write the zlib trailer (adler32) */
12587c478bd9Sstevel@tonic-gate 	putShortMSB(s, (uInt)(strm->adler >> 16));
12597c478bd9Sstevel@tonic-gate 	putShortMSB(s, (uInt)(strm->adler & 0xffff));
12607c478bd9Sstevel@tonic-gate 	flush_pending(strm);
12617c478bd9Sstevel@tonic-gate 	/*
12627c478bd9Sstevel@tonic-gate 	 * If avail_out is zero, the application will call deflate
12637c478bd9Sstevel@tonic-gate 	 * again to flush the rest.
12647c478bd9Sstevel@tonic-gate 	 */
12657c478bd9Sstevel@tonic-gate 	s->noheader = -1;	/* write the trailer only once! */
12667c478bd9Sstevel@tonic-gate 	return (s->pending != 0 ? Z_OK : Z_STREAM_END);
12677c478bd9Sstevel@tonic-gate }
12687c478bd9Sstevel@tonic-gate 
12697c478bd9Sstevel@tonic-gate /* ========================================================================= */
12707c478bd9Sstevel@tonic-gate int
deflateEnd(strm)12717c478bd9Sstevel@tonic-gate deflateEnd(strm)
12727c478bd9Sstevel@tonic-gate     z_streamp strm;
12737c478bd9Sstevel@tonic-gate {
12747c478bd9Sstevel@tonic-gate 	int status;
12757c478bd9Sstevel@tonic-gate 	deflate_state *s;
12767c478bd9Sstevel@tonic-gate 
12777c478bd9Sstevel@tonic-gate 	if (strm == Z_NULL || strm->state == Z_NULL)
12787c478bd9Sstevel@tonic-gate 		return (Z_STREAM_ERROR);
12797c478bd9Sstevel@tonic-gate 	s = (deflate_state *) strm->state;
12807c478bd9Sstevel@tonic-gate 
12817c478bd9Sstevel@tonic-gate 	status = s->status;
12827c478bd9Sstevel@tonic-gate 	if (status != INIT_STATE && status != BUSY_STATE &&
12837c478bd9Sstevel@tonic-gate 	    status != FINISH_STATE) {
12847c478bd9Sstevel@tonic-gate 		return (Z_STREAM_ERROR);
12857c478bd9Sstevel@tonic-gate 	}
12867c478bd9Sstevel@tonic-gate 
12877c478bd9Sstevel@tonic-gate 	/* Deallocate in reverse order of allocations: */
12887c478bd9Sstevel@tonic-gate 	TRY_FREE(strm, s->pending_buf);
12897c478bd9Sstevel@tonic-gate 	TRY_FREE(strm, s->head);
12907c478bd9Sstevel@tonic-gate 	TRY_FREE(strm, s->prev);
12917c478bd9Sstevel@tonic-gate 	TRY_FREE(strm, s->window);
12927c478bd9Sstevel@tonic-gate 
12937c478bd9Sstevel@tonic-gate 	ZFREE(strm, s);
12947c478bd9Sstevel@tonic-gate 	strm->state = Z_NULL;
12957c478bd9Sstevel@tonic-gate 
12967c478bd9Sstevel@tonic-gate 	return (status == BUSY_STATE ? Z_DATA_ERROR : Z_OK);
12977c478bd9Sstevel@tonic-gate }
12987c478bd9Sstevel@tonic-gate 
12997c478bd9Sstevel@tonic-gate /*
13007c478bd9Sstevel@tonic-gate  * =========================================================================
13017c478bd9Sstevel@tonic-gate  * Copy the source state to the destination state.
13027c478bd9Sstevel@tonic-gate  * To simplify the source, this is not supported for 16-bit MSDOS (which
13037c478bd9Sstevel@tonic-gate  * doesn't have enough memory anyway to duplicate compression states).
13047c478bd9Sstevel@tonic-gate  */
13057c478bd9Sstevel@tonic-gate int
deflateCopy(dest,source)13067c478bd9Sstevel@tonic-gate deflateCopy(dest, source)
13077c478bd9Sstevel@tonic-gate     z_streamp dest;
13087c478bd9Sstevel@tonic-gate     z_streamp source;
13097c478bd9Sstevel@tonic-gate {
13107c478bd9Sstevel@tonic-gate #ifdef MAXSEG_64K
13117c478bd9Sstevel@tonic-gate 	return (Z_STREAM_ERROR);
13127c478bd9Sstevel@tonic-gate #else
13137c478bd9Sstevel@tonic-gate 	deflate_state *ds;
13147c478bd9Sstevel@tonic-gate 	deflate_state *ss;
13157c478bd9Sstevel@tonic-gate 	ushf *overlay;
13167c478bd9Sstevel@tonic-gate 
13177c478bd9Sstevel@tonic-gate 	if (source == Z_NULL || dest == Z_NULL || source->state == Z_NULL)
13187c478bd9Sstevel@tonic-gate 		return (Z_STREAM_ERROR);
13197c478bd9Sstevel@tonic-gate 	ss = (deflate_state *) source->state;
13207c478bd9Sstevel@tonic-gate 
13217c478bd9Sstevel@tonic-gate 	zmemcpy(dest, source, sizeof (*dest));
13227c478bd9Sstevel@tonic-gate 
13237c478bd9Sstevel@tonic-gate 	ds = (deflate_state *) ZALLOC(dest, 1, sizeof (deflate_state));
13247c478bd9Sstevel@tonic-gate 	if (ds == Z_NULL)
13257c478bd9Sstevel@tonic-gate 		return (Z_MEM_ERROR);
13267c478bd9Sstevel@tonic-gate 	dest->state = (struct internal_state FAR *) ds;
13277c478bd9Sstevel@tonic-gate 	zmemcpy(ds, ss, sizeof (*ds));
13287c478bd9Sstevel@tonic-gate 	ds->strm = dest;
13297c478bd9Sstevel@tonic-gate 
13307c478bd9Sstevel@tonic-gate 	ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof (Byte));
13317c478bd9Sstevel@tonic-gate 	ds->prev   = (Posf *)  ZALLOC(dest, ds->w_size, sizeof (Pos));
13327c478bd9Sstevel@tonic-gate 	ds->head   = (Posf *)  ZALLOC(dest, ds->hash_size, sizeof (Pos));
13337c478bd9Sstevel@tonic-gate 	overlay = (ushf *) ZALLOC(dest, ds->lit_bufsize, sizeof (ush)+2);
13347c478bd9Sstevel@tonic-gate 	ds->pending_buf = (uchf *) overlay;
13357c478bd9Sstevel@tonic-gate 
13367c478bd9Sstevel@tonic-gate 	if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL ||
13377c478bd9Sstevel@tonic-gate 	    ds->pending_buf == Z_NULL) {
13387c478bd9Sstevel@tonic-gate 		ds->status = INIT_STATE;
13397c478bd9Sstevel@tonic-gate 		(void) deflateEnd(dest);
13407c478bd9Sstevel@tonic-gate 		return (Z_MEM_ERROR);
13417c478bd9Sstevel@tonic-gate 	}
13427c478bd9Sstevel@tonic-gate 	/* following zmemcpy doesn't work for 16-bit MSDOS */
13437c478bd9Sstevel@tonic-gate 	zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof (Byte));
13447c478bd9Sstevel@tonic-gate 	zmemcpy(ds->prev, ss->prev, ds->w_size * sizeof (Pos));
13457c478bd9Sstevel@tonic-gate 	zmemcpy(ds->head, ss->head, ds->hash_size * sizeof (Pos));
13467c478bd9Sstevel@tonic-gate 	zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size);
13477c478bd9Sstevel@tonic-gate 
13487c478bd9Sstevel@tonic-gate 	ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
13497c478bd9Sstevel@tonic-gate 	ds->d_buf = overlay + ds->lit_bufsize/sizeof (ush);
13507c478bd9Sstevel@tonic-gate 	ds->l_buf = ds->pending_buf + (1+sizeof (ush))*ds->lit_bufsize;
13517c478bd9Sstevel@tonic-gate 
13527c478bd9Sstevel@tonic-gate 	ds->l_desc.dyn_tree = ds->dyn_ltree;
13537c478bd9Sstevel@tonic-gate 	ds->d_desc.dyn_tree = ds->dyn_dtree;
13547c478bd9Sstevel@tonic-gate 	ds->bl_desc.dyn_tree = ds->bl_tree;
13557c478bd9Sstevel@tonic-gate 
13567c478bd9Sstevel@tonic-gate 	return (Z_OK);
13577c478bd9Sstevel@tonic-gate #endif
13587c478bd9Sstevel@tonic-gate }
13597c478bd9Sstevel@tonic-gate 
13607c478bd9Sstevel@tonic-gate /*
13617c478bd9Sstevel@tonic-gate  * ===========================================================================
13627c478bd9Sstevel@tonic-gate  * Return the number of bytes of output which are immediately available
13637c478bd9Sstevel@tonic-gate  * for output from the decompressor.		---PPP---
13647c478bd9Sstevel@tonic-gate  */
13657c478bd9Sstevel@tonic-gate int
deflateOutputPending(strm)13667c478bd9Sstevel@tonic-gate deflateOutputPending(strm)
13677c478bd9Sstevel@tonic-gate     z_streamp strm;
13687c478bd9Sstevel@tonic-gate {
13697c478bd9Sstevel@tonic-gate 	if (strm == Z_NULL || strm->state == Z_NULL)
13707c478bd9Sstevel@tonic-gate 		return (0);
13717c478bd9Sstevel@tonic-gate 
13727c478bd9Sstevel@tonic-gate 	return (((deflate_state *)(strm->state))->pending);
13737c478bd9Sstevel@tonic-gate }
13747c478bd9Sstevel@tonic-gate 
13757c478bd9Sstevel@tonic-gate /*
13767c478bd9Sstevel@tonic-gate  * ===========================================================================
13777c478bd9Sstevel@tonic-gate  * Read a new buffer from the current input stream, update the adler32
13787c478bd9Sstevel@tonic-gate  * and total number of bytes read.  All deflate() input goes through
13797c478bd9Sstevel@tonic-gate  * this function so some applications may wish to modify it to avoid
13807c478bd9Sstevel@tonic-gate  * allocating a large strm->next_in buffer and copying from it.
13817c478bd9Sstevel@tonic-gate  * (See also flush_pending()).
13827c478bd9Sstevel@tonic-gate  */
13837c478bd9Sstevel@tonic-gate local int
read_buf(strm,buf,size)13847c478bd9Sstevel@tonic-gate read_buf(strm, buf, size)
13857c478bd9Sstevel@tonic-gate     z_streamp strm;
13867c478bd9Sstevel@tonic-gate     Bytef *buf;
13877c478bd9Sstevel@tonic-gate     unsigned size;
13887c478bd9Sstevel@tonic-gate {
13897c478bd9Sstevel@tonic-gate 	unsigned len = strm->avail_in;
13907c478bd9Sstevel@tonic-gate 
13917c478bd9Sstevel@tonic-gate 	if (len > size) len = size;
13927c478bd9Sstevel@tonic-gate 	if (len == 0)
13937c478bd9Sstevel@tonic-gate 		return (0);
13947c478bd9Sstevel@tonic-gate 
13957c478bd9Sstevel@tonic-gate 	strm->avail_in  -= len;
13967c478bd9Sstevel@tonic-gate 
13977c478bd9Sstevel@tonic-gate 	if (!((deflate_state *)(strm->state))->noheader) {
13987c478bd9Sstevel@tonic-gate 		strm->adler = adler32(strm->adler, strm->next_in, len);
13997c478bd9Sstevel@tonic-gate 	}
14007c478bd9Sstevel@tonic-gate 	zmemcpy(buf, strm->next_in, len);
14017c478bd9Sstevel@tonic-gate 	strm->next_in  += len;
14027c478bd9Sstevel@tonic-gate 	strm->total_in += len;
14037c478bd9Sstevel@tonic-gate 
14047c478bd9Sstevel@tonic-gate 	return ((int)len);
14057c478bd9Sstevel@tonic-gate }
14067c478bd9Sstevel@tonic-gate 
14077c478bd9Sstevel@tonic-gate /*
14087c478bd9Sstevel@tonic-gate  * ===========================================================================
14097c478bd9Sstevel@tonic-gate  * Initialize the "longest match" routines for a new zlib stream
14107c478bd9Sstevel@tonic-gate  */
14117c478bd9Sstevel@tonic-gate local void
lm_init(s)14127c478bd9Sstevel@tonic-gate lm_init(s)
14137c478bd9Sstevel@tonic-gate     deflate_state *s;
14147c478bd9Sstevel@tonic-gate {
14157c478bd9Sstevel@tonic-gate 	s->window_size = (ulg)2L*s->w_size;
14167c478bd9Sstevel@tonic-gate 
14177c478bd9Sstevel@tonic-gate 	CLEAR_HASH(s);
14187c478bd9Sstevel@tonic-gate 
14197c478bd9Sstevel@tonic-gate 	/* Set the default configuration parameters: */
14207c478bd9Sstevel@tonic-gate 	s->max_lazy_match   = configuration_table[s->level].max_lazy;
14217c478bd9Sstevel@tonic-gate 	s->good_match	= configuration_table[s->level].good_length;
14227c478bd9Sstevel@tonic-gate 	s->nice_match	= configuration_table[s->level].nice_length;
14237c478bd9Sstevel@tonic-gate 	s->max_chain_length = configuration_table[s->level].max_chain;
14247c478bd9Sstevel@tonic-gate 
14257c478bd9Sstevel@tonic-gate 	s->strstart = 0;
14267c478bd9Sstevel@tonic-gate 	s->block_start = 0L;
14277c478bd9Sstevel@tonic-gate 	s->lookahead = 0;
14287c478bd9Sstevel@tonic-gate 	s->match_length = s->prev_length = MIN_MATCH-1;
14297c478bd9Sstevel@tonic-gate 	s->match_available = 0;
14307c478bd9Sstevel@tonic-gate 	s->ins_h = 0;
14317c478bd9Sstevel@tonic-gate #ifdef ASMV
14327c478bd9Sstevel@tonic-gate 	match_init();	/* initialize the asm code */
14337c478bd9Sstevel@tonic-gate #endif
14347c478bd9Sstevel@tonic-gate }
14357c478bd9Sstevel@tonic-gate 
14367c478bd9Sstevel@tonic-gate /*
14377c478bd9Sstevel@tonic-gate  * ===========================================================================
14387c478bd9Sstevel@tonic-gate  * Set match_start to the longest match starting at the given string and
14397c478bd9Sstevel@tonic-gate  * return its length. Matches shorter or equal to prev_length are discarded,
14407c478bd9Sstevel@tonic-gate  * in which case the result is equal to prev_length and match_start is
14417c478bd9Sstevel@tonic-gate  * garbage.
14427c478bd9Sstevel@tonic-gate  * IN assertions: cur_match is the head of the hash chain for the current
14437c478bd9Sstevel@tonic-gate  *   string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
14447c478bd9Sstevel@tonic-gate  * OUT assertion: the match length is not greater than s->lookahead.
14457c478bd9Sstevel@tonic-gate  */
14467c478bd9Sstevel@tonic-gate #ifndef ASMV
14477c478bd9Sstevel@tonic-gate /*
14487c478bd9Sstevel@tonic-gate  * For 80x86 and 680x0, an optimized version will be provided in
14497c478bd9Sstevel@tonic-gate  * match.asm or match.S. The code will be functionally equivalent.
14507c478bd9Sstevel@tonic-gate  */
14517c478bd9Sstevel@tonic-gate #ifndef FASTEST
14527c478bd9Sstevel@tonic-gate local uInt
longest_match(s,cur_match)14537c478bd9Sstevel@tonic-gate longest_match(s, cur_match)
14547c478bd9Sstevel@tonic-gate     deflate_state *s;
14557c478bd9Sstevel@tonic-gate     IPos cur_match;	/* current match */
14567c478bd9Sstevel@tonic-gate {
14577c478bd9Sstevel@tonic-gate 	/* max hash chain length */
14587c478bd9Sstevel@tonic-gate 	unsigned chain_length = s->max_chain_length;
14597c478bd9Sstevel@tonic-gate 	register Bytef *scan = s->window + s->strstart;	/* current string */
14607c478bd9Sstevel@tonic-gate 	register Bytef *match;	/* matched string */
14617c478bd9Sstevel@tonic-gate 	register int len;	/* length of current match */
14627c478bd9Sstevel@tonic-gate 	int best_len = s->prev_length;	/* best match length so far */
14637c478bd9Sstevel@tonic-gate 	int nice_match = s->nice_match;	/* stop if match long enough */
14647c478bd9Sstevel@tonic-gate 	IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
14657c478bd9Sstevel@tonic-gate 	    s->strstart - (IPos)MAX_DIST(s) : NIL;
14667c478bd9Sstevel@tonic-gate 	/*
14677c478bd9Sstevel@tonic-gate 	 * Stop when cur_match becomes <= limit. To simplify the code,
14687c478bd9Sstevel@tonic-gate 	 * we prevent matches with the string of window index 0.
14697c478bd9Sstevel@tonic-gate 	 */
14707c478bd9Sstevel@tonic-gate 	Posf *prev = s->prev;
14717c478bd9Sstevel@tonic-gate 	uInt wmask = s->w_mask;
14727c478bd9Sstevel@tonic-gate 
14737c478bd9Sstevel@tonic-gate #ifdef UNALIGNED_OK
14747c478bd9Sstevel@tonic-gate 	/*
14757c478bd9Sstevel@tonic-gate 	 * Compare two bytes at a time. Note: this is not always
14767c478bd9Sstevel@tonic-gate 	 * beneficial.  Try with and without -DUNALIGNED_OK to check.
14777c478bd9Sstevel@tonic-gate 	 */
14787c478bd9Sstevel@tonic-gate 	register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1;
14797c478bd9Sstevel@tonic-gate 	register ush scan_start = *(ushf*)scan;
14807c478bd9Sstevel@tonic-gate 	register ush scan_end   = *(ushf*)(scan+best_len-1);
14817c478bd9Sstevel@tonic-gate #else
14827c478bd9Sstevel@tonic-gate 	register Bytef *strend = s->window + s->strstart + MAX_MATCH;
14837c478bd9Sstevel@tonic-gate 	register Byte scan_end1  = scan[best_len-1];
14847c478bd9Sstevel@tonic-gate 	register Byte scan_end   = scan[best_len];
14857c478bd9Sstevel@tonic-gate #endif
14867c478bd9Sstevel@tonic-gate 
14877c478bd9Sstevel@tonic-gate 	/*
14887c478bd9Sstevel@tonic-gate 	 * The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2
14897c478bd9Sstevel@tonic-gate 	 * multiple of 16.  It is easy to get rid of this optimization
14907c478bd9Sstevel@tonic-gate 	 * if necessary.
14917c478bd9Sstevel@tonic-gate 	 */
14927c478bd9Sstevel@tonic-gate 	Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
14937c478bd9Sstevel@tonic-gate 
14947c478bd9Sstevel@tonic-gate 	/* Do not waste too much time if we already have a good match: */
14957c478bd9Sstevel@tonic-gate 	if (s->prev_length >= s->good_match) {
14967c478bd9Sstevel@tonic-gate 		chain_length >>= 2;
14977c478bd9Sstevel@tonic-gate 	}
14987c478bd9Sstevel@tonic-gate 	/*
14997c478bd9Sstevel@tonic-gate 	 * Do not look for matches beyond the end of the input. This
15007c478bd9Sstevel@tonic-gate 	 * is necessary to make deflate deterministic.
15017c478bd9Sstevel@tonic-gate 	 */
15027c478bd9Sstevel@tonic-gate 	if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead;
15037c478bd9Sstevel@tonic-gate 
15047c478bd9Sstevel@tonic-gate 	Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD,
15057c478bd9Sstevel@tonic-gate 	    "need lookahead");
15067c478bd9Sstevel@tonic-gate 
15077c478bd9Sstevel@tonic-gate 	do {
15087c478bd9Sstevel@tonic-gate 		Assert(cur_match <= s->strstart, "no future");
15097c478bd9Sstevel@tonic-gate 		match = s->window + cur_match;
15107c478bd9Sstevel@tonic-gate 
15117c478bd9Sstevel@tonic-gate 		/*
15127c478bd9Sstevel@tonic-gate 		 * Skip to next match if the match length cannot
15137c478bd9Sstevel@tonic-gate 		 * increase or if the match length is less than 2:
15147c478bd9Sstevel@tonic-gate 		 */
15157c478bd9Sstevel@tonic-gate #if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
15167c478bd9Sstevel@tonic-gate 		/*
15177c478bd9Sstevel@tonic-gate 		 * This code assumes sizeof (unsigned short) == 2. Do
15187c478bd9Sstevel@tonic-gate 		 * not use UNALIGNED_OK if your compiler uses a
15197c478bd9Sstevel@tonic-gate 		 * different size.
15207c478bd9Sstevel@tonic-gate 		 */
15217c478bd9Sstevel@tonic-gate 		if (*(ushf*)(match+best_len-1) != scan_end ||
15227c478bd9Sstevel@tonic-gate 		    *(ushf*)match != scan_start) continue;
15237c478bd9Sstevel@tonic-gate 
15247c478bd9Sstevel@tonic-gate 		/*
15257c478bd9Sstevel@tonic-gate 		 * It is not necessary to compare scan[2] and match[2]
15267c478bd9Sstevel@tonic-gate 		 * since they are always equal when the other bytes
15277c478bd9Sstevel@tonic-gate 		 * match, given that the hash keys are equal and that
15287c478bd9Sstevel@tonic-gate 		 * HASH_BITS >= 8. Compare 2 bytes at a time at
15297c478bd9Sstevel@tonic-gate 		 * strstart+3, +5, ... up to strstart+257. We check
15307c478bd9Sstevel@tonic-gate 		 * for insufficient lookahead only every 4th
15317c478bd9Sstevel@tonic-gate 		 * comparison; the 128th check will be made at
15327c478bd9Sstevel@tonic-gate 		 * strstart+257. If MAX_MATCH-2 is not a multiple of
15337c478bd9Sstevel@tonic-gate 		 * 8, it is necessary to put more guard bytes at the
15347c478bd9Sstevel@tonic-gate 		 * end of the window, or to check more often for
15357c478bd9Sstevel@tonic-gate 		 * insufficient lookahead.
15367c478bd9Sstevel@tonic-gate 		 */
15377c478bd9Sstevel@tonic-gate 		Assert(scan[2] == match[2], "scan[2]?");
15387c478bd9Sstevel@tonic-gate 		scan++, match++;
15397c478bd9Sstevel@tonic-gate 		do {
15407c478bd9Sstevel@tonic-gate 		} while (*(ushf *)(scan += 2) == *(ushf *)(match += 2) &&
15417c478bd9Sstevel@tonic-gate 		    *(ushf *)(scan += 2) == *(ushf *)(match += 2) &&
15427c478bd9Sstevel@tonic-gate 		    *(ushf *)(scan += 2) == *(ushf *)(match += 2) &&
15437c478bd9Sstevel@tonic-gate 		    *(ushf *)(scan += 2) == *(ushf *)(match += 2) &&
15447c478bd9Sstevel@tonic-gate 		    scan < strend);
15457c478bd9Sstevel@tonic-gate 		/* The funny "do {}" generates better code on most compilers */
15467c478bd9Sstevel@tonic-gate 
15477c478bd9Sstevel@tonic-gate 		/* Here, scan <= window+strstart+257 */
15487c478bd9Sstevel@tonic-gate 		Assert(scan <= s->window+(unsigned)(s->window_size-1),
15497c478bd9Sstevel@tonic-gate 		    "wild scan");
15507c478bd9Sstevel@tonic-gate 		if (*scan == *match) scan++;
15517c478bd9Sstevel@tonic-gate 
15527c478bd9Sstevel@tonic-gate 		len = (MAX_MATCH - 1) - (int)(strend-scan);
15537c478bd9Sstevel@tonic-gate 		scan = strend - (MAX_MATCH-1);
15547c478bd9Sstevel@tonic-gate 
15557c478bd9Sstevel@tonic-gate #else /* UNALIGNED_OK */
15567c478bd9Sstevel@tonic-gate 
15577c478bd9Sstevel@tonic-gate 		if (match[best_len]	!= scan_end	||
15587c478bd9Sstevel@tonic-gate 		    match[best_len-1]	!= scan_end1	||
15597c478bd9Sstevel@tonic-gate 		    *match		!= *scan	||
15607c478bd9Sstevel@tonic-gate 		    *++match		!= scan[1])
15617c478bd9Sstevel@tonic-gate 			continue;
15627c478bd9Sstevel@tonic-gate 
15637c478bd9Sstevel@tonic-gate 		/*
15647c478bd9Sstevel@tonic-gate 		 * The check at best_len-1 can be removed because it
15657c478bd9Sstevel@tonic-gate 		 * will be made again later. (This heuristic is not
15667c478bd9Sstevel@tonic-gate 		 * always a win.)  It is not necessary to compare
15677c478bd9Sstevel@tonic-gate 		 * scan[2] and match[2] since they are always equal
15687c478bd9Sstevel@tonic-gate 		 * when the other bytes match, given that the hash
15697c478bd9Sstevel@tonic-gate 		 * keys are equal and that HASH_BITS >= 8.
15707c478bd9Sstevel@tonic-gate 		 */
15717c478bd9Sstevel@tonic-gate 		scan += 2, match++;
15727c478bd9Sstevel@tonic-gate 		Assert(*scan == *match, "match[2]?");
15737c478bd9Sstevel@tonic-gate 
15747c478bd9Sstevel@tonic-gate 		/*
15757c478bd9Sstevel@tonic-gate 		 * We check for insufficient lookahead only every 8th
15767c478bd9Sstevel@tonic-gate 		 * comparison; the 256th check will be made at
15777c478bd9Sstevel@tonic-gate 		 * strstart+258.
15787c478bd9Sstevel@tonic-gate 		 */
15797c478bd9Sstevel@tonic-gate 		do {
15807c478bd9Sstevel@tonic-gate 		} while (*++scan == *++match && *++scan == *++match &&
15817c478bd9Sstevel@tonic-gate 		    *++scan == *++match && *++scan == *++match &&
15827c478bd9Sstevel@tonic-gate 		    *++scan == *++match && *++scan == *++match &&
15837c478bd9Sstevel@tonic-gate 		    *++scan == *++match && *++scan == *++match &&
15847c478bd9Sstevel@tonic-gate 		    scan < strend);
15857c478bd9Sstevel@tonic-gate 
15867c478bd9Sstevel@tonic-gate 		Assert(scan <= s->window+(unsigned)(s->window_size-1),
15877c478bd9Sstevel@tonic-gate 		    "wild scan");
15887c478bd9Sstevel@tonic-gate 
15897c478bd9Sstevel@tonic-gate 		len = MAX_MATCH - (int)(strend - scan);
15907c478bd9Sstevel@tonic-gate 		scan = strend - MAX_MATCH;
15917c478bd9Sstevel@tonic-gate 
15927c478bd9Sstevel@tonic-gate #endif /* UNALIGNED_OK */
15937c478bd9Sstevel@tonic-gate 
15947c478bd9Sstevel@tonic-gate 		if (len > best_len) {
15957c478bd9Sstevel@tonic-gate 			s->match_start = cur_match;
15967c478bd9Sstevel@tonic-gate 			best_len = len;
15977c478bd9Sstevel@tonic-gate 			if (len >= nice_match) break;
15987c478bd9Sstevel@tonic-gate #ifdef UNALIGNED_OK
15997c478bd9Sstevel@tonic-gate 			scan_end = *(ushf*)(scan+best_len-1);
16007c478bd9Sstevel@tonic-gate #else
16017c478bd9Sstevel@tonic-gate 			scan_end1  = scan[best_len-1];
16027c478bd9Sstevel@tonic-gate 			scan_end   = scan[best_len];
16037c478bd9Sstevel@tonic-gate #endif
16047c478bd9Sstevel@tonic-gate 		}
16057c478bd9Sstevel@tonic-gate 	} while ((cur_match = prev[cur_match & wmask]) > limit &&
16067c478bd9Sstevel@tonic-gate 	    --chain_length != 0);
16077c478bd9Sstevel@tonic-gate 
16087c478bd9Sstevel@tonic-gate 	if ((uInt)best_len <= s->lookahead)
16097c478bd9Sstevel@tonic-gate 		return (best_len);
16107c478bd9Sstevel@tonic-gate 	return (s->lookahead);
16117c478bd9Sstevel@tonic-gate }
16127c478bd9Sstevel@tonic-gate #else /* FASTEST */
16137c478bd9Sstevel@tonic-gate /*
16147c478bd9Sstevel@tonic-gate  * ---------------------------------------------------------------------------
16157c478bd9Sstevel@tonic-gate  * Optimized version for level == 1 only
16167c478bd9Sstevel@tonic-gate  */
16177c478bd9Sstevel@tonic-gate local uInt
longest_match(s,cur_match)16187c478bd9Sstevel@tonic-gate longest_match(s, cur_match)
16197c478bd9Sstevel@tonic-gate deflate_state *s;
16207c478bd9Sstevel@tonic-gate IPos cur_match;		/* current match */
16217c478bd9Sstevel@tonic-gate {
16227c478bd9Sstevel@tonic-gate 	register Bytef *scan = s->window + s->strstart; /* current string */
16237c478bd9Sstevel@tonic-gate 	register Bytef *match;		/* matched string */
16247c478bd9Sstevel@tonic-gate 	register int len;			/* length of current match */
16257c478bd9Sstevel@tonic-gate 	register Bytef *strend = s->window + s->strstart + MAX_MATCH;
16267c478bd9Sstevel@tonic-gate 
16277c478bd9Sstevel@tonic-gate 	/*
16287c478bd9Sstevel@tonic-gate 	 * The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2
16297c478bd9Sstevel@tonic-gate 	 * multiple of 16.  It is easy to get rid of this optimization
16307c478bd9Sstevel@tonic-gate 	 * if necessary.
16317c478bd9Sstevel@tonic-gate 	 */
16327c478bd9Sstevel@tonic-gate 	Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
16337c478bd9Sstevel@tonic-gate 
16347c478bd9Sstevel@tonic-gate 	Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD,
16357c478bd9Sstevel@tonic-gate 	    "need lookahead");
16367c478bd9Sstevel@tonic-gate 
16377c478bd9Sstevel@tonic-gate 	Assert(cur_match <= s->strstart, "no future");
16387c478bd9Sstevel@tonic-gate 
16397c478bd9Sstevel@tonic-gate 	match = s->window + cur_match;
16407c478bd9Sstevel@tonic-gate 
16417c478bd9Sstevel@tonic-gate 	/* Return failure if the match length is less than 2: */
16427c478bd9Sstevel@tonic-gate 	if (match[0] != scan[0] || match[1] != scan[1])
16437c478bd9Sstevel@tonic-gate 		return (MIN_MATCH-1);
16447c478bd9Sstevel@tonic-gate 
16457c478bd9Sstevel@tonic-gate 	/*
16467c478bd9Sstevel@tonic-gate 	 * The check at best_len-1 can be removed because it will be
16477c478bd9Sstevel@tonic-gate 	 * made again later. (This heuristic is not always a win.)  It
16487c478bd9Sstevel@tonic-gate 	 * is not necessary to compare scan[2] and match[2] since they
16497c478bd9Sstevel@tonic-gate 	 * are always equal when the other bytes match, given that the
16507c478bd9Sstevel@tonic-gate 	 * hash keys are equal and that HASH_BITS >= 8.
16517c478bd9Sstevel@tonic-gate 	 */
16527c478bd9Sstevel@tonic-gate 	scan += 2, match += 2;
16537c478bd9Sstevel@tonic-gate 	Assert(*scan == *match, "match[2]?");
16547c478bd9Sstevel@tonic-gate 
16557c478bd9Sstevel@tonic-gate 	/*
16567c478bd9Sstevel@tonic-gate 	 * We check for insufficient lookahead only every 8th comparison;
16577c478bd9Sstevel@tonic-gate 	 * the 256th check will be made at strstart+258.
16587c478bd9Sstevel@tonic-gate 	 */
16597c478bd9Sstevel@tonic-gate 	do {
16607c478bd9Sstevel@tonic-gate 	} while (*++scan == *++match && *++scan == *++match &&
16617c478bd9Sstevel@tonic-gate 	    *++scan == *++match && *++scan == *++match &&
16627c478bd9Sstevel@tonic-gate 	    *++scan == *++match && *++scan == *++match &&
16637c478bd9Sstevel@tonic-gate 	    *++scan == *++match && *++scan == *++match &&
16647c478bd9Sstevel@tonic-gate 	    scan < strend);
16657c478bd9Sstevel@tonic-gate 
16667c478bd9Sstevel@tonic-gate 	Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
16677c478bd9Sstevel@tonic-gate 
16687c478bd9Sstevel@tonic-gate 	len = MAX_MATCH - (int)(strend - scan);
16697c478bd9Sstevel@tonic-gate 
16707c478bd9Sstevel@tonic-gate 	if (len < MIN_MATCH)
16717c478bd9Sstevel@tonic-gate 		return (MIN_MATCH - 1);
16727c478bd9Sstevel@tonic-gate 
16737c478bd9Sstevel@tonic-gate 	s->match_start = cur_match;
16747c478bd9Sstevel@tonic-gate 	return (len <= s->lookahead ? len : s->lookahead);
16757c478bd9Sstevel@tonic-gate }
16767c478bd9Sstevel@tonic-gate #endif /* FASTEST */
16777c478bd9Sstevel@tonic-gate #endif /* ASMV */
16787c478bd9Sstevel@tonic-gate 
16797c478bd9Sstevel@tonic-gate #ifdef DEBUG_ZLIB
16807c478bd9Sstevel@tonic-gate /*
16817c478bd9Sstevel@tonic-gate  * ===========================================================================
16827c478bd9Sstevel@tonic-gate  * Check that the match at match_start is indeed a match.
16837c478bd9Sstevel@tonic-gate  */
16847c478bd9Sstevel@tonic-gate local void
check_match(s,start,match,length)16857c478bd9Sstevel@tonic-gate check_match(s, start, match, length)
16867c478bd9Sstevel@tonic-gate     deflate_state *s;
16877c478bd9Sstevel@tonic-gate     IPos start, match;
16887c478bd9Sstevel@tonic-gate     int length;
16897c478bd9Sstevel@tonic-gate {
16907c478bd9Sstevel@tonic-gate 	/* check that the match is indeed a match */
16917c478bd9Sstevel@tonic-gate 	if (zmemcmp(s->window + match, s->window + start, length) != EQUAL) {
16927c478bd9Sstevel@tonic-gate 		fprintf(stderr, " start %u, match %u, length %d\n",
16937c478bd9Sstevel@tonic-gate 		    start, match, length);
16947c478bd9Sstevel@tonic-gate 		do {
16957c478bd9Sstevel@tonic-gate 			fprintf(stderr, "%c%c", s->window[match++],
16967c478bd9Sstevel@tonic-gate 			    s->window[start++]);
16977c478bd9Sstevel@tonic-gate 		} while (--length != 0);
16987c478bd9Sstevel@tonic-gate 		z_error("invalid match");
16997c478bd9Sstevel@tonic-gate 	}
17007c478bd9Sstevel@tonic-gate 	if (z_verbose > 1) {
17017c478bd9Sstevel@tonic-gate 		fprintf(stderr, "\\[%d,%d]", start-match, length);
17027c478bd9Sstevel@tonic-gate 		do { putc(s->window[start++], stderr); } while (--length != 0);
17037c478bd9Sstevel@tonic-gate 	}
17047c478bd9Sstevel@tonic-gate }
17057c478bd9Sstevel@tonic-gate #else
17067c478bd9Sstevel@tonic-gate #define	check_match(s, start, match, length)
17077c478bd9Sstevel@tonic-gate #endif
17087c478bd9Sstevel@tonic-gate 
17097c478bd9Sstevel@tonic-gate /*
17107c478bd9Sstevel@tonic-gate  * ===========================================================================
17117c478bd9Sstevel@tonic-gate  * Fill the window when the lookahead becomes insufficient.
17127c478bd9Sstevel@tonic-gate  * Updates strstart and lookahead.
17137c478bd9Sstevel@tonic-gate  *
17147c478bd9Sstevel@tonic-gate  * IN assertion: lookahead < MIN_LOOKAHEAD
17157c478bd9Sstevel@tonic-gate  * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
17167c478bd9Sstevel@tonic-gate  *    At least one byte has been read, or avail_in == 0; reads are
17177c478bd9Sstevel@tonic-gate  *    performed for at least two bytes (required for the zip translate_eol
17187c478bd9Sstevel@tonic-gate  *    option -- not supported here).
17197c478bd9Sstevel@tonic-gate  */
17207c478bd9Sstevel@tonic-gate local void
fill_window(s)17217c478bd9Sstevel@tonic-gate fill_window(s)
17227c478bd9Sstevel@tonic-gate     deflate_state *s;
17237c478bd9Sstevel@tonic-gate {
17247c478bd9Sstevel@tonic-gate 	register unsigned n, m;
17257c478bd9Sstevel@tonic-gate 	register Posf *p;
17267c478bd9Sstevel@tonic-gate 	unsigned more;	/* Amount of free space at the end of the window. */
17277c478bd9Sstevel@tonic-gate 	uInt wsize = s->w_size;
17287c478bd9Sstevel@tonic-gate 
17297c478bd9Sstevel@tonic-gate 	do {
17307c478bd9Sstevel@tonic-gate 		more = (unsigned)(s->window_size -(ulg)s->lookahead -
17317c478bd9Sstevel@tonic-gate 		    (ulg)s->strstart);
17327c478bd9Sstevel@tonic-gate 
17337c478bd9Sstevel@tonic-gate 		/* Deal with !@#$% 64K limit: */
17347c478bd9Sstevel@tonic-gate 		if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
17357c478bd9Sstevel@tonic-gate 			more = wsize;
17367c478bd9Sstevel@tonic-gate 
17377c478bd9Sstevel@tonic-gate 		} else if (more == (unsigned)(-1)) {
17387c478bd9Sstevel@tonic-gate 			/*
17397c478bd9Sstevel@tonic-gate 			 * Very unlikely, but possible on 16 bit
17407c478bd9Sstevel@tonic-gate 			 * machine if strstart == 0 and lookahead == 1
17417c478bd9Sstevel@tonic-gate 			 * (input done one byte at time)
17427c478bd9Sstevel@tonic-gate 			 */
17437c478bd9Sstevel@tonic-gate 			more--;
17447c478bd9Sstevel@tonic-gate 
17457c478bd9Sstevel@tonic-gate 			/*
17467c478bd9Sstevel@tonic-gate 			 * If the window is almost full and there is
17477c478bd9Sstevel@tonic-gate 			 * insufficient lookahead, move the upper half
17487c478bd9Sstevel@tonic-gate 			 * to the lower one to make room in the upper
17497c478bd9Sstevel@tonic-gate 			 * half.
17507c478bd9Sstevel@tonic-gate 			 */
17517c478bd9Sstevel@tonic-gate 		} else if (s->strstart >= wsize+MAX_DIST(s)) {
17527c478bd9Sstevel@tonic-gate 
17537c478bd9Sstevel@tonic-gate 			Assert(wsize+wsize <= s->window_size, "wsize*2");
17547c478bd9Sstevel@tonic-gate 			zmemcpy(s->window, s->window+wsize, (unsigned)wsize);
17557c478bd9Sstevel@tonic-gate 			s->match_start -= wsize;
17567c478bd9Sstevel@tonic-gate 			/* we now have strstart >= MAX_DIST */
17577c478bd9Sstevel@tonic-gate 			s->strstart    -= wsize;
17587c478bd9Sstevel@tonic-gate 			s->block_start -= (long)wsize;
17597c478bd9Sstevel@tonic-gate 
17607c478bd9Sstevel@tonic-gate 			/*
17617c478bd9Sstevel@tonic-gate 			 * Slide the hash table (could be avoided with
17627c478bd9Sstevel@tonic-gate 			 * 32 bit values at the expense of memory
17637c478bd9Sstevel@tonic-gate 			 * usage). We slide even when level == 0 to
17647c478bd9Sstevel@tonic-gate 			 * keep the hash table consistent if we switch
17657c478bd9Sstevel@tonic-gate 			 * back to level > 0 later. (Using level 0
17667c478bd9Sstevel@tonic-gate 			 * permanently is not an optimal usage of
17677c478bd9Sstevel@tonic-gate 			 * zlib, so we don't care about this
17687c478bd9Sstevel@tonic-gate 			 * pathological case.)
17697c478bd9Sstevel@tonic-gate 			 */
17707c478bd9Sstevel@tonic-gate 			n = s->hash_size;
17717c478bd9Sstevel@tonic-gate 			p = &s->head[n];
17727c478bd9Sstevel@tonic-gate 			do {
17737c478bd9Sstevel@tonic-gate 				m = *--p;
17747c478bd9Sstevel@tonic-gate 				*p = (Pos)(m >= wsize ? m-wsize : NIL);
17757c478bd9Sstevel@tonic-gate 			} while (--n);
17767c478bd9Sstevel@tonic-gate 
17777c478bd9Sstevel@tonic-gate 			n = wsize;
17787c478bd9Sstevel@tonic-gate #ifndef FASTEST
17797c478bd9Sstevel@tonic-gate 			p = &s->prev[n];
17807c478bd9Sstevel@tonic-gate 			do {
17817c478bd9Sstevel@tonic-gate 				m = *--p;
17827c478bd9Sstevel@tonic-gate 				*p = (Pos)(m >= wsize ? m-wsize : NIL);
17837c478bd9Sstevel@tonic-gate 				/*
17847c478bd9Sstevel@tonic-gate 				 * If n is not on any hash chain,
17857c478bd9Sstevel@tonic-gate 				 * prev[n] is garbage but its value
17867c478bd9Sstevel@tonic-gate 				 * will never be used.
17877c478bd9Sstevel@tonic-gate 				 */
17887c478bd9Sstevel@tonic-gate 			} while (--n);
17897c478bd9Sstevel@tonic-gate #endif
17907c478bd9Sstevel@tonic-gate 			more += wsize;
17917c478bd9Sstevel@tonic-gate 		}
17927c478bd9Sstevel@tonic-gate 		if (s->strm->avail_in == 0)
17937c478bd9Sstevel@tonic-gate 			return;
17947c478bd9Sstevel@tonic-gate 
17957c478bd9Sstevel@tonic-gate 		/*
17967c478bd9Sstevel@tonic-gate 		 * If there was no sliding:
17977c478bd9Sstevel@tonic-gate 		 *    strstart <= WSIZE+MAX_DIST-1 &&
17987c478bd9Sstevel@tonic-gate 		 *	lookahead <= MIN_LOOKAHEAD - 1 &&
17997c478bd9Sstevel@tonic-gate 		 *    more == window_size - lookahead - strstart
18007c478bd9Sstevel@tonic-gate 		 * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE +
18017c478bd9Sstevel@tonic-gate 		 *	MAX_DIST-1)
18027c478bd9Sstevel@tonic-gate 		 * => more >= window_size - 2*WSIZE + 2
18037c478bd9Sstevel@tonic-gate 		 * In the BIG_MEM or MMAP case (not yet supported),
18047c478bd9Sstevel@tonic-gate 		 *   window_size == input_size + MIN_LOOKAHEAD  &&
18057c478bd9Sstevel@tonic-gate 		 *   strstart + s->lookahead <= input_size =>
18067c478bd9Sstevel@tonic-gate 		 *	more >= MIN_LOOKAHEAD.
18077c478bd9Sstevel@tonic-gate 		 * Otherwise, window_size == 2*WSIZE so more >= 2.
18087c478bd9Sstevel@tonic-gate 		 * If there was sliding, more >= WSIZE. So in all cases,
18097c478bd9Sstevel@tonic-gate 		 * more >= 2.
18107c478bd9Sstevel@tonic-gate 		 */
18117c478bd9Sstevel@tonic-gate 		Assert(more >= 2, "more < 2");
18127c478bd9Sstevel@tonic-gate 		Assert(s->strstart + s->lookahead + more <= s->window_size,
18137c478bd9Sstevel@tonic-gate 		    "read too much");
18147c478bd9Sstevel@tonic-gate 
18157c478bd9Sstevel@tonic-gate 		n = read_buf(s->strm, s->window + s->strstart + s->lookahead,
18167c478bd9Sstevel@tonic-gate 		    more);
18177c478bd9Sstevel@tonic-gate 		s->lookahead += n;
18187c478bd9Sstevel@tonic-gate 
18197c478bd9Sstevel@tonic-gate 		/* Initialize the hash value now that we have some input: */
18207c478bd9Sstevel@tonic-gate 		if (s->lookahead >= MIN_MATCH) {
18217c478bd9Sstevel@tonic-gate 			s->ins_h = s->window[s->strstart];
18227c478bd9Sstevel@tonic-gate 			UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
18237c478bd9Sstevel@tonic-gate #if MIN_MATCH != 3
18247c478bd9Sstevel@tonic-gate 			Call UPDATE_HASH() MIN_MATCH-3 more times
18257c478bd9Sstevel@tonic-gate #endif
18267c478bd9Sstevel@tonic-gate 			    }
18277c478bd9Sstevel@tonic-gate 		/*
18287c478bd9Sstevel@tonic-gate 		 * If the whole input has less than MIN_MATCH bytes,
18297c478bd9Sstevel@tonic-gate 		 * ins_h is garbage, but this is not important since
18307c478bd9Sstevel@tonic-gate 		 * only literal bytes will be emitted.
18317c478bd9Sstevel@tonic-gate 		 */
18327c478bd9Sstevel@tonic-gate 
18337c478bd9Sstevel@tonic-gate 	} while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
18347c478bd9Sstevel@tonic-gate }
18357c478bd9Sstevel@tonic-gate 
18367c478bd9Sstevel@tonic-gate /*
18377c478bd9Sstevel@tonic-gate  * ===========================================================================
18387c478bd9Sstevel@tonic-gate  * Flush the current block, with given end-of-file flag.
18397c478bd9Sstevel@tonic-gate  * IN assertion: strstart is set to the end of the current match.
18407c478bd9Sstevel@tonic-gate  */
18417c478bd9Sstevel@tonic-gate #define	FLUSH_BLOCK_ONLY(s, eof) { \
18427c478bd9Sstevel@tonic-gate 	_tr_flush_block(s, (s->block_start >= 0L ? \
18437c478bd9Sstevel@tonic-gate 		(charf *)&s->window[(unsigned)s->block_start] : \
18447c478bd9Sstevel@tonic-gate 		(charf *)Z_NULL), \
18457c478bd9Sstevel@tonic-gate 		(ulg)((long)s->strstart - s->block_start), \
18467c478bd9Sstevel@tonic-gate 		(eof)); \
18477c478bd9Sstevel@tonic-gate 	s->block_start = s->strstart; \
18487c478bd9Sstevel@tonic-gate 	flush_pending(s->strm); \
18497c478bd9Sstevel@tonic-gate 	Tracev((stderr, "[FLUSH]")); \
18507c478bd9Sstevel@tonic-gate }
18517c478bd9Sstevel@tonic-gate 
18527c478bd9Sstevel@tonic-gate /* Same but force premature exit if necessary. */
18537c478bd9Sstevel@tonic-gate #define	FLUSH_BLOCK(s, eof) { \
18547c478bd9Sstevel@tonic-gate 	FLUSH_BLOCK_ONLY(s, eof); \
18557c478bd9Sstevel@tonic-gate 	if (s->strm->avail_out == 0) \
18567c478bd9Sstevel@tonic-gate 		return ((eof) ? finish_started : need_more); \
18577c478bd9Sstevel@tonic-gate }
18587c478bd9Sstevel@tonic-gate 
18597c478bd9Sstevel@tonic-gate /*
18607c478bd9Sstevel@tonic-gate  * ===========================================================================
18617c478bd9Sstevel@tonic-gate  * Copy without compression as much as possible from the input stream, return
18627c478bd9Sstevel@tonic-gate  * the current block state.
18637c478bd9Sstevel@tonic-gate  * This function does not insert new strings in the dictionary since
18647c478bd9Sstevel@tonic-gate  * uncompressible data is probably not useful. This function is used
18657c478bd9Sstevel@tonic-gate  * only for the level=0 compression option.
18667c478bd9Sstevel@tonic-gate  * NOTE: this function should be optimized to avoid extra copying from
18677c478bd9Sstevel@tonic-gate  * window to pending_buf.
18687c478bd9Sstevel@tonic-gate  */
18697c478bd9Sstevel@tonic-gate local block_state
deflate_stored(s,flush)18707c478bd9Sstevel@tonic-gate deflate_stored(s, flush)
18717c478bd9Sstevel@tonic-gate     deflate_state *s;
18727c478bd9Sstevel@tonic-gate     int flush;
18737c478bd9Sstevel@tonic-gate {
18747c478bd9Sstevel@tonic-gate 	/*
18757c478bd9Sstevel@tonic-gate 	 * Stored blocks are limited to 0xffff bytes, pending_buf is
18767c478bd9Sstevel@tonic-gate 	 * limited to pending_buf_size, and each stored block has a 5
18777c478bd9Sstevel@tonic-gate 	 * byte header:
18787c478bd9Sstevel@tonic-gate 	 */
18797c478bd9Sstevel@tonic-gate 	ulg max_block_size = 0xffff;
18807c478bd9Sstevel@tonic-gate 	ulg max_start;
18817c478bd9Sstevel@tonic-gate 
18827c478bd9Sstevel@tonic-gate 	if (max_block_size > s->pending_buf_size - 5) {
18837c478bd9Sstevel@tonic-gate 		max_block_size = s->pending_buf_size - 5;
18847c478bd9Sstevel@tonic-gate 	}
18857c478bd9Sstevel@tonic-gate 
18867c478bd9Sstevel@tonic-gate 	/* Copy as much as possible from input to output: */
18877c478bd9Sstevel@tonic-gate 	for (;;) {
18887c478bd9Sstevel@tonic-gate 		/* Fill the window as much as possible: */
18897c478bd9Sstevel@tonic-gate 		if (s->lookahead <= 1) {
18907c478bd9Sstevel@tonic-gate 
18917c478bd9Sstevel@tonic-gate 			Assert(s->strstart < s->w_size+MAX_DIST(s) ||
18927c478bd9Sstevel@tonic-gate 			    s->block_start >= (long)s->w_size,
18937c478bd9Sstevel@tonic-gate 			    "slide too late");
18947c478bd9Sstevel@tonic-gate 
18957c478bd9Sstevel@tonic-gate 			fill_window(s);
18967c478bd9Sstevel@tonic-gate 			if (s->lookahead == 0 && flush == Z_NO_FLUSH)
18977c478bd9Sstevel@tonic-gate 				return (need_more);
18987c478bd9Sstevel@tonic-gate 
18997c478bd9Sstevel@tonic-gate 			if (s->lookahead == 0)
19007c478bd9Sstevel@tonic-gate 				break;	/* flush the current block */
19017c478bd9Sstevel@tonic-gate 		}
19027c478bd9Sstevel@tonic-gate 		Assert(s->block_start >= 0L, "block gone");
19037c478bd9Sstevel@tonic-gate 
19047c478bd9Sstevel@tonic-gate 		s->strstart += s->lookahead;
19057c478bd9Sstevel@tonic-gate 		s->lookahead = 0;
19067c478bd9Sstevel@tonic-gate 
19077c478bd9Sstevel@tonic-gate 		/* Emit a stored block if pending_buf will be full: */
19087c478bd9Sstevel@tonic-gate 		max_start = s->block_start + max_block_size;
19097c478bd9Sstevel@tonic-gate 		if (s->strstart == 0 || (ulg)s->strstart >= max_start) {
19107c478bd9Sstevel@tonic-gate 			/*
19117c478bd9Sstevel@tonic-gate 			 * strstart == 0 is possible when wraparound
19127c478bd9Sstevel@tonic-gate 			 * on 16-bit machine
19137c478bd9Sstevel@tonic-gate 			 */
19147c478bd9Sstevel@tonic-gate 			s->lookahead = (uInt)(s->strstart - max_start);
19157c478bd9Sstevel@tonic-gate 			s->strstart = (uInt)max_start;
19167c478bd9Sstevel@tonic-gate 			FLUSH_BLOCK(s, 0);
19177c478bd9Sstevel@tonic-gate 		}
19187c478bd9Sstevel@tonic-gate 		/*
19197c478bd9Sstevel@tonic-gate 		 * Flush if we may have to slide, otherwise
19207c478bd9Sstevel@tonic-gate 		 * block_start may become negative and the data will
19217c478bd9Sstevel@tonic-gate 		 * be gone:
19227c478bd9Sstevel@tonic-gate 		 */
19237c478bd9Sstevel@tonic-gate 		if (s->strstart - (uInt)s->block_start >= MAX_DIST(s)) {
19247c478bd9Sstevel@tonic-gate 			FLUSH_BLOCK(s, 0);
19257c478bd9Sstevel@tonic-gate 		}
19267c478bd9Sstevel@tonic-gate 	}
19277c478bd9Sstevel@tonic-gate 	FLUSH_BLOCK(s, flush == Z_FINISH);
19287c478bd9Sstevel@tonic-gate 	return (flush == Z_FINISH ? finish_done : block_done);
19297c478bd9Sstevel@tonic-gate }
19307c478bd9Sstevel@tonic-gate 
19317c478bd9Sstevel@tonic-gate /*
19327c478bd9Sstevel@tonic-gate  * ===========================================================================
19337c478bd9Sstevel@tonic-gate  * Compress as much as possible from the input stream, return the current
19347c478bd9Sstevel@tonic-gate  * block state.
19357c478bd9Sstevel@tonic-gate  * This function does not perform lazy evaluation of matches and inserts
19367c478bd9Sstevel@tonic-gate  * new strings in the dictionary only for unmatched strings or for short
19377c478bd9Sstevel@tonic-gate  * matches. It is used only for the fast compression options.
19387c478bd9Sstevel@tonic-gate  */
19397c478bd9Sstevel@tonic-gate local block_state
deflate_fast(s,flush)19407c478bd9Sstevel@tonic-gate deflate_fast(s, flush)
19417c478bd9Sstevel@tonic-gate     deflate_state *s;
19427c478bd9Sstevel@tonic-gate     int flush;
19437c478bd9Sstevel@tonic-gate {
19447c478bd9Sstevel@tonic-gate 	IPos hash_head = NIL;	/* head of the hash chain */
19457c478bd9Sstevel@tonic-gate 	int bflush;	/* set if current block must be flushed */
19467c478bd9Sstevel@tonic-gate 
19477c478bd9Sstevel@tonic-gate 	for (;;) {
19487c478bd9Sstevel@tonic-gate 		/*
19497c478bd9Sstevel@tonic-gate 		 * Make sure that we always have enough lookahead,
19507c478bd9Sstevel@tonic-gate 		 * except at the end of the input file. We need
19517c478bd9Sstevel@tonic-gate 		 * MAX_MATCH bytes for the next match, plus MIN_MATCH
19527c478bd9Sstevel@tonic-gate 		 * bytes to insert the string following the next
19537c478bd9Sstevel@tonic-gate 		 * match.
19547c478bd9Sstevel@tonic-gate 		 */
19557c478bd9Sstevel@tonic-gate 		if (s->lookahead < MIN_LOOKAHEAD) {
19567c478bd9Sstevel@tonic-gate 			fill_window(s);
19577c478bd9Sstevel@tonic-gate 			if (s->lookahead < MIN_LOOKAHEAD &&
19587c478bd9Sstevel@tonic-gate 			    flush == Z_NO_FLUSH) {
19597c478bd9Sstevel@tonic-gate 				return (need_more);
19607c478bd9Sstevel@tonic-gate 			}
19617c478bd9Sstevel@tonic-gate 			if (s->lookahead == 0)
19627c478bd9Sstevel@tonic-gate 				break;	/* flush the current block */
19637c478bd9Sstevel@tonic-gate 		}
19647c478bd9Sstevel@tonic-gate 
19657c478bd9Sstevel@tonic-gate 		/*
19667c478bd9Sstevel@tonic-gate 		 * Insert the string window[strstart .. strstart+2] in
19677c478bd9Sstevel@tonic-gate 		 * the dictionary, and set hash_head to the head of
19687c478bd9Sstevel@tonic-gate 		 * the hash chain:
19697c478bd9Sstevel@tonic-gate 		 */
19707c478bd9Sstevel@tonic-gate 		if (s->lookahead >= MIN_MATCH) {
19717c478bd9Sstevel@tonic-gate 			INSERT_STRING(s, s->strstart, hash_head);
19727c478bd9Sstevel@tonic-gate 		}
19737c478bd9Sstevel@tonic-gate 
19747c478bd9Sstevel@tonic-gate 		/*
19757c478bd9Sstevel@tonic-gate 		 * Find the longest match, discarding those <=
19767c478bd9Sstevel@tonic-gate 		 * prev_length.  At this point we have always
19777c478bd9Sstevel@tonic-gate 		 * match_length < MIN_MATCH
19787c478bd9Sstevel@tonic-gate 		 */
19797c478bd9Sstevel@tonic-gate 		if (hash_head != NIL && s->strstart - hash_head <=
19807c478bd9Sstevel@tonic-gate 		    MAX_DIST(s)) {
19817c478bd9Sstevel@tonic-gate 			/*
19827c478bd9Sstevel@tonic-gate 			 * To simplify the code, we prevent matches
19837c478bd9Sstevel@tonic-gate 			 * with the string of window index 0 (in
19847c478bd9Sstevel@tonic-gate 			 * particular we have to avoid a match of the
19857c478bd9Sstevel@tonic-gate 			 * string with itself at the start of the
19867c478bd9Sstevel@tonic-gate 			 * input file).
19877c478bd9Sstevel@tonic-gate 			 */
19887c478bd9Sstevel@tonic-gate 			if (s->strategy != Z_HUFFMAN_ONLY) {
19897c478bd9Sstevel@tonic-gate 				s->match_length = longest_match(s, hash_head);
19907c478bd9Sstevel@tonic-gate 			}
19917c478bd9Sstevel@tonic-gate 			/* longest_match() sets match_start */
19927c478bd9Sstevel@tonic-gate 		}
19937c478bd9Sstevel@tonic-gate 		if (s->match_length >= MIN_MATCH) {
19947c478bd9Sstevel@tonic-gate 			check_match(s, s->strstart, s->match_start,
19957c478bd9Sstevel@tonic-gate 			    s->match_length);
19967c478bd9Sstevel@tonic-gate 
19977c478bd9Sstevel@tonic-gate 			_tr_tally_dist(s, s->strstart - s->match_start,
19987c478bd9Sstevel@tonic-gate 			    s->match_length - MIN_MATCH, bflush);
19997c478bd9Sstevel@tonic-gate 
20007c478bd9Sstevel@tonic-gate 			s->lookahead -= s->match_length;
20017c478bd9Sstevel@tonic-gate 
20027c478bd9Sstevel@tonic-gate 			/*
20037c478bd9Sstevel@tonic-gate 			 * Insert new strings in the hash table only
20047c478bd9Sstevel@tonic-gate 			 * if the match length is not too large. This
20057c478bd9Sstevel@tonic-gate 			 * saves time but degrades compression.
20067c478bd9Sstevel@tonic-gate 			 */
20077c478bd9Sstevel@tonic-gate #ifndef FASTEST
20087c478bd9Sstevel@tonic-gate 			if (s->match_length <= s->max_insert_length &&
20097c478bd9Sstevel@tonic-gate 			    s->lookahead >= MIN_MATCH) {
20107c478bd9Sstevel@tonic-gate 				/* string at strstart already in hash table */
20117c478bd9Sstevel@tonic-gate 				s->match_length--;
20127c478bd9Sstevel@tonic-gate 				do {
20137c478bd9Sstevel@tonic-gate 					s->strstart++;
20147c478bd9Sstevel@tonic-gate 					INSERT_STRING(s, s->strstart,
20157c478bd9Sstevel@tonic-gate 					    hash_head);
20167c478bd9Sstevel@tonic-gate 					/*
20177c478bd9Sstevel@tonic-gate 					 * strstart never exceeds
20187c478bd9Sstevel@tonic-gate 					 * WSIZE-MAX_MATCH, so there
20197c478bd9Sstevel@tonic-gate 					 * are always MIN_MATCH bytes
20207c478bd9Sstevel@tonic-gate 					 * ahead.
20217c478bd9Sstevel@tonic-gate 					 */
20227c478bd9Sstevel@tonic-gate 				} while (--s->match_length != 0);
20237c478bd9Sstevel@tonic-gate 				s->strstart++;
20247c478bd9Sstevel@tonic-gate 			} else
20257c478bd9Sstevel@tonic-gate #endif
20267c478bd9Sstevel@tonic-gate 			{
20277c478bd9Sstevel@tonic-gate 				s->strstart += s->match_length;
20287c478bd9Sstevel@tonic-gate 				s->match_length = 0;
20297c478bd9Sstevel@tonic-gate 				s->ins_h = s->window[s->strstart];
20307c478bd9Sstevel@tonic-gate 				UPDATE_HASH(s, s->ins_h,
20317c478bd9Sstevel@tonic-gate 				    s->window[s->strstart+1]);
20327c478bd9Sstevel@tonic-gate #if MIN_MATCH != 3
20337c478bd9Sstevel@tonic-gate 				Call UPDATE_HASH() MIN_MATCH-3 more times
20347c478bd9Sstevel@tonic-gate #endif
20357c478bd9Sstevel@tonic-gate 				/*
20367c478bd9Sstevel@tonic-gate 				 * If lookahead < MIN_MATCH, ins_h is
20377c478bd9Sstevel@tonic-gate 				 * garbage, but it does not matter
20387c478bd9Sstevel@tonic-gate 				 * since it will be recomputed at next
20397c478bd9Sstevel@tonic-gate 				 * deflate call.
20407c478bd9Sstevel@tonic-gate 				 */
20417c478bd9Sstevel@tonic-gate 			}
20427c478bd9Sstevel@tonic-gate 		} else {
20437c478bd9Sstevel@tonic-gate 			/* No match, output a literal byte */
20447c478bd9Sstevel@tonic-gate 			Tracevv((stderr, "%c", s->window[s->strstart]));
20457c478bd9Sstevel@tonic-gate 			_tr_tally_lit(s, s->window[s->strstart], bflush);
20467c478bd9Sstevel@tonic-gate 			s->lookahead--;
20477c478bd9Sstevel@tonic-gate 			s->strstart++;
20487c478bd9Sstevel@tonic-gate 		}
20497c478bd9Sstevel@tonic-gate 		if (bflush) FLUSH_BLOCK(s, 0);
20507c478bd9Sstevel@tonic-gate 	}
20517c478bd9Sstevel@tonic-gate 	FLUSH_BLOCK(s, flush == Z_FINISH);
20527c478bd9Sstevel@tonic-gate 	return (flush == Z_FINISH ? finish_done : block_done);
20537c478bd9Sstevel@tonic-gate }
20547c478bd9Sstevel@tonic-gate 
20557c478bd9Sstevel@tonic-gate /*
20567c478bd9Sstevel@tonic-gate  * ===========================================================================
20577c478bd9Sstevel@tonic-gate  * Same as above, but achieves better compression. We use a lazy
20587c478bd9Sstevel@tonic-gate  * evaluation for matches: a match is finally adopted only if there is
20597c478bd9Sstevel@tonic-gate  * no better match at the next window position.
20607c478bd9Sstevel@tonic-gate  */
20617c478bd9Sstevel@tonic-gate local block_state
deflate_slow(s,flush)20627c478bd9Sstevel@tonic-gate deflate_slow(s, flush)
20637c478bd9Sstevel@tonic-gate     deflate_state *s;
20647c478bd9Sstevel@tonic-gate     int flush;
20657c478bd9Sstevel@tonic-gate {
20667c478bd9Sstevel@tonic-gate 	IPos hash_head = NIL;	/* head of hash chain */
20677c478bd9Sstevel@tonic-gate 	int bflush;	/* set if current block must be flushed */
20687c478bd9Sstevel@tonic-gate 
20697c478bd9Sstevel@tonic-gate 	/* Process the input block. */
20707c478bd9Sstevel@tonic-gate 	for (;;) {
20717c478bd9Sstevel@tonic-gate 		/*
20727c478bd9Sstevel@tonic-gate 		 * Make sure that we always have enough lookahead,
20737c478bd9Sstevel@tonic-gate 		 * except at the end of the input file. We need
20747c478bd9Sstevel@tonic-gate 		 * MAX_MATCH bytes for the next match, plus MIN_MATCH
20757c478bd9Sstevel@tonic-gate 		 * bytes to insert the string following the next
20767c478bd9Sstevel@tonic-gate 		 * match.
20777c478bd9Sstevel@tonic-gate 		 */
20787c478bd9Sstevel@tonic-gate 		if (s->lookahead < MIN_LOOKAHEAD) {
20797c478bd9Sstevel@tonic-gate 			fill_window(s);
20807c478bd9Sstevel@tonic-gate 			if (s->lookahead < MIN_LOOKAHEAD &&
20817c478bd9Sstevel@tonic-gate 			    flush == Z_NO_FLUSH) {
20827c478bd9Sstevel@tonic-gate 				return (need_more);
20837c478bd9Sstevel@tonic-gate 			}
20847c478bd9Sstevel@tonic-gate 			/* flush the current block */
20857c478bd9Sstevel@tonic-gate 			if (s->lookahead == 0)
20867c478bd9Sstevel@tonic-gate 				break;
20877c478bd9Sstevel@tonic-gate 		}
20887c478bd9Sstevel@tonic-gate 
20897c478bd9Sstevel@tonic-gate 		/*
20907c478bd9Sstevel@tonic-gate 		 * Insert the string window[strstart .. strstart+2] in
20917c478bd9Sstevel@tonic-gate 		 * the dictionary, and set hash_head to the head of
20927c478bd9Sstevel@tonic-gate 		 * the hash chain:
20937c478bd9Sstevel@tonic-gate 		 */
20947c478bd9Sstevel@tonic-gate 		if (s->lookahead >= MIN_MATCH) {
20957c478bd9Sstevel@tonic-gate 			INSERT_STRING(s, s->strstart, hash_head);
20967c478bd9Sstevel@tonic-gate 		}
20977c478bd9Sstevel@tonic-gate 
20987c478bd9Sstevel@tonic-gate 		/*
20997c478bd9Sstevel@tonic-gate 		 * Find the longest match, discarding those <=
21007c478bd9Sstevel@tonic-gate 		 * prev_length.
21017c478bd9Sstevel@tonic-gate 		 */
21027c478bd9Sstevel@tonic-gate 		s->prev_length = s->match_length;
21037c478bd9Sstevel@tonic-gate 		s->prev_match = s->match_start;
21047c478bd9Sstevel@tonic-gate 		s->match_length = MIN_MATCH-1;
21057c478bd9Sstevel@tonic-gate 
21067c478bd9Sstevel@tonic-gate 		if (hash_head != NIL && s->prev_length < s->max_lazy_match &&
21077c478bd9Sstevel@tonic-gate 		    s->strstart - hash_head <= MAX_DIST(s)) {
21087c478bd9Sstevel@tonic-gate 			/*
21097c478bd9Sstevel@tonic-gate 			 * To simplify the code, we prevent matches
21107c478bd9Sstevel@tonic-gate 			 * with the string of window index 0 (in
21117c478bd9Sstevel@tonic-gate 			 * particular we have to avoid a match of the
21127c478bd9Sstevel@tonic-gate 			 * string with itself at the start of the
21137c478bd9Sstevel@tonic-gate 			 * input file).
21147c478bd9Sstevel@tonic-gate 			 */
21157c478bd9Sstevel@tonic-gate 			if (s->strategy != Z_HUFFMAN_ONLY) {
21167c478bd9Sstevel@tonic-gate 				s->match_length = longest_match(s, hash_head);
21177c478bd9Sstevel@tonic-gate 			}
21187c478bd9Sstevel@tonic-gate 			/* longest_match() sets match_start */
21197c478bd9Sstevel@tonic-gate 
21207c478bd9Sstevel@tonic-gate 			if (s->match_length <= 5 &&
21217c478bd9Sstevel@tonic-gate 			    (s->strategy == Z_FILTERED ||
21227c478bd9Sstevel@tonic-gate 				(s->match_length == MIN_MATCH &&
21237c478bd9Sstevel@tonic-gate 				    s->strstart - s->match_start > TOO_FAR))) {
21247c478bd9Sstevel@tonic-gate 
21257c478bd9Sstevel@tonic-gate 				/*
21267c478bd9Sstevel@tonic-gate 				 * If prev_match is also MIN_MATCH,
21277c478bd9Sstevel@tonic-gate 				 * match_start is garbage but we will
21287c478bd9Sstevel@tonic-gate 				 * ignore the current match anyway.
21297c478bd9Sstevel@tonic-gate 				 */
21307c478bd9Sstevel@tonic-gate 				s->match_length = MIN_MATCH-1;
21317c478bd9Sstevel@tonic-gate 			}
21327c478bd9Sstevel@tonic-gate 		}
21337c478bd9Sstevel@tonic-gate 		/*
21347c478bd9Sstevel@tonic-gate 		 * If there was a match at the previous step and the
21357c478bd9Sstevel@tonic-gate 		 * current match is not better, output the previous
21367c478bd9Sstevel@tonic-gate 		 * match:
21377c478bd9Sstevel@tonic-gate 		 */
21387c478bd9Sstevel@tonic-gate 		if (s->prev_length >= MIN_MATCH &&
21397c478bd9Sstevel@tonic-gate 		    s->match_length <= s->prev_length) {
21407c478bd9Sstevel@tonic-gate 			uInt max_insert = s->strstart + s->lookahead -
21417c478bd9Sstevel@tonic-gate 			    MIN_MATCH;
21427c478bd9Sstevel@tonic-gate 			/* Do not insert strings in hash table beyond this. */
21437c478bd9Sstevel@tonic-gate 
21447c478bd9Sstevel@tonic-gate 			check_match(s, s->strstart-1, s->prev_match,
21457c478bd9Sstevel@tonic-gate 			    s->prev_length);
21467c478bd9Sstevel@tonic-gate 
21477c478bd9Sstevel@tonic-gate 			_tr_tally_dist(s, s->strstart -1 - s->prev_match,
21487c478bd9Sstevel@tonic-gate 			    s->prev_length - MIN_MATCH, bflush);
21497c478bd9Sstevel@tonic-gate 
21507c478bd9Sstevel@tonic-gate 			/*
21517c478bd9Sstevel@tonic-gate 			 * Insert in hash table all strings up to the
21527c478bd9Sstevel@tonic-gate 			 * end of the match.  strstart-1 and strstart
21537c478bd9Sstevel@tonic-gate 			 * are already inserted. If there is not
21547c478bd9Sstevel@tonic-gate 			 * enough lookahead, the last two strings are
21557c478bd9Sstevel@tonic-gate 			 * not inserted in the hash table.
21567c478bd9Sstevel@tonic-gate 			 */
21577c478bd9Sstevel@tonic-gate 			s->lookahead -= s->prev_length-1;
21587c478bd9Sstevel@tonic-gate 			s->prev_length -= 2;
21597c478bd9Sstevel@tonic-gate 			do {
21607c478bd9Sstevel@tonic-gate 				if (++s->strstart <= max_insert) {
21617c478bd9Sstevel@tonic-gate 					INSERT_STRING(s, s->strstart,
21627c478bd9Sstevel@tonic-gate 					    hash_head);
21637c478bd9Sstevel@tonic-gate 				}
21647c478bd9Sstevel@tonic-gate 			} while (--s->prev_length != 0);
21657c478bd9Sstevel@tonic-gate 			s->match_available = 0;
21667c478bd9Sstevel@tonic-gate 			s->match_length = MIN_MATCH-1;
21677c478bd9Sstevel@tonic-gate 			s->strstart++;
21687c478bd9Sstevel@tonic-gate 
21697c478bd9Sstevel@tonic-gate 			if (bflush) FLUSH_BLOCK(s, 0);
21707c478bd9Sstevel@tonic-gate 
21717c478bd9Sstevel@tonic-gate 		} else if (s->match_available) {
21727c478bd9Sstevel@tonic-gate 			/*
21737c478bd9Sstevel@tonic-gate 			 * If there was no match at the previous
21747c478bd9Sstevel@tonic-gate 			 * position, output a single literal. If there
21757c478bd9Sstevel@tonic-gate 			 * was a match but the current match is
21767c478bd9Sstevel@tonic-gate 			 * longer, truncate the previous match to a
21777c478bd9Sstevel@tonic-gate 			 * single literal.
21787c478bd9Sstevel@tonic-gate 			 */
21797c478bd9Sstevel@tonic-gate 			Tracevv((stderr, "%c", s->window[s->strstart-1]));
21807c478bd9Sstevel@tonic-gate 			_tr_tally_lit(s, s->window[s->strstart-1], bflush);
21817c478bd9Sstevel@tonic-gate 			if (bflush) {
21827c478bd9Sstevel@tonic-gate 				FLUSH_BLOCK_ONLY(s, 0);
21837c478bd9Sstevel@tonic-gate 			}
21847c478bd9Sstevel@tonic-gate 			s->strstart++;
21857c478bd9Sstevel@tonic-gate 			s->lookahead--;
21867c478bd9Sstevel@tonic-gate 			if (s->strm->avail_out == 0)
21877c478bd9Sstevel@tonic-gate 				return (need_more);
21887c478bd9Sstevel@tonic-gate 		} else {
21897c478bd9Sstevel@tonic-gate 			/*
21907c478bd9Sstevel@tonic-gate 			 * There is no previous match to compare with,
21917c478bd9Sstevel@tonic-gate 			 * wait for the next step to decide.
21927c478bd9Sstevel@tonic-gate 			 */
21937c478bd9Sstevel@tonic-gate 			s->match_available = 1;
21947c478bd9Sstevel@tonic-gate 			s->strstart++;
21957c478bd9Sstevel@tonic-gate 			s->lookahead--;
21967c478bd9Sstevel@tonic-gate 		}
21977c478bd9Sstevel@tonic-gate 	}
21987c478bd9Sstevel@tonic-gate 	Assert(flush != Z_NO_FLUSH, "no flush?");
21997c478bd9Sstevel@tonic-gate 	if (s->match_available) {
22007c478bd9Sstevel@tonic-gate 		Tracevv((stderr, "%c", s->window[s->strstart-1]));
22017c478bd9Sstevel@tonic-gate 		_tr_tally_lit(s, s->window[s->strstart-1], bflush);
22027c478bd9Sstevel@tonic-gate 		s->match_available = 0;
22037c478bd9Sstevel@tonic-gate 	}
22047c478bd9Sstevel@tonic-gate 	FLUSH_BLOCK(s, flush == Z_FINISH);
22057c478bd9Sstevel@tonic-gate 	return (flush == Z_FINISH ? finish_done : block_done);
22067c478bd9Sstevel@tonic-gate }
22077c478bd9Sstevel@tonic-gate /* --- deflate.c */
22087c478bd9Sstevel@tonic-gate 
22097c478bd9Sstevel@tonic-gate /* +++ trees.c */
22107c478bd9Sstevel@tonic-gate /*
22117c478bd9Sstevel@tonic-gate  * trees.c -- output deflated data using Huffman coding
22127c478bd9Sstevel@tonic-gate  * Copyright (C) 1995-1998 Jean-loup Gailly
22137c478bd9Sstevel@tonic-gate  * For conditions of distribution and use, see copyright notice in zlib.h
22147c478bd9Sstevel@tonic-gate  */
22157c478bd9Sstevel@tonic-gate 
22167c478bd9Sstevel@tonic-gate /*
22177c478bd9Sstevel@tonic-gate  *  ALGORITHM
22187c478bd9Sstevel@tonic-gate  *
22197c478bd9Sstevel@tonic-gate  *      The "deflation" process uses several Huffman trees. The more
22207c478bd9Sstevel@tonic-gate  *      common source values are represented by shorter bit sequences.
22217c478bd9Sstevel@tonic-gate  *
22227c478bd9Sstevel@tonic-gate  *      Each code tree is stored in a compressed form which is itself
22237c478bd9Sstevel@tonic-gate  * a Huffman encoding of the lengths of all the code strings (in
22247c478bd9Sstevel@tonic-gate  * ascending order by source values).  The actual code strings are
22257c478bd9Sstevel@tonic-gate  * reconstructed from the lengths in the inflate process, as described
22267c478bd9Sstevel@tonic-gate  * in the deflate specification.
22277c478bd9Sstevel@tonic-gate  *
22287c478bd9Sstevel@tonic-gate  *  REFERENCES
22297c478bd9Sstevel@tonic-gate  *
22307c478bd9Sstevel@tonic-gate  *      Deutsch, L.P.,"'Deflate' Compressed Data Format Specification".
22317c478bd9Sstevel@tonic-gate  *      Available in ftp.uu.net:/pub/archiving/zip/doc/deflate-1.1.doc
22327c478bd9Sstevel@tonic-gate  *
22337c478bd9Sstevel@tonic-gate  *      Storer, James A.
22347c478bd9Sstevel@tonic-gate  *          Data Compression:  Methods and Theory, pp. 49-50.
22357c478bd9Sstevel@tonic-gate  *          Computer Science Press, 1988.  ISBN 0-7167-8156-5.
22367c478bd9Sstevel@tonic-gate  *
22377c478bd9Sstevel@tonic-gate  *      Sedgewick, R.
22387c478bd9Sstevel@tonic-gate  *          Algorithms, p290.
22397c478bd9Sstevel@tonic-gate  *          Addison-Wesley, 1983. ISBN 0-201-06672-6.
22407c478bd9Sstevel@tonic-gate  */
22417c478bd9Sstevel@tonic-gate 
22427c478bd9Sstevel@tonic-gate /* From: trees.c,v 1.11 1996/07/24 13:41:06 me Exp $ */
22437c478bd9Sstevel@tonic-gate 
22447c478bd9Sstevel@tonic-gate /* #include "deflate.h" */
22457c478bd9Sstevel@tonic-gate 
22467c478bd9Sstevel@tonic-gate #ifdef DEBUG_ZLIB
22477c478bd9Sstevel@tonic-gate #include <ctype.h>
22487c478bd9Sstevel@tonic-gate #endif
22497c478bd9Sstevel@tonic-gate 
22507c478bd9Sstevel@tonic-gate /*
22517c478bd9Sstevel@tonic-gate  * ===========================================================================
22527c478bd9Sstevel@tonic-gate  * Constants
22537c478bd9Sstevel@tonic-gate  */
22547c478bd9Sstevel@tonic-gate 
22557c478bd9Sstevel@tonic-gate #define	MAX_BL_BITS 7
22567c478bd9Sstevel@tonic-gate /* Bit length codes must not exceed MAX_BL_BITS bits */
22577c478bd9Sstevel@tonic-gate 
22587c478bd9Sstevel@tonic-gate #define	END_BLOCK 256
22597c478bd9Sstevel@tonic-gate /* end of block literal code */
22607c478bd9Sstevel@tonic-gate 
22617c478bd9Sstevel@tonic-gate #define	REP_3_6		16
22627c478bd9Sstevel@tonic-gate /* repeat previous bit length 3-6 times (2 bits of repeat count) */
22637c478bd9Sstevel@tonic-gate 
22647c478bd9Sstevel@tonic-gate #define	REPZ_3_10	17
22657c478bd9Sstevel@tonic-gate /* repeat a zero length 3-10 times  (3 bits of repeat count) */
22667c478bd9Sstevel@tonic-gate 
22677c478bd9Sstevel@tonic-gate #define	REPZ_11_138	18
22687c478bd9Sstevel@tonic-gate /* repeat a zero length 11-138 times  (7 bits of repeat count) */
22697c478bd9Sstevel@tonic-gate 
22707c478bd9Sstevel@tonic-gate /* extra bits for each length code */
22717c478bd9Sstevel@tonic-gate local const int extra_lbits[LENGTH_CODES] = {
22727c478bd9Sstevel@tonic-gate 	0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4,
22737c478bd9Sstevel@tonic-gate 	4, 4, 4, 5, 5, 5, 5, 0};
22747c478bd9Sstevel@tonic-gate 
22757c478bd9Sstevel@tonic-gate /* extra bits for each distance code */
22767c478bd9Sstevel@tonic-gate local const int extra_dbits[D_CODES] = {
22777c478bd9Sstevel@tonic-gate 	0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9,
22787c478bd9Sstevel@tonic-gate 	9, 10, 10, 11, 11, 12, 12, 13, 13};
22797c478bd9Sstevel@tonic-gate 
22807c478bd9Sstevel@tonic-gate /* extra bits for each bit length code */
22817c478bd9Sstevel@tonic-gate local const int extra_blbits[BL_CODES] = {
22827c478bd9Sstevel@tonic-gate 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 7};
22837c478bd9Sstevel@tonic-gate 
22847c478bd9Sstevel@tonic-gate local const uch bl_order[BL_CODES] = {
22857c478bd9Sstevel@tonic-gate 	16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
22867c478bd9Sstevel@tonic-gate 
22877c478bd9Sstevel@tonic-gate /*
22887c478bd9Sstevel@tonic-gate  * The lengths of the bit length codes are sent in order of decreasing
22897c478bd9Sstevel@tonic-gate  * probability, to avoid transmitting the lengths for unused bit
22907c478bd9Sstevel@tonic-gate  * length codes.
22917c478bd9Sstevel@tonic-gate  */
22927c478bd9Sstevel@tonic-gate 
22937c478bd9Sstevel@tonic-gate #define	Buf_size (8 * 2*sizeof (char))
22947c478bd9Sstevel@tonic-gate /*
22957c478bd9Sstevel@tonic-gate  * Number of bits used within bi_buf. (bi_buf might be implemented on
22967c478bd9Sstevel@tonic-gate  * more than 16 bits on some systems.)
22977c478bd9Sstevel@tonic-gate  */
22987c478bd9Sstevel@tonic-gate 
22997c478bd9Sstevel@tonic-gate /*
23007c478bd9Sstevel@tonic-gate  * ===========================================================================
23017c478bd9Sstevel@tonic-gate  * Local data. These are initialized only once.
23027c478bd9Sstevel@tonic-gate  */
23037c478bd9Sstevel@tonic-gate #define	DIST_CODE_LEN  512 /* see definition of array dist_code below */
23047c478bd9Sstevel@tonic-gate 
23057c478bd9Sstevel@tonic-gate local ct_data static_ltree[L_CODES+2];
23067c478bd9Sstevel@tonic-gate /*
23077c478bd9Sstevel@tonic-gate  * The static literal tree. Since the bit lengths are imposed, there
23087c478bd9Sstevel@tonic-gate  * is no need for the L_CODES extra codes used during heap
23097c478bd9Sstevel@tonic-gate  * construction. However The codes 286 and 287 are needed to build a
23107c478bd9Sstevel@tonic-gate  * canonical tree (see _tr_init below).
23117c478bd9Sstevel@tonic-gate  */
23127c478bd9Sstevel@tonic-gate 
23137c478bd9Sstevel@tonic-gate local ct_data static_dtree[D_CODES];
23147c478bd9Sstevel@tonic-gate /*
23157c478bd9Sstevel@tonic-gate  * The static distance tree. (Actually a trivial tree since all codes
23167c478bd9Sstevel@tonic-gate  * use 5 bits.)
23177c478bd9Sstevel@tonic-gate  */
23187c478bd9Sstevel@tonic-gate 
23197c478bd9Sstevel@tonic-gate local uch _dist_code[512];
23207c478bd9Sstevel@tonic-gate /*
23217c478bd9Sstevel@tonic-gate  * distance codes. The first 256 values correspond to the distances 3
23227c478bd9Sstevel@tonic-gate  * .. 258, the last 256 values correspond to the top 8 bits of the 15
23237c478bd9Sstevel@tonic-gate  * bit distances.
23247c478bd9Sstevel@tonic-gate  */
23257c478bd9Sstevel@tonic-gate 
23267c478bd9Sstevel@tonic-gate local uch _length_code[MAX_MATCH-MIN_MATCH+1];
23277c478bd9Sstevel@tonic-gate /* length code for each normalized match length (0 == MIN_MATCH) */
23287c478bd9Sstevel@tonic-gate 
23297c478bd9Sstevel@tonic-gate local int base_length[LENGTH_CODES];
23307c478bd9Sstevel@tonic-gate /* First normalized length for each code (0 = MIN_MATCH) */
23317c478bd9Sstevel@tonic-gate 
23327c478bd9Sstevel@tonic-gate local int base_dist[D_CODES];
23337c478bd9Sstevel@tonic-gate /* First normalized distance for each code (0 = distance of 1) */
23347c478bd9Sstevel@tonic-gate 
23357c478bd9Sstevel@tonic-gate struct static_tree_desc_s {
23367c478bd9Sstevel@tonic-gate 	const ct_data *static_tree;	/* static tree or NULL */
23377c478bd9Sstevel@tonic-gate 	const intf    *extra_bits;	/* extra bits for each code or NULL */
23387c478bd9Sstevel@tonic-gate 	int	extra_base;	/* base index for extra_bits */
23397c478bd9Sstevel@tonic-gate 	int	elems;	/* max number of elements in the tree */
23407c478bd9Sstevel@tonic-gate 	int	max_length;	/* max bit length for the codes */
23417c478bd9Sstevel@tonic-gate };
23427c478bd9Sstevel@tonic-gate 
23437c478bd9Sstevel@tonic-gate local static_tree_desc  static_l_desc = {
23447c478bd9Sstevel@tonic-gate 	static_ltree, extra_lbits, LITERALS+1,	L_CODES, MAX_BITS};
23457c478bd9Sstevel@tonic-gate 
23467c478bd9Sstevel@tonic-gate local static_tree_desc  static_d_desc = {
23477c478bd9Sstevel@tonic-gate 	static_dtree, extra_dbits, 0,		D_CODES, MAX_BITS};
23487c478bd9Sstevel@tonic-gate 
23497c478bd9Sstevel@tonic-gate local static_tree_desc  static_bl_desc = {
23507c478bd9Sstevel@tonic-gate 	(const ct_data *)0, extra_blbits, 0,		BL_CODES, MAX_BL_BITS};
23517c478bd9Sstevel@tonic-gate 
23527c478bd9Sstevel@tonic-gate /*
23537c478bd9Sstevel@tonic-gate  * ===========================================================================
23547c478bd9Sstevel@tonic-gate  * Local (static) routines in this file.
23557c478bd9Sstevel@tonic-gate  */
23567c478bd9Sstevel@tonic-gate 
23577c478bd9Sstevel@tonic-gate local void tr_static_init OF((void));
23587c478bd9Sstevel@tonic-gate local void init_block	OF((deflate_state *s));
23597c478bd9Sstevel@tonic-gate local void pqdownheap	OF((deflate_state *s, ct_data *tree, int k));
23607c478bd9Sstevel@tonic-gate local void gen_bitlen	OF((deflate_state *s, tree_desc *desc));
23617c478bd9Sstevel@tonic-gate local void gen_codes	OF((ct_data *tree, int max_code, ushf *bl_count));
23627c478bd9Sstevel@tonic-gate local void build_tree	OF((deflate_state *s, tree_desc *desc));
23637c478bd9Sstevel@tonic-gate local void scan_tree	OF((deflate_state *s, ct_data *tree, int max_code));
23647c478bd9Sstevel@tonic-gate local void send_tree	OF((deflate_state *s, ct_data *tree, int max_code));
23657c478bd9Sstevel@tonic-gate local int  build_bl_tree	OF((deflate_state *s));
23667c478bd9Sstevel@tonic-gate local void send_all_trees	OF((deflate_state *s, int lcodes, int dcodes,
23677c478bd9Sstevel@tonic-gate     int blcodes));
23687c478bd9Sstevel@tonic-gate local void compress_block OF((deflate_state *s, ct_data *ltree,
23697c478bd9Sstevel@tonic-gate     ct_data *dtree));
23707c478bd9Sstevel@tonic-gate local void set_data_type	OF((deflate_state *s));
23717c478bd9Sstevel@tonic-gate local unsigned bi_reverse	OF((unsigned value, int length));
23727c478bd9Sstevel@tonic-gate local void bi_windup	OF((deflate_state *s));
23737c478bd9Sstevel@tonic-gate local void bi_flush	OF((deflate_state *s));
23747c478bd9Sstevel@tonic-gate local void copy_block	OF((deflate_state *s, charf *buf, unsigned len,
23757c478bd9Sstevel@tonic-gate     int header));
23767c478bd9Sstevel@tonic-gate 
23777c478bd9Sstevel@tonic-gate #ifndef DEBUG_ZLIB
23787c478bd9Sstevel@tonic-gate #define	send_code(s, c, tree) send_bits(s, tree[c].Code, tree[c].Len)
23797c478bd9Sstevel@tonic-gate /* Send a code of the given tree. c and tree must not have side effects */
23807c478bd9Sstevel@tonic-gate 
23817c478bd9Sstevel@tonic-gate #else /* DEBUG_ZLIB */
23827c478bd9Sstevel@tonic-gate #define	send_code(s, c, tree) \
23837c478bd9Sstevel@tonic-gate 	{ if (z_verbose > 2) fprintf(stderr, "\ncd %3d ", (c)); \
23847c478bd9Sstevel@tonic-gate 	send_bits(s, tree[c].Code, tree[c].Len); }
23857c478bd9Sstevel@tonic-gate #endif
23867c478bd9Sstevel@tonic-gate 
23877c478bd9Sstevel@tonic-gate /*
23887c478bd9Sstevel@tonic-gate  * ===========================================================================
23897c478bd9Sstevel@tonic-gate  * Output a short LSB first on the stream.
23907c478bd9Sstevel@tonic-gate  * IN assertion: there is enough room in pendingBuf.
23917c478bd9Sstevel@tonic-gate  */
23927c478bd9Sstevel@tonic-gate #define	put_short(s, w) { \
23937c478bd9Sstevel@tonic-gate 	put_byte(s, (uch)((w) & 0xff)); \
23947c478bd9Sstevel@tonic-gate 	put_byte(s, (uch)((ush)(w) >> 8)); \
23957c478bd9Sstevel@tonic-gate }
23967c478bd9Sstevel@tonic-gate 
23977c478bd9Sstevel@tonic-gate /*
23987c478bd9Sstevel@tonic-gate  * ===========================================================================
23997c478bd9Sstevel@tonic-gate  * Send a value on a given number of bits.
24007c478bd9Sstevel@tonic-gate  * IN assertion: length <= 16 and value fits in length bits.
24017c478bd9Sstevel@tonic-gate  */
24027c478bd9Sstevel@tonic-gate #ifdef DEBUG_ZLIB
24037c478bd9Sstevel@tonic-gate local void send_bits	OF((deflate_state *s, int value, int length));
24047c478bd9Sstevel@tonic-gate 
24057c478bd9Sstevel@tonic-gate local void
send_bits(s,value,length)24067c478bd9Sstevel@tonic-gate send_bits(s, value, length)
24077c478bd9Sstevel@tonic-gate     deflate_state *s;
24087c478bd9Sstevel@tonic-gate     int value;	/* value to send */
24097c478bd9Sstevel@tonic-gate     int length;	/* number of bits */
24107c478bd9Sstevel@tonic-gate {
24117c478bd9Sstevel@tonic-gate 	Tracevv((stderr, " l %2d v %4x ", length, value));
24127c478bd9Sstevel@tonic-gate 	Assert(length > 0 && length <= 15, "invalid length");
24137c478bd9Sstevel@tonic-gate 	s->bits_sent += (ulg)length;
24147c478bd9Sstevel@tonic-gate 
24157c478bd9Sstevel@tonic-gate 	/*
24167c478bd9Sstevel@tonic-gate 	 * If not enough room in bi_buf, use (valid) bits from bi_buf
24177c478bd9Sstevel@tonic-gate 	 * and (16 - bi_valid) bits from value, leaving (width -
24187c478bd9Sstevel@tonic-gate 	 * (16-bi_valid)) unused bits in value.
24197c478bd9Sstevel@tonic-gate 	 */
24207c478bd9Sstevel@tonic-gate 	if (s->bi_valid > (int)Buf_size - length) {
24217c478bd9Sstevel@tonic-gate 		s->bi_buf |= (value << s->bi_valid);
24227c478bd9Sstevel@tonic-gate 		put_short(s, s->bi_buf);
24237c478bd9Sstevel@tonic-gate 		s->bi_buf = (ush)value >> (Buf_size - s->bi_valid);
24247c478bd9Sstevel@tonic-gate 		s->bi_valid += length - Buf_size;
24257c478bd9Sstevel@tonic-gate 	} else {
24267c478bd9Sstevel@tonic-gate 		s->bi_buf |= value << s->bi_valid;
24277c478bd9Sstevel@tonic-gate 		s->bi_valid += length;
24287c478bd9Sstevel@tonic-gate 	}
24297c478bd9Sstevel@tonic-gate }
24307c478bd9Sstevel@tonic-gate #else /* !DEBUG_ZLIB */
24317c478bd9Sstevel@tonic-gate 
24327c478bd9Sstevel@tonic-gate #define	send_bits(s, value, length) \
24337c478bd9Sstevel@tonic-gate {	int len = length; \
24347c478bd9Sstevel@tonic-gate 	if (s->bi_valid > (int)Buf_size - len) {\
24357c478bd9Sstevel@tonic-gate 		int val = value; \
24367c478bd9Sstevel@tonic-gate 		s->bi_buf |= (val << s->bi_valid); \
24377c478bd9Sstevel@tonic-gate 		put_short(s, s->bi_buf); \
24387c478bd9Sstevel@tonic-gate 		s->bi_buf = (ush)val >> (Buf_size - s->bi_valid); \
24397c478bd9Sstevel@tonic-gate 		s->bi_valid += len - Buf_size; \
24407c478bd9Sstevel@tonic-gate 	} else {\
24417c478bd9Sstevel@tonic-gate 		s->bi_buf |= (value) << s->bi_valid; \
24427c478bd9Sstevel@tonic-gate 		s->bi_valid += len; \
24437c478bd9Sstevel@tonic-gate 	}\
24447c478bd9Sstevel@tonic-gate }
24457c478bd9Sstevel@tonic-gate #endif /* DEBUG_ZLIB */
24467c478bd9Sstevel@tonic-gate 
24477c478bd9Sstevel@tonic-gate 
24487c478bd9Sstevel@tonic-gate #define	MAX(a, b) (a >= b ? a : b)
24497c478bd9Sstevel@tonic-gate /* the arguments must not have side effects */
24507c478bd9Sstevel@tonic-gate 
24517c478bd9Sstevel@tonic-gate /*
24527c478bd9Sstevel@tonic-gate  * ===========================================================================
24537c478bd9Sstevel@tonic-gate  * Initialize the various 'constant' tables. In a multi-threaded environment,
24547c478bd9Sstevel@tonic-gate  * this function may be called by two threads concurrently, but this is
24557c478bd9Sstevel@tonic-gate  * harmless since both invocations do exactly the same thing.
24567c478bd9Sstevel@tonic-gate  */
24577c478bd9Sstevel@tonic-gate local void
tr_static_init()24587c478bd9Sstevel@tonic-gate tr_static_init()
24597c478bd9Sstevel@tonic-gate {
24607c478bd9Sstevel@tonic-gate 	static int static_init_done = 0;
24617c478bd9Sstevel@tonic-gate 	int n;	/* iterates over tree elements */
24627c478bd9Sstevel@tonic-gate 	int bits;	/* bit counter */
24637c478bd9Sstevel@tonic-gate 	int length;	/* length value */
24647c478bd9Sstevel@tonic-gate 	int code;	/* code value */
24657c478bd9Sstevel@tonic-gate 	int dist;	/* distance index */
24667c478bd9Sstevel@tonic-gate 	ush bl_count[MAX_BITS+1];
24677c478bd9Sstevel@tonic-gate 	/* number of codes at each bit length for an optimal tree */
24687c478bd9Sstevel@tonic-gate 
24697c478bd9Sstevel@tonic-gate 	if (static_init_done)
24707c478bd9Sstevel@tonic-gate 		return;
24717c478bd9Sstevel@tonic-gate 
24727c478bd9Sstevel@tonic-gate 	/* For some embedded targets, global variables are not initialized: */
24737c478bd9Sstevel@tonic-gate 	static_l_desc.static_tree = static_ltree;
24747c478bd9Sstevel@tonic-gate 	static_l_desc.extra_bits = extra_lbits;
24757c478bd9Sstevel@tonic-gate 	static_d_desc.static_tree = static_dtree;
24767c478bd9Sstevel@tonic-gate 	static_d_desc.extra_bits = extra_dbits;
24777c478bd9Sstevel@tonic-gate 	static_bl_desc.extra_bits = extra_blbits;
24787c478bd9Sstevel@tonic-gate 
24797c478bd9Sstevel@tonic-gate 	/* Initialize the mapping length (0..255) -> length code (0..28) */
24807c478bd9Sstevel@tonic-gate 	length = 0;
24817c478bd9Sstevel@tonic-gate 	for (code = 0; code < LENGTH_CODES-1; code++) {
24827c478bd9Sstevel@tonic-gate 		base_length[code] = length;
24837c478bd9Sstevel@tonic-gate 		for (n = 0; n < (1<<extra_lbits[code]); n++) {
24847c478bd9Sstevel@tonic-gate 			_length_code[length++] = (uch)code;
24857c478bd9Sstevel@tonic-gate 		}
24867c478bd9Sstevel@tonic-gate 	}
24877c478bd9Sstevel@tonic-gate 	Assert(length == 256, "tr_static_init: length != 256");
24887c478bd9Sstevel@tonic-gate 	/*
24897c478bd9Sstevel@tonic-gate 	 * Note that the length 255 (match length 258) can be
24907c478bd9Sstevel@tonic-gate 	 * represented in two different ways: code 284 + 5 bits or
24917c478bd9Sstevel@tonic-gate 	 * code 285, so we overwrite _length_code[255] to use the best
24927c478bd9Sstevel@tonic-gate 	 * encoding:
24937c478bd9Sstevel@tonic-gate 	 */
24947c478bd9Sstevel@tonic-gate 	_length_code[length-1] = (uch)code;
24957c478bd9Sstevel@tonic-gate 
24967c478bd9Sstevel@tonic-gate 	/* Initialize the mapping dist (0..32K) -> dist code (0..29) */
24977c478bd9Sstevel@tonic-gate 	dist = 0;
24987c478bd9Sstevel@tonic-gate 	for (code = 0; code < 16; code++) {
24997c478bd9Sstevel@tonic-gate 		base_dist[code] = dist;
25007c478bd9Sstevel@tonic-gate 		for (n = 0; n < (1<<extra_dbits[code]); n++) {
25017c478bd9Sstevel@tonic-gate 			_dist_code[dist++] = (uch)code;
25027c478bd9Sstevel@tonic-gate 		}
25037c478bd9Sstevel@tonic-gate 	}
25047c478bd9Sstevel@tonic-gate 	Assert(dist == 256, "tr_static_init: dist != 256");
25057c478bd9Sstevel@tonic-gate 	dist >>= 7;	/* from now on, all distances are divided by 128 */
25067c478bd9Sstevel@tonic-gate 	for (; code < D_CODES; code++) {
25077c478bd9Sstevel@tonic-gate 		base_dist[code] = dist << 7;
25087c478bd9Sstevel@tonic-gate 		for (n = 0; n < (1<<(extra_dbits[code]-7)); n++) {
25097c478bd9Sstevel@tonic-gate 			_dist_code[256 + dist++] = (uch)code;
25107c478bd9Sstevel@tonic-gate 		}
25117c478bd9Sstevel@tonic-gate 	}
25127c478bd9Sstevel@tonic-gate 	Assert(dist == 256, "tr_static_init: 256+dist != 512");
25137c478bd9Sstevel@tonic-gate 
25147c478bd9Sstevel@tonic-gate 	/* Construct the codes of the static literal tree */
25157c478bd9Sstevel@tonic-gate 	for (bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 0;
25167c478bd9Sstevel@tonic-gate 	n = 0;
25177c478bd9Sstevel@tonic-gate 	while (n <= 143) static_ltree[n++].Len = 8, bl_count[8]++;
25187c478bd9Sstevel@tonic-gate 	while (n <= 255) static_ltree[n++].Len = 9, bl_count[9]++;
25197c478bd9Sstevel@tonic-gate 	while (n <= 279) static_ltree[n++].Len = 7, bl_count[7]++;
25207c478bd9Sstevel@tonic-gate 	while (n <= 287) static_ltree[n++].Len = 8, bl_count[8]++;
25217c478bd9Sstevel@tonic-gate 	/*
25227c478bd9Sstevel@tonic-gate 	 * Codes 286 and 287 do not exist, but we must include them in the
25237c478bd9Sstevel@tonic-gate 	 * tree construction to get a canonical Huffman tree (longest code
25247c478bd9Sstevel@tonic-gate 	 * all ones)
25257c478bd9Sstevel@tonic-gate 	 */
25267c478bd9Sstevel@tonic-gate 	gen_codes((ct_data *)static_ltree, L_CODES+1, bl_count);
25277c478bd9Sstevel@tonic-gate 
25287c478bd9Sstevel@tonic-gate 	/* The static distance tree is trivial: */
25297c478bd9Sstevel@tonic-gate 	for (n = 0; n < D_CODES; n++) {
25307c478bd9Sstevel@tonic-gate 		static_dtree[n].Len = 5;
25317c478bd9Sstevel@tonic-gate 		static_dtree[n].Code = bi_reverse((unsigned)n, 5);
25327c478bd9Sstevel@tonic-gate 	}
25337c478bd9Sstevel@tonic-gate 	static_init_done = 1;
25347c478bd9Sstevel@tonic-gate }
25357c478bd9Sstevel@tonic-gate 
25367c478bd9Sstevel@tonic-gate /*
25377c478bd9Sstevel@tonic-gate  * ===========================================================================
25387c478bd9Sstevel@tonic-gate  * Initialize the tree data structures for a new zlib stream.
25397c478bd9Sstevel@tonic-gate  */
25407c478bd9Sstevel@tonic-gate void
_tr_init(s)25417c478bd9Sstevel@tonic-gate _tr_init(s)
25427c478bd9Sstevel@tonic-gate     deflate_state *s;
25437c478bd9Sstevel@tonic-gate {
25447c478bd9Sstevel@tonic-gate 	tr_static_init();
25457c478bd9Sstevel@tonic-gate 
25467c478bd9Sstevel@tonic-gate 	s->l_desc.dyn_tree = s->dyn_ltree;
25477c478bd9Sstevel@tonic-gate 	s->l_desc.stat_desc = &static_l_desc;
25487c478bd9Sstevel@tonic-gate 
25497c478bd9Sstevel@tonic-gate 	s->d_desc.dyn_tree = s->dyn_dtree;
25507c478bd9Sstevel@tonic-gate 	s->d_desc.stat_desc = &static_d_desc;
25517c478bd9Sstevel@tonic-gate 
25527c478bd9Sstevel@tonic-gate 	s->bl_desc.dyn_tree = s->bl_tree;
25537c478bd9Sstevel@tonic-gate 	s->bl_desc.stat_desc = &static_bl_desc;
25547c478bd9Sstevel@tonic-gate 
25557c478bd9Sstevel@tonic-gate 	s->bi_buf = 0;
25567c478bd9Sstevel@tonic-gate 	s->bi_valid = 0;
25577c478bd9Sstevel@tonic-gate 	s->last_eob_len = 8;	/* enough lookahead for inflate */
25587c478bd9Sstevel@tonic-gate 	s->compressed_len = 0L;		/* PPP */
25597c478bd9Sstevel@tonic-gate #ifdef DEBUG_ZLIB
25607c478bd9Sstevel@tonic-gate 	s->bits_sent = 0L;
25617c478bd9Sstevel@tonic-gate #endif
25627c478bd9Sstevel@tonic-gate 
25637c478bd9Sstevel@tonic-gate 	/* Initialize the first block of the first file: */
25647c478bd9Sstevel@tonic-gate 	init_block(s);
25657c478bd9Sstevel@tonic-gate }
25667c478bd9Sstevel@tonic-gate 
25677c478bd9Sstevel@tonic-gate /*
25687c478bd9Sstevel@tonic-gate  * ===========================================================================
25697c478bd9Sstevel@tonic-gate  * Initialize a new block.
25707c478bd9Sstevel@tonic-gate  */
25717c478bd9Sstevel@tonic-gate local void
init_block(s)25727c478bd9Sstevel@tonic-gate init_block(s)
25737c478bd9Sstevel@tonic-gate     deflate_state *s;
25747c478bd9Sstevel@tonic-gate {
25757c478bd9Sstevel@tonic-gate 	int n;	/* iterates over tree elements */
25767c478bd9Sstevel@tonic-gate 
25777c478bd9Sstevel@tonic-gate 	/* Initialize the trees. */
25787c478bd9Sstevel@tonic-gate 	for (n = 0; n < L_CODES;  n++) s->dyn_ltree[n].Freq = 0;
25797c478bd9Sstevel@tonic-gate 	for (n = 0; n < D_CODES;  n++) s->dyn_dtree[n].Freq = 0;
25807c478bd9Sstevel@tonic-gate 	for (n = 0; n < BL_CODES; n++) s->bl_tree[n].Freq = 0;
25817c478bd9Sstevel@tonic-gate 
25827c478bd9Sstevel@tonic-gate 	s->dyn_ltree[END_BLOCK].Freq = 1;
25837c478bd9Sstevel@tonic-gate 	s->opt_len = s->static_len = 0L;
25847c478bd9Sstevel@tonic-gate 	s->last_lit = s->matches = 0;
25857c478bd9Sstevel@tonic-gate }
25867c478bd9Sstevel@tonic-gate 
25877c478bd9Sstevel@tonic-gate #define	SMALLEST 1
25887c478bd9Sstevel@tonic-gate /* Index within the heap array of least frequent node in the Huffman tree */
25897c478bd9Sstevel@tonic-gate 
25907c478bd9Sstevel@tonic-gate 
25917c478bd9Sstevel@tonic-gate /*
25927c478bd9Sstevel@tonic-gate  * ===========================================================================
25937c478bd9Sstevel@tonic-gate  * Remove the smallest element from the heap and recreate the heap with
25947c478bd9Sstevel@tonic-gate  * one less element. Updates heap and heap_len.
25957c478bd9Sstevel@tonic-gate  */
25967c478bd9Sstevel@tonic-gate #define	pqremove(s, tree, top) \
25977c478bd9Sstevel@tonic-gate {\
25987c478bd9Sstevel@tonic-gate 	top = s->heap[SMALLEST]; \
25997c478bd9Sstevel@tonic-gate 	s->heap[SMALLEST] = s->heap[s->heap_len--]; \
26007c478bd9Sstevel@tonic-gate 	pqdownheap(s, tree, SMALLEST); \
26017c478bd9Sstevel@tonic-gate }
26027c478bd9Sstevel@tonic-gate 
26037c478bd9Sstevel@tonic-gate /*
26047c478bd9Sstevel@tonic-gate  * ===========================================================================
26057c478bd9Sstevel@tonic-gate  * Compares to subtrees, using the tree depth as tie breaker when
26067c478bd9Sstevel@tonic-gate  * the subtrees have equal frequency. This minimizes the worst case length.
26077c478bd9Sstevel@tonic-gate  */
26087c478bd9Sstevel@tonic-gate #define	smaller(tree, n, m, depth) \
26097c478bd9Sstevel@tonic-gate 	(tree[n].Freq < tree[m].Freq || \
26107c478bd9Sstevel@tonic-gate 	(tree[n].Freq == tree[m].Freq && depth[n] <= depth[m]))
26117c478bd9Sstevel@tonic-gate /*
26127c478bd9Sstevel@tonic-gate  * ===========================================================================
26137c478bd9Sstevel@tonic-gate  * Restore the heap property by moving down the tree starting at node k,
26147c478bd9Sstevel@tonic-gate  * exchanging a node with the smallest of its two sons if necessary, stopping
26157c478bd9Sstevel@tonic-gate  * when the heap property is re-established (each father smaller than its
26167c478bd9Sstevel@tonic-gate  * two sons).
26177c478bd9Sstevel@tonic-gate  */
26187c478bd9Sstevel@tonic-gate local void
pqdownheap(s,tree,k)26197c478bd9Sstevel@tonic-gate pqdownheap(s, tree, k)
26207c478bd9Sstevel@tonic-gate     deflate_state *s;
26217c478bd9Sstevel@tonic-gate     ct_data *tree;	/* the tree to restore */
26227c478bd9Sstevel@tonic-gate     int k;	/* node to move down */
26237c478bd9Sstevel@tonic-gate {
26247c478bd9Sstevel@tonic-gate 	int v = s->heap[k];
26257c478bd9Sstevel@tonic-gate 	int j = k << 1;	/* left son of k */
26267c478bd9Sstevel@tonic-gate 	while (j <= s->heap_len) {
26277c478bd9Sstevel@tonic-gate 		/* Set j to the smallest of the two sons: */
26287c478bd9Sstevel@tonic-gate 		if (j < s->heap_len &&
26297c478bd9Sstevel@tonic-gate 		    smaller(tree, s->heap[j+1], s->heap[j], s->depth)) {
26307c478bd9Sstevel@tonic-gate 			j++;
26317c478bd9Sstevel@tonic-gate 		}
26327c478bd9Sstevel@tonic-gate 		/* Exit if v is smaller than both sons */
26337c478bd9Sstevel@tonic-gate 		if (smaller(tree, v, s->heap[j], s->depth)) break;
26347c478bd9Sstevel@tonic-gate 
26357c478bd9Sstevel@tonic-gate 		/* Exchange v with the smallest son */
26367c478bd9Sstevel@tonic-gate 		s->heap[k] = s->heap[j];  k = j;
26377c478bd9Sstevel@tonic-gate 
26387c478bd9Sstevel@tonic-gate 		/* And continue down the tree, setting j to the left son of k */
26397c478bd9Sstevel@tonic-gate 		j <<= 1;
26407c478bd9Sstevel@tonic-gate 	}
26417c478bd9Sstevel@tonic-gate 	s->heap[k] = v;
26427c478bd9Sstevel@tonic-gate }
26437c478bd9Sstevel@tonic-gate 
26447c478bd9Sstevel@tonic-gate /*
26457c478bd9Sstevel@tonic-gate  * ===========================================================================
26467c478bd9Sstevel@tonic-gate  * Compute the optimal bit lengths for a tree and update the total bit length
26477c478bd9Sstevel@tonic-gate  * for the current block.
26487c478bd9Sstevel@tonic-gate  * IN assertion: the fields freq and dad are set, heap[heap_max] and
26497c478bd9Sstevel@tonic-gate  *    above are the tree nodes sorted by increasing frequency.
26507c478bd9Sstevel@tonic-gate  * OUT assertions: the field len is set to the optimal bit length, the
26517c478bd9Sstevel@tonic-gate  *     array bl_count contains the frequencies for each bit length.
26527c478bd9Sstevel@tonic-gate  *     The length opt_len is updated; static_len is also updated if stree is
26537c478bd9Sstevel@tonic-gate  *     not null.
26547c478bd9Sstevel@tonic-gate  */
26557c478bd9Sstevel@tonic-gate local void
gen_bitlen(s,desc)26567c478bd9Sstevel@tonic-gate gen_bitlen(s, desc)
26577c478bd9Sstevel@tonic-gate     deflate_state *s;
26587c478bd9Sstevel@tonic-gate     tree_desc *desc;	/* the tree descriptor */
26597c478bd9Sstevel@tonic-gate {
26607c478bd9Sstevel@tonic-gate 	ct_data *tree  = desc->dyn_tree;
26617c478bd9Sstevel@tonic-gate 	int max_code   = desc->max_code;
26627c478bd9Sstevel@tonic-gate 	const ct_data *stree = desc->stat_desc->static_tree;
26637c478bd9Sstevel@tonic-gate 	const intf *extra    = desc->stat_desc->extra_bits;
26647c478bd9Sstevel@tonic-gate 	int base	= desc->stat_desc->extra_base;
26657c478bd9Sstevel@tonic-gate 	int max_length = desc->stat_desc->max_length;
26667c478bd9Sstevel@tonic-gate 	int h;	/* heap index */
26677c478bd9Sstevel@tonic-gate 	int n, m;	/* iterate over the tree elements */
26687c478bd9Sstevel@tonic-gate 	int bits;	/* bit length */
26697c478bd9Sstevel@tonic-gate 	int xbits;	/* extra bits */
26707c478bd9Sstevel@tonic-gate 	ush f;	/* frequency */
26717c478bd9Sstevel@tonic-gate 	/* number of elements with bit length too large */
26727c478bd9Sstevel@tonic-gate 	int overflow = 0;
26737c478bd9Sstevel@tonic-gate 
26747c478bd9Sstevel@tonic-gate 	for (bits = 0; bits <= MAX_BITS; bits++) s->bl_count[bits] = 0;
26757c478bd9Sstevel@tonic-gate 
26767c478bd9Sstevel@tonic-gate 	/*
26777c478bd9Sstevel@tonic-gate 	 * In a first pass, compute the optimal bit lengths (which may
26787c478bd9Sstevel@tonic-gate 	 * overflow in the case of the bit length tree).
26797c478bd9Sstevel@tonic-gate 	 */
26807c478bd9Sstevel@tonic-gate 	tree[s->heap[s->heap_max]].Len = 0;	/* root of the heap */
26817c478bd9Sstevel@tonic-gate 
26827c478bd9Sstevel@tonic-gate 	for (h = s->heap_max+1; h < HEAP_SIZE; h++) {
26837c478bd9Sstevel@tonic-gate 		n = s->heap[h];
26847c478bd9Sstevel@tonic-gate 		bits = tree[tree[n].Dad].Len + 1;
26857c478bd9Sstevel@tonic-gate 		if (bits > max_length) bits = max_length, overflow++;
26867c478bd9Sstevel@tonic-gate 		tree[n].Len = (ush)bits;
26877c478bd9Sstevel@tonic-gate 		/* We overwrite tree[n].Dad which is no longer needed */
26887c478bd9Sstevel@tonic-gate 
26897c478bd9Sstevel@tonic-gate 		if (n > max_code) continue;	/* not a leaf node */
26907c478bd9Sstevel@tonic-gate 
26917c478bd9Sstevel@tonic-gate 		s->bl_count[bits]++;
26927c478bd9Sstevel@tonic-gate 		xbits = 0;
26937c478bd9Sstevel@tonic-gate 		if (n >= base) xbits = extra[n-base];
26947c478bd9Sstevel@tonic-gate 		f = tree[n].Freq;
26957c478bd9Sstevel@tonic-gate 		s->opt_len += (ulg)f * (bits + xbits);
26967c478bd9Sstevel@tonic-gate 		if (stree) s->static_len += (ulg)f * (stree[n].Len + xbits);
26977c478bd9Sstevel@tonic-gate 	}
26987c478bd9Sstevel@tonic-gate 	if (overflow == 0)
26997c478bd9Sstevel@tonic-gate 		return;
27007c478bd9Sstevel@tonic-gate 
27017c478bd9Sstevel@tonic-gate 	Trace((stderr, "\nbit length overflow\n"));
27027c478bd9Sstevel@tonic-gate 	/* This happens for example on obj2 and pic of the Calgary corpus */
27037c478bd9Sstevel@tonic-gate 
27047c478bd9Sstevel@tonic-gate 	/* Find the first bit length which could increase: */
27057c478bd9Sstevel@tonic-gate 	do {
27067c478bd9Sstevel@tonic-gate 		bits = max_length-1;
27077c478bd9Sstevel@tonic-gate 		while (s->bl_count[bits] == 0) bits--;
27087c478bd9Sstevel@tonic-gate 		s->bl_count[bits]--;	/* move one leaf down the tree */
27097c478bd9Sstevel@tonic-gate 		/* move one overflow item as its brother */
27107c478bd9Sstevel@tonic-gate 		s->bl_count[bits+1] += 2;
27117c478bd9Sstevel@tonic-gate 		s->bl_count[max_length]--;
27127c478bd9Sstevel@tonic-gate 		/*
27137c478bd9Sstevel@tonic-gate 		 * The brother of the overflow item also moves one
27147c478bd9Sstevel@tonic-gate 		 * step up, but this does not affect
27157c478bd9Sstevel@tonic-gate 		 * bl_count[max_length]
27167c478bd9Sstevel@tonic-gate 		 */
27177c478bd9Sstevel@tonic-gate 		overflow -= 2;
27187c478bd9Sstevel@tonic-gate 	} while (overflow > 0);
27197c478bd9Sstevel@tonic-gate 
27207c478bd9Sstevel@tonic-gate 	/*
27217c478bd9Sstevel@tonic-gate 	 * Now recompute all bit lengths, scanning in increasing
27227c478bd9Sstevel@tonic-gate 	 * frequency.  h is still equal to HEAP_SIZE. (It is simpler
27237c478bd9Sstevel@tonic-gate 	 * to reconstruct all lengths instead of fixing only the wrong
27247c478bd9Sstevel@tonic-gate 	 * ones. This idea is taken from 'ar' written by Haruhiko
27257c478bd9Sstevel@tonic-gate 	 * Okumura.)
27267c478bd9Sstevel@tonic-gate 	 */
27277c478bd9Sstevel@tonic-gate 	for (bits = max_length; bits != 0; bits--) {
27287c478bd9Sstevel@tonic-gate 		n = s->bl_count[bits];
27297c478bd9Sstevel@tonic-gate 		while (n != 0) {
27307c478bd9Sstevel@tonic-gate 			m = s->heap[--h];
27317c478bd9Sstevel@tonic-gate 			if (m > max_code) continue;
27327c478bd9Sstevel@tonic-gate 			if (tree[m].Len != (unsigned)bits) {
27337c478bd9Sstevel@tonic-gate 				Trace((stderr, "code %d bits %d->%d\n", m,
27347c478bd9Sstevel@tonic-gate 				    tree[m].Len, bits));
27357c478bd9Sstevel@tonic-gate 				s->opt_len += ((long)bits - (long)tree[m].Len)
27367c478bd9Sstevel@tonic-gate 				    *(long)tree[m].Freq;
27377c478bd9Sstevel@tonic-gate 				tree[m].Len = (ush)bits;
27387c478bd9Sstevel@tonic-gate 			}
27397c478bd9Sstevel@tonic-gate 			n--;
27407c478bd9Sstevel@tonic-gate 		}
27417c478bd9Sstevel@tonic-gate 	}
27427c478bd9Sstevel@tonic-gate }
27437c478bd9Sstevel@tonic-gate 
27447c478bd9Sstevel@tonic-gate /*
27457c478bd9Sstevel@tonic-gate  * ===========================================================================
27467c478bd9Sstevel@tonic-gate  * Generate the codes for a given tree and bit counts (which need not be
27477c478bd9Sstevel@tonic-gate  * optimal).
27487c478bd9Sstevel@tonic-gate  * IN assertion: the array bl_count contains the bit length statistics for
27497c478bd9Sstevel@tonic-gate  * the given tree and the field len is set for all tree elements.
27507c478bd9Sstevel@tonic-gate  * OUT assertion: the field code is set for all tree elements of non
27517c478bd9Sstevel@tonic-gate  *     zero code length.
27527c478bd9Sstevel@tonic-gate  */
27537c478bd9Sstevel@tonic-gate local void
gen_codes(tree,max_code,bl_count)27547c478bd9Sstevel@tonic-gate gen_codes(tree, max_code, bl_count)
27557c478bd9Sstevel@tonic-gate     ct_data *tree;	/* the tree to decorate */
27567c478bd9Sstevel@tonic-gate     int max_code;	/* largest code with non zero frequency */
27577c478bd9Sstevel@tonic-gate     ushf *bl_count;	/* number of codes at each bit length */
27587c478bd9Sstevel@tonic-gate {
27597c478bd9Sstevel@tonic-gate 	/* next code value for each bit length */
27607c478bd9Sstevel@tonic-gate 	ush next_code[MAX_BITS+1];
27617c478bd9Sstevel@tonic-gate 	ush code = 0;	/* running code value */
27627c478bd9Sstevel@tonic-gate 	int bits;	/* bit index */
27637c478bd9Sstevel@tonic-gate 	int n;	/* code index */
27647c478bd9Sstevel@tonic-gate 
27657c478bd9Sstevel@tonic-gate 	/*
27667c478bd9Sstevel@tonic-gate 	 * The distribution counts are first used to generate the code
27677c478bd9Sstevel@tonic-gate 	 * values without bit reversal.
27687c478bd9Sstevel@tonic-gate 	 */
27697c478bd9Sstevel@tonic-gate 	for (bits = 1; bits <= MAX_BITS; bits++) {
27707c478bd9Sstevel@tonic-gate 		next_code[bits] = code = (code + bl_count[bits-1]) << 1;
27717c478bd9Sstevel@tonic-gate 	}
27727c478bd9Sstevel@tonic-gate 	/*
27737c478bd9Sstevel@tonic-gate 	 * Check that the bit counts in bl_count are consistent. The
27747c478bd9Sstevel@tonic-gate 	 * last code must be all ones.
27757c478bd9Sstevel@tonic-gate 	 */
27767c478bd9Sstevel@tonic-gate 	Assert(code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1,
27777c478bd9Sstevel@tonic-gate 	    "inconsistent bit counts");
27787c478bd9Sstevel@tonic-gate 	Tracev((stderr, "\ngen_codes: max_code %d ", max_code));
27797c478bd9Sstevel@tonic-gate 
27807c478bd9Sstevel@tonic-gate 	for (n = 0;  n <= max_code; n++) {
27817c478bd9Sstevel@tonic-gate 		int len = tree[n].Len;
27827c478bd9Sstevel@tonic-gate 		if (len == 0) continue;
27837c478bd9Sstevel@tonic-gate 		/* Now reverse the bits */
27847c478bd9Sstevel@tonic-gate 		tree[n].Code = bi_reverse(next_code[len]++, len);
27857c478bd9Sstevel@tonic-gate 
27867c478bd9Sstevel@tonic-gate 		Tracecv(tree != static_ltree,
27877c478bd9Sstevel@tonic-gate 		    (stderr, "\nn %3d %c l %2d c %4x (%x) ",
27887c478bd9Sstevel@tonic-gate 		    n, (isgraph(n) ? n : ' '), len, tree[n].Code,
27897c478bd9Sstevel@tonic-gate 			next_code[len]-1));
27907c478bd9Sstevel@tonic-gate 	}
27917c478bd9Sstevel@tonic-gate }
27927c478bd9Sstevel@tonic-gate 
27937c478bd9Sstevel@tonic-gate /*
27947c478bd9Sstevel@tonic-gate  * ===========================================================================
27957c478bd9Sstevel@tonic-gate  * Construct one Huffman tree and assigns the code bit strings and lengths.
27967c478bd9Sstevel@tonic-gate  * Update the total bit length for the current block.
27977c478bd9Sstevel@tonic-gate  * IN assertion: the field freq is set for all tree elements.
27987c478bd9Sstevel@tonic-gate  * OUT assertions: the fields len and code are set to the optimal bit length
27997c478bd9Sstevel@tonic-gate  *     and corresponding code. The length opt_len is updated; static_len is
28007c478bd9Sstevel@tonic-gate  *     also updated if stree is not null. The field max_code is set.
28017c478bd9Sstevel@tonic-gate  */
28027c478bd9Sstevel@tonic-gate local void
build_tree(s,desc)28037c478bd9Sstevel@tonic-gate build_tree(s, desc)
28047c478bd9Sstevel@tonic-gate     deflate_state *s;
28057c478bd9Sstevel@tonic-gate     tree_desc *desc;	/* the tree descriptor */
28067c478bd9Sstevel@tonic-gate {
28077c478bd9Sstevel@tonic-gate 	ct_data *tree   = desc->dyn_tree;
28087c478bd9Sstevel@tonic-gate 	const ct_data *stree  = desc->stat_desc->static_tree;
28097c478bd9Sstevel@tonic-gate 	int elems	= desc->stat_desc->elems;
28107c478bd9Sstevel@tonic-gate 	int n, m;	/* iterate over heap elements */
28117c478bd9Sstevel@tonic-gate 	int max_code = -1;	/* largest code with non zero frequency */
28127c478bd9Sstevel@tonic-gate 	int node;	/* new node being created */
28137c478bd9Sstevel@tonic-gate 
28147c478bd9Sstevel@tonic-gate 	/*
28157c478bd9Sstevel@tonic-gate 	 * Construct the initial heap, with least frequent element in
28167c478bd9Sstevel@tonic-gate 	 * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and
28177c478bd9Sstevel@tonic-gate 	 * heap[2*n+1].  heap[0] is not used.
28187c478bd9Sstevel@tonic-gate 	 */
28197c478bd9Sstevel@tonic-gate 	s->heap_len = 0, s->heap_max = HEAP_SIZE;
28207c478bd9Sstevel@tonic-gate 
28217c478bd9Sstevel@tonic-gate 	for (n = 0; n < elems; n++) {
28227c478bd9Sstevel@tonic-gate 		if (tree[n].Freq != 0) {
28237c478bd9Sstevel@tonic-gate 			s->heap[++(s->heap_len)] = max_code = n;
28247c478bd9Sstevel@tonic-gate 			s->depth[n] = 0;
28257c478bd9Sstevel@tonic-gate 		} else {
28267c478bd9Sstevel@tonic-gate 			tree[n].Len = 0;
28277c478bd9Sstevel@tonic-gate 		}
28287c478bd9Sstevel@tonic-gate 	}
28297c478bd9Sstevel@tonic-gate 
28307c478bd9Sstevel@tonic-gate 	/*
28317c478bd9Sstevel@tonic-gate 	 * The pkzip format requires that at least one distance code
28327c478bd9Sstevel@tonic-gate 	 * exists, and that at least one bit should be sent even if
28337c478bd9Sstevel@tonic-gate 	 * there is only one possible code. So to avoid special checks
28347c478bd9Sstevel@tonic-gate 	 * later on we force at least two codes of non zero frequency.
28357c478bd9Sstevel@tonic-gate 	 */
28367c478bd9Sstevel@tonic-gate 	while (s->heap_len < 2) {
28377c478bd9Sstevel@tonic-gate 		node = s->heap[++(s->heap_len)] = (max_code < 2 ?
28387c478bd9Sstevel@tonic-gate 		    ++max_code : 0);
28397c478bd9Sstevel@tonic-gate 		tree[node].Freq = 1;
28407c478bd9Sstevel@tonic-gate 		s->depth[node] = 0;
28417c478bd9Sstevel@tonic-gate 		s->opt_len--; if (stree) s->static_len -= stree[node].Len;
28427c478bd9Sstevel@tonic-gate 		/* node is 0 or 1 so it does not have extra bits */
28437c478bd9Sstevel@tonic-gate 	}
28447c478bd9Sstevel@tonic-gate 	desc->max_code = max_code;
28457c478bd9Sstevel@tonic-gate 
28467c478bd9Sstevel@tonic-gate 	/*
28477c478bd9Sstevel@tonic-gate 	 * The elements heap[heap_len/2+1 .. heap_len] are leaves of
28487c478bd9Sstevel@tonic-gate 	 * the tree, establish sub-heaps of increasing lengths:
28497c478bd9Sstevel@tonic-gate 	 */
28507c478bd9Sstevel@tonic-gate 	for (n = s->heap_len/2; n >= 1; n--) pqdownheap(s, tree, n);
28517c478bd9Sstevel@tonic-gate 
28527c478bd9Sstevel@tonic-gate 	/*
28537c478bd9Sstevel@tonic-gate 	 * Construct the Huffman tree by repeatedly combining the
28547c478bd9Sstevel@tonic-gate 	 * least two frequent nodes.
28557c478bd9Sstevel@tonic-gate 	 */
28567c478bd9Sstevel@tonic-gate 	node = elems;	/* next internal node of the tree */
28577c478bd9Sstevel@tonic-gate 	do {
28587c478bd9Sstevel@tonic-gate 		pqremove(s, tree, n);	/* n = node of least frequency */
28597c478bd9Sstevel@tonic-gate 		m = s->heap[SMALLEST];	/* m = node of next least frequency */
28607c478bd9Sstevel@tonic-gate 
28617c478bd9Sstevel@tonic-gate 		/* keep the nodes sorted by frequency */
28627c478bd9Sstevel@tonic-gate 		s->heap[--(s->heap_max)] = n;
28637c478bd9Sstevel@tonic-gate 		s->heap[--(s->heap_max)] = m;
28647c478bd9Sstevel@tonic-gate 
28657c478bd9Sstevel@tonic-gate 		/* Create a new node father of n and m */
28667c478bd9Sstevel@tonic-gate 		tree[node].Freq = tree[n].Freq + tree[m].Freq;
28677c478bd9Sstevel@tonic-gate 		s->depth[node] = (uch) (MAX(s->depth[n], s->depth[m]) + 1);
28687c478bd9Sstevel@tonic-gate 		tree[n].Dad = tree[m].Dad = (ush)node;
28697c478bd9Sstevel@tonic-gate #ifdef DUMP_BL_TREE
28707c478bd9Sstevel@tonic-gate 		if (tree == s->bl_tree) {
28717c478bd9Sstevel@tonic-gate 			fprintf(stderr, "\nnode %d(%d), sons %d(%d) %d(%d)",
28727c478bd9Sstevel@tonic-gate 			    node, tree[node].Freq, n, tree[n].Freq, m,
28737c478bd9Sstevel@tonic-gate 			    tree[m].Freq);
28747c478bd9Sstevel@tonic-gate 		}
28757c478bd9Sstevel@tonic-gate #endif
28767c478bd9Sstevel@tonic-gate 		/* and insert the new node in the heap */
28777c478bd9Sstevel@tonic-gate 		s->heap[SMALLEST] = node++;
28787c478bd9Sstevel@tonic-gate 		pqdownheap(s, tree, SMALLEST);
28797c478bd9Sstevel@tonic-gate 
28807c478bd9Sstevel@tonic-gate 	} while (s->heap_len >= 2);
28817c478bd9Sstevel@tonic-gate 
28827c478bd9Sstevel@tonic-gate 	s->heap[--(s->heap_max)] = s->heap[SMALLEST];
28837c478bd9Sstevel@tonic-gate 
28847c478bd9Sstevel@tonic-gate 	/*
28857c478bd9Sstevel@tonic-gate 	 * At this point, the fields freq and dad are set. We can now
28867c478bd9Sstevel@tonic-gate 	 * generate the bit lengths.
28877c478bd9Sstevel@tonic-gate 	 */
28887c478bd9Sstevel@tonic-gate 	gen_bitlen(s, (tree_desc *)desc);
28897c478bd9Sstevel@tonic-gate 
28907c478bd9Sstevel@tonic-gate 	/* The field len is now set, we can generate the bit codes */
28917c478bd9Sstevel@tonic-gate 	gen_codes((ct_data *)tree, max_code, s->bl_count);
28927c478bd9Sstevel@tonic-gate }
28937c478bd9Sstevel@tonic-gate 
28947c478bd9Sstevel@tonic-gate /*
28957c478bd9Sstevel@tonic-gate  * ===========================================================================
28967c478bd9Sstevel@tonic-gate  * Scan a literal or distance tree to determine the frequencies of the codes
28977c478bd9Sstevel@tonic-gate  * in the bit length tree.
28987c478bd9Sstevel@tonic-gate  */
28997c478bd9Sstevel@tonic-gate local void
scan_tree(s,tree,max_code)29007c478bd9Sstevel@tonic-gate scan_tree(s, tree, max_code)
29017c478bd9Sstevel@tonic-gate     deflate_state *s;
29027c478bd9Sstevel@tonic-gate     ct_data *tree;	/* the tree to be scanned */
29037c478bd9Sstevel@tonic-gate     int max_code;	/* and its largest code of non zero frequency */
29047c478bd9Sstevel@tonic-gate {
29057c478bd9Sstevel@tonic-gate 	int n;	/* iterates over all tree elements */
29067c478bd9Sstevel@tonic-gate 	int prevlen = -1;	/* last emitted length */
29077c478bd9Sstevel@tonic-gate 	int curlen;	/* length of current code */
29087c478bd9Sstevel@tonic-gate 	int nextlen = tree[0].Len;	/* length of next code */
29097c478bd9Sstevel@tonic-gate 	int count = 0;	/* repeat count of the current code */
29107c478bd9Sstevel@tonic-gate 	int max_count = 7;	/* max repeat count */
29117c478bd9Sstevel@tonic-gate 	int min_count = 4;	/* min repeat count */
29127c478bd9Sstevel@tonic-gate 
29137c478bd9Sstevel@tonic-gate 	if (nextlen == 0) max_count = 138, min_count = 3;
29147c478bd9Sstevel@tonic-gate 	tree[max_code+1].Len = (ush)0xffff;	/* guard */
29157c478bd9Sstevel@tonic-gate 
29167c478bd9Sstevel@tonic-gate 	for (n = 0; n <= max_code; n++) {
29177c478bd9Sstevel@tonic-gate 		curlen = nextlen; nextlen = tree[n+1].Len;
29187c478bd9Sstevel@tonic-gate 		if (++count < max_count && curlen == nextlen) {
29197c478bd9Sstevel@tonic-gate 			continue;
29207c478bd9Sstevel@tonic-gate 		} else if (count < min_count) {
29217c478bd9Sstevel@tonic-gate 			s->bl_tree[curlen].Freq += count;
29227c478bd9Sstevel@tonic-gate 		} else if (curlen != 0) {
29237c478bd9Sstevel@tonic-gate 			if (curlen != prevlen) s->bl_tree[curlen].Freq++;
29247c478bd9Sstevel@tonic-gate 			s->bl_tree[REP_3_6].Freq++;
29257c478bd9Sstevel@tonic-gate 		} else if (count <= 10) {
29267c478bd9Sstevel@tonic-gate 			s->bl_tree[REPZ_3_10].Freq++;
29277c478bd9Sstevel@tonic-gate 		} else {
29287c478bd9Sstevel@tonic-gate 			s->bl_tree[REPZ_11_138].Freq++;
29297c478bd9Sstevel@tonic-gate 		}
29307c478bd9Sstevel@tonic-gate 		count = 0; prevlen = curlen;
29317c478bd9Sstevel@tonic-gate 		if (nextlen == 0) {
29327c478bd9Sstevel@tonic-gate 			max_count = 138, min_count = 3;
29337c478bd9Sstevel@tonic-gate 		} else if (curlen == nextlen) {
29347c478bd9Sstevel@tonic-gate 			max_count = 6, min_count = 3;
29357c478bd9Sstevel@tonic-gate 		} else {
29367c478bd9Sstevel@tonic-gate 			max_count = 7, min_count = 4;
29377c478bd9Sstevel@tonic-gate 		}
29387c478bd9Sstevel@tonic-gate 	}
29397c478bd9Sstevel@tonic-gate }
29407c478bd9Sstevel@tonic-gate 
29417c478bd9Sstevel@tonic-gate /*
29427c478bd9Sstevel@tonic-gate  * ===========================================================================
29437c478bd9Sstevel@tonic-gate  * Send a literal or distance tree in compressed form, using the codes in
29447c478bd9Sstevel@tonic-gate  * bl_tree.
29457c478bd9Sstevel@tonic-gate  */
29467c478bd9Sstevel@tonic-gate local void
send_tree(s,tree,max_code)29477c478bd9Sstevel@tonic-gate send_tree(s, tree, max_code)
29487c478bd9Sstevel@tonic-gate     deflate_state *s;
29497c478bd9Sstevel@tonic-gate     ct_data *tree;	/* the tree to be scanned */
29507c478bd9Sstevel@tonic-gate     int max_code;	/* and its largest code of non zero frequency */
29517c478bd9Sstevel@tonic-gate {
29527c478bd9Sstevel@tonic-gate 	int n;	/* iterates over all tree elements */
29537c478bd9Sstevel@tonic-gate 	int prevlen = -1;	/* last emitted length */
29547c478bd9Sstevel@tonic-gate 	int curlen;	/* length of current code */
29557c478bd9Sstevel@tonic-gate 	int nextlen = tree[0].Len;	/* length of next code */
29567c478bd9Sstevel@tonic-gate 	int count = 0;	/* repeat count of the current code */
29577c478bd9Sstevel@tonic-gate 	int max_count = 7;	/* max repeat count */
29587c478bd9Sstevel@tonic-gate 	int min_count = 4;	/* min repeat count */
29597c478bd9Sstevel@tonic-gate 
29607c478bd9Sstevel@tonic-gate 	/* tree[max_code+1].Len = -1; */  /* guard already set */
29617c478bd9Sstevel@tonic-gate 	if (nextlen == 0) max_count = 138, min_count = 3;
29627c478bd9Sstevel@tonic-gate 
29637c478bd9Sstevel@tonic-gate 	for (n = 0; n <= max_code; n++) {
29647c478bd9Sstevel@tonic-gate 		curlen = nextlen; nextlen = tree[n+1].Len;
29657c478bd9Sstevel@tonic-gate 		if (++count < max_count && curlen == nextlen) {
29667c478bd9Sstevel@tonic-gate 			continue;
29677c478bd9Sstevel@tonic-gate 		} else if (count < min_count) {
29687c478bd9Sstevel@tonic-gate 			do { send_code(s, curlen, s->bl_tree); }
29697c478bd9Sstevel@tonic-gate 			while (--count != 0);
29707c478bd9Sstevel@tonic-gate 
29717c478bd9Sstevel@tonic-gate 		} else if (curlen != 0) {
29727c478bd9Sstevel@tonic-gate 			if (curlen != prevlen) {
29737c478bd9Sstevel@tonic-gate 				send_code(s, curlen, s->bl_tree); count--;
29747c478bd9Sstevel@tonic-gate 			}
29757c478bd9Sstevel@tonic-gate 			Assert(count >= 3 && count <= 6, " 3_6?");
29767c478bd9Sstevel@tonic-gate 			send_code(s, REP_3_6, s->bl_tree);
29777c478bd9Sstevel@tonic-gate 			send_bits(s, count-3, 2);
29787c478bd9Sstevel@tonic-gate 
29797c478bd9Sstevel@tonic-gate 		} else if (count <= 10) {
29807c478bd9Sstevel@tonic-gate 			send_code(s, REPZ_3_10, s->bl_tree);
29817c478bd9Sstevel@tonic-gate 			send_bits(s, count-3, 3);
29827c478bd9Sstevel@tonic-gate 
29837c478bd9Sstevel@tonic-gate 		} else {
29847c478bd9Sstevel@tonic-gate 			send_code(s, REPZ_11_138, s->bl_tree);
29857c478bd9Sstevel@tonic-gate 			send_bits(s, count-11, 7);
29867c478bd9Sstevel@tonic-gate 		}
29877c478bd9Sstevel@tonic-gate 		count = 0; prevlen = curlen;
29887c478bd9Sstevel@tonic-gate 		if (nextlen == 0) {
29897c478bd9Sstevel@tonic-gate 			max_count = 138, min_count = 3;
29907c478bd9Sstevel@tonic-gate 		} else if (curlen == nextlen) {
29917c478bd9Sstevel@tonic-gate 			max_count = 6, min_count = 3;
29927c478bd9Sstevel@tonic-gate 		} else {
29937c478bd9Sstevel@tonic-gate 			max_count = 7, min_count = 4;
29947c478bd9Sstevel@tonic-gate 		}
29957c478bd9Sstevel@tonic-gate 	}
29967c478bd9Sstevel@tonic-gate }
29977c478bd9Sstevel@tonic-gate 
29987c478bd9Sstevel@tonic-gate /*
29997c478bd9Sstevel@tonic-gate  * ===========================================================================
30007c478bd9Sstevel@tonic-gate  * Construct the Huffman tree for the bit lengths and return the index in
30017c478bd9Sstevel@tonic-gate  * bl_order of the last bit length code to send.
30027c478bd9Sstevel@tonic-gate  */
30037c478bd9Sstevel@tonic-gate local int
build_bl_tree(s)30047c478bd9Sstevel@tonic-gate build_bl_tree(s)
30057c478bd9Sstevel@tonic-gate     deflate_state *s;
30067c478bd9Sstevel@tonic-gate {
30077c478bd9Sstevel@tonic-gate 	/* index of last bit length code of non zero freq */
30087c478bd9Sstevel@tonic-gate 	int max_blindex;
30097c478bd9Sstevel@tonic-gate 
30107c478bd9Sstevel@tonic-gate 	/*
30117c478bd9Sstevel@tonic-gate 	 * Determine the bit length frequencies for literal and
30127c478bd9Sstevel@tonic-gate 	 * distance trees
30137c478bd9Sstevel@tonic-gate 	 */
30147c478bd9Sstevel@tonic-gate 	scan_tree(s, (ct_data *)s->dyn_ltree, s->l_desc.max_code);
30157c478bd9Sstevel@tonic-gate 	scan_tree(s, (ct_data *)s->dyn_dtree, s->d_desc.max_code);
30167c478bd9Sstevel@tonic-gate 
30177c478bd9Sstevel@tonic-gate 	/* Build the bit length tree: */
30187c478bd9Sstevel@tonic-gate 	build_tree(s, (tree_desc *)(&(s->bl_desc)));
30197c478bd9Sstevel@tonic-gate 	/*
30207c478bd9Sstevel@tonic-gate 	 * opt_len now includes the length of the tree
30217c478bd9Sstevel@tonic-gate 	 * representations, except the lengths of the bit lengths
30227c478bd9Sstevel@tonic-gate 	 * codes and the 5+5+4 bits for the counts.
30237c478bd9Sstevel@tonic-gate 	 */
30247c478bd9Sstevel@tonic-gate 
30257c478bd9Sstevel@tonic-gate 	/*
30267c478bd9Sstevel@tonic-gate 	 * Determine the number of bit length codes to send. The pkzip
30277c478bd9Sstevel@tonic-gate 	 * format requires that at least 4 bit length codes be
30287c478bd9Sstevel@tonic-gate 	 * sent. (appnote.txt says 3 but the actual value used is 4.)
30297c478bd9Sstevel@tonic-gate 	 */
30307c478bd9Sstevel@tonic-gate 	for (max_blindex = BL_CODES-1; max_blindex >= 3; max_blindex--) {
30317c478bd9Sstevel@tonic-gate 		if (s->bl_tree[bl_order[max_blindex]].Len != 0) break;
30327c478bd9Sstevel@tonic-gate 	}
30337c478bd9Sstevel@tonic-gate 	/* Update opt_len to include the bit length tree and counts */
30347c478bd9Sstevel@tonic-gate 	s->opt_len += 3*(max_blindex+1) + 5+5+4;
30357c478bd9Sstevel@tonic-gate 	Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld",
30367c478bd9Sstevel@tonic-gate 	    s->opt_len, s->static_len));
30377c478bd9Sstevel@tonic-gate 
30387c478bd9Sstevel@tonic-gate 	return (max_blindex);
30397c478bd9Sstevel@tonic-gate }
30407c478bd9Sstevel@tonic-gate 
30417c478bd9Sstevel@tonic-gate /*
30427c478bd9Sstevel@tonic-gate  * ===========================================================================
30437c478bd9Sstevel@tonic-gate  * Send the header for a block using dynamic Huffman trees: the counts, the
30447c478bd9Sstevel@tonic-gate  * lengths of the bit length codes, the literal tree and the distance tree.
30457c478bd9Sstevel@tonic-gate  * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.
30467c478bd9Sstevel@tonic-gate  */
30477c478bd9Sstevel@tonic-gate local void
send_all_trees(s,lcodes,dcodes,blcodes)30487c478bd9Sstevel@tonic-gate send_all_trees(s, lcodes, dcodes, blcodes)
30497c478bd9Sstevel@tonic-gate     deflate_state *s;
30507c478bd9Sstevel@tonic-gate     int lcodes, dcodes, blcodes;	/* number of codes for each tree */
30517c478bd9Sstevel@tonic-gate {
30527c478bd9Sstevel@tonic-gate 	int rank;	/* index in bl_order */
30537c478bd9Sstevel@tonic-gate 
30547c478bd9Sstevel@tonic-gate 	Assert(lcodes >= 257 && dcodes >= 1 && blcodes >= 4,
30557c478bd9Sstevel@tonic-gate 	    "not enough codes");
30567c478bd9Sstevel@tonic-gate 	Assert(lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES,
30577c478bd9Sstevel@tonic-gate 	    "too many codes");
30587c478bd9Sstevel@tonic-gate 	Tracev((stderr, "\nbl counts: "));
30597c478bd9Sstevel@tonic-gate 	send_bits(s, lcodes-257, 5);	/* not +255 as stated in appnote.txt */
30607c478bd9Sstevel@tonic-gate 	send_bits(s, dcodes-1,   5);
30617c478bd9Sstevel@tonic-gate 	send_bits(s, blcodes-4,  4);	/* not -3 as stated in appnote.txt */
30627c478bd9Sstevel@tonic-gate 	for (rank = 0; rank < blcodes; rank++) {
30637c478bd9Sstevel@tonic-gate 		Tracev((stderr, "\nbl code %2d ", bl_order[rank]));
30647c478bd9Sstevel@tonic-gate 		send_bits(s, s->bl_tree[bl_order[rank]].Len, 3);
30657c478bd9Sstevel@tonic-gate 	}
30667c478bd9Sstevel@tonic-gate #ifdef DEBUG_ZLIB
30677c478bd9Sstevel@tonic-gate 	Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent));
30687c478bd9Sstevel@tonic-gate #endif
30697c478bd9Sstevel@tonic-gate 
30707c478bd9Sstevel@tonic-gate 	/* literal tree */
30717c478bd9Sstevel@tonic-gate 	send_tree(s, (ct_data *)s->dyn_ltree, lcodes-1);
30727c478bd9Sstevel@tonic-gate #ifdef DEBUG_ZLIB
30737c478bd9Sstevel@tonic-gate 	Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent));
30747c478bd9Sstevel@tonic-gate #endif
30757c478bd9Sstevel@tonic-gate 
30767c478bd9Sstevel@tonic-gate 	/* distance tree */
30777c478bd9Sstevel@tonic-gate 	send_tree(s, (ct_data *)s->dyn_dtree, dcodes-1);
30787c478bd9Sstevel@tonic-gate #ifdef DEBUG_ZLIB
30797c478bd9Sstevel@tonic-gate 	Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent));
30807c478bd9Sstevel@tonic-gate #endif
30817c478bd9Sstevel@tonic-gate }
30827c478bd9Sstevel@tonic-gate 
30837c478bd9Sstevel@tonic-gate /*
30847c478bd9Sstevel@tonic-gate  * ===========================================================================
30857c478bd9Sstevel@tonic-gate  * Send a stored block
30867c478bd9Sstevel@tonic-gate  */
30877c478bd9Sstevel@tonic-gate void
_tr_stored_block(s,buf,stored_len,eof)30887c478bd9Sstevel@tonic-gate _tr_stored_block(s, buf, stored_len, eof)
30897c478bd9Sstevel@tonic-gate     deflate_state *s;
30907c478bd9Sstevel@tonic-gate     charf *buf;	/* input block */
30917c478bd9Sstevel@tonic-gate     ulg stored_len;	/* length of input block */
30927c478bd9Sstevel@tonic-gate     int eof;	/* true if this is the last block for a file */
30937c478bd9Sstevel@tonic-gate {
30947c478bd9Sstevel@tonic-gate 	send_bits(s, (STORED_BLOCK<<1)+eof, 3);	/* send block type */
30957c478bd9Sstevel@tonic-gate 	s->compressed_len = (s->compressed_len + 3 + 7) & (ulg)~7L; /* PPP */
30967c478bd9Sstevel@tonic-gate 	s->compressed_len += (stored_len + 4) << 3;	/* PPP */
30977c478bd9Sstevel@tonic-gate 
30987c478bd9Sstevel@tonic-gate 	copy_block(s, buf, (unsigned)stored_len, 1);	/* with header */
30997c478bd9Sstevel@tonic-gate }
31007c478bd9Sstevel@tonic-gate 
31017c478bd9Sstevel@tonic-gate /*
31027c478bd9Sstevel@tonic-gate  * Send just the `stored block' type code without any length bytes or data.
31037c478bd9Sstevel@tonic-gate  * ---PPP---
31047c478bd9Sstevel@tonic-gate  */
31057c478bd9Sstevel@tonic-gate void
_tr_stored_type_only(s)31067c478bd9Sstevel@tonic-gate _tr_stored_type_only(s)
31077c478bd9Sstevel@tonic-gate     deflate_state *s;
31087c478bd9Sstevel@tonic-gate {
31097c478bd9Sstevel@tonic-gate 	send_bits(s, (STORED_BLOCK << 1), 3);
31107c478bd9Sstevel@tonic-gate 	bi_windup(s);
31117c478bd9Sstevel@tonic-gate 	s->compressed_len = (s->compressed_len + 3) & ~7L;	/* PPP */
31127c478bd9Sstevel@tonic-gate }
31137c478bd9Sstevel@tonic-gate 
31147c478bd9Sstevel@tonic-gate 
31157c478bd9Sstevel@tonic-gate /*
31167c478bd9Sstevel@tonic-gate  * ===========================================================================
31177c478bd9Sstevel@tonic-gate  * Send one empty static block to give enough lookahead for inflate.
31187c478bd9Sstevel@tonic-gate  * This takes 10 bits, of which 7 may remain in the bit buffer.
31197c478bd9Sstevel@tonic-gate  * The current inflate code requires 9 bits of lookahead. If the
31207c478bd9Sstevel@tonic-gate  * last two codes for the previous block (real code plus EOB) were coded
31217c478bd9Sstevel@tonic-gate  * on 5 bits or less, inflate may have only 5+3 bits of lookahead to decode
31227c478bd9Sstevel@tonic-gate  * the last real code. In this case we send two empty static blocks instead
31237c478bd9Sstevel@tonic-gate  * of one. (There are no problems if the previous block is stored or fixed.)
31247c478bd9Sstevel@tonic-gate  * To simplify the code, we assume the worst case of last real code encoded
31257c478bd9Sstevel@tonic-gate  * on one bit only.
31267c478bd9Sstevel@tonic-gate  */
31277c478bd9Sstevel@tonic-gate void
_tr_align(s)31287c478bd9Sstevel@tonic-gate _tr_align(s)
31297c478bd9Sstevel@tonic-gate     deflate_state *s;
31307c478bd9Sstevel@tonic-gate {
31317c478bd9Sstevel@tonic-gate 	send_bits(s, STATIC_TREES<<1, 3);
31327c478bd9Sstevel@tonic-gate 	send_code(s, END_BLOCK, static_ltree);
31337c478bd9Sstevel@tonic-gate 	s->compressed_len += 10L;	/* 3 for block type, 7 for EOB */
31347c478bd9Sstevel@tonic-gate 	bi_flush(s);
31357c478bd9Sstevel@tonic-gate 	/*
31367c478bd9Sstevel@tonic-gate 	 * Of the 10 bits for the empty block, we have already sent
31377c478bd9Sstevel@tonic-gate 	 * (10 - bi_valid) bits. The lookahead for the last real code
31387c478bd9Sstevel@tonic-gate 	 * (before the EOB of the previous block) was thus at least
31397c478bd9Sstevel@tonic-gate 	 * one plus the length of the EOB plus what we have just sent
31407c478bd9Sstevel@tonic-gate 	 * of the empty static block.
31417c478bd9Sstevel@tonic-gate 	 */
31427c478bd9Sstevel@tonic-gate 	if (1 + s->last_eob_len + 10 - s->bi_valid < 9) {
31437c478bd9Sstevel@tonic-gate 		send_bits(s, STATIC_TREES<<1, 3);
31447c478bd9Sstevel@tonic-gate 		send_code(s, END_BLOCK, static_ltree);
31457c478bd9Sstevel@tonic-gate 		s->compressed_len += 10L;
31467c478bd9Sstevel@tonic-gate 		bi_flush(s);
31477c478bd9Sstevel@tonic-gate 	}
31487c478bd9Sstevel@tonic-gate 	s->last_eob_len = 7;
31497c478bd9Sstevel@tonic-gate }
31507c478bd9Sstevel@tonic-gate 
31517c478bd9Sstevel@tonic-gate /*
31527c478bd9Sstevel@tonic-gate  * ===========================================================================
31537c478bd9Sstevel@tonic-gate  * Determine the best encoding for the current block: dynamic trees, static
31547c478bd9Sstevel@tonic-gate  * trees or store, and output the encoded block to the zip file.
31557c478bd9Sstevel@tonic-gate  */
31567c478bd9Sstevel@tonic-gate void
_tr_flush_block(s,buf,stored_len,eof)31577c478bd9Sstevel@tonic-gate _tr_flush_block(s, buf, stored_len, eof)
31587c478bd9Sstevel@tonic-gate     deflate_state *s;
31597c478bd9Sstevel@tonic-gate     charf *buf;	/* input block, or NULL if too old */
31607c478bd9Sstevel@tonic-gate     ulg stored_len;	/* length of input block */
31617c478bd9Sstevel@tonic-gate     int eof;	/* true if this is the last block for a file */
31627c478bd9Sstevel@tonic-gate {
31637c478bd9Sstevel@tonic-gate 	ulg opt_lenb, static_lenb;	/* opt_len and static_len in bytes */
31647c478bd9Sstevel@tonic-gate 	/* index of last bit length code of non zero freq */
31657c478bd9Sstevel@tonic-gate 	int max_blindex = 0;
31667c478bd9Sstevel@tonic-gate 
31677c478bd9Sstevel@tonic-gate 	/* Build the Huffman trees unless a stored block is forced */
31687c478bd9Sstevel@tonic-gate 	if (s->level > 0) {
31697c478bd9Sstevel@tonic-gate 
31707c478bd9Sstevel@tonic-gate 		/* Check if the file is ascii or binary */
31717c478bd9Sstevel@tonic-gate 		if (s->data_type == Z_UNKNOWN) set_data_type(s);
31727c478bd9Sstevel@tonic-gate 
31737c478bd9Sstevel@tonic-gate 		/* Construct the literal and distance trees */
31747c478bd9Sstevel@tonic-gate 		build_tree(s, (tree_desc *)(&(s->l_desc)));
31757c478bd9Sstevel@tonic-gate 		Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len,
31767c478bd9Sstevel@tonic-gate 		    s->static_len));
31777c478bd9Sstevel@tonic-gate 
31787c478bd9Sstevel@tonic-gate 		build_tree(s, (tree_desc *)(&(s->d_desc)));
31797c478bd9Sstevel@tonic-gate 		Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len,
31807c478bd9Sstevel@tonic-gate 		    s->static_len));
31817c478bd9Sstevel@tonic-gate 		/*
31827c478bd9Sstevel@tonic-gate 		 * At this point, opt_len and static_len are the total
31837c478bd9Sstevel@tonic-gate 		 * bit lengths of the compressed block data, excluding
31847c478bd9Sstevel@tonic-gate 		 * the tree representations.
31857c478bd9Sstevel@tonic-gate 		 */
31867c478bd9Sstevel@tonic-gate 
31877c478bd9Sstevel@tonic-gate 		/*
31887c478bd9Sstevel@tonic-gate 		 * Build the bit length tree for the above two trees,
31897c478bd9Sstevel@tonic-gate 		 * and get the index in bl_order of the last bit
31907c478bd9Sstevel@tonic-gate 		 * length code to send.
31917c478bd9Sstevel@tonic-gate 		 */
31927c478bd9Sstevel@tonic-gate 		max_blindex = build_bl_tree(s);
31937c478bd9Sstevel@tonic-gate 
31947c478bd9Sstevel@tonic-gate 		/*
31957c478bd9Sstevel@tonic-gate 		 * Determine the best encoding. Compute first the
31967c478bd9Sstevel@tonic-gate 		 * block length in bytes
31977c478bd9Sstevel@tonic-gate 		 */
31987c478bd9Sstevel@tonic-gate 		opt_lenb = (s->opt_len+3+7)>>3;
31997c478bd9Sstevel@tonic-gate 		static_lenb = (s->static_len+3+7)>>3;
32007c478bd9Sstevel@tonic-gate 
32017c478bd9Sstevel@tonic-gate 		Tracev((stderr,
32027c478bd9Sstevel@tonic-gate 		    "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ",
32037c478bd9Sstevel@tonic-gate 		    opt_lenb, s->opt_len, static_lenb, s->static_len,
32047c478bd9Sstevel@tonic-gate 		    stored_len, s->last_lit));
32057c478bd9Sstevel@tonic-gate 
32067c478bd9Sstevel@tonic-gate 		if (static_lenb <= opt_lenb) opt_lenb = static_lenb;
32077c478bd9Sstevel@tonic-gate 
32087c478bd9Sstevel@tonic-gate 	} else {
32097c478bd9Sstevel@tonic-gate 		Assert(buf != (char *)0, "lost buf");
32107c478bd9Sstevel@tonic-gate 		/* force a stored block */
32117c478bd9Sstevel@tonic-gate 		opt_lenb = static_lenb = stored_len + 5;
32127c478bd9Sstevel@tonic-gate 	}
32137c478bd9Sstevel@tonic-gate 
32147c478bd9Sstevel@tonic-gate 	/*
32157c478bd9Sstevel@tonic-gate 	 * If compression failed and this is the first and last block,
32167c478bd9Sstevel@tonic-gate 	 * and if the .zip file can be seeked (to rewrite the local
32177c478bd9Sstevel@tonic-gate 	 * header), the whole file is transformed into a stored file:
32187c478bd9Sstevel@tonic-gate 	 */
32197c478bd9Sstevel@tonic-gate #ifdef STORED_FILE_OK
32207c478bd9Sstevel@tonic-gate #ifdef FORCE_STORED_FILE
32217c478bd9Sstevel@tonic-gate #define	FRC_STR_COND	eof && s->compressed_len == 0L /* force stored file */
32227c478bd9Sstevel@tonic-gate #else
32237c478bd9Sstevel@tonic-gate #define	FRC_STR_COND	stored_len <= opt_lenb && eof && \
32247c478bd9Sstevel@tonic-gate 			s->compressed_len == 0L && seekable()
32257c478bd9Sstevel@tonic-gate #endif
32267c478bd9Sstevel@tonic-gate 	if (FRC_STR_COND) {
32277c478bd9Sstevel@tonic-gate #undef FRC_STR_COND
32287c478bd9Sstevel@tonic-gate 		/*
32297c478bd9Sstevel@tonic-gate 		 * Since LIT_BUFSIZE <= 2*WSIZE, the input data must
32307c478bd9Sstevel@tonic-gate 		 * be there:
32317c478bd9Sstevel@tonic-gate 		 */
32327c478bd9Sstevel@tonic-gate 		if (buf == (charf*)0) error("block vanished");
32337c478bd9Sstevel@tonic-gate 
32347c478bd9Sstevel@tonic-gate 		/* without header */
32357c478bd9Sstevel@tonic-gate 		copy_block(s, buf, (unsigned)stored_len, 0);
32367c478bd9Sstevel@tonic-gate 		s->compressed_len = stored_len << 3;
32377c478bd9Sstevel@tonic-gate 		s->method = STORED;
32387c478bd9Sstevel@tonic-gate 	} else
32397c478bd9Sstevel@tonic-gate #endif /* STORED_FILE_OK */
32407c478bd9Sstevel@tonic-gate 
32417c478bd9Sstevel@tonic-gate #ifdef FORCE_STORED
32427c478bd9Sstevel@tonic-gate #define	FRC_STR_COND	buf != (char *)0	/* force stored block */
32437c478bd9Sstevel@tonic-gate #else
32447c478bd9Sstevel@tonic-gate 			/* 4: two words for the lengths */
32457c478bd9Sstevel@tonic-gate #define	FRC_STR_COND	stored_len+4 <= opt_lenb && buf != (char *)0
32467c478bd9Sstevel@tonic-gate #endif
32477c478bd9Sstevel@tonic-gate 		if (FRC_STR_COND) {
32487c478bd9Sstevel@tonic-gate #undef FRC_STR_COND
32497c478bd9Sstevel@tonic-gate 			/*
32507c478bd9Sstevel@tonic-gate 			 * The test buf != NULL is only necessary if
32517c478bd9Sstevel@tonic-gate 			 * LIT_BUFSIZE > WSIZE.  Otherwise we can't
32527c478bd9Sstevel@tonic-gate 			 * have processed more than WSIZE input bytes
32537c478bd9Sstevel@tonic-gate 			 * since the last block flush, because
32547c478bd9Sstevel@tonic-gate 			 * compression would have been successful. If
32557c478bd9Sstevel@tonic-gate 			 * LIT_BUFSIZE <= WSIZE, it is never too late
32567c478bd9Sstevel@tonic-gate 			 * to transform a block into a stored block.
32577c478bd9Sstevel@tonic-gate 			 */
32587c478bd9Sstevel@tonic-gate 			_tr_stored_block(s, buf, stored_len, eof);
32597c478bd9Sstevel@tonic-gate #ifdef FORCE_STATIC
32607c478bd9Sstevel@tonic-gate #define	FRC_STAT_COND	static_lenb >= 0 /* force static trees */
32617c478bd9Sstevel@tonic-gate #else
32627c478bd9Sstevel@tonic-gate #define	FRC_STAT_COND	static_lenb == opt_lenb
32637c478bd9Sstevel@tonic-gate #endif
32647c478bd9Sstevel@tonic-gate 		} else if (FRC_STAT_COND) {
32657c478bd9Sstevel@tonic-gate #undef FRC_STAT_COND
32667c478bd9Sstevel@tonic-gate 			send_bits(s, (STATIC_TREES<<1)+eof, 3);
32677c478bd9Sstevel@tonic-gate 			compress_block(s, (ct_data *)static_ltree,
32687c478bd9Sstevel@tonic-gate 			    (ct_data *)static_dtree);
32697c478bd9Sstevel@tonic-gate 			s->compressed_len += 3 + s->static_len;	/* PPP */
32707c478bd9Sstevel@tonic-gate 		} else {
32717c478bd9Sstevel@tonic-gate 			send_bits(s, (DYN_TREES<<1)+eof, 3);
32727c478bd9Sstevel@tonic-gate 			send_all_trees(s, s->l_desc.max_code+1,
32737c478bd9Sstevel@tonic-gate 			    s->d_desc.max_code+1,
32747c478bd9Sstevel@tonic-gate 			    max_blindex+1);
32757c478bd9Sstevel@tonic-gate 			compress_block(s, (ct_data *)s->dyn_ltree,
32767c478bd9Sstevel@tonic-gate 			    (ct_data *)s->dyn_dtree);
32777c478bd9Sstevel@tonic-gate 			s->compressed_len += 3 + s->opt_len;	/* PPP */
32787c478bd9Sstevel@tonic-gate 		}
32797c478bd9Sstevel@tonic-gate #ifdef DEBUG_ZLIB
32807c478bd9Sstevel@tonic-gate 	Assert(s->compressed_len == s->bits_sent, "bad compressed size");
32817c478bd9Sstevel@tonic-gate #endif
32827c478bd9Sstevel@tonic-gate 	/*
32837c478bd9Sstevel@tonic-gate 	 * The above check is made mod 2^32, for files larger than 512
32847c478bd9Sstevel@tonic-gate 	 * MB and uLong implemented on 32 bits.
32857c478bd9Sstevel@tonic-gate 	 */
32867c478bd9Sstevel@tonic-gate 	init_block(s);
32877c478bd9Sstevel@tonic-gate 
32887c478bd9Sstevel@tonic-gate 	if (eof) {
32897c478bd9Sstevel@tonic-gate 		bi_windup(s);
32907c478bd9Sstevel@tonic-gate 		s->compressed_len += 7;	/* align on byte boundary PPP */
32917c478bd9Sstevel@tonic-gate 	}
32927c478bd9Sstevel@tonic-gate 	Tracev((stderr, "\ncomprlen %lu(%lu) ", s->compressed_len>>3,
32937c478bd9Sstevel@tonic-gate 	    s->compressed_len-7*eof));
32947c478bd9Sstevel@tonic-gate 
32957c478bd9Sstevel@tonic-gate 	/* return (s->compressed_len >> 3); */
32967c478bd9Sstevel@tonic-gate }
32977c478bd9Sstevel@tonic-gate 
32987c478bd9Sstevel@tonic-gate /*
32997c478bd9Sstevel@tonic-gate  * ===========================================================================
33007c478bd9Sstevel@tonic-gate  * Save the match info and tally the frequency counts. Return true if
33017c478bd9Sstevel@tonic-gate  * the current block must be flushed.
33027c478bd9Sstevel@tonic-gate  */
33037c478bd9Sstevel@tonic-gate int
_tr_tally(s,dist,lc)33047c478bd9Sstevel@tonic-gate _tr_tally(s, dist, lc)
33057c478bd9Sstevel@tonic-gate     deflate_state *s;
33067c478bd9Sstevel@tonic-gate     unsigned dist;	/* distance of matched string */
33077c478bd9Sstevel@tonic-gate 	/* match length-MIN_MATCH or unmatched char (if dist==0) */
33087c478bd9Sstevel@tonic-gate     unsigned lc;
33097c478bd9Sstevel@tonic-gate {
33107c478bd9Sstevel@tonic-gate 	s->d_buf[s->last_lit] = (ush)dist;
33117c478bd9Sstevel@tonic-gate 	s->l_buf[s->last_lit++] = (uch)lc;
33127c478bd9Sstevel@tonic-gate 	if (dist == 0) {
33137c478bd9Sstevel@tonic-gate 		/* lc is the unmatched char */
33147c478bd9Sstevel@tonic-gate 		s->dyn_ltree[lc].Freq++;
33157c478bd9Sstevel@tonic-gate 	} else {
33167c478bd9Sstevel@tonic-gate 		s->matches++;
33177c478bd9Sstevel@tonic-gate 		/* Here, lc is the match length - MIN_MATCH */
33187c478bd9Sstevel@tonic-gate 		dist--;	/* dist = match distance - 1 */
33197c478bd9Sstevel@tonic-gate 		Assert((ush)dist < (ush)MAX_DIST(s) &&
33207c478bd9Sstevel@tonic-gate 		    (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) &&
33217c478bd9Sstevel@tonic-gate 		    (ush)d_code(dist) < (ush)D_CODES,  "_tr_tally: bad match");
33227c478bd9Sstevel@tonic-gate 
33237c478bd9Sstevel@tonic-gate 		s->dyn_ltree[_length_code[lc]+LITERALS+1].Freq++;
33247c478bd9Sstevel@tonic-gate 		s->dyn_dtree[d_code(dist)].Freq++;
33257c478bd9Sstevel@tonic-gate 	}
33267c478bd9Sstevel@tonic-gate 
33277c478bd9Sstevel@tonic-gate #ifdef TRUNCATE_BLOCK
33287c478bd9Sstevel@tonic-gate 	/* Try to guess if it is profitable to stop the current block here */
33297c478bd9Sstevel@tonic-gate 	if ((s->last_lit & 0x1fff) == 0 && s->level > 2) {
33307c478bd9Sstevel@tonic-gate 		/* Compute an upper bound for the compressed length */
33317c478bd9Sstevel@tonic-gate 		ulg out_length = (ulg)s->last_lit*8L;
33327c478bd9Sstevel@tonic-gate 		ulg in_length = (ulg)((long)s->strstart - s->block_start);
33337c478bd9Sstevel@tonic-gate 		int dcode;
33347c478bd9Sstevel@tonic-gate 		for (dcode = 0; dcode < D_CODES; dcode++) {
33357c478bd9Sstevel@tonic-gate 			out_length += (ulg)s->dyn_dtree[dcode].Freq *
33367c478bd9Sstevel@tonic-gate 			    (5L+extra_dbits[dcode]);
33377c478bd9Sstevel@tonic-gate 		}
33387c478bd9Sstevel@tonic-gate 		out_length >>= 3;
33397c478bd9Sstevel@tonic-gate 		Tracev((stderr, "\nlast_lit %u, in %ld, out ~%ld(%ld%%) ",
33407c478bd9Sstevel@tonic-gate 		    s->last_lit, in_length, out_length,
33417c478bd9Sstevel@tonic-gate 		    100L - out_length*100L/in_length));
33427c478bd9Sstevel@tonic-gate 		if (s->matches < s->last_lit/2 && out_length < in_length/2)
33437c478bd9Sstevel@tonic-gate 			return (1);
33447c478bd9Sstevel@tonic-gate 	}
33457c478bd9Sstevel@tonic-gate #endif
33467c478bd9Sstevel@tonic-gate 	return (s->last_lit == s->lit_bufsize-1);
33477c478bd9Sstevel@tonic-gate 	/*
33487c478bd9Sstevel@tonic-gate 	 * We avoid equality with lit_bufsize because of wraparound at 64K
33497c478bd9Sstevel@tonic-gate 	 * on 16 bit machines and because stored blocks are restricted to
33507c478bd9Sstevel@tonic-gate 	 * 64K-1 bytes.
33517c478bd9Sstevel@tonic-gate 	 */
33527c478bd9Sstevel@tonic-gate }
33537c478bd9Sstevel@tonic-gate 
33547c478bd9Sstevel@tonic-gate /*
33557c478bd9Sstevel@tonic-gate  * ===========================================================================
33567c478bd9Sstevel@tonic-gate  * Send the block data compressed using the given Huffman trees
33577c478bd9Sstevel@tonic-gate  */
33587c478bd9Sstevel@tonic-gate local void
compress_block(s,ltree,dtree)33597c478bd9Sstevel@tonic-gate compress_block(s, ltree, dtree)
33607c478bd9Sstevel@tonic-gate     deflate_state *s;
33617c478bd9Sstevel@tonic-gate     ct_data *ltree;	/* literal tree */
33627c478bd9Sstevel@tonic-gate     ct_data *dtree;	/* distance tree */
33637c478bd9Sstevel@tonic-gate {
33647c478bd9Sstevel@tonic-gate 	unsigned dist;	/* distance of matched string */
33657c478bd9Sstevel@tonic-gate 	int lc;	/* match length or unmatched char (if dist == 0) */
33667c478bd9Sstevel@tonic-gate 	unsigned lx = 0;	/* running index in l_buf */
33677c478bd9Sstevel@tonic-gate 	unsigned code;	/* the code to send */
33687c478bd9Sstevel@tonic-gate 	int extra;	/* number of extra bits to send */
33697c478bd9Sstevel@tonic-gate 
33707c478bd9Sstevel@tonic-gate 	if (s->last_lit != 0) do {
33717c478bd9Sstevel@tonic-gate 		dist = s->d_buf[lx];
33727c478bd9Sstevel@tonic-gate 		lc = s->l_buf[lx++];
33737c478bd9Sstevel@tonic-gate 		if (dist == 0) {
33747c478bd9Sstevel@tonic-gate 			/* send a literal byte */
33757c478bd9Sstevel@tonic-gate 			send_code(s, lc, ltree);
33767c478bd9Sstevel@tonic-gate 			Tracecv(isgraph(lc), (stderr, " '%c' ", lc));
33777c478bd9Sstevel@tonic-gate 		} else {
33787c478bd9Sstevel@tonic-gate 			/* Here, lc is the match length - MIN_MATCH */
33797c478bd9Sstevel@tonic-gate 			code = _length_code[lc];
33807c478bd9Sstevel@tonic-gate 			/* send the length code */
33817c478bd9Sstevel@tonic-gate 			send_code(s, code+LITERALS+1, ltree);
33827c478bd9Sstevel@tonic-gate 			extra = extra_lbits[code];
33837c478bd9Sstevel@tonic-gate 			if (extra != 0) {
33847c478bd9Sstevel@tonic-gate 				lc -= base_length[code];
33857c478bd9Sstevel@tonic-gate 				/* send the extra length bits */
33867c478bd9Sstevel@tonic-gate 				send_bits(s, lc, extra);
33877c478bd9Sstevel@tonic-gate 			}
33887c478bd9Sstevel@tonic-gate 			/* dist is now the match distance - 1 */
33897c478bd9Sstevel@tonic-gate 			dist--;
33907c478bd9Sstevel@tonic-gate 			code = d_code(dist);
33917c478bd9Sstevel@tonic-gate 			Assert(code < D_CODES, "bad d_code");
33927c478bd9Sstevel@tonic-gate 
33937c478bd9Sstevel@tonic-gate 			/* send the distance code */
33947c478bd9Sstevel@tonic-gate 			send_code(s, code, dtree);
33957c478bd9Sstevel@tonic-gate 			extra = extra_dbits[code];
33967c478bd9Sstevel@tonic-gate 			if (extra != 0) {
33977c478bd9Sstevel@tonic-gate 				dist -= base_dist[code];
33987c478bd9Sstevel@tonic-gate 				/* send the extra distance bits */
33997c478bd9Sstevel@tonic-gate 				send_bits(s, dist, extra);
34007c478bd9Sstevel@tonic-gate 			}
34017c478bd9Sstevel@tonic-gate 		} /* literal or match pair ? */
34027c478bd9Sstevel@tonic-gate 
34037c478bd9Sstevel@tonic-gate 		/*
34047c478bd9Sstevel@tonic-gate 		 * Check that the overlay between pending_buf and
34057c478bd9Sstevel@tonic-gate 		 * d_buf+l_buf is ok:
34067c478bd9Sstevel@tonic-gate 		 */
34077c478bd9Sstevel@tonic-gate 		Assert(s->pending < s->lit_bufsize + 2*lx,
34087c478bd9Sstevel@tonic-gate 		    "pendingBuf overflow");
34097c478bd9Sstevel@tonic-gate 
34107c478bd9Sstevel@tonic-gate 	} while (lx < s->last_lit);
34117c478bd9Sstevel@tonic-gate 
34127c478bd9Sstevel@tonic-gate 	send_code(s, END_BLOCK, ltree);
34137c478bd9Sstevel@tonic-gate 	s->last_eob_len = ltree[END_BLOCK].Len;
34147c478bd9Sstevel@tonic-gate }
34157c478bd9Sstevel@tonic-gate 
34167c478bd9Sstevel@tonic-gate /*
34177c478bd9Sstevel@tonic-gate  * ===========================================================================
34187c478bd9Sstevel@tonic-gate  * Set the data type to ASCII or BINARY, using a crude approximation:
34197c478bd9Sstevel@tonic-gate  * binary if more than 20% of the bytes are <= 6 or >= 128, ascii otherwise.
34207c478bd9Sstevel@tonic-gate  * IN assertion: the fields freq of dyn_ltree are set and the total of all
34217c478bd9Sstevel@tonic-gate  * frequencies does not exceed 64K (to fit in an int on 16 bit machines).
34227c478bd9Sstevel@tonic-gate  */
34237c478bd9Sstevel@tonic-gate local void
set_data_type(s)34247c478bd9Sstevel@tonic-gate set_data_type(s)
34257c478bd9Sstevel@tonic-gate     deflate_state *s;
34267c478bd9Sstevel@tonic-gate {
34277c478bd9Sstevel@tonic-gate 	int n = 0;
34287c478bd9Sstevel@tonic-gate 	unsigned ascii_freq = 0;
34297c478bd9Sstevel@tonic-gate 	unsigned bin_freq = 0;
34307c478bd9Sstevel@tonic-gate 	while (n < 7)	bin_freq	+= s->dyn_ltree[n++].Freq;
34317c478bd9Sstevel@tonic-gate 	while (n < 128)	ascii_freq	+= s->dyn_ltree[n++].Freq;
34327c478bd9Sstevel@tonic-gate 	while (n < LITERALS) bin_freq	+= s->dyn_ltree[n++].Freq;
34337c478bd9Sstevel@tonic-gate 	s->data_type = (Byte)(bin_freq > (ascii_freq >> 2) ?
34347c478bd9Sstevel@tonic-gate 	    Z_BINARY : Z_ASCII);
34357c478bd9Sstevel@tonic-gate }
34367c478bd9Sstevel@tonic-gate 
34377c478bd9Sstevel@tonic-gate /*
34387c478bd9Sstevel@tonic-gate  * ===========================================================================
34397c478bd9Sstevel@tonic-gate  * Reverse the first len bits of a code, using straightforward code (a faster
34407c478bd9Sstevel@tonic-gate  * method would use a table)
34417c478bd9Sstevel@tonic-gate  * IN assertion: 1 <= len <= 15
34427c478bd9Sstevel@tonic-gate  */
34437c478bd9Sstevel@tonic-gate local unsigned
bi_reverse(code,len)34447c478bd9Sstevel@tonic-gate bi_reverse(code, len)
34457c478bd9Sstevel@tonic-gate     unsigned code;	/* the value to invert */
34467c478bd9Sstevel@tonic-gate     int len;	/* its bit length */
34477c478bd9Sstevel@tonic-gate {
34487c478bd9Sstevel@tonic-gate 	register unsigned res = 0;
34497c478bd9Sstevel@tonic-gate 	do {
34507c478bd9Sstevel@tonic-gate 		res |= code & 1;
34517c478bd9Sstevel@tonic-gate 		code >>= 1, res <<= 1;
34527c478bd9Sstevel@tonic-gate 	} while (--len > 0);
34537c478bd9Sstevel@tonic-gate 	return (res >> 1);
34547c478bd9Sstevel@tonic-gate }
34557c478bd9Sstevel@tonic-gate 
34567c478bd9Sstevel@tonic-gate /*
34577c478bd9Sstevel@tonic-gate  * ===========================================================================
34587c478bd9Sstevel@tonic-gate  * Flush the bit buffer, keeping at most 7 bits in it.
34597c478bd9Sstevel@tonic-gate  */
34607c478bd9Sstevel@tonic-gate local void
bi_flush(s)34617c478bd9Sstevel@tonic-gate bi_flush(s)
34627c478bd9Sstevel@tonic-gate     deflate_state *s;
34637c478bd9Sstevel@tonic-gate {
34647c478bd9Sstevel@tonic-gate 	if (s->bi_valid == 16) {
34657c478bd9Sstevel@tonic-gate 		put_short(s, s->bi_buf);
34667c478bd9Sstevel@tonic-gate 		s->bi_buf = 0;
34677c478bd9Sstevel@tonic-gate 		s->bi_valid = 0;
34687c478bd9Sstevel@tonic-gate 	} else if (s->bi_valid >= 8) {
34697c478bd9Sstevel@tonic-gate 		put_byte(s, (Byte)s->bi_buf);
34707c478bd9Sstevel@tonic-gate 		s->bi_buf >>= 8;
34717c478bd9Sstevel@tonic-gate 		s->bi_valid -= 8;
34727c478bd9Sstevel@tonic-gate 	}
34737c478bd9Sstevel@tonic-gate }
34747c478bd9Sstevel@tonic-gate 
34757c478bd9Sstevel@tonic-gate /*
34767c478bd9Sstevel@tonic-gate  * ===========================================================================
34777c478bd9Sstevel@tonic-gate  * Flush the bit buffer and align the output on a byte boundary
34787c478bd9Sstevel@tonic-gate  */
34797c478bd9Sstevel@tonic-gate local void
bi_windup(s)34807c478bd9Sstevel@tonic-gate bi_windup(s)
34817c478bd9Sstevel@tonic-gate     deflate_state *s;
34827c478bd9Sstevel@tonic-gate {
34837c478bd9Sstevel@tonic-gate 	if (s->bi_valid > 8) {
34847c478bd9Sstevel@tonic-gate 		put_short(s, s->bi_buf);
34857c478bd9Sstevel@tonic-gate 	} else if (s->bi_valid > 0) {
34867c478bd9Sstevel@tonic-gate 		put_byte(s, (Byte)s->bi_buf);
34877c478bd9Sstevel@tonic-gate 	}
34887c478bd9Sstevel@tonic-gate 	s->bi_buf = 0;
34897c478bd9Sstevel@tonic-gate 	s->bi_valid = 0;
34907c478bd9Sstevel@tonic-gate #ifdef DEBUG_ZLIB
34917c478bd9Sstevel@tonic-gate 	s->bits_sent = (s->bits_sent+7) & ~7;
34927c478bd9Sstevel@tonic-gate #endif
34937c478bd9Sstevel@tonic-gate }
34947c478bd9Sstevel@tonic-gate 
34957c478bd9Sstevel@tonic-gate /*
34967c478bd9Sstevel@tonic-gate  * ===========================================================================
34977c478bd9Sstevel@tonic-gate  * Copy a stored block, storing first the length and its
34987c478bd9Sstevel@tonic-gate  * one's complement if requested.
34997c478bd9Sstevel@tonic-gate  */
35007c478bd9Sstevel@tonic-gate local void
copy_block(s,buf,len,header)35017c478bd9Sstevel@tonic-gate copy_block(s, buf, len, header)
35027c478bd9Sstevel@tonic-gate     deflate_state *s;
35037c478bd9Sstevel@tonic-gate     charf    *buf;	/* the input data */
35047c478bd9Sstevel@tonic-gate     unsigned len;	/* its length */
35057c478bd9Sstevel@tonic-gate     int	header;	/* true if block header must be written */
35067c478bd9Sstevel@tonic-gate {
35077c478bd9Sstevel@tonic-gate 	bi_windup(s);	/* align on byte boundary */
35087c478bd9Sstevel@tonic-gate 	s->last_eob_len = 8;	/* enough lookahead for inflate */
35097c478bd9Sstevel@tonic-gate 
35107c478bd9Sstevel@tonic-gate 	if (header) {
35117c478bd9Sstevel@tonic-gate 		put_short(s, (ush)len);
35127c478bd9Sstevel@tonic-gate 		put_short(s, (ush)~len);
35137c478bd9Sstevel@tonic-gate #ifdef DEBUG_ZLIB
35147c478bd9Sstevel@tonic-gate 		s->bits_sent += 2*16;
35157c478bd9Sstevel@tonic-gate #endif
35167c478bd9Sstevel@tonic-gate 	}
35177c478bd9Sstevel@tonic-gate #ifdef DEBUG_ZLIB
35187c478bd9Sstevel@tonic-gate 	s->bits_sent += (ulg)len<<3;
35197c478bd9Sstevel@tonic-gate #endif
35207c478bd9Sstevel@tonic-gate 	/* bundle up the put_byte(s, *buf++) calls PPP */
35217c478bd9Sstevel@tonic-gate 	Assert(s->pending + len < s->pending_buf_size, "pending_buf overrun");
35227c478bd9Sstevel@tonic-gate 	zmemcpy(&s->pending_buf[s->pending], buf, len);	/* PPP */
35237c478bd9Sstevel@tonic-gate 	s->pending += len;				/* PPP */
35247c478bd9Sstevel@tonic-gate }
35257c478bd9Sstevel@tonic-gate /* --- trees.c */
35267c478bd9Sstevel@tonic-gate 
35277c478bd9Sstevel@tonic-gate /* +++ inflate.c */
35287c478bd9Sstevel@tonic-gate /*
35297c478bd9Sstevel@tonic-gate  * inflate.c -- zlib interface to inflate modules
35307c478bd9Sstevel@tonic-gate  * Copyright (C) 1995-1998 Mark Adler
35317c478bd9Sstevel@tonic-gate  * For conditions of distribution and use, see copyright notice in zlib.h
35327c478bd9Sstevel@tonic-gate  */
35337c478bd9Sstevel@tonic-gate 
35347c478bd9Sstevel@tonic-gate /* #include "zutil.h" */
35357c478bd9Sstevel@tonic-gate 
35367c478bd9Sstevel@tonic-gate /* +++ infblock.h */
35377c478bd9Sstevel@tonic-gate /*
35387c478bd9Sstevel@tonic-gate  * infblock.h -- header to use infblock.c
35397c478bd9Sstevel@tonic-gate  * Copyright (C) 1995-1998 Mark Adler
35407c478bd9Sstevel@tonic-gate  * For conditions of distribution and use, see copyright notice in zlib.h
35417c478bd9Sstevel@tonic-gate  */
35427c478bd9Sstevel@tonic-gate 
35437c478bd9Sstevel@tonic-gate /*
35447c478bd9Sstevel@tonic-gate  * WARNING: this file should *not* be used by applications. It is part
35457c478bd9Sstevel@tonic-gate  * of the implementation of the compression library and is subject to
35467c478bd9Sstevel@tonic-gate  * change. Applications should only use zlib.h.
35477c478bd9Sstevel@tonic-gate  */
35487c478bd9Sstevel@tonic-gate 
35497c478bd9Sstevel@tonic-gate struct inflate_blocks_state;
35507c478bd9Sstevel@tonic-gate typedef struct inflate_blocks_state FAR inflate_blocks_statef;
35517c478bd9Sstevel@tonic-gate 
35527c478bd9Sstevel@tonic-gate extern inflate_blocks_statef * inflate_blocks_new OF((
35537c478bd9Sstevel@tonic-gate     z_streamp z,
35547c478bd9Sstevel@tonic-gate     check_func c,	/* check function */
35557c478bd9Sstevel@tonic-gate     uInt w));	/* window size */
35567c478bd9Sstevel@tonic-gate 
35577c478bd9Sstevel@tonic-gate extern int inflate_blocks OF((
35587c478bd9Sstevel@tonic-gate     inflate_blocks_statef *,
35597c478bd9Sstevel@tonic-gate     z_streamp,
35607c478bd9Sstevel@tonic-gate     int));	/* initial return code */
35617c478bd9Sstevel@tonic-gate 
35627c478bd9Sstevel@tonic-gate extern void inflate_blocks_reset OF((
35637c478bd9Sstevel@tonic-gate     inflate_blocks_statef *,
35647c478bd9Sstevel@tonic-gate     z_streamp,
35657c478bd9Sstevel@tonic-gate     uLongf *));	/* check value on output */
35667c478bd9Sstevel@tonic-gate 
35677c478bd9Sstevel@tonic-gate extern int inflate_blocks_free OF((
35687c478bd9Sstevel@tonic-gate     inflate_blocks_statef *,
35697c478bd9Sstevel@tonic-gate     z_streamp));
35707c478bd9Sstevel@tonic-gate 
35717c478bd9Sstevel@tonic-gate extern void inflate_set_dictionary OF((
35727c478bd9Sstevel@tonic-gate     inflate_blocks_statef *s,
35737c478bd9Sstevel@tonic-gate     const Bytef *d,  /* dictionary */
35747c478bd9Sstevel@tonic-gate     uInt  n));	/* dictionary length */
35757c478bd9Sstevel@tonic-gate 
35767c478bd9Sstevel@tonic-gate extern int inflate_blocks_sync_point OF((
35777c478bd9Sstevel@tonic-gate     inflate_blocks_statef *s));
35787c478bd9Sstevel@tonic-gate 
35797c478bd9Sstevel@tonic-gate /* PPP -- added function */
35807c478bd9Sstevel@tonic-gate extern int inflate_addhistory OF((
35817c478bd9Sstevel@tonic-gate     inflate_blocks_statef *,
35827c478bd9Sstevel@tonic-gate     z_streamp));
35837c478bd9Sstevel@tonic-gate 
35847c478bd9Sstevel@tonic-gate /* PPP -- added function */
35857c478bd9Sstevel@tonic-gate extern int inflate_packet_flush OF((
35867c478bd9Sstevel@tonic-gate     inflate_blocks_statef *));
35877c478bd9Sstevel@tonic-gate /* --- infblock.h */
35887c478bd9Sstevel@tonic-gate 
35897c478bd9Sstevel@tonic-gate #ifndef NO_DUMMY_DECL
35907c478bd9Sstevel@tonic-gate struct inflate_blocks_state {int dummy; };	/* for buggy compilers */
35917c478bd9Sstevel@tonic-gate #endif
35927c478bd9Sstevel@tonic-gate 
35937c478bd9Sstevel@tonic-gate /* inflate private state */
35947c478bd9Sstevel@tonic-gate struct internal_state {
35957c478bd9Sstevel@tonic-gate 
35967c478bd9Sstevel@tonic-gate 	/* mode */
35977c478bd9Sstevel@tonic-gate 	enum {
35987c478bd9Sstevel@tonic-gate 		METHOD,	/* waiting for method byte */
35997c478bd9Sstevel@tonic-gate 		FLAG,	/* waiting for flag byte */
36007c478bd9Sstevel@tonic-gate 		DICT4,	/* four dictionary check bytes to go */
36017c478bd9Sstevel@tonic-gate 		DICT3,	/* three dictionary check bytes to go */
36027c478bd9Sstevel@tonic-gate 		DICT2,	/* two dictionary check bytes to go */
36037c478bd9Sstevel@tonic-gate 		DICT1,	/* one dictionary check byte to go */
36047c478bd9Sstevel@tonic-gate 		DICT0,	/* waiting for inflateSetDictionary */
36057c478bd9Sstevel@tonic-gate 		BLOCKS,	/* decompressing blocks */
36067c478bd9Sstevel@tonic-gate 		CHECK4,	/* four check bytes to go */
36077c478bd9Sstevel@tonic-gate 		CHECK3,	/* three check bytes to go */
36087c478bd9Sstevel@tonic-gate 		CHECK2,	/* two check bytes to go */
36097c478bd9Sstevel@tonic-gate 		CHECK1,	/* one check byte to go */
36107c478bd9Sstevel@tonic-gate 		DONE,	/* finished check, done */
36117c478bd9Sstevel@tonic-gate 		BAD}	/* got an error--stay here */
36127c478bd9Sstevel@tonic-gate 	mode;	/* current inflate mode */
36137c478bd9Sstevel@tonic-gate 
36147c478bd9Sstevel@tonic-gate 	/* mode dependent information */
36157c478bd9Sstevel@tonic-gate 	union {
36167c478bd9Sstevel@tonic-gate 		uInt method;	/* if FLAGS, method byte */
36177c478bd9Sstevel@tonic-gate 		struct {
36187c478bd9Sstevel@tonic-gate 			uLong was;	/* computed check value */
36197c478bd9Sstevel@tonic-gate 			uLong need;	/* stream check value */
36207c478bd9Sstevel@tonic-gate 		} check;	/* if CHECK, check values to compare */
36217c478bd9Sstevel@tonic-gate 		uInt marker;	/* if BAD, inflateSync's marker bytes count */
36227c478bd9Sstevel@tonic-gate 	} sub;	/* submode */
36237c478bd9Sstevel@tonic-gate 
36247c478bd9Sstevel@tonic-gate 	/* mode independent information */
36257c478bd9Sstevel@tonic-gate 	int  nowrap;	/* flag for no wrapper */
36267c478bd9Sstevel@tonic-gate 	uInt wbits;	/* log2(window size)  (8..15, defaults to 15) */
36277c478bd9Sstevel@tonic-gate 	/* current inflate_blocks state */
36287c478bd9Sstevel@tonic-gate 	inflate_blocks_statef *blocks;
36297c478bd9Sstevel@tonic-gate };
36307c478bd9Sstevel@tonic-gate 
36317c478bd9Sstevel@tonic-gate 
36327c478bd9Sstevel@tonic-gate int
inflateReset(z)36337c478bd9Sstevel@tonic-gate inflateReset(z)
36347c478bd9Sstevel@tonic-gate z_streamp z;
36357c478bd9Sstevel@tonic-gate {
36367c478bd9Sstevel@tonic-gate 	if (z == Z_NULL || z->state == Z_NULL)
36377c478bd9Sstevel@tonic-gate 		return (Z_STREAM_ERROR);
36387c478bd9Sstevel@tonic-gate 	z->total_in = z->total_out = 0;
36397c478bd9Sstevel@tonic-gate 	z->msg = Z_NULL;
36407c478bd9Sstevel@tonic-gate 	z->state->mode = z->state->nowrap ? BLOCKS : METHOD;
36417c478bd9Sstevel@tonic-gate 	inflate_blocks_reset(z->state->blocks, z, Z_NULL);
36427c478bd9Sstevel@tonic-gate 	Trace((stderr, "inflate: reset\n"));
36437c478bd9Sstevel@tonic-gate 	return (Z_OK);
36447c478bd9Sstevel@tonic-gate }
36457c478bd9Sstevel@tonic-gate 
36467c478bd9Sstevel@tonic-gate 
36477c478bd9Sstevel@tonic-gate int
inflateEnd(z)36487c478bd9Sstevel@tonic-gate inflateEnd(z)
36497c478bd9Sstevel@tonic-gate z_streamp z;
36507c478bd9Sstevel@tonic-gate {
36517c478bd9Sstevel@tonic-gate 	if (z == Z_NULL || z->state == Z_NULL || z->zfree == Z_NULL)
36527c478bd9Sstevel@tonic-gate 		return (Z_STREAM_ERROR);
36537c478bd9Sstevel@tonic-gate 	if (z->state->blocks != Z_NULL) {
36547c478bd9Sstevel@tonic-gate 		(void) inflate_blocks_free(z->state->blocks, z);
36557c478bd9Sstevel@tonic-gate 		z->state->blocks = Z_NULL;
36567c478bd9Sstevel@tonic-gate 	}
36577c478bd9Sstevel@tonic-gate 	ZFREE(z, z->state);
36587c478bd9Sstevel@tonic-gate 	z->state = Z_NULL;
36597c478bd9Sstevel@tonic-gate 	Trace((stderr, "inflate: end\n"));
36607c478bd9Sstevel@tonic-gate 	return (Z_OK);
36617c478bd9Sstevel@tonic-gate }
36627c478bd9Sstevel@tonic-gate 
36637c478bd9Sstevel@tonic-gate 
36647c478bd9Sstevel@tonic-gate int
inflateInit2_(z,w,version,stream_size)36657c478bd9Sstevel@tonic-gate inflateInit2_(z, w, version, stream_size)
36667c478bd9Sstevel@tonic-gate z_streamp z;
36677c478bd9Sstevel@tonic-gate int w;
36687c478bd9Sstevel@tonic-gate const char *version;
36697c478bd9Sstevel@tonic-gate int stream_size;
36707c478bd9Sstevel@tonic-gate {
36717c478bd9Sstevel@tonic-gate 	if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
36727c478bd9Sstevel@tonic-gate 	    stream_size != sizeof (z_stream))
36737c478bd9Sstevel@tonic-gate 		return (Z_VERSION_ERROR);
36747c478bd9Sstevel@tonic-gate 
36757c478bd9Sstevel@tonic-gate 	/* initialize state */
36767c478bd9Sstevel@tonic-gate 	if (z == Z_NULL)
36777c478bd9Sstevel@tonic-gate 		return (Z_STREAM_ERROR);
36787c478bd9Sstevel@tonic-gate 	z->msg = Z_NULL;
36797c478bd9Sstevel@tonic-gate #ifndef NO_ZCFUNCS
36807c478bd9Sstevel@tonic-gate 	if (z->zalloc == Z_NULL)
36817c478bd9Sstevel@tonic-gate 	{
36827c478bd9Sstevel@tonic-gate 		z->zalloc = zcalloc;
36837c478bd9Sstevel@tonic-gate 		z->opaque = (voidpf)0;
36847c478bd9Sstevel@tonic-gate 	}
36857c478bd9Sstevel@tonic-gate 	if (z->zfree == Z_NULL) z->zfree = zcfree;
36867c478bd9Sstevel@tonic-gate #endif
36877c478bd9Sstevel@tonic-gate 	if ((z->state = (struct internal_state FAR *)
36887c478bd9Sstevel@tonic-gate 	    ZALLOC(z, 1, sizeof (struct internal_state))) == Z_NULL)
36897c478bd9Sstevel@tonic-gate 		return (Z_MEM_ERROR);
36907c478bd9Sstevel@tonic-gate 	z->state->blocks = Z_NULL;
36917c478bd9Sstevel@tonic-gate 
36927c478bd9Sstevel@tonic-gate 	/* handle undocumented nowrap option (no zlib header or check) */
36937c478bd9Sstevel@tonic-gate 	z->state->nowrap = 0;
36947c478bd9Sstevel@tonic-gate 	if (w < 0)
36957c478bd9Sstevel@tonic-gate 	{
36967c478bd9Sstevel@tonic-gate 		w = - w;
36977c478bd9Sstevel@tonic-gate 		z->state->nowrap = 1;
36987c478bd9Sstevel@tonic-gate 	}
36997c478bd9Sstevel@tonic-gate 
37007c478bd9Sstevel@tonic-gate 	/* set window size */
37017c478bd9Sstevel@tonic-gate 	if (w < 8 || w > 15)
37027c478bd9Sstevel@tonic-gate 	{
37037c478bd9Sstevel@tonic-gate 		(void) inflateEnd(z);
37047c478bd9Sstevel@tonic-gate 		return (Z_STREAM_ERROR);
37057c478bd9Sstevel@tonic-gate 	}
37067c478bd9Sstevel@tonic-gate 	z->state->wbits = (uInt)w;
37077c478bd9Sstevel@tonic-gate 
37087c478bd9Sstevel@tonic-gate 	/* create inflate_blocks state */
37097c478bd9Sstevel@tonic-gate 	if ((z->state->blocks =
37107c478bd9Sstevel@tonic-gate 	    inflate_blocks_new(z, z->state->nowrap ?
37117c478bd9Sstevel@tonic-gate 		Z_NULL : adler32, (uInt)1 << w))
37127c478bd9Sstevel@tonic-gate 	    == Z_NULL)
37137c478bd9Sstevel@tonic-gate 	{
37147c478bd9Sstevel@tonic-gate 		(void) inflateEnd(z);
37157c478bd9Sstevel@tonic-gate 		return (Z_MEM_ERROR);
37167c478bd9Sstevel@tonic-gate 	}
37177c478bd9Sstevel@tonic-gate 	Trace((stderr, "inflate: allocated\n"));
37187c478bd9Sstevel@tonic-gate 
37197c478bd9Sstevel@tonic-gate 	/* reset state */
37207c478bd9Sstevel@tonic-gate 	(void) inflateReset(z);
37217c478bd9Sstevel@tonic-gate 	return (Z_OK);
37227c478bd9Sstevel@tonic-gate }
37237c478bd9Sstevel@tonic-gate 
37247c478bd9Sstevel@tonic-gate 
37257c478bd9Sstevel@tonic-gate int
inflateInit_(z,version,stream_size)37267c478bd9Sstevel@tonic-gate inflateInit_(z, version, stream_size)
37277c478bd9Sstevel@tonic-gate z_streamp z;
37287c478bd9Sstevel@tonic-gate const char *version;
37297c478bd9Sstevel@tonic-gate int stream_size;
37307c478bd9Sstevel@tonic-gate {
37317c478bd9Sstevel@tonic-gate 	return (inflateInit2_(z, DEF_WBITS, version, stream_size));
37327c478bd9Sstevel@tonic-gate }
37337c478bd9Sstevel@tonic-gate 
37347c478bd9Sstevel@tonic-gate /* PPP -- added "empty" label and changed f to Z_OK */
37357c478bd9Sstevel@tonic-gate #define	NEEDBYTE {if (z->avail_in == 0) goto empty; r = Z_OK; } ((void)0)
37367c478bd9Sstevel@tonic-gate #define	NEXTBYTE (z->avail_in--, z->total_in++, *z->next_in++)
37377c478bd9Sstevel@tonic-gate 
37387c478bd9Sstevel@tonic-gate int
inflate(z,f)37397c478bd9Sstevel@tonic-gate inflate(z, f)
37407c478bd9Sstevel@tonic-gate z_streamp z;
37417c478bd9Sstevel@tonic-gate int f;
37427c478bd9Sstevel@tonic-gate {
37437c478bd9Sstevel@tonic-gate 	int r;
37447c478bd9Sstevel@tonic-gate 	uInt b;
37457c478bd9Sstevel@tonic-gate 
37467c478bd9Sstevel@tonic-gate 	if (z == Z_NULL || z->state == Z_NULL || z->next_in == Z_NULL)
37477c478bd9Sstevel@tonic-gate 		return (Z_STREAM_ERROR);
37487c478bd9Sstevel@tonic-gate 	/* f = f == Z_FINISH ? Z_BUF_ERROR : Z_OK; -- PPP; Z_FINISH unused */
37497c478bd9Sstevel@tonic-gate 	r = Z_BUF_ERROR;
37507c478bd9Sstevel@tonic-gate 	/* CONSTCOND */
37517c478bd9Sstevel@tonic-gate 	while (1)
37527c478bd9Sstevel@tonic-gate 		switch (z->state->mode)
37537c478bd9Sstevel@tonic-gate 	{
37547c478bd9Sstevel@tonic-gate 	case METHOD:
37557c478bd9Sstevel@tonic-gate 		NEEDBYTE;
37567c478bd9Sstevel@tonic-gate 		if (((z->state->sub.method = NEXTBYTE) & 0xf) != Z_DEFLATED)
37577c478bd9Sstevel@tonic-gate 		{
37587c478bd9Sstevel@tonic-gate 			z->state->mode = BAD;
37597c478bd9Sstevel@tonic-gate 			z->msg = "unknown compression method";
37607c478bd9Sstevel@tonic-gate 			/* can't try inflateSync */
37617c478bd9Sstevel@tonic-gate 			z->state->sub.marker = 5;
37627c478bd9Sstevel@tonic-gate 			break;
37637c478bd9Sstevel@tonic-gate 		}
37647c478bd9Sstevel@tonic-gate 		if ((z->state->sub.method >> 4) + 8 > z->state->wbits)
37657c478bd9Sstevel@tonic-gate 		{
37667c478bd9Sstevel@tonic-gate 			z->state->mode = BAD;
37677c478bd9Sstevel@tonic-gate 			z->msg = "invalid window size";
37687c478bd9Sstevel@tonic-gate 			/* can't try inflateSync */
37697c478bd9Sstevel@tonic-gate 			z->state->sub.marker = 5;
37707c478bd9Sstevel@tonic-gate 			break;
37717c478bd9Sstevel@tonic-gate 		}
37727c478bd9Sstevel@tonic-gate 		z->state->mode = FLAG;
37737c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
37747c478bd9Sstevel@tonic-gate 	case FLAG:
37757c478bd9Sstevel@tonic-gate 		NEEDBYTE;
37767c478bd9Sstevel@tonic-gate 		b = NEXTBYTE;
37777c478bd9Sstevel@tonic-gate 		if (((z->state->sub.method << 8) + b) % 31)
37787c478bd9Sstevel@tonic-gate 		{
37797c478bd9Sstevel@tonic-gate 			z->state->mode = BAD;
37807c478bd9Sstevel@tonic-gate 			z->msg = "incorrect header check";
37817c478bd9Sstevel@tonic-gate 			/* can't try inflateSync */
37827c478bd9Sstevel@tonic-gate 			z->state->sub.marker = 5;
37837c478bd9Sstevel@tonic-gate 			break;
37847c478bd9Sstevel@tonic-gate 		}
37857c478bd9Sstevel@tonic-gate 		Trace((stderr, "inflate: zlib header ok\n"));
37867c478bd9Sstevel@tonic-gate 		if (!(b & PRESET_DICT))
37877c478bd9Sstevel@tonic-gate 		{
37887c478bd9Sstevel@tonic-gate 			z->state->mode = BLOCKS;
37897c478bd9Sstevel@tonic-gate 			break;
37907c478bd9Sstevel@tonic-gate 		}
37917c478bd9Sstevel@tonic-gate 		z->state->mode = DICT4;
37927c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
37937c478bd9Sstevel@tonic-gate 	case DICT4:
37947c478bd9Sstevel@tonic-gate 		NEEDBYTE;
37957c478bd9Sstevel@tonic-gate 		z->state->sub.check.need = (uLong)NEXTBYTE << 24;
37967c478bd9Sstevel@tonic-gate 		z->state->mode = DICT3;
37977c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
37987c478bd9Sstevel@tonic-gate 	case DICT3:
37997c478bd9Sstevel@tonic-gate 		NEEDBYTE;
38007c478bd9Sstevel@tonic-gate 		z->state->sub.check.need += (uLong)NEXTBYTE << 16;
38017c478bd9Sstevel@tonic-gate 		z->state->mode = DICT2;
38027c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
38037c478bd9Sstevel@tonic-gate 	case DICT2:
38047c478bd9Sstevel@tonic-gate 		NEEDBYTE;
38057c478bd9Sstevel@tonic-gate 		z->state->sub.check.need += (uLong)NEXTBYTE << 8;
38067c478bd9Sstevel@tonic-gate 		z->state->mode = DICT1;
38077c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
38087c478bd9Sstevel@tonic-gate 	case DICT1:
38097c478bd9Sstevel@tonic-gate 		NEEDBYTE;
38107c478bd9Sstevel@tonic-gate 		z->state->sub.check.need += (uLong)NEXTBYTE;
38117c478bd9Sstevel@tonic-gate 		z->adler = z->state->sub.check.need;
38127c478bd9Sstevel@tonic-gate 		z->state->mode = DICT0;
38137c478bd9Sstevel@tonic-gate 		return (Z_NEED_DICT);
38147c478bd9Sstevel@tonic-gate 	case DICT0:
38157c478bd9Sstevel@tonic-gate 		z->state->mode = BAD;
38167c478bd9Sstevel@tonic-gate 		z->msg = "need dictionary";
38177c478bd9Sstevel@tonic-gate 		z->state->sub.marker = 0;	/* can try inflateSync */
38187c478bd9Sstevel@tonic-gate 		return (Z_STREAM_ERROR);
38197c478bd9Sstevel@tonic-gate 	case BLOCKS:
38207c478bd9Sstevel@tonic-gate 		r = inflate_blocks(z->state->blocks, z, r);
38217c478bd9Sstevel@tonic-gate 		if (f == Z_PACKET_FLUSH && z->avail_in == 0 &&	/* PPP */
38227c478bd9Sstevel@tonic-gate 		    z->avail_out != 0)				/* PPP */
38237c478bd9Sstevel@tonic-gate 			r = inflate_packet_flush(z->state->blocks); /* PPP */
38247c478bd9Sstevel@tonic-gate 		if (r == Z_DATA_ERROR)
38257c478bd9Sstevel@tonic-gate 		{
38267c478bd9Sstevel@tonic-gate 			z->state->mode = BAD;
38277c478bd9Sstevel@tonic-gate 			/* can try inflateSync */
38287c478bd9Sstevel@tonic-gate 			z->state->sub.marker = 0;
38297c478bd9Sstevel@tonic-gate 			break;
38307c478bd9Sstevel@tonic-gate 		}
38317c478bd9Sstevel@tonic-gate 		/* PPP */
38327c478bd9Sstevel@tonic-gate 		if (r != Z_STREAM_END)
38337c478bd9Sstevel@tonic-gate 			return (r);
38347c478bd9Sstevel@tonic-gate 		r = Z_OK;	/* PPP */
38357c478bd9Sstevel@tonic-gate 		inflate_blocks_reset(z->state->blocks, z,
38367c478bd9Sstevel@tonic-gate 		    &z->state->sub.check.was);
38377c478bd9Sstevel@tonic-gate 		if (z->state->nowrap)
38387c478bd9Sstevel@tonic-gate 		{
38397c478bd9Sstevel@tonic-gate 			z->state->mode = DONE;
38407c478bd9Sstevel@tonic-gate 			break;
38417c478bd9Sstevel@tonic-gate 		}
38427c478bd9Sstevel@tonic-gate 		z->state->mode = CHECK4;
38437c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
38447c478bd9Sstevel@tonic-gate 	case CHECK4:
38457c478bd9Sstevel@tonic-gate 		NEEDBYTE;
38467c478bd9Sstevel@tonic-gate 		z->state->sub.check.need = (uLong)NEXTBYTE << 24;
38477c478bd9Sstevel@tonic-gate 		z->state->mode = CHECK3;
38487c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
38497c478bd9Sstevel@tonic-gate 	case CHECK3:
38507c478bd9Sstevel@tonic-gate 		NEEDBYTE;
38517c478bd9Sstevel@tonic-gate 		z->state->sub.check.need += (uLong)NEXTBYTE << 16;
38527c478bd9Sstevel@tonic-gate 		z->state->mode = CHECK2;
38537c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
38547c478bd9Sstevel@tonic-gate 	case CHECK2:
38557c478bd9Sstevel@tonic-gate 		NEEDBYTE;
38567c478bd9Sstevel@tonic-gate 		z->state->sub.check.need += (uLong)NEXTBYTE << 8;
38577c478bd9Sstevel@tonic-gate 		z->state->mode = CHECK1;
38587c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
38597c478bd9Sstevel@tonic-gate 	case CHECK1:
38607c478bd9Sstevel@tonic-gate 		NEEDBYTE;
38617c478bd9Sstevel@tonic-gate 		z->state->sub.check.need += (uLong)NEXTBYTE;
38627c478bd9Sstevel@tonic-gate 
38637c478bd9Sstevel@tonic-gate 		if (z->state->sub.check.was != z->state->sub.check.need)
38647c478bd9Sstevel@tonic-gate 		{
38657c478bd9Sstevel@tonic-gate 			z->state->mode = BAD;
38667c478bd9Sstevel@tonic-gate 			z->msg = "incorrect data check";
38677c478bd9Sstevel@tonic-gate 			/* can't try inflateSync */
38687c478bd9Sstevel@tonic-gate 			z->state->sub.marker = 5;
38697c478bd9Sstevel@tonic-gate 			break;
38707c478bd9Sstevel@tonic-gate 		}
38717c478bd9Sstevel@tonic-gate 		Trace((stderr, "inflate: zlib check ok\n"));
38727c478bd9Sstevel@tonic-gate 		z->state->mode = DONE;
38737c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
38747c478bd9Sstevel@tonic-gate 	case DONE:
38757c478bd9Sstevel@tonic-gate 		return (Z_STREAM_END);
38767c478bd9Sstevel@tonic-gate 	case BAD:
38777c478bd9Sstevel@tonic-gate 		return (Z_DATA_ERROR);
38787c478bd9Sstevel@tonic-gate 	default:
38797c478bd9Sstevel@tonic-gate 		return (Z_STREAM_ERROR);
38807c478bd9Sstevel@tonic-gate 	}
38817c478bd9Sstevel@tonic-gate 
38827c478bd9Sstevel@tonic-gate /* PPP -- packet flush handling */
38837c478bd9Sstevel@tonic-gate empty:
38847c478bd9Sstevel@tonic-gate 	if (f != Z_PACKET_FLUSH)
38857c478bd9Sstevel@tonic-gate 		return (r);
38867c478bd9Sstevel@tonic-gate 	z->state->mode = BAD;
38877c478bd9Sstevel@tonic-gate 	z->msg = "need more for packet flush";
38887c478bd9Sstevel@tonic-gate 	z->state->sub.marker = 0;	/* can try inflateSync */
38897c478bd9Sstevel@tonic-gate 	return (Z_DATA_ERROR);
38907c478bd9Sstevel@tonic-gate }
38917c478bd9Sstevel@tonic-gate 
38927c478bd9Sstevel@tonic-gate 
38937c478bd9Sstevel@tonic-gate int
inflateSetDictionary(z,dictionary,dictLength)38947c478bd9Sstevel@tonic-gate inflateSetDictionary(z, dictionary, dictLength)
38957c478bd9Sstevel@tonic-gate z_streamp z;
38967c478bd9Sstevel@tonic-gate const Bytef *dictionary;
38977c478bd9Sstevel@tonic-gate uInt  dictLength;
38987c478bd9Sstevel@tonic-gate {
38997c478bd9Sstevel@tonic-gate 	uInt length = dictLength;
39007c478bd9Sstevel@tonic-gate 
39017c478bd9Sstevel@tonic-gate 	if (z == Z_NULL || z->state == Z_NULL || z->state->mode != DICT0)
39027c478bd9Sstevel@tonic-gate 		return (Z_STREAM_ERROR);
39037c478bd9Sstevel@tonic-gate 
39047c478bd9Sstevel@tonic-gate 	if (adler32(1L, dictionary, dictLength) != z->adler)
39057c478bd9Sstevel@tonic-gate 		return (Z_DATA_ERROR);
39067c478bd9Sstevel@tonic-gate 	z->adler = 1L;
39077c478bd9Sstevel@tonic-gate 
39087c478bd9Sstevel@tonic-gate 	if (length >= ((uInt)1<<z->state->wbits))
39097c478bd9Sstevel@tonic-gate 	{
39107c478bd9Sstevel@tonic-gate 		length = (1<<z->state->wbits)-1;
39117c478bd9Sstevel@tonic-gate 		dictionary += dictLength - length;
39127c478bd9Sstevel@tonic-gate 	}
39137c478bd9Sstevel@tonic-gate 	inflate_set_dictionary(z->state->blocks, dictionary, length);
39147c478bd9Sstevel@tonic-gate 	z->state->mode = BLOCKS;
39157c478bd9Sstevel@tonic-gate 	return (Z_OK);
39167c478bd9Sstevel@tonic-gate }
39177c478bd9Sstevel@tonic-gate 
39187c478bd9Sstevel@tonic-gate /*
39197c478bd9Sstevel@tonic-gate  * This subroutine adds the data at next_in/avail_in to the output history
39207c478bd9Sstevel@tonic-gate  * without performing any output.  The output buffer must be "caught up";
39217c478bd9Sstevel@tonic-gate  * i.e. no pending output (hence s->read equals s->write), and the state must
39227c478bd9Sstevel@tonic-gate  * be BLOCKS (i.e. we should be willing to see the start of a series of
39237c478bd9Sstevel@tonic-gate  * BLOCKS).  On exit, the output will also be caught up, and the checksum
39247c478bd9Sstevel@tonic-gate  * will have been updated if need be.
39257c478bd9Sstevel@tonic-gate  *
39267c478bd9Sstevel@tonic-gate  * Added for PPP.
39277c478bd9Sstevel@tonic-gate  */
39287c478bd9Sstevel@tonic-gate 
39297c478bd9Sstevel@tonic-gate int
inflateIncomp(z)39307c478bd9Sstevel@tonic-gate inflateIncomp(z)
39317c478bd9Sstevel@tonic-gate z_stream *z;
39327c478bd9Sstevel@tonic-gate {
39337c478bd9Sstevel@tonic-gate 	if (z->state->mode != BLOCKS)
39347c478bd9Sstevel@tonic-gate 		return (Z_DATA_ERROR);
39357c478bd9Sstevel@tonic-gate 	return (inflate_addhistory(z->state->blocks, z));
39367c478bd9Sstevel@tonic-gate }
39377c478bd9Sstevel@tonic-gate 
39387c478bd9Sstevel@tonic-gate 
39397c478bd9Sstevel@tonic-gate int
inflateSync(z)39407c478bd9Sstevel@tonic-gate inflateSync(z)
39417c478bd9Sstevel@tonic-gate z_streamp z;
39427c478bd9Sstevel@tonic-gate {
39437c478bd9Sstevel@tonic-gate 	uInt n;	/* number of bytes to look at */
39447c478bd9Sstevel@tonic-gate 	Bytef *p;	/* pointer to bytes */
39457c478bd9Sstevel@tonic-gate 	uInt m;	/* number of marker bytes found in a row */
39467c478bd9Sstevel@tonic-gate 	uLong r, w;	/* temporaries to save total_in and total_out */
39477c478bd9Sstevel@tonic-gate 
39487c478bd9Sstevel@tonic-gate 	/* set up */
39497c478bd9Sstevel@tonic-gate 	if (z == Z_NULL || z->state == Z_NULL)
39507c478bd9Sstevel@tonic-gate 		return (Z_STREAM_ERROR);
39517c478bd9Sstevel@tonic-gate 	if (z->state->mode != BAD)
39527c478bd9Sstevel@tonic-gate 	{
39537c478bd9Sstevel@tonic-gate 		z->state->mode = BAD;
39547c478bd9Sstevel@tonic-gate 		z->state->sub.marker = 0;
39557c478bd9Sstevel@tonic-gate 	}
39567c478bd9Sstevel@tonic-gate 	if ((n = z->avail_in) == 0)
39577c478bd9Sstevel@tonic-gate 		return (Z_BUF_ERROR);
39587c478bd9Sstevel@tonic-gate 	p = z->next_in;
39597c478bd9Sstevel@tonic-gate 	m = z->state->sub.marker;
39607c478bd9Sstevel@tonic-gate 
39617c478bd9Sstevel@tonic-gate 	/* search */
39627c478bd9Sstevel@tonic-gate 	while (n && m < 4)
39637c478bd9Sstevel@tonic-gate 	{
39647c478bd9Sstevel@tonic-gate 		static const Byte mark[4] = { 0, 0, 0xff, 0xff };
39657c478bd9Sstevel@tonic-gate 		if (*p == mark[m])
39667c478bd9Sstevel@tonic-gate 			m++;
39677c478bd9Sstevel@tonic-gate 		else if (*p)
39687c478bd9Sstevel@tonic-gate 			m = 0;
39697c478bd9Sstevel@tonic-gate 		else
39707c478bd9Sstevel@tonic-gate 			/*
39717c478bd9Sstevel@tonic-gate 			 * This statement maps 2->2 and 3->1 because a
39727c478bd9Sstevel@tonic-gate 			 * mismatch with input byte 0x00 on the first
39737c478bd9Sstevel@tonic-gate 			 * 0xFF in the pattern means that we still
39747c478bd9Sstevel@tonic-gate 			 * have two contiguous zeros matched (thus
39757c478bd9Sstevel@tonic-gate 			 * offset 2 is kept), but a mismatch on the
39767c478bd9Sstevel@tonic-gate 			 * second 0xFF means that only one 0x00 byte
39777c478bd9Sstevel@tonic-gate 			 * has been matched.  (Boyer-Moore like
39787c478bd9Sstevel@tonic-gate 			 * search.)
39797c478bd9Sstevel@tonic-gate 			 */
39807c478bd9Sstevel@tonic-gate 			m = 4 - m;
39817c478bd9Sstevel@tonic-gate 		p++, n--;
39827c478bd9Sstevel@tonic-gate 	}
39837c478bd9Sstevel@tonic-gate 
39847c478bd9Sstevel@tonic-gate 	/* restore */
39857c478bd9Sstevel@tonic-gate 	z->total_in += p - z->next_in;
39867c478bd9Sstevel@tonic-gate 	z->next_in = p;
39877c478bd9Sstevel@tonic-gate 	z->avail_in = n;
39887c478bd9Sstevel@tonic-gate 	z->state->sub.marker = m;
39897c478bd9Sstevel@tonic-gate 
39907c478bd9Sstevel@tonic-gate 	/* return no joy or set up to restart on a new block */
39917c478bd9Sstevel@tonic-gate 	if (m != 4)
39927c478bd9Sstevel@tonic-gate 		return (Z_DATA_ERROR);
39937c478bd9Sstevel@tonic-gate 	r = z->total_in;  w = z->total_out;
39947c478bd9Sstevel@tonic-gate 	(void) inflateReset(z);
39957c478bd9Sstevel@tonic-gate 	z->total_in = r;  z->total_out = w;
39967c478bd9Sstevel@tonic-gate 	z->state->mode = BLOCKS;
39977c478bd9Sstevel@tonic-gate 	return (Z_OK);
39987c478bd9Sstevel@tonic-gate }
39997c478bd9Sstevel@tonic-gate 
40007c478bd9Sstevel@tonic-gate /*
40017c478bd9Sstevel@tonic-gate  * Returns true if inflate is currently at the end of a block
40027c478bd9Sstevel@tonic-gate  * generated by Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by
40037c478bd9Sstevel@tonic-gate  * one PPP implementation to provide an additional safety check. PPP
40047c478bd9Sstevel@tonic-gate  * uses Z_SYNC_FLUSH but removes the length bytes of the resulting
40057c478bd9Sstevel@tonic-gate  * empty stored block. When decompressing, PPP checks that at the end
40067c478bd9Sstevel@tonic-gate  * of input packet, inflate is waiting for these length bytes.
40077c478bd9Sstevel@tonic-gate  */
40087c478bd9Sstevel@tonic-gate int
inflateSyncPoint(z)40097c478bd9Sstevel@tonic-gate inflateSyncPoint(z)
40107c478bd9Sstevel@tonic-gate z_streamp z;
40117c478bd9Sstevel@tonic-gate {
40127c478bd9Sstevel@tonic-gate 	if (z == Z_NULL || z->state == Z_NULL || z->state->blocks == Z_NULL)
40137c478bd9Sstevel@tonic-gate 		return (Z_STREAM_ERROR);
40147c478bd9Sstevel@tonic-gate 	return (inflate_blocks_sync_point(z->state->blocks));
40157c478bd9Sstevel@tonic-gate }
40167c478bd9Sstevel@tonic-gate 
40177c478bd9Sstevel@tonic-gate #undef NEEDBYTE
40187c478bd9Sstevel@tonic-gate #undef NEXTBYTE
40197c478bd9Sstevel@tonic-gate /* --- inflate.c */
40207c478bd9Sstevel@tonic-gate 
40217c478bd9Sstevel@tonic-gate /* +++ infblock.c */
40227c478bd9Sstevel@tonic-gate /*
40237c478bd9Sstevel@tonic-gate  * infblock.c -- interpret and process block types to last block
40247c478bd9Sstevel@tonic-gate  * Copyright (C) 1995-1998 Mark Adler
40257c478bd9Sstevel@tonic-gate  * For conditions of distribution and use, see copyright notice in zlib.h
40267c478bd9Sstevel@tonic-gate  */
40277c478bd9Sstevel@tonic-gate 
40287c478bd9Sstevel@tonic-gate /* #include "zutil.h" */
40297c478bd9Sstevel@tonic-gate /* #include "infblock.h" */
40307c478bd9Sstevel@tonic-gate 
40317c478bd9Sstevel@tonic-gate /* +++ inftrees.h */
40327c478bd9Sstevel@tonic-gate /*
40337c478bd9Sstevel@tonic-gate  * inftrees.h -- header to use inftrees.c
40347c478bd9Sstevel@tonic-gate  * Copyright (C) 1995-1998 Mark Adler
40357c478bd9Sstevel@tonic-gate  * For conditions of distribution and use, see copyright notice in zlib.h
40367c478bd9Sstevel@tonic-gate  */
40377c478bd9Sstevel@tonic-gate 
40387c478bd9Sstevel@tonic-gate /*
40397c478bd9Sstevel@tonic-gate  * WARNING: this file should *not* be used by applications. It is part
40407c478bd9Sstevel@tonic-gate  * of the implementation of the compression library and is subject to
40417c478bd9Sstevel@tonic-gate  * change. Applications should only use zlib.h.
40427c478bd9Sstevel@tonic-gate  */
40437c478bd9Sstevel@tonic-gate 
40447c478bd9Sstevel@tonic-gate /*
40457c478bd9Sstevel@tonic-gate  * Huffman code lookup table entry--this entry is four bytes for
40467c478bd9Sstevel@tonic-gate  * machines that have 16-bit pointers (e.g. PC's in the small or
40477c478bd9Sstevel@tonic-gate  * medium model).
40487c478bd9Sstevel@tonic-gate  */
40497c478bd9Sstevel@tonic-gate 
40507c478bd9Sstevel@tonic-gate typedef struct inflate_huft_s FAR inflate_huft;
40517c478bd9Sstevel@tonic-gate 
40527c478bd9Sstevel@tonic-gate struct inflate_huft_s {
40537c478bd9Sstevel@tonic-gate 	union {
40547c478bd9Sstevel@tonic-gate 		struct {
40557c478bd9Sstevel@tonic-gate 			Byte Exop;	/* number of extra bits or operation */
40567c478bd9Sstevel@tonic-gate 			/* number of bits in this code or subcode */
40577c478bd9Sstevel@tonic-gate 			Byte Bits;
40587c478bd9Sstevel@tonic-gate 		} what;
40597c478bd9Sstevel@tonic-gate 		Bytef *pad;	/* pad structure to a power of 2 (4 bytes for */
40607c478bd9Sstevel@tonic-gate 	} word;	/*  16-bit, 8 bytes for 32-bit machines) */
40617c478bd9Sstevel@tonic-gate 	/* literal, length base, distance base, or table offset */
40627c478bd9Sstevel@tonic-gate 	uInt base;
40637c478bd9Sstevel@tonic-gate };
40647c478bd9Sstevel@tonic-gate 
40657c478bd9Sstevel@tonic-gate /*
40667c478bd9Sstevel@tonic-gate  * Maximum size of dynamic tree.  The maximum found in a long but non-
40677c478bd9Sstevel@tonic-gate  * exhaustive search was 1004 huft structures (850 for length/literals
40687c478bd9Sstevel@tonic-gate  * and 154 for distances, the latter actually the result of an
40697c478bd9Sstevel@tonic-gate  * exhaustive search).  The actual maximum is not known, but the value
40707c478bd9Sstevel@tonic-gate  * below is more than safe.
40717c478bd9Sstevel@tonic-gate  */
40727c478bd9Sstevel@tonic-gate #define	MANY 1440
40737c478bd9Sstevel@tonic-gate 
40747c478bd9Sstevel@tonic-gate extern int inflate_trees_bits OF((
40757c478bd9Sstevel@tonic-gate     uIntf *,			/* 19 code lengths */
40767c478bd9Sstevel@tonic-gate     uIntf *,			/* bits tree desired/actual depth */
40777c478bd9Sstevel@tonic-gate     inflate_huft * FAR *,	/* bits tree result */
40787c478bd9Sstevel@tonic-gate     inflate_huft *,		/* space for trees */
40797c478bd9Sstevel@tonic-gate     z_streamp));	/* for zalloc, zfree functions */
40807c478bd9Sstevel@tonic-gate 
40817c478bd9Sstevel@tonic-gate extern int inflate_trees_dynamic OF((
40827c478bd9Sstevel@tonic-gate     uInt,	/* number of literal/length codes */
40837c478bd9Sstevel@tonic-gate     uInt,	/* number of distance codes */
40847c478bd9Sstevel@tonic-gate     uIntf *,	/* that many (total) code lengths */
40857c478bd9Sstevel@tonic-gate     uIntf *,	/* literal desired/actual bit depth */
40867c478bd9Sstevel@tonic-gate     uIntf *,	/* distance desired/actual bit depth */
40877c478bd9Sstevel@tonic-gate     inflate_huft * FAR *,	/* literal/length tree result */
40887c478bd9Sstevel@tonic-gate     inflate_huft * FAR *,	/* distance tree result */
40897c478bd9Sstevel@tonic-gate     inflate_huft *,		/* space for trees */
40907c478bd9Sstevel@tonic-gate     z_streamp));	/* for zalloc, zfree functions */
40917c478bd9Sstevel@tonic-gate 
40927c478bd9Sstevel@tonic-gate extern int inflate_trees_fixed OF((
40937c478bd9Sstevel@tonic-gate     uIntf *,	/* literal desired/actual bit depth */
40947c478bd9Sstevel@tonic-gate     uIntf *,	/* distance desired/actual bit depth */
40957c478bd9Sstevel@tonic-gate     const inflate_huft * FAR *,	/* literal/length tree result */
40967c478bd9Sstevel@tonic-gate     const inflate_huft * FAR *,	/* distance tree result */
40977c478bd9Sstevel@tonic-gate     z_streamp));
40987c478bd9Sstevel@tonic-gate 
40997c478bd9Sstevel@tonic-gate /* --- inftrees.h */
41007c478bd9Sstevel@tonic-gate 
41017c478bd9Sstevel@tonic-gate /* +++ infcodes.h */
41027c478bd9Sstevel@tonic-gate /*
41037c478bd9Sstevel@tonic-gate  * infcodes.h -- header to use infcodes.c
41047c478bd9Sstevel@tonic-gate  * Copyright (C) 1995-1998 Mark Adler
41057c478bd9Sstevel@tonic-gate  * For conditions of distribution and use, see copyright notice in zlib.h
41067c478bd9Sstevel@tonic-gate  */
41077c478bd9Sstevel@tonic-gate 
41087c478bd9Sstevel@tonic-gate /*
41097c478bd9Sstevel@tonic-gate  * WARNING: this file should *not* be used by applications. It is part
41107c478bd9Sstevel@tonic-gate  * of the implementation of the compression library and is subject to
41117c478bd9Sstevel@tonic-gate  * change. Applications should only use zlib.h.
41127c478bd9Sstevel@tonic-gate  */
41137c478bd9Sstevel@tonic-gate 
41147c478bd9Sstevel@tonic-gate struct inflate_codes_state;
41157c478bd9Sstevel@tonic-gate typedef struct inflate_codes_state FAR inflate_codes_statef;
41167c478bd9Sstevel@tonic-gate 
41177c478bd9Sstevel@tonic-gate extern inflate_codes_statef *inflate_codes_new OF((
41187c478bd9Sstevel@tonic-gate     uInt, uInt,
41197c478bd9Sstevel@tonic-gate     const inflate_huft *, const inflate_huft *,
41207c478bd9Sstevel@tonic-gate     z_streamp));
41217c478bd9Sstevel@tonic-gate 
41227c478bd9Sstevel@tonic-gate extern int inflate_codes OF((
41237c478bd9Sstevel@tonic-gate     inflate_blocks_statef *,
41247c478bd9Sstevel@tonic-gate     z_streamp,
41257c478bd9Sstevel@tonic-gate     int));
41267c478bd9Sstevel@tonic-gate 
41277c478bd9Sstevel@tonic-gate extern void inflate_codes_free OF((
41287c478bd9Sstevel@tonic-gate     inflate_codes_statef *,
41297c478bd9Sstevel@tonic-gate     z_streamp));
41307c478bd9Sstevel@tonic-gate 
41317c478bd9Sstevel@tonic-gate /* --- infcodes.h */
41327c478bd9Sstevel@tonic-gate 
41337c478bd9Sstevel@tonic-gate /* +++ infutil.h */
41347c478bd9Sstevel@tonic-gate /*
41357c478bd9Sstevel@tonic-gate  * infutil.h -- types and macros common to blocks and codes
41367c478bd9Sstevel@tonic-gate  * Copyright (C) 1995-1998 Mark Adler
41377c478bd9Sstevel@tonic-gate  * For conditions of distribution and use, see copyright notice in zlib.h
41387c478bd9Sstevel@tonic-gate  */
41397c478bd9Sstevel@tonic-gate 
41407c478bd9Sstevel@tonic-gate /*
41417c478bd9Sstevel@tonic-gate  * WARNING: this file should *not* be used by applications. It is part
41427c478bd9Sstevel@tonic-gate  * of the implementation of the compression library and is subject to
41437c478bd9Sstevel@tonic-gate  * change. Applications should only use zlib.h.
41447c478bd9Sstevel@tonic-gate  */
41457c478bd9Sstevel@tonic-gate 
41467c478bd9Sstevel@tonic-gate #ifndef _INFUTIL_H
41477c478bd9Sstevel@tonic-gate #define	_INFUTIL_H
41487c478bd9Sstevel@tonic-gate 
41497c478bd9Sstevel@tonic-gate typedef enum {
41507c478bd9Sstevel@tonic-gate 	TYPE,	/* get type bits (3, including end bit) */
41517c478bd9Sstevel@tonic-gate 	LENS,	/* get lengths for stored */
41527c478bd9Sstevel@tonic-gate 	STORED,	/* processing stored block */
41537c478bd9Sstevel@tonic-gate 	TABLE,	/* get table lengths */
41547c478bd9Sstevel@tonic-gate 	BTREE,	/* get bit lengths tree for a dynamic block */
41557c478bd9Sstevel@tonic-gate 	DTREE,	/* get length, distance trees for a dynamic block */
41567c478bd9Sstevel@tonic-gate 	CODES,	/* processing fixed or dynamic block */
41577c478bd9Sstevel@tonic-gate 	DRY,	/* output remaining window bytes */
41587c478bd9Sstevel@tonic-gate 	DONEB,	/* finished last block, done */
41597c478bd9Sstevel@tonic-gate 	BADB}	/* got a data error--stuck here */
41607c478bd9Sstevel@tonic-gate inflate_block_mode;
41617c478bd9Sstevel@tonic-gate 
41627c478bd9Sstevel@tonic-gate /* inflate blocks semi-private state */
41637c478bd9Sstevel@tonic-gate struct inflate_blocks_state {
41647c478bd9Sstevel@tonic-gate 
41657c478bd9Sstevel@tonic-gate 	/* mode */
41667c478bd9Sstevel@tonic-gate 	inflate_block_mode  mode;	/* current inflate_block mode */
41677c478bd9Sstevel@tonic-gate 
41687c478bd9Sstevel@tonic-gate 	/* mode dependent information */
41697c478bd9Sstevel@tonic-gate 	union {
41707c478bd9Sstevel@tonic-gate 		uInt left;	/* if STORED, bytes left to copy */
41717c478bd9Sstevel@tonic-gate 		struct {
41727c478bd9Sstevel@tonic-gate 			uInt table;	/* table lengths (14 bits) */
41737c478bd9Sstevel@tonic-gate 			uInt index;	/* index into blens (or border) */
41747c478bd9Sstevel@tonic-gate 			uIntf *blens;	/* bit lengths of codes */
41757c478bd9Sstevel@tonic-gate 			uInt bb;	/* bit length tree depth */
41767c478bd9Sstevel@tonic-gate 			inflate_huft *tb;	/* bit length decoding tree */
41777c478bd9Sstevel@tonic-gate 		} trees;	/* if DTREE, decoding info for trees */
41787c478bd9Sstevel@tonic-gate 		struct {
41797c478bd9Sstevel@tonic-gate 			inflate_codes_statef *codes;
41807c478bd9Sstevel@tonic-gate 		} decode;	/* if CODES, current state */
41817c478bd9Sstevel@tonic-gate 	} sub;	/* submode */
41827c478bd9Sstevel@tonic-gate 	uInt last;	/* true if this block is the last block */
41837c478bd9Sstevel@tonic-gate 
41847c478bd9Sstevel@tonic-gate 	/* mode independent information */
41857c478bd9Sstevel@tonic-gate 	uInt bitk;	/* bits in bit buffer */
41867c478bd9Sstevel@tonic-gate 	uLong bitb;	/* bit buffer */
41877c478bd9Sstevel@tonic-gate 	inflate_huft *hufts;  /* single malloc for tree space */
41887c478bd9Sstevel@tonic-gate 	Bytef *window;	/* sliding window */
41897c478bd9Sstevel@tonic-gate 	Bytef *end;	/* one byte after sliding window */
41907c478bd9Sstevel@tonic-gate 	Bytef *read;	/* window read pointer */
41917c478bd9Sstevel@tonic-gate 	Bytef *write;	/* window write pointer */
41927c478bd9Sstevel@tonic-gate 	check_func checkfn;	/* check function */
41937c478bd9Sstevel@tonic-gate 	uLong check;	/* check on output */
41947c478bd9Sstevel@tonic-gate 
41957c478bd9Sstevel@tonic-gate };
41967c478bd9Sstevel@tonic-gate 
41977c478bd9Sstevel@tonic-gate 
41987c478bd9Sstevel@tonic-gate /* defines for inflate input/output */
41997c478bd9Sstevel@tonic-gate /*   update pointers and return */
42007c478bd9Sstevel@tonic-gate #define	UPDBITS {s->bitb = b; s->bitk = k; }
42017c478bd9Sstevel@tonic-gate #define	UPDIN {z->avail_in = n; z->total_in += p-z->next_in; z->next_in = p; }
42027c478bd9Sstevel@tonic-gate #define	UPDOUT {s->write = q; }
42037c478bd9Sstevel@tonic-gate #define	UPDATE {UPDBITS UPDIN UPDOUT}
42047c478bd9Sstevel@tonic-gate #define	LEAVE {UPDATE return (inflate_flush(s, z, r)); }
42057c478bd9Sstevel@tonic-gate /*   get bytes and bits */
42067c478bd9Sstevel@tonic-gate #define	LOADIN {p = z->next_in; n = z->avail_in; b = s->bitb; k = s->bitk; }
42077c478bd9Sstevel@tonic-gate #define	NEEDBYTE { if (n) r = Z_OK; else LEAVE }
42087c478bd9Sstevel@tonic-gate #define	NEXTBYTE (n--, *p++)
42097c478bd9Sstevel@tonic-gate #define	NEEDBITS(j) { while (k < (j)) { NEEDBYTE; b |= ((uLong)NEXTBYTE)<<k; \
42107c478bd9Sstevel@tonic-gate 	k += 8; }}
42117c478bd9Sstevel@tonic-gate #define	DUMPBITS(j) {b >>= (j); k -= (j); }
42127c478bd9Sstevel@tonic-gate /*   output bytes */
42137c478bd9Sstevel@tonic-gate #define	WAVAIL (uInt)(q < s->read ? s->read-q-1 : s->end-q)
42147c478bd9Sstevel@tonic-gate #define	LOADOUT {q = s->write; m = (uInt)WAVAIL; }
42157c478bd9Sstevel@tonic-gate #define	WWRAP {if (q == s->end && s->read != s->window) {q = s->window; \
42167c478bd9Sstevel@tonic-gate 	m = (uInt)WAVAIL; }}
42177c478bd9Sstevel@tonic-gate #define	FLUSH {UPDOUT r = inflate_flush(s, z, r); LOADOUT}
42187c478bd9Sstevel@tonic-gate #define	NEEDOUT {if (m == 0) {WWRAP if (m == 0) { FLUSH WWRAP \
42197c478bd9Sstevel@tonic-gate 	if (m == 0) LEAVE }} r = Z_OK; }
42207c478bd9Sstevel@tonic-gate #define	OUTBYTE(a) {*q++ = (Byte)(a); m--; }
42217c478bd9Sstevel@tonic-gate /*   load local pointers */
42227c478bd9Sstevel@tonic-gate #define	LOAD {LOADIN LOADOUT}
42237c478bd9Sstevel@tonic-gate 
42247c478bd9Sstevel@tonic-gate /* masks for lower bits (size given to avoid silly warnings with Visual C++) */
42257c478bd9Sstevel@tonic-gate extern uInt inflate_mask[17];
42267c478bd9Sstevel@tonic-gate 
42277c478bd9Sstevel@tonic-gate /* copy as much as possible from the sliding window to the output area */
42287c478bd9Sstevel@tonic-gate extern int inflate_flush OF((
42297c478bd9Sstevel@tonic-gate     inflate_blocks_statef *,
42307c478bd9Sstevel@tonic-gate     z_streamp,
42317c478bd9Sstevel@tonic-gate     int));
42327c478bd9Sstevel@tonic-gate 
42337c478bd9Sstevel@tonic-gate #ifndef NO_DUMMY_DECL
42347c478bd9Sstevel@tonic-gate struct internal_state {int dummy; };	/* for buggy compilers */
42357c478bd9Sstevel@tonic-gate #endif
42367c478bd9Sstevel@tonic-gate 
42377c478bd9Sstevel@tonic-gate #endif
42387c478bd9Sstevel@tonic-gate /* --- infutil.h */
42397c478bd9Sstevel@tonic-gate 
42407c478bd9Sstevel@tonic-gate #ifndef NO_DUMMY_DECL
42417c478bd9Sstevel@tonic-gate struct inflate_codes_state {int dummy; };	/* for buggy compilers */
42427c478bd9Sstevel@tonic-gate #endif
42437c478bd9Sstevel@tonic-gate 
42447c478bd9Sstevel@tonic-gate /* Table for deflate from PKZIP's appnote.txt. */
42457c478bd9Sstevel@tonic-gate local const uInt border[] = { /* Order of the bit length code lengths */
42467c478bd9Sstevel@tonic-gate 	16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
42477c478bd9Sstevel@tonic-gate 
42487c478bd9Sstevel@tonic-gate /*
42497c478bd9Sstevel@tonic-gate  * Notes beyond the 1.93a appnote.txt:
42507c478bd9Sstevel@tonic-gate  *
42517c478bd9Sstevel@tonic-gate  *   1. Distance pointers never point before the beginning of the output
42527c478bd9Sstevel@tonic-gate  *      stream.
42537c478bd9Sstevel@tonic-gate  *   2. Distance pointers can point back across blocks, up to 32k away.
42547c478bd9Sstevel@tonic-gate  *   3. There is an implied maximum of 7 bits for the bit length table and
42557c478bd9Sstevel@tonic-gate  *      15 bits for the actual data.
42567c478bd9Sstevel@tonic-gate  *   4. If only one code exists, then it is encoded using one bit.  (Zero
42577c478bd9Sstevel@tonic-gate  *      would be more efficient, but perhaps a little confusing.)  If two
42587c478bd9Sstevel@tonic-gate  *      codes exist, they are coded using one bit each (0 and 1).
42597c478bd9Sstevel@tonic-gate  *   5. There is no way of sending zero distance codes--a dummy must be
42607c478bd9Sstevel@tonic-gate  *      sent if there are none.  (History: a pre 2.0 version of PKZIP would
42617c478bd9Sstevel@tonic-gate  *      store blocks with no distance codes, but this was discovered to be
42627c478bd9Sstevel@tonic-gate  *      too harsh a criterion.)  Valid only for 1.93a.  2.04c does allow
42637c478bd9Sstevel@tonic-gate  *      zero distance codes, which is sent as one code of zero bits in
42647c478bd9Sstevel@tonic-gate  *      length.
42657c478bd9Sstevel@tonic-gate  *   6. There are up to 286 literal/length codes.  Code 256 represents the
42667c478bd9Sstevel@tonic-gate  *      end-of-block.  Note however that the static length tree defines
42677c478bd9Sstevel@tonic-gate  *      288 codes just to fill out the Huffman codes.  Codes 286 and 287
42687c478bd9Sstevel@tonic-gate  *      cannot be used though, since there is no length base or extra bits
42697c478bd9Sstevel@tonic-gate  *      defined for them.  Similarily, there are up to 30 distance codes.
42707c478bd9Sstevel@tonic-gate  *      However, static trees define 32 codes (all 5 bits) to fill out the
42717c478bd9Sstevel@tonic-gate  *      Huffman codes, but the last two had better not show up in the data.
42727c478bd9Sstevel@tonic-gate  *   7. Unzip can check dynamic Huffman blocks for complete code sets.
42737c478bd9Sstevel@tonic-gate  *      The exception is that a single code would not be complete (see #4).
42747c478bd9Sstevel@tonic-gate  *   8. The five bits following the block type is really the number of
42757c478bd9Sstevel@tonic-gate  *      literal codes sent minus 257.
42767c478bd9Sstevel@tonic-gate  *   9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits
42777c478bd9Sstevel@tonic-gate  *      (1+6+6).  Therefore, to output three times the length, you output
42787c478bd9Sstevel@tonic-gate  *      three codes (1+1+1), whereas to output four times the same length,
42797c478bd9Sstevel@tonic-gate  *      you only need two codes (1+3).  Hmm.
42807c478bd9Sstevel@tonic-gate  *  10. In the tree reconstruction algorithm, Code = Code + Increment
42817c478bd9Sstevel@tonic-gate  *      only if BitLength(i) is not zero.  (Pretty obvious.)
42827c478bd9Sstevel@tonic-gate  *  11. Correction: 4 Bits: #of Bit Length codes - 4     (4 - 19)
42837c478bd9Sstevel@tonic-gate  *  12. Note: length code 284 can represent 227-258, but length code 285
42847c478bd9Sstevel@tonic-gate  *      really is 258.  The last length deserves its own, short code
42857c478bd9Sstevel@tonic-gate  *      since it gets used a lot in very redundant files.  The length
42867c478bd9Sstevel@tonic-gate  *      258 is special since 258 - 3 (the min match length) is 255.
42877c478bd9Sstevel@tonic-gate  *  13. The literal/length and distance code bit lengths are read as a
42887c478bd9Sstevel@tonic-gate  *      single stream of lengths.  It is possible (and advantageous) for
42897c478bd9Sstevel@tonic-gate  *      a repeat code (16, 17, or 18) to go across the boundary between
42907c478bd9Sstevel@tonic-gate  *      the two sets of lengths.
42917c478bd9Sstevel@tonic-gate  */
42927c478bd9Sstevel@tonic-gate 
42937c478bd9Sstevel@tonic-gate 
42947c478bd9Sstevel@tonic-gate void
inflate_blocks_reset(s,z,c)42957c478bd9Sstevel@tonic-gate inflate_blocks_reset(s, z, c)
42967c478bd9Sstevel@tonic-gate inflate_blocks_statef *s;
42977c478bd9Sstevel@tonic-gate z_streamp z;
42987c478bd9Sstevel@tonic-gate uLongf *c;
42997c478bd9Sstevel@tonic-gate {
43007c478bd9Sstevel@tonic-gate 	if (c != Z_NULL)
43017c478bd9Sstevel@tonic-gate 		*c = s->check;
43027c478bd9Sstevel@tonic-gate 	if ((s->mode == BTREE || s->mode == DTREE) &&
43037c478bd9Sstevel@tonic-gate 	    s->sub.trees.blens != Z_NULL) {
43047c478bd9Sstevel@tonic-gate 		ZFREE(z, s->sub.trees.blens);
43057c478bd9Sstevel@tonic-gate 		s->sub.trees.blens = Z_NULL;
43067c478bd9Sstevel@tonic-gate 	}
43077c478bd9Sstevel@tonic-gate 	if (s->mode == CODES && s->sub.decode.codes != Z_NULL) {
43087c478bd9Sstevel@tonic-gate 		(void) inflate_codes_free(s->sub.decode.codes, z);
43097c478bd9Sstevel@tonic-gate 		s->sub.decode.codes = Z_NULL;
43107c478bd9Sstevel@tonic-gate 	}
43117c478bd9Sstevel@tonic-gate 	s->mode = TYPE;
43127c478bd9Sstevel@tonic-gate 	s->bitk = 0;
43137c478bd9Sstevel@tonic-gate 	s->bitb = 0;
43147c478bd9Sstevel@tonic-gate 	s->read = s->write = s->window;
43157c478bd9Sstevel@tonic-gate 	if (s->checkfn != Z_NULL)
43167c478bd9Sstevel@tonic-gate 		z->adler = s->check = (*s->checkfn)(0L, Z_NULL, 0);
43177c478bd9Sstevel@tonic-gate 	Trace((stderr, "inflate:   blocks reset\n"));
43187c478bd9Sstevel@tonic-gate }
43197c478bd9Sstevel@tonic-gate 
43207c478bd9Sstevel@tonic-gate inflate_blocks_statef *
inflate_blocks_new(z,c,w)43217c478bd9Sstevel@tonic-gate inflate_blocks_new(z, c, w)
43227c478bd9Sstevel@tonic-gate z_streamp z;
43237c478bd9Sstevel@tonic-gate check_func c;
43247c478bd9Sstevel@tonic-gate uInt w;
43257c478bd9Sstevel@tonic-gate {
43267c478bd9Sstevel@tonic-gate 	inflate_blocks_statef *s;
43277c478bd9Sstevel@tonic-gate 
43287c478bd9Sstevel@tonic-gate 	if ((s = (inflate_blocks_statef *)ZALLOC
43297c478bd9Sstevel@tonic-gate 	    (z, 1, sizeof (struct inflate_blocks_state))) == Z_NULL)
43307c478bd9Sstevel@tonic-gate 		return (s);
43317c478bd9Sstevel@tonic-gate 	s->hufts = (inflate_huft *)ZALLOC(z, MANY, sizeof (inflate_huft));
43327c478bd9Sstevel@tonic-gate 	if (s->hufts == Z_NULL) {
43337c478bd9Sstevel@tonic-gate 		ZFREE(z, s);
43347c478bd9Sstevel@tonic-gate 		return (Z_NULL);
43357c478bd9Sstevel@tonic-gate 	}
43367c478bd9Sstevel@tonic-gate 	if ((s->window = (Bytef *)ZALLOC(z, 1, w)) == Z_NULL)
43377c478bd9Sstevel@tonic-gate 	{
43387c478bd9Sstevel@tonic-gate 		ZFREE(z, s->hufts);
43397c478bd9Sstevel@tonic-gate 		ZFREE(z, s);
43407c478bd9Sstevel@tonic-gate 		return (Z_NULL);
43417c478bd9Sstevel@tonic-gate 	}
43427c478bd9Sstevel@tonic-gate 	s->end = s->window + w;
43437c478bd9Sstevel@tonic-gate 	s->checkfn = c;
43447c478bd9Sstevel@tonic-gate 	s->mode = TYPE;
43457c478bd9Sstevel@tonic-gate 	Trace((stderr, "inflate:   blocks allocated\n"));
43467c478bd9Sstevel@tonic-gate 	inflate_blocks_reset(s, z, Z_NULL);
43477c478bd9Sstevel@tonic-gate 	return (s);
43487c478bd9Sstevel@tonic-gate }
43497c478bd9Sstevel@tonic-gate 
43507c478bd9Sstevel@tonic-gate 
43517c478bd9Sstevel@tonic-gate int
inflate_blocks(s,z,r)43527c478bd9Sstevel@tonic-gate inflate_blocks(s, z, r)
43537c478bd9Sstevel@tonic-gate inflate_blocks_statef *s;
43547c478bd9Sstevel@tonic-gate z_streamp z;
43557c478bd9Sstevel@tonic-gate int r;
43567c478bd9Sstevel@tonic-gate {
43577c478bd9Sstevel@tonic-gate 	uInt t;	/* temporary storage */
43587c478bd9Sstevel@tonic-gate 	uLong b;	/* bit buffer */
43597c478bd9Sstevel@tonic-gate 	uInt k;	/* bits in bit buffer */
43607c478bd9Sstevel@tonic-gate 	Bytef *p;	/* input data pointer */
43617c478bd9Sstevel@tonic-gate 	uInt n;	/* bytes available there */
43627c478bd9Sstevel@tonic-gate 	Bytef *q;	/* output window write pointer */
43637c478bd9Sstevel@tonic-gate 	uInt m;	/* bytes to end of window or read pointer */
43647c478bd9Sstevel@tonic-gate 
43657c478bd9Sstevel@tonic-gate 	/* copy input/output information to locals (UPDATE macro restores) */
43667c478bd9Sstevel@tonic-gate 	LOAD;
43677c478bd9Sstevel@tonic-gate 
43687c478bd9Sstevel@tonic-gate 	/* process input based on current state */
43697c478bd9Sstevel@tonic-gate 	/* CONSTCOND */
43707c478bd9Sstevel@tonic-gate 	while (1)
43717c478bd9Sstevel@tonic-gate 		switch (s->mode)
43727c478bd9Sstevel@tonic-gate 	{
43737c478bd9Sstevel@tonic-gate 	case TYPE:
43747c478bd9Sstevel@tonic-gate 		NEEDBITS(3);
43757c478bd9Sstevel@tonic-gate 		t = (uInt)b & 7;
43767c478bd9Sstevel@tonic-gate 		s->last = t & 1;
43777c478bd9Sstevel@tonic-gate 		switch (t >> 1)
43787c478bd9Sstevel@tonic-gate 		{
43797c478bd9Sstevel@tonic-gate 		case 0:			/* stored */
43807c478bd9Sstevel@tonic-gate 			Trace((stderr, "inflate:     stored block%s\n",
43817c478bd9Sstevel@tonic-gate 			    s->last ? " (last)" : ""));
43827c478bd9Sstevel@tonic-gate 			DUMPBITS(3);
43837c478bd9Sstevel@tonic-gate 			t = k & 7;	/* go to byte boundary */
43847c478bd9Sstevel@tonic-gate 			DUMPBITS(t);
43857c478bd9Sstevel@tonic-gate 			s->mode = LENS;	/* get length of stored block */
43867c478bd9Sstevel@tonic-gate 			break;
43877c478bd9Sstevel@tonic-gate 		case 1:			/* fixed */
43887c478bd9Sstevel@tonic-gate 			Trace((stderr, "inflate:     fixed codes block%s\n",
43897c478bd9Sstevel@tonic-gate 			    s->last ? " (last)" : ""));
43907c478bd9Sstevel@tonic-gate 			{
43917c478bd9Sstevel@tonic-gate 				uInt bl, bd;
43927c478bd9Sstevel@tonic-gate 				const inflate_huft *tl, *td;
43937c478bd9Sstevel@tonic-gate 
43947c478bd9Sstevel@tonic-gate 				(void) inflate_trees_fixed(&bl, &bd, &tl, &td,
43957c478bd9Sstevel@tonic-gate 				    z);
43967c478bd9Sstevel@tonic-gate 				s->sub.decode.codes = inflate_codes_new(bl,
43977c478bd9Sstevel@tonic-gate 				    bd, tl, td, z);
43987c478bd9Sstevel@tonic-gate 				if (s->sub.decode.codes == Z_NULL)
43997c478bd9Sstevel@tonic-gate 				{
44007c478bd9Sstevel@tonic-gate 					r = Z_MEM_ERROR;
44017c478bd9Sstevel@tonic-gate 					LEAVE
44027c478bd9Sstevel@tonic-gate 				}
44037c478bd9Sstevel@tonic-gate 			}
44047c478bd9Sstevel@tonic-gate 			DUMPBITS(3);
44057c478bd9Sstevel@tonic-gate 			s->mode = CODES;
44067c478bd9Sstevel@tonic-gate 			break;
44077c478bd9Sstevel@tonic-gate 		case 2:			/* dynamic */
44087c478bd9Sstevel@tonic-gate 			Trace((stderr, "inflate:     dynamic codes block%s\n",
44097c478bd9Sstevel@tonic-gate 			    s->last ? " (last)" : ""));
44107c478bd9Sstevel@tonic-gate 			DUMPBITS(3);
44117c478bd9Sstevel@tonic-gate 			s->mode = TABLE;
44127c478bd9Sstevel@tonic-gate 			break;
44137c478bd9Sstevel@tonic-gate 		case 3:			/* illegal */
44147c478bd9Sstevel@tonic-gate 			DUMPBITS(3);
44157c478bd9Sstevel@tonic-gate 			s->mode = BADB;
44167c478bd9Sstevel@tonic-gate 			z->msg = "invalid block type";
44177c478bd9Sstevel@tonic-gate 			r = Z_DATA_ERROR;
44187c478bd9Sstevel@tonic-gate 			LEAVE
44197c478bd9Sstevel@tonic-gate 		}
44207c478bd9Sstevel@tonic-gate 		break;
44217c478bd9Sstevel@tonic-gate 	case LENS:
44227c478bd9Sstevel@tonic-gate 		NEEDBITS(32);
44237c478bd9Sstevel@tonic-gate 		if ((((~b) >> 16) & 0xffff) != (b & 0xffff))
44247c478bd9Sstevel@tonic-gate 		{
44257c478bd9Sstevel@tonic-gate 			s->mode = BADB;
44267c478bd9Sstevel@tonic-gate 			z->msg = "invalid stored block lengths";
44277c478bd9Sstevel@tonic-gate 			r = Z_DATA_ERROR;
44287c478bd9Sstevel@tonic-gate 			LEAVE
44297c478bd9Sstevel@tonic-gate 		}
44307c478bd9Sstevel@tonic-gate 		s->sub.left = (uInt)b & 0xffff;
44317c478bd9Sstevel@tonic-gate 		b = k = 0;	/* dump bits */
44327c478bd9Sstevel@tonic-gate 		Tracev((stderr, "inflate:       stored length %u\n",
44337c478bd9Sstevel@tonic-gate 		    s->sub.left));
44347c478bd9Sstevel@tonic-gate 		s->mode = s->sub.left ? STORED : (s->last ? DRY : TYPE);
44357c478bd9Sstevel@tonic-gate 		break;
44367c478bd9Sstevel@tonic-gate 	case STORED:
44377c478bd9Sstevel@tonic-gate 		if (n == 0)
44387c478bd9Sstevel@tonic-gate 			LEAVE
44397c478bd9Sstevel@tonic-gate 		NEEDOUT;
44407c478bd9Sstevel@tonic-gate 		t = s->sub.left;
44417c478bd9Sstevel@tonic-gate 		if (t > n) t = n;
44427c478bd9Sstevel@tonic-gate 		if (t > m) t = m;
44437c478bd9Sstevel@tonic-gate 		zmemcpy(q, p, t);
44447c478bd9Sstevel@tonic-gate 		p += t;  n -= t;
44457c478bd9Sstevel@tonic-gate 		q += t;  m -= t;
44467c478bd9Sstevel@tonic-gate 		if ((s->sub.left -= t) != 0)
44477c478bd9Sstevel@tonic-gate 			break;
44487c478bd9Sstevel@tonic-gate 		Tracev((stderr,
44497c478bd9Sstevel@tonic-gate 		    "inflate:       stored end, %lu total out\n",
44507c478bd9Sstevel@tonic-gate 		    z->total_out + (q >= s->read ? q - s->read :
44517c478bd9Sstevel@tonic-gate 			(s->end - s->read) + (q - s->window))));
44527c478bd9Sstevel@tonic-gate 		s->mode = s->last ? DRY : TYPE;
44537c478bd9Sstevel@tonic-gate 		break;
44547c478bd9Sstevel@tonic-gate 	case TABLE:
44557c478bd9Sstevel@tonic-gate 		NEEDBITS(14);
44567c478bd9Sstevel@tonic-gate 		s->sub.trees.table = t = (uInt)b & 0x3fff;
44577c478bd9Sstevel@tonic-gate #ifndef PKZIP_BUG_WORKAROUND
44587c478bd9Sstevel@tonic-gate 		if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29)
44597c478bd9Sstevel@tonic-gate 		{
44607c478bd9Sstevel@tonic-gate 			s->mode = BADB;
44617c478bd9Sstevel@tonic-gate 			z->msg =
44627c478bd9Sstevel@tonic-gate 			    (char *)"too many length or distance symbols";
44637c478bd9Sstevel@tonic-gate 			r = Z_DATA_ERROR;
44647c478bd9Sstevel@tonic-gate 			LEAVE
44657c478bd9Sstevel@tonic-gate 		}
44667c478bd9Sstevel@tonic-gate #endif
44677c478bd9Sstevel@tonic-gate 		t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f);
44687c478bd9Sstevel@tonic-gate 		/* if (t < 19) t = 19; */
44697c478bd9Sstevel@tonic-gate 		if ((s->sub.trees.blens = (uIntf*)ZALLOC(z, t,
44707c478bd9Sstevel@tonic-gate 		    sizeof (uInt))) == Z_NULL)
44717c478bd9Sstevel@tonic-gate 		{
44727c478bd9Sstevel@tonic-gate 			r = Z_MEM_ERROR;
44737c478bd9Sstevel@tonic-gate 			LEAVE
44747c478bd9Sstevel@tonic-gate 		}
44757c478bd9Sstevel@tonic-gate 		DUMPBITS(14);
44767c478bd9Sstevel@tonic-gate 		s->sub.trees.index = 0;
44777c478bd9Sstevel@tonic-gate 		Tracev((stderr, "inflate:       table sizes ok\n"));
44787c478bd9Sstevel@tonic-gate 		s->mode = BTREE;
44797c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
44807c478bd9Sstevel@tonic-gate 	case BTREE:
44817c478bd9Sstevel@tonic-gate 		while (s->sub.trees.index < 4 + (s->sub.trees.table >> 10))
44827c478bd9Sstevel@tonic-gate 		{
44837c478bd9Sstevel@tonic-gate 			NEEDBITS(3);
44847c478bd9Sstevel@tonic-gate 			s->sub.trees.blens[border[s->sub.trees.index++]] =
44857c478bd9Sstevel@tonic-gate 			    (uInt)b & 7;
44867c478bd9Sstevel@tonic-gate 			DUMPBITS(3);
44877c478bd9Sstevel@tonic-gate 		}
44887c478bd9Sstevel@tonic-gate 		while (s->sub.trees.index < 19)
44897c478bd9Sstevel@tonic-gate 			s->sub.trees.blens[border[s->sub.trees.index++]] =
44907c478bd9Sstevel@tonic-gate 			    0;
44917c478bd9Sstevel@tonic-gate 		s->sub.trees.bb = 7;
44927c478bd9Sstevel@tonic-gate 		t = inflate_trees_bits(s->sub.trees.blens, &s->sub.trees.bb,
44937c478bd9Sstevel@tonic-gate 		    &s->sub.trees.tb, s->hufts, z);
44947c478bd9Sstevel@tonic-gate 		if (t != Z_OK)
44957c478bd9Sstevel@tonic-gate 		{
44967c478bd9Sstevel@tonic-gate 			ZFREE(z, s->sub.trees.blens);
44977c478bd9Sstevel@tonic-gate 			s->sub.trees.blens = Z_NULL;
44987c478bd9Sstevel@tonic-gate 			r = t;
44997c478bd9Sstevel@tonic-gate 			if (r == Z_DATA_ERROR)
45007c478bd9Sstevel@tonic-gate 				s->mode = BADB;
45017c478bd9Sstevel@tonic-gate 			LEAVE
45027c478bd9Sstevel@tonic-gate 		}
45037c478bd9Sstevel@tonic-gate 		s->sub.trees.index = 0;
45047c478bd9Sstevel@tonic-gate 		Tracev((stderr, "inflate:       bits tree ok\n"));
45057c478bd9Sstevel@tonic-gate 		s->mode = DTREE;
45067c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
45077c478bd9Sstevel@tonic-gate 	case DTREE:
45087c478bd9Sstevel@tonic-gate 		while (t = s->sub.trees.table,
45097c478bd9Sstevel@tonic-gate 		    s->sub.trees.index < 258 + (t & 0x1f) +
45107c478bd9Sstevel@tonic-gate 		    ((t >> 5) & 0x1f))
45117c478bd9Sstevel@tonic-gate 		{
45127c478bd9Sstevel@tonic-gate 			inflate_huft *h;
45137c478bd9Sstevel@tonic-gate 			uInt i, j, c;
45147c478bd9Sstevel@tonic-gate 
45157c478bd9Sstevel@tonic-gate 			t = s->sub.trees.bb;
45167c478bd9Sstevel@tonic-gate 			NEEDBITS(t);
45177c478bd9Sstevel@tonic-gate 			h = s->sub.trees.tb + ((uInt)b & inflate_mask[t]);
45187c478bd9Sstevel@tonic-gate 			t = h->word.what.Bits;
45197c478bd9Sstevel@tonic-gate 			c = h->base;
45207c478bd9Sstevel@tonic-gate 			if (c < 16)
45217c478bd9Sstevel@tonic-gate 			{
45227c478bd9Sstevel@tonic-gate 				DUMPBITS(t);
45237c478bd9Sstevel@tonic-gate 				s->sub.trees.blens[s->sub.trees.index++] =
45247c478bd9Sstevel@tonic-gate 				    c;
45257c478bd9Sstevel@tonic-gate 			} else { /* c == 16..18 */
45267c478bd9Sstevel@tonic-gate 				i = c == 18 ? 7 : c - 14;
45277c478bd9Sstevel@tonic-gate 				j = c == 18 ? 11 : 3;
45287c478bd9Sstevel@tonic-gate 				NEEDBITS(t + i);
45297c478bd9Sstevel@tonic-gate 				DUMPBITS(t);
45307c478bd9Sstevel@tonic-gate 				j += (uInt)b & inflate_mask[i];
45317c478bd9Sstevel@tonic-gate 				DUMPBITS(i);
45327c478bd9Sstevel@tonic-gate 				i = s->sub.trees.index;
45337c478bd9Sstevel@tonic-gate 				t = s->sub.trees.table;
45347c478bd9Sstevel@tonic-gate 				if (i + j > 258 + (t & 0x1f) +
45357c478bd9Sstevel@tonic-gate 				    ((t >> 5) & 0x1f) ||
45367c478bd9Sstevel@tonic-gate 				    (c == 16 && i < 1))
45377c478bd9Sstevel@tonic-gate 				{
45387c478bd9Sstevel@tonic-gate 					ZFREE(z, s->sub.trees.blens);
45397c478bd9Sstevel@tonic-gate 					s->sub.trees.blens = Z_NULL;
45407c478bd9Sstevel@tonic-gate 					s->mode = BADB;
45417c478bd9Sstevel@tonic-gate 					z->msg = "invalid bit length repeat";
45427c478bd9Sstevel@tonic-gate 					r = Z_DATA_ERROR;
45437c478bd9Sstevel@tonic-gate 					LEAVE
45447c478bd9Sstevel@tonic-gate 				}
45457c478bd9Sstevel@tonic-gate 				c = c == 16 ? s->sub.trees.blens[i - 1] : 0;
45467c478bd9Sstevel@tonic-gate 				do {
45477c478bd9Sstevel@tonic-gate 					s->sub.trees.blens[i++] = c;
45487c478bd9Sstevel@tonic-gate 				} while (--j);
45497c478bd9Sstevel@tonic-gate 				s->sub.trees.index = i;
45507c478bd9Sstevel@tonic-gate 			}
45517c478bd9Sstevel@tonic-gate 		}
45527c478bd9Sstevel@tonic-gate 		s->sub.trees.tb = Z_NULL;
45537c478bd9Sstevel@tonic-gate 		{
45547c478bd9Sstevel@tonic-gate 			uInt bl, bd;
45557c478bd9Sstevel@tonic-gate 			inflate_huft *tl, *td;
45567c478bd9Sstevel@tonic-gate 			inflate_codes_statef *c;
45577c478bd9Sstevel@tonic-gate 
45587c478bd9Sstevel@tonic-gate 				/* must be <= 9 for lookahead assumptions */
45597c478bd9Sstevel@tonic-gate 			bl = 9;
45607c478bd9Sstevel@tonic-gate 				/* must be <= 9 for lookahead assumptions */
45617c478bd9Sstevel@tonic-gate 			bd = 6;
45627c478bd9Sstevel@tonic-gate 			t = s->sub.trees.table;
45637c478bd9Sstevel@tonic-gate 			t = inflate_trees_dynamic(257 + (t & 0x1f),
45647c478bd9Sstevel@tonic-gate 			    1 + ((t >> 5) & 0x1f),
45657c478bd9Sstevel@tonic-gate 			    s->sub.trees.blens, &bl, &bd, &tl, &td,
45667c478bd9Sstevel@tonic-gate 			    s->hufts, z);
45677c478bd9Sstevel@tonic-gate 			ZFREE(z, s->sub.trees.blens);
45687c478bd9Sstevel@tonic-gate 			s->sub.trees.blens = Z_NULL;
45697c478bd9Sstevel@tonic-gate 			if (t != Z_OK)
45707c478bd9Sstevel@tonic-gate 			{
45717c478bd9Sstevel@tonic-gate 				if (t == (uInt)Z_DATA_ERROR)
45727c478bd9Sstevel@tonic-gate 					s->mode = BADB;
45737c478bd9Sstevel@tonic-gate 				r = t;
45747c478bd9Sstevel@tonic-gate 				LEAVE
45757c478bd9Sstevel@tonic-gate 			}
45767c478bd9Sstevel@tonic-gate 			Tracev((stderr, "inflate:       trees ok\n"));
45777c478bd9Sstevel@tonic-gate 			if ((c = inflate_codes_new(bl, bd, tl, td, z)) ==
45787c478bd9Sstevel@tonic-gate 			    Z_NULL)
45797c478bd9Sstevel@tonic-gate 			{
45807c478bd9Sstevel@tonic-gate 				r = Z_MEM_ERROR;
45817c478bd9Sstevel@tonic-gate 				LEAVE
45827c478bd9Sstevel@tonic-gate 			}
45837c478bd9Sstevel@tonic-gate 			s->sub.decode.codes = c;
45847c478bd9Sstevel@tonic-gate 		}
45857c478bd9Sstevel@tonic-gate 		s->mode = CODES;
45867c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
45877c478bd9Sstevel@tonic-gate 	case CODES:
45887c478bd9Sstevel@tonic-gate 		UPDATE;
45897c478bd9Sstevel@tonic-gate 		if ((r = inflate_codes(s, z, r)) != Z_STREAM_END)
45907c478bd9Sstevel@tonic-gate 			return (inflate_flush(s, z, r));
45917c478bd9Sstevel@tonic-gate 		r = Z_OK;
45927c478bd9Sstevel@tonic-gate 		(void) inflate_codes_free(s->sub.decode.codes, z);
45937c478bd9Sstevel@tonic-gate 		LOAD;
45947c478bd9Sstevel@tonic-gate 		Tracev((stderr, "inflate:       codes end, %lu total out\n",
45957c478bd9Sstevel@tonic-gate 		    z->total_out + (q >= s->read ? q - s->read :
45967c478bd9Sstevel@tonic-gate 			(s->end - s->read) + (q - s->window))));
45977c478bd9Sstevel@tonic-gate 		if (!s->last)
45987c478bd9Sstevel@tonic-gate 		{
45997c478bd9Sstevel@tonic-gate 			s->mode = TYPE;
46007c478bd9Sstevel@tonic-gate 			break;
46017c478bd9Sstevel@tonic-gate 		}
46027c478bd9Sstevel@tonic-gate 		s->mode = DRY;
46037c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
46047c478bd9Sstevel@tonic-gate 	case DRY:
46057c478bd9Sstevel@tonic-gate 		FLUSH;
46067c478bd9Sstevel@tonic-gate 		if (s->read != s->write)
46077c478bd9Sstevel@tonic-gate 			LEAVE
46087c478bd9Sstevel@tonic-gate 		s->mode = DONEB;
46097c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
46107c478bd9Sstevel@tonic-gate 	case DONEB:
46117c478bd9Sstevel@tonic-gate 		r = Z_STREAM_END;
46127c478bd9Sstevel@tonic-gate 		LEAVE
46137c478bd9Sstevel@tonic-gate 	case BADB:
46147c478bd9Sstevel@tonic-gate 		r = Z_DATA_ERROR;
46157c478bd9Sstevel@tonic-gate 		LEAVE
46167c478bd9Sstevel@tonic-gate 	default:
46177c478bd9Sstevel@tonic-gate 		r = Z_STREAM_ERROR;
46187c478bd9Sstevel@tonic-gate 		LEAVE
46197c478bd9Sstevel@tonic-gate 	}
46207c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
46217c478bd9Sstevel@tonic-gate 	/* otherwise lint complains */
46227c478bd9Sstevel@tonic-gate }
46237c478bd9Sstevel@tonic-gate 
46247c478bd9Sstevel@tonic-gate 
46257c478bd9Sstevel@tonic-gate int
inflate_blocks_free(s,z)46267c478bd9Sstevel@tonic-gate inflate_blocks_free(s, z)
46277c478bd9Sstevel@tonic-gate inflate_blocks_statef *s;
46287c478bd9Sstevel@tonic-gate z_streamp z;
46297c478bd9Sstevel@tonic-gate {
46307c478bd9Sstevel@tonic-gate 	inflate_blocks_reset(s, z, Z_NULL);
46317c478bd9Sstevel@tonic-gate 	ZFREE(z, s->window);
46327c478bd9Sstevel@tonic-gate 	s->window = Z_NULL;
46337c478bd9Sstevel@tonic-gate 	ZFREE(z, s->hufts);
46347c478bd9Sstevel@tonic-gate 	s->hufts = Z_NULL;
46357c478bd9Sstevel@tonic-gate 	ZFREE(z, s);
46367c478bd9Sstevel@tonic-gate 	Trace((stderr, "inflate:   blocks freed\n"));
46377c478bd9Sstevel@tonic-gate 	return (Z_OK);
46387c478bd9Sstevel@tonic-gate }
46397c478bd9Sstevel@tonic-gate 
46407c478bd9Sstevel@tonic-gate 
46417c478bd9Sstevel@tonic-gate void
inflate_set_dictionary(s,d,n)46427c478bd9Sstevel@tonic-gate inflate_set_dictionary(s, d, n)
46437c478bd9Sstevel@tonic-gate inflate_blocks_statef *s;
46447c478bd9Sstevel@tonic-gate const Bytef *d;
46457c478bd9Sstevel@tonic-gate uInt  n;
46467c478bd9Sstevel@tonic-gate {
46477c478bd9Sstevel@tonic-gate 	Assert(s->window + n <= s->end, "set dict");
46487c478bd9Sstevel@tonic-gate 	zmemcpy((charf *)s->window, d, n);
46497c478bd9Sstevel@tonic-gate 	s->read = s->write = s->window + n;
46507c478bd9Sstevel@tonic-gate }
46517c478bd9Sstevel@tonic-gate 
46527c478bd9Sstevel@tonic-gate /*
46537c478bd9Sstevel@tonic-gate  * Returns true if inflate is currently at the end of a block
46547c478bd9Sstevel@tonic-gate  * generated by Z_SYNC_FLUSH or Z_FULL_FLUSH.
46557c478bd9Sstevel@tonic-gate  * IN assertion: s != Z_NULL
46567c478bd9Sstevel@tonic-gate  */
46577c478bd9Sstevel@tonic-gate int
inflate_blocks_sync_point(s)46587c478bd9Sstevel@tonic-gate inflate_blocks_sync_point(s)
46597c478bd9Sstevel@tonic-gate inflate_blocks_statef *s;
46607c478bd9Sstevel@tonic-gate {
46617c478bd9Sstevel@tonic-gate 	return (s->mode == LENS);
46627c478bd9Sstevel@tonic-gate }
46637c478bd9Sstevel@tonic-gate 
46647c478bd9Sstevel@tonic-gate /*
46657c478bd9Sstevel@tonic-gate  * This subroutine adds the data at next_in/avail_in to the output history
46667c478bd9Sstevel@tonic-gate  * without performing any output.  The output buffer must be "caught up";
46677c478bd9Sstevel@tonic-gate  * i.e. no pending output (hence s->read equals s->write), and the state must
46687c478bd9Sstevel@tonic-gate  * be BLOCKS (i.e. we should be willing to see the start of a series of
46697c478bd9Sstevel@tonic-gate  * BLOCKS).  On exit, the output will also be caught up, and the checksum
46707c478bd9Sstevel@tonic-gate  * will have been updated if need be.
46717c478bd9Sstevel@tonic-gate  */
46727c478bd9Sstevel@tonic-gate int
inflate_addhistory(s,z)46737c478bd9Sstevel@tonic-gate inflate_addhistory(s, z)
46747c478bd9Sstevel@tonic-gate inflate_blocks_statef *s;
46757c478bd9Sstevel@tonic-gate z_stream *z;
46767c478bd9Sstevel@tonic-gate {
46777c478bd9Sstevel@tonic-gate 	uLong b;	/* bit buffer */  /* NOT USED HERE */
46787c478bd9Sstevel@tonic-gate 	uInt k;	/* bits in bit buffer */ /* NOT USED HERE */
46797c478bd9Sstevel@tonic-gate 	uInt t;	/* temporary storage */
46807c478bd9Sstevel@tonic-gate 	Bytef *p;	/* input data pointer */
46817c478bd9Sstevel@tonic-gate 	uInt n;	/* bytes available there */
46827c478bd9Sstevel@tonic-gate 	Bytef *q;	/* output window write pointer */
46837c478bd9Sstevel@tonic-gate 	uInt m;	/* bytes to end of window or read pointer */
46847c478bd9Sstevel@tonic-gate 
46857c478bd9Sstevel@tonic-gate 	if (s->read != s->write)
46867c478bd9Sstevel@tonic-gate 		return (Z_STREAM_ERROR);
46877c478bd9Sstevel@tonic-gate 	if (s->mode != TYPE)
46887c478bd9Sstevel@tonic-gate 		return (Z_DATA_ERROR);
46897c478bd9Sstevel@tonic-gate 
46907c478bd9Sstevel@tonic-gate 	/* we're ready to rock */
46917c478bd9Sstevel@tonic-gate 	LOAD;
46927c478bd9Sstevel@tonic-gate 	/*
46937c478bd9Sstevel@tonic-gate 	 * while there is input ready, copy to output buffer, moving
46947c478bd9Sstevel@tonic-gate 	 * pointers as needed.
46957c478bd9Sstevel@tonic-gate 	 */
46967c478bd9Sstevel@tonic-gate 	while (n) {
46977c478bd9Sstevel@tonic-gate 		t = n;	/* how many to do */
46987c478bd9Sstevel@tonic-gate 		/* is there room until end of buffer? */
46997c478bd9Sstevel@tonic-gate 		if (t > m) t = m;
47007c478bd9Sstevel@tonic-gate 		/* update check information */
47017c478bd9Sstevel@tonic-gate 		if (s->checkfn != Z_NULL)
47027c478bd9Sstevel@tonic-gate 			s->check = (*s->checkfn)(s->check, q, t);
47037c478bd9Sstevel@tonic-gate 		zmemcpy(q, p, t);
47047c478bd9Sstevel@tonic-gate 		q += t;
47057c478bd9Sstevel@tonic-gate 		p += t;
47067c478bd9Sstevel@tonic-gate 		n -= t;
47077c478bd9Sstevel@tonic-gate 		z->total_out += t;
47087c478bd9Sstevel@tonic-gate 		s->read = q;	/* drag read pointer forward */
47097c478bd9Sstevel@tonic-gate /* WWRAP */	/* expand WWRAP macro by hand to handle s->read */
47107c478bd9Sstevel@tonic-gate 		if (q == s->end) {
47117c478bd9Sstevel@tonic-gate 			s->read = q = s->window;
47127c478bd9Sstevel@tonic-gate 			m = WAVAIL;
47137c478bd9Sstevel@tonic-gate 		}
47147c478bd9Sstevel@tonic-gate 	}
47157c478bd9Sstevel@tonic-gate 	UPDATE;
47167c478bd9Sstevel@tonic-gate 	return (Z_OK);
47177c478bd9Sstevel@tonic-gate }
47187c478bd9Sstevel@tonic-gate 
47197c478bd9Sstevel@tonic-gate 
47207c478bd9Sstevel@tonic-gate /*
47217c478bd9Sstevel@tonic-gate  * At the end of a Deflate-compressed PPP packet, we expect to have seen
47227c478bd9Sstevel@tonic-gate  * a `stored' block type value but not the (zero) length bytes.
47237c478bd9Sstevel@tonic-gate  */
47247c478bd9Sstevel@tonic-gate int
inflate_packet_flush(s)47257c478bd9Sstevel@tonic-gate inflate_packet_flush(s)
47267c478bd9Sstevel@tonic-gate     inflate_blocks_statef *s;
47277c478bd9Sstevel@tonic-gate {
47287c478bd9Sstevel@tonic-gate 	if (s->mode != LENS)
47297c478bd9Sstevel@tonic-gate 		return (Z_DATA_ERROR);
47307c478bd9Sstevel@tonic-gate 	s->mode = TYPE;
47317c478bd9Sstevel@tonic-gate 	return (Z_OK);
47327c478bd9Sstevel@tonic-gate }
47337c478bd9Sstevel@tonic-gate /* --- infblock.c */
47347c478bd9Sstevel@tonic-gate 
47357c478bd9Sstevel@tonic-gate /* +++ inftrees.c */
47367c478bd9Sstevel@tonic-gate /*
47377c478bd9Sstevel@tonic-gate  * inftrees.c -- generate Huffman trees for efficient decoding
47387c478bd9Sstevel@tonic-gate  * Copyright (C) 1995-1998 Mark Adler
47397c478bd9Sstevel@tonic-gate  * For conditions of distribution and use, see copyright notice in zlib.h
47407c478bd9Sstevel@tonic-gate  */
47417c478bd9Sstevel@tonic-gate 
47427c478bd9Sstevel@tonic-gate /* #include "zutil.h" */
47437c478bd9Sstevel@tonic-gate /* #include "inftrees.h" */
47447c478bd9Sstevel@tonic-gate 
47457c478bd9Sstevel@tonic-gate const char inflate_copyright[] =
47467c478bd9Sstevel@tonic-gate " inflate 1.1.3 Copyright 1995-1998 Mark Adler ";
47477c478bd9Sstevel@tonic-gate /*
47487c478bd9Sstevel@tonic-gate  * If you use the zlib library in a product, an acknowledgment is
47497c478bd9Sstevel@tonic-gate  * welcome in the documentation of your product. If for some reason
47507c478bd9Sstevel@tonic-gate  * you cannot include such an acknowledgment, I would appreciate that
47517c478bd9Sstevel@tonic-gate  * you keep this copyright string in the executable of your product.
47527c478bd9Sstevel@tonic-gate  */
47537c478bd9Sstevel@tonic-gate 
47547c478bd9Sstevel@tonic-gate #ifndef NO_DUMMY_DECL
47557c478bd9Sstevel@tonic-gate struct internal_state  {int dummy; };	/* for buggy compilers */
47567c478bd9Sstevel@tonic-gate #endif
47577c478bd9Sstevel@tonic-gate 
47587c478bd9Sstevel@tonic-gate /* simplify the use of the inflate_huft type with some defines */
47597c478bd9Sstevel@tonic-gate #define	exop word.what.Exop
47607c478bd9Sstevel@tonic-gate #define	bits word.what.Bits
47617c478bd9Sstevel@tonic-gate 
47627c478bd9Sstevel@tonic-gate 
47637c478bd9Sstevel@tonic-gate local int huft_build OF((
47647c478bd9Sstevel@tonic-gate 	uIntf *,	/* code lengths in bits */
47657c478bd9Sstevel@tonic-gate 	uInt,		/* number of codes */
47667c478bd9Sstevel@tonic-gate 	uInt,		/* number of "simple" codes */
47677c478bd9Sstevel@tonic-gate 	const uIntf *,	/* list of base values for non-simple codes */
47687c478bd9Sstevel@tonic-gate 	const uIntf *,	/* list of extra bits for non-simple codes */
47697c478bd9Sstevel@tonic-gate 	inflate_huft * FAR*, /* result: starting table */
47707c478bd9Sstevel@tonic-gate 	uIntf *,	/* maximum lookup bits (returns actual) */
47717c478bd9Sstevel@tonic-gate 	inflate_huft *hp,	/* space for trees */
47727c478bd9Sstevel@tonic-gate 	uInt *hn,	/* hufts used in space */
47737c478bd9Sstevel@tonic-gate 	uIntf *v));	/* working area: values in order of bit length */
47747c478bd9Sstevel@tonic-gate 
47757c478bd9Sstevel@tonic-gate /* Tables for deflate from PKZIP's appnote.txt. */
47767c478bd9Sstevel@tonic-gate local const uInt cplens[31] = { /* Copy lengths for literal codes 257..285 */
47777c478bd9Sstevel@tonic-gate 	3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
47787c478bd9Sstevel@tonic-gate 	35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
47797c478bd9Sstevel@tonic-gate 	/* see note #13 above about 258 */
47807c478bd9Sstevel@tonic-gate local const uInt cplext[31] = { /* Extra bits for literal codes 257..285 */
47817c478bd9Sstevel@tonic-gate 	0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
47827c478bd9Sstevel@tonic-gate 	3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 112, 112};
47837c478bd9Sstevel@tonic-gate 	/* 112==invalid */
47847c478bd9Sstevel@tonic-gate local const uInt cpdist[30] = { /* Copy offsets for distance codes 0..29 */
47857c478bd9Sstevel@tonic-gate 	1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
47867c478bd9Sstevel@tonic-gate 	257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
47877c478bd9Sstevel@tonic-gate 	8193, 12289, 16385, 24577};
47887c478bd9Sstevel@tonic-gate local const uInt cpdext[30] = { /* Extra bits for distance codes */
47897c478bd9Sstevel@tonic-gate 	0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
47907c478bd9Sstevel@tonic-gate 	7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
47917c478bd9Sstevel@tonic-gate 	12, 12, 13, 13};
47927c478bd9Sstevel@tonic-gate 
47937c478bd9Sstevel@tonic-gate /*
47947c478bd9Sstevel@tonic-gate  * Huffman code decoding is performed using a multi-level table
47957c478bd9Sstevel@tonic-gate  * lookup.  The fastest way to decode is to simply build a lookup
47967c478bd9Sstevel@tonic-gate  * table whose size is determined by the longest code.  However, the
47977c478bd9Sstevel@tonic-gate  * time it takes to build this table can also be a factor if the data
47987c478bd9Sstevel@tonic-gate  * being decoded is not very long.  The most common codes are
47997c478bd9Sstevel@tonic-gate  * necessarily the shortest codes, so those codes dominate the
48007c478bd9Sstevel@tonic-gate  * decoding time, and hence the speed.  The idea is you can have a
48017c478bd9Sstevel@tonic-gate  * shorter table that decodes the shorter, more probable codes, and
48027c478bd9Sstevel@tonic-gate  * then point to subsidiary tables for the longer codes.  The time it
48037c478bd9Sstevel@tonic-gate  * costs to decode the longer codes is then traded against the time it
48047c478bd9Sstevel@tonic-gate  * takes to make longer tables.
48057c478bd9Sstevel@tonic-gate  *
48067c478bd9Sstevel@tonic-gate  * This results of this trade are in the variables lbits and dbits
48077c478bd9Sstevel@tonic-gate  * below.  lbits is the number of bits the first level table for
48087c478bd9Sstevel@tonic-gate  * literal/ length codes can decode in one step, and dbits is the same
48097c478bd9Sstevel@tonic-gate  * thing for the distance codes.  Subsequent tables are also less than
48107c478bd9Sstevel@tonic-gate  * or equal to those sizes.  These values may be adjusted either when
48117c478bd9Sstevel@tonic-gate  * all of the codes are shorter than that, in which case the longest
48127c478bd9Sstevel@tonic-gate  * code length in bits is used, or when the shortest code is *longer*
48137c478bd9Sstevel@tonic-gate  * than the requested table size, in which case the length of the
48147c478bd9Sstevel@tonic-gate  * shortest code in bits is used.
48157c478bd9Sstevel@tonic-gate  *
48167c478bd9Sstevel@tonic-gate  * There are two different values for the two tables, since they code
48177c478bd9Sstevel@tonic-gate  * a different number of possibilities each.  The literal/length table
48187c478bd9Sstevel@tonic-gate  * codes 286 possible values, or in a flat code, a little over eight
48197c478bd9Sstevel@tonic-gate  * bits.  The distance table codes 30 possible values, or a little
48207c478bd9Sstevel@tonic-gate  * less than five bits, flat.  The optimum values for speed end up
48217c478bd9Sstevel@tonic-gate  * being about one bit more than those, so lbits is 8+1 and dbits is
48227c478bd9Sstevel@tonic-gate  * 5+1.  The optimum values may differ though from machine to machine,
48237c478bd9Sstevel@tonic-gate  * and possibly even between compilers.  Your mileage may vary.
48247c478bd9Sstevel@tonic-gate  */
48257c478bd9Sstevel@tonic-gate 
48267c478bd9Sstevel@tonic-gate 
48277c478bd9Sstevel@tonic-gate /* If BMAX needs to be larger than 16, then h and x[] should be uLong. */
48287c478bd9Sstevel@tonic-gate #define	BMAX 15		/* maximum bit length of any code */
48297c478bd9Sstevel@tonic-gate 
48307c478bd9Sstevel@tonic-gate 
48317c478bd9Sstevel@tonic-gate local int
huft_build(b,n,s,d,e,t,m,hp,hn,v)48327c478bd9Sstevel@tonic-gate huft_build(b, n, s, d, e, t, m, hp, hn, v)
48337c478bd9Sstevel@tonic-gate uIntf *b;	/* code lengths in bits (all assumed <= BMAX) */
48347c478bd9Sstevel@tonic-gate uInt n;	/* number of codes (assumed <= 288) */
48357c478bd9Sstevel@tonic-gate uInt s;	/* number of simple-valued codes (0..s-1) */
48367c478bd9Sstevel@tonic-gate const uIntf *d;	/* list of base values for non-simple codes */
48377c478bd9Sstevel@tonic-gate const uIntf *e;	/* list of extra bits for non-simple codes */
48387c478bd9Sstevel@tonic-gate inflate_huft * FAR *t;	/* result: starting table */
48397c478bd9Sstevel@tonic-gate uIntf *m;	/* maximum lookup bits, returns actual */
48407c478bd9Sstevel@tonic-gate inflate_huft *hp;	/* space for trees */
48417c478bd9Sstevel@tonic-gate uInt *hn;		/* hufts used in space */
48427c478bd9Sstevel@tonic-gate uIntf *v;		/* working area: values in order of bit length */
48437c478bd9Sstevel@tonic-gate /*
48447c478bd9Sstevel@tonic-gate  * Given a list of code lengths and a maximum table size, make a set
48457c478bd9Sstevel@tonic-gate  * of tables to decode that set of codes.  Return Z_OK on success,
48467c478bd9Sstevel@tonic-gate  * Z_BUF_ERROR if the given code set is incomplete (the tables are
48477c478bd9Sstevel@tonic-gate  * still built in this case), Z_DATA_ERROR if the input is invalid (an
48487c478bd9Sstevel@tonic-gate  * over-subscribed set of lengths), or Z_MEM_ERROR if not enough
48497c478bd9Sstevel@tonic-gate  * memory.
48507c478bd9Sstevel@tonic-gate  */
48517c478bd9Sstevel@tonic-gate {
48527c478bd9Sstevel@tonic-gate 
48537c478bd9Sstevel@tonic-gate 	uInt a;	/* counter for codes of length k */
48547c478bd9Sstevel@tonic-gate 	uInt c[BMAX+1];	/* bit length count table */
48557c478bd9Sstevel@tonic-gate 	uInt f;	/* i repeats in table every f entries */
48567c478bd9Sstevel@tonic-gate 	int g;	/* maximum code length */
48577c478bd9Sstevel@tonic-gate 	int h;	/* table level */
48587c478bd9Sstevel@tonic-gate 	register uInt i;	/* counter, current code */
48597c478bd9Sstevel@tonic-gate 	register uInt j;	/* counter */
48607c478bd9Sstevel@tonic-gate 	register int k;	/* number of bits in current code */
48617c478bd9Sstevel@tonic-gate 	int l;	/* bits per table (returned in m) */
48627c478bd9Sstevel@tonic-gate 	register uIntf *p;	/* pointer into c[], b[], or v[] */
48637c478bd9Sstevel@tonic-gate 	inflate_huft *q;	/* points to current table */
48647c478bd9Sstevel@tonic-gate 	struct inflate_huft_s r; /* table entry for structure assignment */
48657c478bd9Sstevel@tonic-gate 	inflate_huft *u[BMAX];	/* table stack */
48667c478bd9Sstevel@tonic-gate 	uInt mask;	/* (1 << w) - 1, to avoid cc -O bug on HP */
48677c478bd9Sstevel@tonic-gate 	register int w;	/* bits before this table == (l * h) */
48687c478bd9Sstevel@tonic-gate 	uInt x[BMAX+1];	/* bit offsets, then code stack */
48697c478bd9Sstevel@tonic-gate 	uIntf *xp;	/* pointer into x */
48707c478bd9Sstevel@tonic-gate 	int y;	/* number of dummy codes added */
48717c478bd9Sstevel@tonic-gate 	uInt z;	/* number of entries in current table */
48727c478bd9Sstevel@tonic-gate 
48737c478bd9Sstevel@tonic-gate 	(void) inflate_copyright;
48747c478bd9Sstevel@tonic-gate 	/* Generate counts for each bit length */
48757c478bd9Sstevel@tonic-gate 	p = c;
48767c478bd9Sstevel@tonic-gate #define	C0 *p++ = 0;
48777c478bd9Sstevel@tonic-gate #define	C2 C0 C0 C0 C0
48787c478bd9Sstevel@tonic-gate #define	C4 C2 C2 C2 C2
48797c478bd9Sstevel@tonic-gate 	C4	/* clear c[]--assume BMAX+1 is 16 */
48807c478bd9Sstevel@tonic-gate 	    p = b;  i = n;
48817c478bd9Sstevel@tonic-gate 	do {
48827c478bd9Sstevel@tonic-gate 		c[*p++]++;	/* assume all entries <= BMAX */
48837c478bd9Sstevel@tonic-gate 	} while (--i);
48847c478bd9Sstevel@tonic-gate 	if (c[0] == n)		/* null input--all zero length codes */
48857c478bd9Sstevel@tonic-gate 	{
48867c478bd9Sstevel@tonic-gate 		*t = (inflate_huft *)Z_NULL;
48877c478bd9Sstevel@tonic-gate 		*m = 0;
48887c478bd9Sstevel@tonic-gate 		return (Z_OK);
48897c478bd9Sstevel@tonic-gate 	}
48907c478bd9Sstevel@tonic-gate 
48917c478bd9Sstevel@tonic-gate 
48927c478bd9Sstevel@tonic-gate 	/* Find minimum and maximum length, bound *m by those */
48937c478bd9Sstevel@tonic-gate 	l = *m;
48947c478bd9Sstevel@tonic-gate 	for (j = 1; j <= BMAX; j++)
48957c478bd9Sstevel@tonic-gate 		if (c[j])
48967c478bd9Sstevel@tonic-gate 			break;
48977c478bd9Sstevel@tonic-gate 	k = j;	/* minimum code length */
48987c478bd9Sstevel@tonic-gate 	if ((uInt)l < j)
48997c478bd9Sstevel@tonic-gate 		l = j;
49007c478bd9Sstevel@tonic-gate 	for (i = BMAX; i; i--)
49017c478bd9Sstevel@tonic-gate 		if (c[i])
49027c478bd9Sstevel@tonic-gate 			break;
49037c478bd9Sstevel@tonic-gate 	g = i;	/* maximum code length */
49047c478bd9Sstevel@tonic-gate 	if ((uInt)l > i)
49057c478bd9Sstevel@tonic-gate 		l = i;
49067c478bd9Sstevel@tonic-gate 	*m = l;
49077c478bd9Sstevel@tonic-gate 
49087c478bd9Sstevel@tonic-gate 
49097c478bd9Sstevel@tonic-gate 	/* Adjust last length count to fill out codes, if needed */
49107c478bd9Sstevel@tonic-gate 	for (y = 1 << j; j < i; j++, y <<= 1)
49117c478bd9Sstevel@tonic-gate 		if ((y -= c[j]) < 0)
49127c478bd9Sstevel@tonic-gate 			return (Z_DATA_ERROR);
49137c478bd9Sstevel@tonic-gate 	if ((y -= c[i]) < 0)
49147c478bd9Sstevel@tonic-gate 		return (Z_DATA_ERROR);
49157c478bd9Sstevel@tonic-gate 	c[i] += y;
49167c478bd9Sstevel@tonic-gate 
49177c478bd9Sstevel@tonic-gate 
49187c478bd9Sstevel@tonic-gate 	/* Generate starting offsets into the value table for each length */
49197c478bd9Sstevel@tonic-gate 	x[1] = j = 0;
49207c478bd9Sstevel@tonic-gate 	p = c + 1;  xp = x + 2;
49217c478bd9Sstevel@tonic-gate 	while (--i) {		/* note that i == g from above */
49227c478bd9Sstevel@tonic-gate 		*xp++ = (j += *p++);
49237c478bd9Sstevel@tonic-gate 	}
49247c478bd9Sstevel@tonic-gate 
49257c478bd9Sstevel@tonic-gate 
49267c478bd9Sstevel@tonic-gate 	/* Make a table of values in order of bit lengths */
49277c478bd9Sstevel@tonic-gate 	p = b;  i = 0;
49287c478bd9Sstevel@tonic-gate 	do {
49297c478bd9Sstevel@tonic-gate 		if ((j = *p++) != 0)
49307c478bd9Sstevel@tonic-gate 			v[x[j]++] = i;
49317c478bd9Sstevel@tonic-gate 	} while (++i < n);
49327c478bd9Sstevel@tonic-gate 	n = x[g];	/* set n to length of v */
49337c478bd9Sstevel@tonic-gate 
49347c478bd9Sstevel@tonic-gate 
49357c478bd9Sstevel@tonic-gate 	/* Generate the Huffman codes and for each, make the table entries */
49367c478bd9Sstevel@tonic-gate 	x[0] = i = 0;	/* first Huffman code is zero */
49377c478bd9Sstevel@tonic-gate 	p = v;	/* grab values in bit order */
49387c478bd9Sstevel@tonic-gate 	h = -1;	/* no tables yet--level -1 */
49397c478bd9Sstevel@tonic-gate 	w = -l;	/* bits decoded == (l * h) */
49407c478bd9Sstevel@tonic-gate 	u[0] = (inflate_huft *)Z_NULL;	/* just to keep compilers happy */
49417c478bd9Sstevel@tonic-gate 	q = (inflate_huft *)Z_NULL;	/* ditto */
49427c478bd9Sstevel@tonic-gate 	z = 0;	/* ditto */
49437c478bd9Sstevel@tonic-gate 
49447c478bd9Sstevel@tonic-gate 	/* go through the bit lengths (k already is bits in shortest code) */
49457c478bd9Sstevel@tonic-gate 	for (; k <= g; k++) {
49467c478bd9Sstevel@tonic-gate 		a = c[k];
49477c478bd9Sstevel@tonic-gate 		while (a--) {
49487c478bd9Sstevel@tonic-gate 			/*
49497c478bd9Sstevel@tonic-gate 			 * here i is the Huffman code of length k bits
49507c478bd9Sstevel@tonic-gate 			 * for value *p.  make tables up to required
49517c478bd9Sstevel@tonic-gate 			 * level.
49527c478bd9Sstevel@tonic-gate 			 */
49537c478bd9Sstevel@tonic-gate 			while (k > w + l) {
49547c478bd9Sstevel@tonic-gate 				h++;
49557c478bd9Sstevel@tonic-gate 				w += l;	/* previous table always l bits */
49567c478bd9Sstevel@tonic-gate 
49577c478bd9Sstevel@tonic-gate 				/*
49587c478bd9Sstevel@tonic-gate 				 * compute minimum size table less
49597c478bd9Sstevel@tonic-gate 				 * than or equal to l bits
49607c478bd9Sstevel@tonic-gate 				 */
49617c478bd9Sstevel@tonic-gate 				z = g - w;
49627c478bd9Sstevel@tonic-gate 				/* table size upper limit */
49637c478bd9Sstevel@tonic-gate 				z = z > (uInt)l ? l : z;
49647c478bd9Sstevel@tonic-gate 				/* try a k-w bit table */
49657c478bd9Sstevel@tonic-gate 				if ((f = 1 << (j = k - w)) > a + 1) {
49667c478bd9Sstevel@tonic-gate 					/* too few codes for k-w bit table */
49677c478bd9Sstevel@tonic-gate 					/* deduct codes from patterns left */
49687c478bd9Sstevel@tonic-gate 					f -= a + 1;
49697c478bd9Sstevel@tonic-gate 					xp = c + k;
49707c478bd9Sstevel@tonic-gate 					if (j < z)
49717c478bd9Sstevel@tonic-gate 						/*
49727c478bd9Sstevel@tonic-gate 						 * try smaller tables
49737c478bd9Sstevel@tonic-gate 						 * up to z bits
49747c478bd9Sstevel@tonic-gate 						 */
49757c478bd9Sstevel@tonic-gate 						while (++j < z) {
49767c478bd9Sstevel@tonic-gate 							/*
49777c478bd9Sstevel@tonic-gate 							 * enough
49787c478bd9Sstevel@tonic-gate 							 * codes to
49797c478bd9Sstevel@tonic-gate 							 * use up j
49807c478bd9Sstevel@tonic-gate 							 * bits
49817c478bd9Sstevel@tonic-gate 							 */
49827c478bd9Sstevel@tonic-gate 							if ((f <<= 1) <= *++xp)
49837c478bd9Sstevel@tonic-gate 								break;
49847c478bd9Sstevel@tonic-gate 							f -= *xp;
49857c478bd9Sstevel@tonic-gate 							/*
49867c478bd9Sstevel@tonic-gate 							 * else deduct
49877c478bd9Sstevel@tonic-gate 							 * codes from
49887c478bd9Sstevel@tonic-gate 							 * patterns
49897c478bd9Sstevel@tonic-gate 							 */
49907c478bd9Sstevel@tonic-gate 						}
49917c478bd9Sstevel@tonic-gate 				}
49927c478bd9Sstevel@tonic-gate 				/* table entries for j-bit table */
49937c478bd9Sstevel@tonic-gate 				z = 1 << j;
49947c478bd9Sstevel@tonic-gate 
49957c478bd9Sstevel@tonic-gate 				/* allocate new table */
49967c478bd9Sstevel@tonic-gate 				/* (note: doesn't matter for fixed) */
49977c478bd9Sstevel@tonic-gate 				/* not enough memory */
49987c478bd9Sstevel@tonic-gate 				if (*hn + z > MANY)
49997c478bd9Sstevel@tonic-gate 					return (Z_MEM_ERROR);
50007c478bd9Sstevel@tonic-gate 				u[h] = q = hp + *hn;
50017c478bd9Sstevel@tonic-gate 				*hn += z;
50027c478bd9Sstevel@tonic-gate 
50037c478bd9Sstevel@tonic-gate 				/* connect to last table, if there is one */
50047c478bd9Sstevel@tonic-gate 				if (h) {
50057c478bd9Sstevel@tonic-gate 					/* save pattern for backing up */
50067c478bd9Sstevel@tonic-gate 					x[h] = i;
50077c478bd9Sstevel@tonic-gate 					/* bits to dump before this table */
50087c478bd9Sstevel@tonic-gate 					r.bits = (Byte)l;
50097c478bd9Sstevel@tonic-gate 					/* bits in this table */
50107c478bd9Sstevel@tonic-gate 					r.exop = (Byte)j;
50117c478bd9Sstevel@tonic-gate 					j = i >> (w - l);
50127c478bd9Sstevel@tonic-gate 					/* offset to this table */
50137c478bd9Sstevel@tonic-gate 					r.base = (uInt)(q - u[h-1] - j);
50147c478bd9Sstevel@tonic-gate 					/* connect to last table */
50157c478bd9Sstevel@tonic-gate 					u[h-1][j] = r;
50167c478bd9Sstevel@tonic-gate 				} else
50177c478bd9Sstevel@tonic-gate 					/* first table is returned result */
50187c478bd9Sstevel@tonic-gate 					*t = q;
50197c478bd9Sstevel@tonic-gate 			}
50207c478bd9Sstevel@tonic-gate 
50217c478bd9Sstevel@tonic-gate 			/* set up table entry in r */
50227c478bd9Sstevel@tonic-gate 			r.bits = (Byte)(k - w);
50237c478bd9Sstevel@tonic-gate 			if (p >= v + n)
50247c478bd9Sstevel@tonic-gate 				/* out of values--invalid code */
50257c478bd9Sstevel@tonic-gate 				r.exop = 128 + 64;
50267c478bd9Sstevel@tonic-gate 			else if (*p < s)
50277c478bd9Sstevel@tonic-gate 			{
50287c478bd9Sstevel@tonic-gate 				/* 256 is end-of-block */
50297c478bd9Sstevel@tonic-gate 				r.exop = (Byte)(*p < 256 ? 0 : 32 + 64);
50307c478bd9Sstevel@tonic-gate 				/* simple code is just the value */
50317c478bd9Sstevel@tonic-gate 				r.base = *p++;
50327c478bd9Sstevel@tonic-gate 			}
50337c478bd9Sstevel@tonic-gate 			else
50347c478bd9Sstevel@tonic-gate 			{
50357c478bd9Sstevel@tonic-gate 				/* non-simple--look up in lists */
50367c478bd9Sstevel@tonic-gate 				r.exop = (Byte)(e[*p - s] + 16 + 64);
50377c478bd9Sstevel@tonic-gate 				r.base = d[*p++ - s];
50387c478bd9Sstevel@tonic-gate 			}
50397c478bd9Sstevel@tonic-gate 
50407c478bd9Sstevel@tonic-gate 			/* fill code-like entries with r */
50417c478bd9Sstevel@tonic-gate 			f = 1 << (k - w);
50427c478bd9Sstevel@tonic-gate 			for (j = i >> w; j < z; j += f)
50437c478bd9Sstevel@tonic-gate 				q[j] = r;
50447c478bd9Sstevel@tonic-gate 
50457c478bd9Sstevel@tonic-gate 			/* backwards increment the k-bit code i */
50467c478bd9Sstevel@tonic-gate 			for (j = 1 << (k - 1); i & j; j >>= 1)
50477c478bd9Sstevel@tonic-gate 				i ^= j;
50487c478bd9Sstevel@tonic-gate 			i ^= j;
50497c478bd9Sstevel@tonic-gate 
50507c478bd9Sstevel@tonic-gate 			/* backup over finished tables */
50517c478bd9Sstevel@tonic-gate 			mask = (1 << w) - 1;	/* needed on HP, cc -O bug */
50527c478bd9Sstevel@tonic-gate 			while ((i & mask) != x[h])
50537c478bd9Sstevel@tonic-gate 			{
50547c478bd9Sstevel@tonic-gate 				h--;	/* don't need to update q */
50557c478bd9Sstevel@tonic-gate 				w -= l;
50567c478bd9Sstevel@tonic-gate 				mask = (1 << w) - 1;
50577c478bd9Sstevel@tonic-gate 			}
50587c478bd9Sstevel@tonic-gate 		}
50597c478bd9Sstevel@tonic-gate 	}
50607c478bd9Sstevel@tonic-gate 
50617c478bd9Sstevel@tonic-gate 
50627c478bd9Sstevel@tonic-gate 	/* Return Z_BUF_ERROR if we were given an incomplete table */
50637c478bd9Sstevel@tonic-gate 	return (y != 0 && g != 1 ? Z_BUF_ERROR : Z_OK);
50647c478bd9Sstevel@tonic-gate }
50657c478bd9Sstevel@tonic-gate 
50667c478bd9Sstevel@tonic-gate 
50677c478bd9Sstevel@tonic-gate int
inflate_trees_bits(c,bb,tb,hp,z)50687c478bd9Sstevel@tonic-gate inflate_trees_bits(c, bb, tb, hp, z)
50697c478bd9Sstevel@tonic-gate uIntf *c;	/* 19 code lengths */
50707c478bd9Sstevel@tonic-gate uIntf *bb;	/* bits tree desired/actual depth */
50717c478bd9Sstevel@tonic-gate inflate_huft * FAR *tb;	/* bits tree result */
50727c478bd9Sstevel@tonic-gate inflate_huft *hp;	/* space for trees */
50737c478bd9Sstevel@tonic-gate z_streamp z;	/* for zfree function */
50747c478bd9Sstevel@tonic-gate {
50757c478bd9Sstevel@tonic-gate 	int r;
50767c478bd9Sstevel@tonic-gate 	uInt hn = 0;		/* hufts used in space */
50777c478bd9Sstevel@tonic-gate 	uIntf v[19];		/* work area for huft_build */
50787c478bd9Sstevel@tonic-gate 
50797c478bd9Sstevel@tonic-gate 	r = huft_build(c, 19, 19, (uIntf*)Z_NULL, (uIntf*)Z_NULL, tb, bb,
50807c478bd9Sstevel@tonic-gate 	    hp, &hn, v);
50817c478bd9Sstevel@tonic-gate 	if (r == Z_DATA_ERROR)
50827c478bd9Sstevel@tonic-gate 		z->msg = "oversubscribed dynamic bit lengths tree";
50837c478bd9Sstevel@tonic-gate 	else if (r == Z_BUF_ERROR || *bb == 0)
50847c478bd9Sstevel@tonic-gate 	{
50857c478bd9Sstevel@tonic-gate 		z->msg = "incomplete dynamic bit lengths tree";
50867c478bd9Sstevel@tonic-gate 		r = Z_DATA_ERROR;
50877c478bd9Sstevel@tonic-gate 	}
50887c478bd9Sstevel@tonic-gate 	return (r);
50897c478bd9Sstevel@tonic-gate }
50907c478bd9Sstevel@tonic-gate 
50917c478bd9Sstevel@tonic-gate 
50927c478bd9Sstevel@tonic-gate int
inflate_trees_dynamic(nl,nd,c,bl,bd,tl,td,hp,z)50937c478bd9Sstevel@tonic-gate inflate_trees_dynamic(nl, nd, c, bl, bd, tl, td, hp, z)
50947c478bd9Sstevel@tonic-gate uInt nl;	/* number of literal/length codes */
50957c478bd9Sstevel@tonic-gate uInt nd;	/* number of distance codes */
50967c478bd9Sstevel@tonic-gate uIntf *c;	/* that many (total) code lengths */
50977c478bd9Sstevel@tonic-gate uIntf *bl;	/* literal desired/actual bit depth */
50987c478bd9Sstevel@tonic-gate uIntf *bd;	/* distance desired/actual bit depth */
50997c478bd9Sstevel@tonic-gate inflate_huft * FAR *tl;	/* literal/length tree result */
51007c478bd9Sstevel@tonic-gate inflate_huft * FAR *td;	/* distance tree result */
51017c478bd9Sstevel@tonic-gate inflate_huft *hp;	/* space for trees */
51027c478bd9Sstevel@tonic-gate z_streamp z;	/* for zfree function */
51037c478bd9Sstevel@tonic-gate {
51047c478bd9Sstevel@tonic-gate 	int r;
51057c478bd9Sstevel@tonic-gate 	uInt hn = 0;		/* hufts used in space */
51067c478bd9Sstevel@tonic-gate 	uIntf v[288];		/* work area for huft_build */
51077c478bd9Sstevel@tonic-gate 
51087c478bd9Sstevel@tonic-gate 	/* build literal/length tree */
51097c478bd9Sstevel@tonic-gate 	r = huft_build(c, nl, 257, cplens, cplext, tl, bl, hp, &hn, v);
51107c478bd9Sstevel@tonic-gate 	if (r != Z_OK || *bl == 0)
51117c478bd9Sstevel@tonic-gate 	{
51127c478bd9Sstevel@tonic-gate 		if (r == Z_DATA_ERROR)
51137c478bd9Sstevel@tonic-gate 			z->msg = "oversubscribed literal/length tree";
51147c478bd9Sstevel@tonic-gate 		else if (r != Z_MEM_ERROR)
51157c478bd9Sstevel@tonic-gate 		{
51167c478bd9Sstevel@tonic-gate 			z->msg = "incomplete literal/length tree";
51177c478bd9Sstevel@tonic-gate 			r = Z_DATA_ERROR;
51187c478bd9Sstevel@tonic-gate 		}
51197c478bd9Sstevel@tonic-gate 		return (r);
51207c478bd9Sstevel@tonic-gate 	}
51217c478bd9Sstevel@tonic-gate 
51227c478bd9Sstevel@tonic-gate 	/* build distance tree */
51237c478bd9Sstevel@tonic-gate 	r = huft_build(c + nl, nd, 0, cpdist, cpdext, td, bd, hp, &hn, v);
51247c478bd9Sstevel@tonic-gate 	if (r != Z_OK || (*bd == 0 && nl > 257))
51257c478bd9Sstevel@tonic-gate 	{
51267c478bd9Sstevel@tonic-gate 		if (r == Z_DATA_ERROR)
51277c478bd9Sstevel@tonic-gate 			z->msg = "oversubscribed distance tree";
51287c478bd9Sstevel@tonic-gate 		else if (r == Z_BUF_ERROR) {
51297c478bd9Sstevel@tonic-gate #ifdef PKZIP_BUG_WORKAROUND
51307c478bd9Sstevel@tonic-gate 			r = Z_OK;
51317c478bd9Sstevel@tonic-gate #else
51327c478bd9Sstevel@tonic-gate 			z->msg = "incomplete distance tree";
51337c478bd9Sstevel@tonic-gate 			r = Z_DATA_ERROR;
51347c478bd9Sstevel@tonic-gate 		} else if (r != Z_MEM_ERROR) {
51357c478bd9Sstevel@tonic-gate 			z->msg = "empty distance tree with lengths";
51367c478bd9Sstevel@tonic-gate 			r = Z_DATA_ERROR;
51377c478bd9Sstevel@tonic-gate #endif
51387c478bd9Sstevel@tonic-gate 		}
51397c478bd9Sstevel@tonic-gate 		return (r);
51407c478bd9Sstevel@tonic-gate 	}
51417c478bd9Sstevel@tonic-gate 
51427c478bd9Sstevel@tonic-gate /* done */
51437c478bd9Sstevel@tonic-gate 	return (Z_OK);
51447c478bd9Sstevel@tonic-gate }
51457c478bd9Sstevel@tonic-gate 
51467c478bd9Sstevel@tonic-gate 
51477c478bd9Sstevel@tonic-gate /* build fixed tables only once--keep them here */
51487c478bd9Sstevel@tonic-gate /* #define	BUILDFIXED */
51497c478bd9Sstevel@tonic-gate #ifdef BUILDFIXED
51507c478bd9Sstevel@tonic-gate local int fixed_built = 0;
51517c478bd9Sstevel@tonic-gate #define	FIXEDH 544	/* number of hufts used by fixed tables */
51527c478bd9Sstevel@tonic-gate local inflate_huft fixed_mem[FIXEDH];
51537c478bd9Sstevel@tonic-gate local uInt fixed_bl;
51547c478bd9Sstevel@tonic-gate local uInt fixed_bd;
51557c478bd9Sstevel@tonic-gate local inflate_huft *fixed_tl;
51567c478bd9Sstevel@tonic-gate local inflate_huft *fixed_td;
51577c478bd9Sstevel@tonic-gate #else
51587c478bd9Sstevel@tonic-gate #include "inffixed.h"
51597c478bd9Sstevel@tonic-gate #endif
51607c478bd9Sstevel@tonic-gate 
51617c478bd9Sstevel@tonic-gate /*ARGSUSED*/
51627c478bd9Sstevel@tonic-gate int
inflate_trees_fixed(bl,bd,tl,td,z)51637c478bd9Sstevel@tonic-gate inflate_trees_fixed(bl, bd, tl, td, z)
51647c478bd9Sstevel@tonic-gate uIntf *bl;	/* literal desired/actual bit depth */
51657c478bd9Sstevel@tonic-gate uIntf *bd;	/* distance desired/actual bit depth */
51667c478bd9Sstevel@tonic-gate const inflate_huft * FAR *tl;	/* literal/length tree result */
51677c478bd9Sstevel@tonic-gate const inflate_huft * FAR *td;	/* distance tree result */
51687c478bd9Sstevel@tonic-gate z_streamp z;	/* for memory allocation */
51697c478bd9Sstevel@tonic-gate {
51707c478bd9Sstevel@tonic-gate #ifdef BUILDFIXED
51717c478bd9Sstevel@tonic-gate 	/*
51727c478bd9Sstevel@tonic-gate 	 * build fixed tables if not already (multiple overlapped
51737c478bd9Sstevel@tonic-gate 	 * executions ok)
51747c478bd9Sstevel@tonic-gate 	 */
51757c478bd9Sstevel@tonic-gate 	if (!fixed_built)
51767c478bd9Sstevel@tonic-gate 	{
51777c478bd9Sstevel@tonic-gate 		int k;	/* temporary variable */
51787c478bd9Sstevel@tonic-gate 		uInt f = 0;	/* number of hufts used in fixed_mem */
51797c478bd9Sstevel@tonic-gate 		uIntf *c;	/* length list for huft_build */
51807c478bd9Sstevel@tonic-gate 		uIntf *v;
51817c478bd9Sstevel@tonic-gate 
51827c478bd9Sstevel@tonic-gate 		/* allocate memory */
51837c478bd9Sstevel@tonic-gate 		if ((c = (uIntf*)ZALLOC(z, 288, sizeof (uInt))) == Z_NULL)
51847c478bd9Sstevel@tonic-gate 			return (Z_MEM_ERROR);
51857c478bd9Sstevel@tonic-gate 		if ((v = (uIntf*)ZALLOC(z, 288, sizeof (uInt))) == Z_NULL)
51867c478bd9Sstevel@tonic-gate 		{
51877c478bd9Sstevel@tonic-gate 			ZFREE(z, c);
51887c478bd9Sstevel@tonic-gate 			return (Z_MEM_ERROR);
51897c478bd9Sstevel@tonic-gate 		}
51907c478bd9Sstevel@tonic-gate 		/* literal table */
51917c478bd9Sstevel@tonic-gate 		for (k = 0; k < 144; k++)
51927c478bd9Sstevel@tonic-gate 			c[k] = 8;
51937c478bd9Sstevel@tonic-gate 		for (; k < 256; k++)
51947c478bd9Sstevel@tonic-gate 			c[k] = 9;
51957c478bd9Sstevel@tonic-gate 		for (; k < 280; k++)
51967c478bd9Sstevel@tonic-gate 			c[k] = 7;
51977c478bd9Sstevel@tonic-gate 		for (; k < 288; k++)
51987c478bd9Sstevel@tonic-gate 			c[k] = 8;
51997c478bd9Sstevel@tonic-gate 		fixed_bl = 9;
52007c478bd9Sstevel@tonic-gate 		(void) huft_build(c, 288, 257, cplens, cplext, &fixed_tl,
52017c478bd9Sstevel@tonic-gate 		    &fixed_bl, fixed_mem, &f, v);
52027c478bd9Sstevel@tonic-gate 
52037c478bd9Sstevel@tonic-gate 		/* distance table */
52047c478bd9Sstevel@tonic-gate 		for (k = 0; k < 30; k++)
52057c478bd9Sstevel@tonic-gate 			c[k] = 5;
52067c478bd9Sstevel@tonic-gate 		fixed_bd = 5;
52077c478bd9Sstevel@tonic-gate 		(void) huft_build(c, 30, 0, cpdist, cpdext, &fixed_td,
52087c478bd9Sstevel@tonic-gate 		    &fixed_bd, fixed_mem, &f, v);
52097c478bd9Sstevel@tonic-gate 
52107c478bd9Sstevel@tonic-gate 		/* done */
52117c478bd9Sstevel@tonic-gate 		ZFREE(z, v);
52127c478bd9Sstevel@tonic-gate 		ZFREE(z, c);
52137c478bd9Sstevel@tonic-gate 		fixed_built = 1;
52147c478bd9Sstevel@tonic-gate 	}
52157c478bd9Sstevel@tonic-gate #endif
52167c478bd9Sstevel@tonic-gate 	*bl = fixed_bl;
52177c478bd9Sstevel@tonic-gate 	*bd = fixed_bd;
52187c478bd9Sstevel@tonic-gate 	*tl = fixed_tl;
52197c478bd9Sstevel@tonic-gate 	*td = fixed_td;
52207c478bd9Sstevel@tonic-gate 	return (Z_OK);
52217c478bd9Sstevel@tonic-gate }
52227c478bd9Sstevel@tonic-gate /* --- inftrees.c */
52237c478bd9Sstevel@tonic-gate 
52247c478bd9Sstevel@tonic-gate /* +++ infcodes.c */
52257c478bd9Sstevel@tonic-gate /*
52267c478bd9Sstevel@tonic-gate  * infcodes.c -- process literals and length/distance pairs
52277c478bd9Sstevel@tonic-gate  * Copyright (C) 1995-1998 Mark Adler
52287c478bd9Sstevel@tonic-gate  * For conditions of distribution and use, see copyright notice in zlib.h
52297c478bd9Sstevel@tonic-gate  */
52307c478bd9Sstevel@tonic-gate 
52317c478bd9Sstevel@tonic-gate /* #include "zutil.h" */
52327c478bd9Sstevel@tonic-gate /* #include "inftrees.h" */
52337c478bd9Sstevel@tonic-gate /* #include "infblock.h" */
52347c478bd9Sstevel@tonic-gate /* #include "infcodes.h" */
52357c478bd9Sstevel@tonic-gate /* #include "infutil.h" */
52367c478bd9Sstevel@tonic-gate 
52377c478bd9Sstevel@tonic-gate /* +++ inffast.h */
52387c478bd9Sstevel@tonic-gate /*
52397c478bd9Sstevel@tonic-gate  * inffast.h -- header to use inffast.c
52407c478bd9Sstevel@tonic-gate  * Copyright (C) 1995-1998 Mark Adler
52417c478bd9Sstevel@tonic-gate  * For conditions of distribution and use, see copyright notice in zlib.h
52427c478bd9Sstevel@tonic-gate  */
52437c478bd9Sstevel@tonic-gate 
52447c478bd9Sstevel@tonic-gate /*
52457c478bd9Sstevel@tonic-gate  * WARNING: this file should *not* be used by applications. It is part
52467c478bd9Sstevel@tonic-gate  * of the implementation of the compression library and is subject to
52477c478bd9Sstevel@tonic-gate  * change. Applications should only use zlib.h.
52487c478bd9Sstevel@tonic-gate  */
52497c478bd9Sstevel@tonic-gate 
52507c478bd9Sstevel@tonic-gate extern int inflate_fast OF((
52517c478bd9Sstevel@tonic-gate     uInt,
52527c478bd9Sstevel@tonic-gate     uInt,
52537c478bd9Sstevel@tonic-gate     const inflate_huft *,
52547c478bd9Sstevel@tonic-gate     const inflate_huft *,
52557c478bd9Sstevel@tonic-gate     inflate_blocks_statef *,
52567c478bd9Sstevel@tonic-gate     z_streamp));
52577c478bd9Sstevel@tonic-gate /* --- inffast.h */
52587c478bd9Sstevel@tonic-gate 
52597c478bd9Sstevel@tonic-gate /* simplify the use of the inflate_huft type with some defines */
52607c478bd9Sstevel@tonic-gate #define	exop word.what.Exop
52617c478bd9Sstevel@tonic-gate #define	bits word.what.Bits
52627c478bd9Sstevel@tonic-gate 
52637c478bd9Sstevel@tonic-gate /* inflate codes private state */
52647c478bd9Sstevel@tonic-gate struct inflate_codes_state {
52657c478bd9Sstevel@tonic-gate 
52667c478bd9Sstevel@tonic-gate 	/* mode */
52677c478bd9Sstevel@tonic-gate 	enum {	/* waiting for "i:"=input, "o:"=output, "x:"=nothing */
52687c478bd9Sstevel@tonic-gate 		START,	/* x: set up for LEN */
52697c478bd9Sstevel@tonic-gate 		LEN,	/* i: get length/literal/eob next */
52707c478bd9Sstevel@tonic-gate 		LENEXT,	/* i: getting length extra (have base) */
52717c478bd9Sstevel@tonic-gate 		DIST,	/* i: get distance next */
52727c478bd9Sstevel@tonic-gate 		DISTEXT,	/* i: getting distance extra */
52737c478bd9Sstevel@tonic-gate 		COPY,	/* o: copying bytes in window, waiting for space */
52747c478bd9Sstevel@tonic-gate 		LIT,	/* o: got literal, waiting for output space */
52757c478bd9Sstevel@tonic-gate 		WASH,	/* o: got eob, possibly still output waiting */
52767c478bd9Sstevel@tonic-gate 		END,	/* x: got eob and all data flushed */
52777c478bd9Sstevel@tonic-gate 		BADCODE}	/* x: got error */
52787c478bd9Sstevel@tonic-gate 	mode;	/* current inflate_codes mode */
52797c478bd9Sstevel@tonic-gate 
52807c478bd9Sstevel@tonic-gate 	/* mode dependent information */
52817c478bd9Sstevel@tonic-gate 	uInt len;
52827c478bd9Sstevel@tonic-gate 	union {
52837c478bd9Sstevel@tonic-gate 		struct {
52847c478bd9Sstevel@tonic-gate 			const inflate_huft *tree;	/* pointer into tree */
52857c478bd9Sstevel@tonic-gate 			uInt need;	/* bits needed */
52867c478bd9Sstevel@tonic-gate 		} code;	/* if LEN or DIST, where in tree */
52877c478bd9Sstevel@tonic-gate 		uInt lit;	/* if LIT, literal */
52887c478bd9Sstevel@tonic-gate 		struct {
52897c478bd9Sstevel@tonic-gate 			uInt get;	/* bits to get for extra */
52907c478bd9Sstevel@tonic-gate 			uInt dist;	/* distance back to copy from */
52917c478bd9Sstevel@tonic-gate 		} copy;	/* if EXT or COPY, where and how much */
52927c478bd9Sstevel@tonic-gate 	} sub;	/* submode */
52937c478bd9Sstevel@tonic-gate 
52947c478bd9Sstevel@tonic-gate 	/* mode independent information */
52957c478bd9Sstevel@tonic-gate 	Byte lbits;	/* ltree bits decoded per branch */
52967c478bd9Sstevel@tonic-gate 	Byte dbits;	/* dtree bits decoder per branch */
52977c478bd9Sstevel@tonic-gate 	const inflate_huft *ltree;	/* literal/length/eob tree */
52987c478bd9Sstevel@tonic-gate 	const inflate_huft *dtree;	/* distance tree */
52997c478bd9Sstevel@tonic-gate 
53007c478bd9Sstevel@tonic-gate };
53017c478bd9Sstevel@tonic-gate 
53027c478bd9Sstevel@tonic-gate 
53037c478bd9Sstevel@tonic-gate inflate_codes_statef *
inflate_codes_new(bl,bd,tl,td,z)53047c478bd9Sstevel@tonic-gate inflate_codes_new(bl, bd, tl, td, z)
53057c478bd9Sstevel@tonic-gate uInt bl, bd;
53067c478bd9Sstevel@tonic-gate const inflate_huft *tl;
53077c478bd9Sstevel@tonic-gate const inflate_huft *td;	/* need separate declaration for Borland C++ */
53087c478bd9Sstevel@tonic-gate z_streamp z;
53097c478bd9Sstevel@tonic-gate {
53107c478bd9Sstevel@tonic-gate 	inflate_codes_statef *c;
53117c478bd9Sstevel@tonic-gate 
53127c478bd9Sstevel@tonic-gate 	if ((c = (inflate_codes_statef *)
53137c478bd9Sstevel@tonic-gate 	    ZALLOC(z, 1, sizeof (struct inflate_codes_state))) != Z_NULL)
53147c478bd9Sstevel@tonic-gate 	{
53157c478bd9Sstevel@tonic-gate 		c->mode = START;
53167c478bd9Sstevel@tonic-gate 		c->lbits = (Byte)bl;
53177c478bd9Sstevel@tonic-gate 		c->dbits = (Byte)bd;
53187c478bd9Sstevel@tonic-gate 		c->ltree = tl;
53197c478bd9Sstevel@tonic-gate 		c->dtree = td;
53207c478bd9Sstevel@tonic-gate 		Tracev((stderr, "inflate:       codes new\n"));
53217c478bd9Sstevel@tonic-gate 	}
53227c478bd9Sstevel@tonic-gate 	return (c);
53237c478bd9Sstevel@tonic-gate }
53247c478bd9Sstevel@tonic-gate 
53257c478bd9Sstevel@tonic-gate 
53267c478bd9Sstevel@tonic-gate int
inflate_codes(s,z,r)53277c478bd9Sstevel@tonic-gate inflate_codes(s, z, r)
53287c478bd9Sstevel@tonic-gate inflate_blocks_statef *s;
53297c478bd9Sstevel@tonic-gate z_streamp z;
53307c478bd9Sstevel@tonic-gate int r;
53317c478bd9Sstevel@tonic-gate {
53327c478bd9Sstevel@tonic-gate 	uInt j;	/* temporary storage */
53337c478bd9Sstevel@tonic-gate 	const inflate_huft *t;	/* temporary pointer */
53347c478bd9Sstevel@tonic-gate 	uInt e;	/* extra bits or operation */
53357c478bd9Sstevel@tonic-gate 	uLong b;	/* bit buffer */
53367c478bd9Sstevel@tonic-gate 	uInt k;	/* bits in bit buffer */
53377c478bd9Sstevel@tonic-gate 	Bytef *p;	/* input data pointer */
53387c478bd9Sstevel@tonic-gate 	uInt n;	/* bytes available there */
53397c478bd9Sstevel@tonic-gate 	Bytef *q;	/* output window write pointer */
53407c478bd9Sstevel@tonic-gate 	uInt m;	/* bytes to end of window or read pointer */
53417c478bd9Sstevel@tonic-gate 	Bytef *f;	/* pointer to copy strings from */
53427c478bd9Sstevel@tonic-gate 	inflate_codes_statef *c = s->sub.decode.codes;	/* codes state */
53437c478bd9Sstevel@tonic-gate 
53447c478bd9Sstevel@tonic-gate 	/* copy input/output information to locals (UPDATE macro restores) */
53457c478bd9Sstevel@tonic-gate 	LOAD;
53467c478bd9Sstevel@tonic-gate 
53477c478bd9Sstevel@tonic-gate 	/* process input and output based on current state */
53487c478bd9Sstevel@tonic-gate 	/* CONSTCOND */
53497c478bd9Sstevel@tonic-gate 	while (1)
53507c478bd9Sstevel@tonic-gate 		/* waiting for "i:"=input, "o:"=output, "x:"=nothing */
53517c478bd9Sstevel@tonic-gate 		switch (c->mode) {
53527c478bd9Sstevel@tonic-gate 		case START:	/* x: set up for LEN */
53537c478bd9Sstevel@tonic-gate #ifndef SLOW
53547c478bd9Sstevel@tonic-gate 			if (m >= 258 && n >= 10)
53557c478bd9Sstevel@tonic-gate 			{
53567c478bd9Sstevel@tonic-gate 				UPDATE;
53577c478bd9Sstevel@tonic-gate 				r = inflate_fast(c->lbits, c->dbits,
53587c478bd9Sstevel@tonic-gate 				    c->ltree, c->dtree, s, z);
53597c478bd9Sstevel@tonic-gate 				LOAD;
53607c478bd9Sstevel@tonic-gate 				if (r != Z_OK) {
53617c478bd9Sstevel@tonic-gate 					c->mode = r == Z_STREAM_END ?
53627c478bd9Sstevel@tonic-gate 					    WASH : BADCODE;
53637c478bd9Sstevel@tonic-gate 					break;
53647c478bd9Sstevel@tonic-gate 				}
53657c478bd9Sstevel@tonic-gate 			}
53667c478bd9Sstevel@tonic-gate #endif /* !SLOW */
53677c478bd9Sstevel@tonic-gate 			c->sub.code.need = c->lbits;
53687c478bd9Sstevel@tonic-gate 			c->sub.code.tree = c->ltree;
53697c478bd9Sstevel@tonic-gate 			c->mode = LEN;
53707c478bd9Sstevel@tonic-gate 			/* FALLTHRU */
53717c478bd9Sstevel@tonic-gate 		case LEN:	/* i: get length/literal/eob next */
53727c478bd9Sstevel@tonic-gate 			j = c->sub.code.need;
53737c478bd9Sstevel@tonic-gate 			NEEDBITS(j);
53747c478bd9Sstevel@tonic-gate 			t = c->sub.code.tree +
53757c478bd9Sstevel@tonic-gate 			    ((uInt)b & inflate_mask[j]);
53767c478bd9Sstevel@tonic-gate 			DUMPBITS(t->bits);
53777c478bd9Sstevel@tonic-gate 			e = (uInt)(t->exop);
53787c478bd9Sstevel@tonic-gate 			if (e == 0) {	/* literal */
53797c478bd9Sstevel@tonic-gate 				c->sub.lit = t->base;
53807c478bd9Sstevel@tonic-gate 				Tracevv((stderr, t->base >= 0x20 &&
53817c478bd9Sstevel@tonic-gate 				    t->base < 0x7f ?
53827c478bd9Sstevel@tonic-gate 				    "inflate:         literal '%c'\n" :
53837c478bd9Sstevel@tonic-gate 				    "inflate:         literal 0x%02x\n",
53847c478bd9Sstevel@tonic-gate 				    t->base));
53857c478bd9Sstevel@tonic-gate 				c->mode = LIT;
53867c478bd9Sstevel@tonic-gate 				break;
53877c478bd9Sstevel@tonic-gate 			}
53887c478bd9Sstevel@tonic-gate 			if (e & 16) {	/* length */
53897c478bd9Sstevel@tonic-gate 				c->sub.copy.get = e & 15;
53907c478bd9Sstevel@tonic-gate 				c->len = t->base;
53917c478bd9Sstevel@tonic-gate 				c->mode = LENEXT;
53927c478bd9Sstevel@tonic-gate 				break;
53937c478bd9Sstevel@tonic-gate 			}
53947c478bd9Sstevel@tonic-gate 			if ((e & 64) == 0) {	/* next table */
53957c478bd9Sstevel@tonic-gate 				c->sub.code.need = e;
53967c478bd9Sstevel@tonic-gate 				c->sub.code.tree = t + t->base;
53977c478bd9Sstevel@tonic-gate 				break;
53987c478bd9Sstevel@tonic-gate 			}
53997c478bd9Sstevel@tonic-gate 			if (e & 32) {	/* end of block */
54007c478bd9Sstevel@tonic-gate 				Tracevv((stderr,
54017c478bd9Sstevel@tonic-gate 				    "inflate:         end of block\n"));
54027c478bd9Sstevel@tonic-gate 				c->mode = WASH;
54037c478bd9Sstevel@tonic-gate 				break;
54047c478bd9Sstevel@tonic-gate 			}
54057c478bd9Sstevel@tonic-gate 			c->mode = BADCODE;	/* invalid code */
54067c478bd9Sstevel@tonic-gate 			z->msg = "invalid literal/length code";
54077c478bd9Sstevel@tonic-gate 			r = Z_DATA_ERROR;
54087c478bd9Sstevel@tonic-gate 			LEAVE
54097c478bd9Sstevel@tonic-gate 		case LENEXT:	/* i: getting length extra (have base) */
54107c478bd9Sstevel@tonic-gate 			j = c->sub.copy.get;
54117c478bd9Sstevel@tonic-gate 			NEEDBITS(j);
54127c478bd9Sstevel@tonic-gate 			c->len += (uInt)b & inflate_mask[j];
54137c478bd9Sstevel@tonic-gate 			DUMPBITS(j);
54147c478bd9Sstevel@tonic-gate 			c->sub.code.need = c->dbits;
54157c478bd9Sstevel@tonic-gate 			c->sub.code.tree = c->dtree;
54167c478bd9Sstevel@tonic-gate 			Tracevv((stderr,
54177c478bd9Sstevel@tonic-gate 			    "inflate:         length %u\n", c->len));
54187c478bd9Sstevel@tonic-gate 			c->mode = DIST;
54197c478bd9Sstevel@tonic-gate 			/* FALLTHRU */
54207c478bd9Sstevel@tonic-gate 		case DIST:	/* i: get distance next */
54217c478bd9Sstevel@tonic-gate 			j = c->sub.code.need;
54227c478bd9Sstevel@tonic-gate 			NEEDBITS(j);
54237c478bd9Sstevel@tonic-gate 			t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
54247c478bd9Sstevel@tonic-gate 			DUMPBITS(t->bits);
54257c478bd9Sstevel@tonic-gate 			e = (uInt)(t->exop);
54267c478bd9Sstevel@tonic-gate 			if (e & 16) {	/* distance */
54277c478bd9Sstevel@tonic-gate 				c->sub.copy.get = e & 15;
54287c478bd9Sstevel@tonic-gate 				c->sub.copy.dist = t->base;
54297c478bd9Sstevel@tonic-gate 				c->mode = DISTEXT;
54307c478bd9Sstevel@tonic-gate 				break;
54317c478bd9Sstevel@tonic-gate 			}
54327c478bd9Sstevel@tonic-gate 			if ((e & 64) == 0) {	/* next table */
54337c478bd9Sstevel@tonic-gate 				c->sub.code.need = e;
54347c478bd9Sstevel@tonic-gate 				c->sub.code.tree = t + t->base;
54357c478bd9Sstevel@tonic-gate 				break;
54367c478bd9Sstevel@tonic-gate 			}
54377c478bd9Sstevel@tonic-gate 			c->mode = BADCODE;	/* invalid code */
54387c478bd9Sstevel@tonic-gate 			z->msg = "invalid distance code";
54397c478bd9Sstevel@tonic-gate 			r = Z_DATA_ERROR;
54407c478bd9Sstevel@tonic-gate 			LEAVE
54417c478bd9Sstevel@tonic-gate 		case DISTEXT:	/* i: getting distance extra */
54427c478bd9Sstevel@tonic-gate 			j = c->sub.copy.get;
54437c478bd9Sstevel@tonic-gate 			NEEDBITS(j);
54447c478bd9Sstevel@tonic-gate 			c->sub.copy.dist += (uInt)b & inflate_mask[j];
54457c478bd9Sstevel@tonic-gate 			DUMPBITS(j);
54467c478bd9Sstevel@tonic-gate 			Tracevv((stderr,
54477c478bd9Sstevel@tonic-gate 			    "inflate:         distance %u\n",
54487c478bd9Sstevel@tonic-gate 			    c->sub.copy.dist));
54497c478bd9Sstevel@tonic-gate 			c->mode = COPY;
54507c478bd9Sstevel@tonic-gate 			/* FALLTHRU */
54517c478bd9Sstevel@tonic-gate 		case COPY:
54527c478bd9Sstevel@tonic-gate 			/* o: copying bytes in window, waiting for space */
54537c478bd9Sstevel@tonic-gate #ifndef __TURBOC__ /* Turbo C bug for following expression */
54547c478bd9Sstevel@tonic-gate 			f = (uInt)(q - s->window) < c->sub.copy.dist ?
54557c478bd9Sstevel@tonic-gate 			    s->end - (c->sub.copy.dist - (q - s->window)) :
54567c478bd9Sstevel@tonic-gate 				q - c->sub.copy.dist;
54577c478bd9Sstevel@tonic-gate #else
54587c478bd9Sstevel@tonic-gate 			f = q - c->sub.copy.dist;
54597c478bd9Sstevel@tonic-gate 			if ((uInt)(q - s->window) < c->sub.copy.dist)
54607c478bd9Sstevel@tonic-gate 				f = s->end - (c->sub.copy.dist -
54617c478bd9Sstevel@tonic-gate 				    (uInt)(q - s->window));
54627c478bd9Sstevel@tonic-gate #endif
54637c478bd9Sstevel@tonic-gate 			while (c->len)
54647c478bd9Sstevel@tonic-gate 			{
54657c478bd9Sstevel@tonic-gate 				NEEDOUT;
54667c478bd9Sstevel@tonic-gate 				OUTBYTE(*f++);
54677c478bd9Sstevel@tonic-gate 				if (f == s->end)
54687c478bd9Sstevel@tonic-gate 					f = s->window;
54697c478bd9Sstevel@tonic-gate 				c->len--;
54707c478bd9Sstevel@tonic-gate 			}
54717c478bd9Sstevel@tonic-gate 			c->mode = START;
54727c478bd9Sstevel@tonic-gate 			break;
54737c478bd9Sstevel@tonic-gate 		case LIT:	/* o: got literal, waiting for output space */
54747c478bd9Sstevel@tonic-gate 			NEEDOUT;
54757c478bd9Sstevel@tonic-gate 			OUTBYTE(c->sub.lit);
54767c478bd9Sstevel@tonic-gate 			c->mode = START;
54777c478bd9Sstevel@tonic-gate 			break;
54787c478bd9Sstevel@tonic-gate 		case WASH:	/* o: got eob, possibly more output */
54797c478bd9Sstevel@tonic-gate 			if (k > 7) {	/* return unused byte, if any */
54807c478bd9Sstevel@tonic-gate 				Assert(k < 16,
54817c478bd9Sstevel@tonic-gate 				    "inflate_codes grabbed too many bytes");
54827c478bd9Sstevel@tonic-gate 				k -= 8;
54837c478bd9Sstevel@tonic-gate 				n++;
54847c478bd9Sstevel@tonic-gate 				p--;	/* can always return one */
54857c478bd9Sstevel@tonic-gate 			}
54867c478bd9Sstevel@tonic-gate 			FLUSH;
54877c478bd9Sstevel@tonic-gate 			if (s->read != s->write)
54887c478bd9Sstevel@tonic-gate 				LEAVE
54897c478bd9Sstevel@tonic-gate 			c->mode = END;
54907c478bd9Sstevel@tonic-gate 			/* FALLTHRU */
54917c478bd9Sstevel@tonic-gate 		case END:
54927c478bd9Sstevel@tonic-gate 			r = Z_STREAM_END;
54937c478bd9Sstevel@tonic-gate 			LEAVE
54947c478bd9Sstevel@tonic-gate 		case BADCODE:	/* x: got error */
54957c478bd9Sstevel@tonic-gate 			r = Z_DATA_ERROR;
54967c478bd9Sstevel@tonic-gate 			LEAVE
54977c478bd9Sstevel@tonic-gate 		default:
54987c478bd9Sstevel@tonic-gate 			r = Z_STREAM_ERROR;
54997c478bd9Sstevel@tonic-gate 			LEAVE
55007c478bd9Sstevel@tonic-gate 		}
55017c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
55027c478bd9Sstevel@tonic-gate 	/* otherwise lint complains */
55037c478bd9Sstevel@tonic-gate }
55047c478bd9Sstevel@tonic-gate 
55057c478bd9Sstevel@tonic-gate 
55067c478bd9Sstevel@tonic-gate void
inflate_codes_free(c,z)55077c478bd9Sstevel@tonic-gate inflate_codes_free(c, z)
55087c478bd9Sstevel@tonic-gate inflate_codes_statef *c;
55097c478bd9Sstevel@tonic-gate z_streamp z;
55107c478bd9Sstevel@tonic-gate {
55117c478bd9Sstevel@tonic-gate 	ZFREE(z, c);
55127c478bd9Sstevel@tonic-gate 	Tracev((stderr, "inflate:       codes free\n"));
55137c478bd9Sstevel@tonic-gate }
55147c478bd9Sstevel@tonic-gate /* --- infcodes.c */
55157c478bd9Sstevel@tonic-gate 
55167c478bd9Sstevel@tonic-gate /* +++ infutil.c */
55177c478bd9Sstevel@tonic-gate /*
55187c478bd9Sstevel@tonic-gate  * inflate_util.c -- data and routines common to blocks and codes
55197c478bd9Sstevel@tonic-gate  * Copyright (C) 1995-1998 Mark Adler
55207c478bd9Sstevel@tonic-gate  * For conditions of distribution and use, see copyright notice in zlib.h
55217c478bd9Sstevel@tonic-gate  */
55227c478bd9Sstevel@tonic-gate 
55237c478bd9Sstevel@tonic-gate /* #include "zutil.h" */
55247c478bd9Sstevel@tonic-gate /* #include "infblock.h" */
55257c478bd9Sstevel@tonic-gate /* #include "inftrees.h" */
55267c478bd9Sstevel@tonic-gate /* #include "infcodes.h" */
55277c478bd9Sstevel@tonic-gate /* #include "infutil.h" */
55287c478bd9Sstevel@tonic-gate 
55297c478bd9Sstevel@tonic-gate #ifndef NO_DUMMY_DECL
55307c478bd9Sstevel@tonic-gate struct inflate_codes_state {int dummy; };	/* for buggy compilers */
55317c478bd9Sstevel@tonic-gate #endif
55327c478bd9Sstevel@tonic-gate 
55337c478bd9Sstevel@tonic-gate /* And'ing with mask[n] masks the lower n bits */
55347c478bd9Sstevel@tonic-gate uInt inflate_mask[17] = {
55357c478bd9Sstevel@tonic-gate 	0x0000,
55367c478bd9Sstevel@tonic-gate 	0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
55377c478bd9Sstevel@tonic-gate 	0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
55387c478bd9Sstevel@tonic-gate };
55397c478bd9Sstevel@tonic-gate 
55407c478bd9Sstevel@tonic-gate 
55417c478bd9Sstevel@tonic-gate /* copy as much as possible from the sliding window to the output area */
55427c478bd9Sstevel@tonic-gate int
inflate_flush(s,z,r)55437c478bd9Sstevel@tonic-gate inflate_flush(s, z, r)
55447c478bd9Sstevel@tonic-gate inflate_blocks_statef *s;
55457c478bd9Sstevel@tonic-gate z_streamp z;
55467c478bd9Sstevel@tonic-gate int r;
55477c478bd9Sstevel@tonic-gate {
55487c478bd9Sstevel@tonic-gate 	uInt n;
55497c478bd9Sstevel@tonic-gate 	Bytef *p;
55507c478bd9Sstevel@tonic-gate 	Bytef *q;
55517c478bd9Sstevel@tonic-gate 
55527c478bd9Sstevel@tonic-gate 	/* local copies of source and destination pointers */
55537c478bd9Sstevel@tonic-gate 	p = z->next_out;
55547c478bd9Sstevel@tonic-gate 	q = s->read;
55557c478bd9Sstevel@tonic-gate 
55567c478bd9Sstevel@tonic-gate 	/* compute number of bytes to copy as far as end of window */
55577c478bd9Sstevel@tonic-gate 	n = (uInt)((q <= s->write ? s->write : s->end) - q);
55587c478bd9Sstevel@tonic-gate 	if (n > z->avail_out) n = z->avail_out;
55597c478bd9Sstevel@tonic-gate 	if (n && r == Z_BUF_ERROR) r = Z_OK;
55607c478bd9Sstevel@tonic-gate 
55617c478bd9Sstevel@tonic-gate 	/* update counters */
55627c478bd9Sstevel@tonic-gate 	z->avail_out -= n;
55637c478bd9Sstevel@tonic-gate 	z->total_out += n;
55647c478bd9Sstevel@tonic-gate 
55657c478bd9Sstevel@tonic-gate 	/* update check information */
55667c478bd9Sstevel@tonic-gate 	if (s->checkfn != Z_NULL)
55677c478bd9Sstevel@tonic-gate 		z->adler = s->check = (*s->checkfn)(s->check, q, n);
55687c478bd9Sstevel@tonic-gate 
55697c478bd9Sstevel@tonic-gate 	/* copy as far as end of window */
55707c478bd9Sstevel@tonic-gate 	if (p != Z_NULL) {	/* PPP */
55717c478bd9Sstevel@tonic-gate 		zmemcpy(p, q, n);
55727c478bd9Sstevel@tonic-gate 		p += n;
55737c478bd9Sstevel@tonic-gate 	}	/* PPP */
55747c478bd9Sstevel@tonic-gate 	q += n;
55757c478bd9Sstevel@tonic-gate 
55767c478bd9Sstevel@tonic-gate 	/* see if more to copy at beginning of window */
55777c478bd9Sstevel@tonic-gate 	if (q == s->end)
55787c478bd9Sstevel@tonic-gate 	{
55797c478bd9Sstevel@tonic-gate 		/* wrap pointers */
55807c478bd9Sstevel@tonic-gate 		q = s->window;
55817c478bd9Sstevel@tonic-gate 		if (s->write == s->end)
55827c478bd9Sstevel@tonic-gate 			s->write = s->window;
55837c478bd9Sstevel@tonic-gate 
55847c478bd9Sstevel@tonic-gate 		/* compute bytes to copy */
55857c478bd9Sstevel@tonic-gate 		n = (uInt)(s->write - q);
55867c478bd9Sstevel@tonic-gate 		if (n > z->avail_out) n = z->avail_out;
55877c478bd9Sstevel@tonic-gate 		if (n && r == Z_BUF_ERROR) r = Z_OK;
55887c478bd9Sstevel@tonic-gate 
55897c478bd9Sstevel@tonic-gate 		/* update counters */
55907c478bd9Sstevel@tonic-gate 		z->avail_out -= n;
55917c478bd9Sstevel@tonic-gate 		z->total_out += n;
55927c478bd9Sstevel@tonic-gate 
55937c478bd9Sstevel@tonic-gate 		/* update check information */
55947c478bd9Sstevel@tonic-gate 		if (s->checkfn != Z_NULL)
55957c478bd9Sstevel@tonic-gate 			z->adler = s->check = (*s->checkfn)(s->check, q, n);
55967c478bd9Sstevel@tonic-gate 
55977c478bd9Sstevel@tonic-gate 		/* copy */
55987c478bd9Sstevel@tonic-gate 		if (p != Z_NULL) {	/* PPP */
55997c478bd9Sstevel@tonic-gate 			zmemcpy(p, q, n);
56007c478bd9Sstevel@tonic-gate 			p += n;
56017c478bd9Sstevel@tonic-gate 		}	/* PPP */
56027c478bd9Sstevel@tonic-gate 		q += n;
56037c478bd9Sstevel@tonic-gate 	}
56047c478bd9Sstevel@tonic-gate 
56057c478bd9Sstevel@tonic-gate 	/* update pointers */
56067c478bd9Sstevel@tonic-gate 	z->next_out = p;
56077c478bd9Sstevel@tonic-gate 	s->read = q;
56087c478bd9Sstevel@tonic-gate 
56097c478bd9Sstevel@tonic-gate 	/* done */
56107c478bd9Sstevel@tonic-gate 	return (r);
56117c478bd9Sstevel@tonic-gate }
56127c478bd9Sstevel@tonic-gate /* --- infutil.c */
56137c478bd9Sstevel@tonic-gate 
56147c478bd9Sstevel@tonic-gate /* +++ inffast.c */
56157c478bd9Sstevel@tonic-gate /*
56167c478bd9Sstevel@tonic-gate  * inffast.c -- process literals and length/distance pairs fast
56177c478bd9Sstevel@tonic-gate  * Copyright (C) 1995-1998 Mark Adler
56187c478bd9Sstevel@tonic-gate  * For conditions of distribution and use, see copyright notice in zlib.h
56197c478bd9Sstevel@tonic-gate  */
56207c478bd9Sstevel@tonic-gate 
56217c478bd9Sstevel@tonic-gate /* #include "zutil.h" */
56227c478bd9Sstevel@tonic-gate /* #include "inftrees.h" */
56237c478bd9Sstevel@tonic-gate /* #include "infblock.h" */
56247c478bd9Sstevel@tonic-gate /* #include "infcodes.h" */
56257c478bd9Sstevel@tonic-gate /* #include "infutil.h" */
56267c478bd9Sstevel@tonic-gate /* #include "inffast.h" */
56277c478bd9Sstevel@tonic-gate 
56287c478bd9Sstevel@tonic-gate #ifndef NO_DUMMY_DECL
56297c478bd9Sstevel@tonic-gate struct inflate_codes_state {int dummy; };	/* for buggy compilers */
56307c478bd9Sstevel@tonic-gate #endif
56317c478bd9Sstevel@tonic-gate 
56327c478bd9Sstevel@tonic-gate /* simplify the use of the inflate_huft type with some defines */
56337c478bd9Sstevel@tonic-gate #define	exop word.what.Exop
56347c478bd9Sstevel@tonic-gate #define	bits word.what.Bits
56357c478bd9Sstevel@tonic-gate 
56367c478bd9Sstevel@tonic-gate /* macros for bit input with no checking and for returning unused bytes */
56377c478bd9Sstevel@tonic-gate #define	GRABBITS(j) { while (k < (j)) {b |= ((uLong)NEXTBYTE)<<k; k += 8; }}
56387c478bd9Sstevel@tonic-gate #define	UNGRAB {c = z->avail_in-n; c = (k>>3) < c?k>>3:c; n += c; p -= c; \
56397c478bd9Sstevel@tonic-gate 	k -= c<<3; }
56407c478bd9Sstevel@tonic-gate 
56417c478bd9Sstevel@tonic-gate /*
56427c478bd9Sstevel@tonic-gate  * Called with number of bytes left to write in window at least 258
56437c478bd9Sstevel@tonic-gate  * (the maximum string length) and number of input bytes available at
56447c478bd9Sstevel@tonic-gate  * least ten.  The ten bytes are six bytes for the longest length/
56457c478bd9Sstevel@tonic-gate  * distance pair plus four bytes for overloading the bit buffer.
56467c478bd9Sstevel@tonic-gate  */
56477c478bd9Sstevel@tonic-gate 
56487c478bd9Sstevel@tonic-gate int
inflate_fast(bl,bd,tl,td,s,z)56497c478bd9Sstevel@tonic-gate inflate_fast(bl, bd, tl, td, s, z)
56507c478bd9Sstevel@tonic-gate uInt bl, bd;
56517c478bd9Sstevel@tonic-gate const inflate_huft *tl;
56527c478bd9Sstevel@tonic-gate const inflate_huft *td;	/* need separate declaration for Borland C++ */
56537c478bd9Sstevel@tonic-gate inflate_blocks_statef *s;
56547c478bd9Sstevel@tonic-gate z_streamp z;
56557c478bd9Sstevel@tonic-gate {
56567c478bd9Sstevel@tonic-gate 	const inflate_huft *t;	/* temporary pointer */
56577c478bd9Sstevel@tonic-gate 	uInt e;	/* extra bits or operation */
56587c478bd9Sstevel@tonic-gate 	uLong b;	/* bit buffer */
56597c478bd9Sstevel@tonic-gate 	uInt k;	/* bits in bit buffer */
56607c478bd9Sstevel@tonic-gate 	Bytef *p;	/* input data pointer */
56617c478bd9Sstevel@tonic-gate 	uInt n;	/* bytes available there */
56627c478bd9Sstevel@tonic-gate 	Bytef *q;	/* output window write pointer */
56637c478bd9Sstevel@tonic-gate 	uInt m;	/* bytes to end of window or read pointer */
56647c478bd9Sstevel@tonic-gate 	uInt ml;	/* mask for literal/length tree */
56657c478bd9Sstevel@tonic-gate 	uInt md;	/* mask for distance tree */
56667c478bd9Sstevel@tonic-gate 	uInt c;	/* bytes to copy */
56677c478bd9Sstevel@tonic-gate 	uInt d;	/* distance back to copy from */
56687c478bd9Sstevel@tonic-gate 	Bytef *r;	/* copy source pointer */
56697c478bd9Sstevel@tonic-gate 
56707c478bd9Sstevel@tonic-gate 	/* load input, output, bit values */
56717c478bd9Sstevel@tonic-gate 	LOAD;
56727c478bd9Sstevel@tonic-gate 
56737c478bd9Sstevel@tonic-gate 	/* initialize masks */
56747c478bd9Sstevel@tonic-gate 	ml = inflate_mask[bl];
56757c478bd9Sstevel@tonic-gate 	md = inflate_mask[bd];
56767c478bd9Sstevel@tonic-gate 
56777c478bd9Sstevel@tonic-gate 	/* do until not enough input or output space for fast loop */
56787c478bd9Sstevel@tonic-gate 	do {	/* assume called with m >= 258 && n >= 10 */
56797c478bd9Sstevel@tonic-gate 		/* get literal/length code */
56807c478bd9Sstevel@tonic-gate 		/* max bits for literal/length code */
56817c478bd9Sstevel@tonic-gate 		GRABBITS(20);
56827c478bd9Sstevel@tonic-gate 		if ((e = (t = tl + ((uInt)b & ml))->exop) == 0) {
56837c478bd9Sstevel@tonic-gate 			DUMPBITS(t->bits);
56847c478bd9Sstevel@tonic-gate 			Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
56857c478bd9Sstevel@tonic-gate 			    "inflate:         * literal '%c'\n" :
56867c478bd9Sstevel@tonic-gate 			    "inflate:         * literal 0x%02x\n", t->base));
56877c478bd9Sstevel@tonic-gate 			*q++ = (Byte)t->base;
56887c478bd9Sstevel@tonic-gate 			m--;
56897c478bd9Sstevel@tonic-gate 			continue;
56907c478bd9Sstevel@tonic-gate 		}
56917c478bd9Sstevel@tonic-gate 		do {
56927c478bd9Sstevel@tonic-gate 			DUMPBITS(t->bits);
56937c478bd9Sstevel@tonic-gate 			if (e & 16) {
56947c478bd9Sstevel@tonic-gate 				/* get extra bits for length */
56957c478bd9Sstevel@tonic-gate 				e &= 15;
56967c478bd9Sstevel@tonic-gate 				c = t->base + ((uInt)b & inflate_mask[e]);
56977c478bd9Sstevel@tonic-gate 				DUMPBITS(e);
56987c478bd9Sstevel@tonic-gate 				Tracevv((stderr,
56997c478bd9Sstevel@tonic-gate 				    "inflate:         * length %u\n", c));
57007c478bd9Sstevel@tonic-gate 
57017c478bd9Sstevel@tonic-gate 				/* decode distance base of block to copy */
57027c478bd9Sstevel@tonic-gate 				GRABBITS(15);	/* max bits for distance code */
57037c478bd9Sstevel@tonic-gate 				e = (t = td + ((uInt)b & md))->exop;
57047c478bd9Sstevel@tonic-gate 				do {
57057c478bd9Sstevel@tonic-gate 					DUMPBITS(t->bits);
57067c478bd9Sstevel@tonic-gate 					if (e & 16) {
57077c478bd9Sstevel@tonic-gate 						/*
57087c478bd9Sstevel@tonic-gate 						 * get extra bits to
57097c478bd9Sstevel@tonic-gate 						 * add to distance
57107c478bd9Sstevel@tonic-gate 						 * base
57117c478bd9Sstevel@tonic-gate 						 */
57127c478bd9Sstevel@tonic-gate 						e &= 15;
57137c478bd9Sstevel@tonic-gate 						/* get extra bits (up to 13) */
57147c478bd9Sstevel@tonic-gate 						GRABBITS(e);
57157c478bd9Sstevel@tonic-gate 						d = t->base + ((uInt)b &
57167c478bd9Sstevel@tonic-gate 						    inflate_mask[e]);
57177c478bd9Sstevel@tonic-gate 						DUMPBITS(e);
57187c478bd9Sstevel@tonic-gate 						Tracevv((stderr,
57197c478bd9Sstevel@tonic-gate 						    "inflate:         * "
57207c478bd9Sstevel@tonic-gate 						    "distance %u\n", d));
57217c478bd9Sstevel@tonic-gate 
57227c478bd9Sstevel@tonic-gate 						/* do the copy */
57237c478bd9Sstevel@tonic-gate 						m -= c;
57247c478bd9Sstevel@tonic-gate 						/* offset before dest */
57257c478bd9Sstevel@tonic-gate 						if ((uInt)(q - s->window) >= d)
57267c478bd9Sstevel@tonic-gate 							/*  just copy */
57277c478bd9Sstevel@tonic-gate 						{
57287c478bd9Sstevel@tonic-gate 							r = q - d;
57297c478bd9Sstevel@tonic-gate 							/*
57307c478bd9Sstevel@tonic-gate 							 * minimum
57317c478bd9Sstevel@tonic-gate 							 * count is
57327c478bd9Sstevel@tonic-gate 							 * three, so
57337c478bd9Sstevel@tonic-gate 							 * unroll loop
57347c478bd9Sstevel@tonic-gate 							 * a little
57357c478bd9Sstevel@tonic-gate 							 */
57367c478bd9Sstevel@tonic-gate 							*q++ = *r++;  c--;
57377c478bd9Sstevel@tonic-gate 							*q++ = *r++;  c--;
57387c478bd9Sstevel@tonic-gate 						}
57397c478bd9Sstevel@tonic-gate 					/* else offset after destination */
57407c478bd9Sstevel@tonic-gate 						else {
57417c478bd9Sstevel@tonic-gate 	/* bytes from offset to end */
57427c478bd9Sstevel@tonic-gate 							e = d - (uInt)(q -
57437c478bd9Sstevel@tonic-gate 							    s->window);
57447c478bd9Sstevel@tonic-gate 	/* pointer to offset */
57457c478bd9Sstevel@tonic-gate 							r = s->end - e;
57467c478bd9Sstevel@tonic-gate 							/* if source crosses */
57477c478bd9Sstevel@tonic-gate 							if (c > e) {
57487c478bd9Sstevel@tonic-gate 	/* copy to end of window */
57497c478bd9Sstevel@tonic-gate 								c -= e;
57507c478bd9Sstevel@tonic-gate 								do {
57517c478bd9Sstevel@tonic-gate 									*q++ =
57527c478bd9Sstevel@tonic-gate 									    *r
57537c478bd9Sstevel@tonic-gate 									    ++;
57547c478bd9Sstevel@tonic-gate 								} while (--e);
57557c478bd9Sstevel@tonic-gate 	/* copy rest from start of window */
57567c478bd9Sstevel@tonic-gate 								r = s->window;
57577c478bd9Sstevel@tonic-gate 							}
57587c478bd9Sstevel@tonic-gate 						}
57597c478bd9Sstevel@tonic-gate 						/* copy all or what's left */
57607c478bd9Sstevel@tonic-gate 						do {
57617c478bd9Sstevel@tonic-gate 							*q++ = *r++;
57627c478bd9Sstevel@tonic-gate 						} while (--c);
57637c478bd9Sstevel@tonic-gate 						break;
57647c478bd9Sstevel@tonic-gate 					} else if ((e & 64) == 0) {
57657c478bd9Sstevel@tonic-gate 						t += t->base;
57667c478bd9Sstevel@tonic-gate 						e = (t += ((uInt)b &
57677c478bd9Sstevel@tonic-gate 						    inflate_mask[e]))->exop;
57687c478bd9Sstevel@tonic-gate 					} else {
57697c478bd9Sstevel@tonic-gate 						z->msg =
57707c478bd9Sstevel@tonic-gate 						    "invalid distance code";
57717c478bd9Sstevel@tonic-gate 						UNGRAB;
57727c478bd9Sstevel@tonic-gate 						UPDATE;
57737c478bd9Sstevel@tonic-gate 						return (Z_DATA_ERROR);
57747c478bd9Sstevel@tonic-gate 					}
57757c478bd9Sstevel@tonic-gate 					/* CONSTCOND */
57767c478bd9Sstevel@tonic-gate 				} while (1);
57777c478bd9Sstevel@tonic-gate 				break;
57787c478bd9Sstevel@tonic-gate 			}
57797c478bd9Sstevel@tonic-gate 			if ((e & 64) == 0)
57807c478bd9Sstevel@tonic-gate 			{
57817c478bd9Sstevel@tonic-gate 				t += t->base;
57827c478bd9Sstevel@tonic-gate 				if ((e = (t += ((uInt)b &
57837c478bd9Sstevel@tonic-gate 				    inflate_mask[e]))->exop) == 0)
57847c478bd9Sstevel@tonic-gate 				{
57857c478bd9Sstevel@tonic-gate 					DUMPBITS(t->bits);
57867c478bd9Sstevel@tonic-gate 					Tracevv((stderr, t->base >= 0x20 &&
57877c478bd9Sstevel@tonic-gate 					    t->base < 0x7f ?
57887c478bd9Sstevel@tonic-gate 					    "inflate:         * literal '%c'\n"
57897c478bd9Sstevel@tonic-gate 					    :
57907c478bd9Sstevel@tonic-gate 					    "inflate:         * literal "
57917c478bd9Sstevel@tonic-gate 					    "0x%02x\n", t->base));
57927c478bd9Sstevel@tonic-gate 					*q++ = (Byte)t->base;
57937c478bd9Sstevel@tonic-gate 					m--;
57947c478bd9Sstevel@tonic-gate 					break;
57957c478bd9Sstevel@tonic-gate 				}
57967c478bd9Sstevel@tonic-gate 			} else if (e & 32) {
57977c478bd9Sstevel@tonic-gate 				Tracevv((stderr,
57987c478bd9Sstevel@tonic-gate 				    "inflate:         * end of block\n"));
57997c478bd9Sstevel@tonic-gate 				UNGRAB;
58007c478bd9Sstevel@tonic-gate 				UPDATE;
58017c478bd9Sstevel@tonic-gate 				return (Z_STREAM_END);
58027c478bd9Sstevel@tonic-gate 			} else {
58037c478bd9Sstevel@tonic-gate 				z->msg = "invalid literal/length code";
58047c478bd9Sstevel@tonic-gate 				UNGRAB;
58057c478bd9Sstevel@tonic-gate 				UPDATE;
58067c478bd9Sstevel@tonic-gate 				return (Z_DATA_ERROR);
58077c478bd9Sstevel@tonic-gate 			}
58087c478bd9Sstevel@tonic-gate 			/* CONSTCOND */
58097c478bd9Sstevel@tonic-gate 		} while (1);
58107c478bd9Sstevel@tonic-gate 	} while (m >= 258 && n >= 10);
58117c478bd9Sstevel@tonic-gate 
58127c478bd9Sstevel@tonic-gate 	/* not enough input or output--restore pointers and return */
58137c478bd9Sstevel@tonic-gate 	UNGRAB;
58147c478bd9Sstevel@tonic-gate 	UPDATE;
58157c478bd9Sstevel@tonic-gate 	return (Z_OK);
58167c478bd9Sstevel@tonic-gate }
58177c478bd9Sstevel@tonic-gate /* --- inffast.c */
58187c478bd9Sstevel@tonic-gate 
58197c478bd9Sstevel@tonic-gate /* +++ zutil.c */
58207c478bd9Sstevel@tonic-gate /*
58217c478bd9Sstevel@tonic-gate  * zutil.c -- target dependent utility functions for the compression library
58227c478bd9Sstevel@tonic-gate  * Copyright (C) 1995-1998 Jean-loup Gailly.
58237c478bd9Sstevel@tonic-gate  * For conditions of distribution and use, see copyright notice in zlib.h
58247c478bd9Sstevel@tonic-gate  */
58257c478bd9Sstevel@tonic-gate 
58267c478bd9Sstevel@tonic-gate /* From: zutil.c,v 1.17 1996/07/24 13:41:12 me Exp $ */
58277c478bd9Sstevel@tonic-gate 
58287c478bd9Sstevel@tonic-gate #ifdef DEBUG_ZLIB
58297c478bd9Sstevel@tonic-gate #include <stdio.h>
58307c478bd9Sstevel@tonic-gate #endif
58317c478bd9Sstevel@tonic-gate 
58327c478bd9Sstevel@tonic-gate /* #include "zutil.h" */
58337c478bd9Sstevel@tonic-gate 
58347c478bd9Sstevel@tonic-gate #ifndef NO_DUMMY_DECL
58357c478bd9Sstevel@tonic-gate struct internal_state	{int dummy; };	/* for buggy compilers */
58367c478bd9Sstevel@tonic-gate #endif
58377c478bd9Sstevel@tonic-gate 
58387c478bd9Sstevel@tonic-gate #ifndef STDC
58397c478bd9Sstevel@tonic-gate extern void exit OF((int));
58407c478bd9Sstevel@tonic-gate #endif
58417c478bd9Sstevel@tonic-gate 
5842*c9431fa1Sahl static const char *z_errmsg[10] = {
58437c478bd9Sstevel@tonic-gate "need dictionary",	/* Z_NEED_DICT		2 */
58447c478bd9Sstevel@tonic-gate "stream end",		/* Z_STREAM_END		1 */
58457c478bd9Sstevel@tonic-gate "",			/* Z_OK			0 */
58467c478bd9Sstevel@tonic-gate "file error",		/* Z_ERRNO		(-1) */
58477c478bd9Sstevel@tonic-gate "stream error",		/* Z_STREAM_ERROR	(-2) */
58487c478bd9Sstevel@tonic-gate "data error",		/* Z_DATA_ERROR		(-3) */
58497c478bd9Sstevel@tonic-gate "insufficient memory",	/* Z_MEM_ERROR		(-4) */
58507c478bd9Sstevel@tonic-gate "buffer error",		/* Z_BUF_ERROR		(-5) */
58517c478bd9Sstevel@tonic-gate "incompatible version",	/* Z_VERSION_ERROR	(-6) */
58527c478bd9Sstevel@tonic-gate ""};
58537c478bd9Sstevel@tonic-gate 
58547c478bd9Sstevel@tonic-gate 
58557c478bd9Sstevel@tonic-gate const char *
zlibVersion()58567c478bd9Sstevel@tonic-gate zlibVersion()
58577c478bd9Sstevel@tonic-gate {
58587c478bd9Sstevel@tonic-gate 	return (ZLIB_VERSION);
58597c478bd9Sstevel@tonic-gate }
58607c478bd9Sstevel@tonic-gate 
58617c478bd9Sstevel@tonic-gate #ifdef DEBUG_ZLIB
58627c478bd9Sstevel@tonic-gate void
z_error(m)58637c478bd9Sstevel@tonic-gate z_error(m)
58647c478bd9Sstevel@tonic-gate     char *m;
58657c478bd9Sstevel@tonic-gate {
58667c478bd9Sstevel@tonic-gate 	fprintf(stderr, "%s\n", m);
58677c478bd9Sstevel@tonic-gate 	exit(1);
58687c478bd9Sstevel@tonic-gate }
58697c478bd9Sstevel@tonic-gate #endif
58707c478bd9Sstevel@tonic-gate 
58717c478bd9Sstevel@tonic-gate #ifndef HAVE_MEMCPY
58727c478bd9Sstevel@tonic-gate 
58737c478bd9Sstevel@tonic-gate void
zmemcpy(dest,source,len)58747c478bd9Sstevel@tonic-gate zmemcpy(dest, source, len)
58757c478bd9Sstevel@tonic-gate     Bytef* dest;
58767c478bd9Sstevel@tonic-gate     const Bytef* source;
58777c478bd9Sstevel@tonic-gate     uInt  len;
58787c478bd9Sstevel@tonic-gate {
58797c478bd9Sstevel@tonic-gate 	if (len == 0)
58807c478bd9Sstevel@tonic-gate 		return;
58817c478bd9Sstevel@tonic-gate 	do {
58827c478bd9Sstevel@tonic-gate 		*dest++ = *source++;	/* ??? to be unrolled */
58837c478bd9Sstevel@tonic-gate 	} while (--len != 0);
58847c478bd9Sstevel@tonic-gate }
58857c478bd9Sstevel@tonic-gate 
58867c478bd9Sstevel@tonic-gate int
zmemcmp(s1,s2,len)58877c478bd9Sstevel@tonic-gate zmemcmp(s1, s2, len)
58887c478bd9Sstevel@tonic-gate const Bytef* s1;
58897c478bd9Sstevel@tonic-gate const Bytef* s2;
58907c478bd9Sstevel@tonic-gate uInt  len;
58917c478bd9Sstevel@tonic-gate {
58927c478bd9Sstevel@tonic-gate 	uInt j;
58937c478bd9Sstevel@tonic-gate 
58947c478bd9Sstevel@tonic-gate 	for (j = 0; j < len; j++) {
58957c478bd9Sstevel@tonic-gate 		if (s1[j] != s2[j])
58967c478bd9Sstevel@tonic-gate 			return (2*(s1[j] > s2[j])-1);
58977c478bd9Sstevel@tonic-gate 	}
58987c478bd9Sstevel@tonic-gate 	return (0);
58997c478bd9Sstevel@tonic-gate }
59007c478bd9Sstevel@tonic-gate 
59017c478bd9Sstevel@tonic-gate void
zmemzero(dest,len)59027c478bd9Sstevel@tonic-gate zmemzero(dest, len)
59037c478bd9Sstevel@tonic-gate     Bytef* dest;
59047c478bd9Sstevel@tonic-gate     uInt  len;
59057c478bd9Sstevel@tonic-gate {
59067c478bd9Sstevel@tonic-gate 	if (len == 0)
59077c478bd9Sstevel@tonic-gate 		return;
59087c478bd9Sstevel@tonic-gate 	do {
59097c478bd9Sstevel@tonic-gate 		*dest++ = 0;	/* ??? to be unrolled */
59107c478bd9Sstevel@tonic-gate 	} while (--len != 0);
59117c478bd9Sstevel@tonic-gate }
59127c478bd9Sstevel@tonic-gate #endif
59137c478bd9Sstevel@tonic-gate 
59147c478bd9Sstevel@tonic-gate #ifdef __TURBOC__
59157c478bd9Sstevel@tonic-gate #if (defined(__BORLANDC__) || !defined(SMALL_MEDIUM)) && !defined(__32BIT__)
59167c478bd9Sstevel@tonic-gate /*
59177c478bd9Sstevel@tonic-gate  * Small and medium model in Turbo C are for now limited to near
59187c478bd9Sstevel@tonic-gate  * allocation with reduced MAX_WBITS and MAX_MEM_LEVEL
59197c478bd9Sstevel@tonic-gate  */
59207c478bd9Sstevel@tonic-gate #define	MY_ZCALLOC
59217c478bd9Sstevel@tonic-gate 
59227c478bd9Sstevel@tonic-gate /*
59237c478bd9Sstevel@tonic-gate  * Turbo C malloc() does not allow dynamic allocation of 64K bytes and
59247c478bd9Sstevel@tonic-gate  * farmalloc(64K) returns a pointer with an offset of 8, so we must
59257c478bd9Sstevel@tonic-gate  * fix the pointer. Warning: the pointer must be put back to its
59267c478bd9Sstevel@tonic-gate  * original form in order to free it, use zcfree().
59277c478bd9Sstevel@tonic-gate  */
59287c478bd9Sstevel@tonic-gate 
59297c478bd9Sstevel@tonic-gate #define	MAX_PTR 10
59307c478bd9Sstevel@tonic-gate /* 10*64K = 640K */
59317c478bd9Sstevel@tonic-gate 
59327c478bd9Sstevel@tonic-gate local int next_ptr = 0;
59337c478bd9Sstevel@tonic-gate 
59347c478bd9Sstevel@tonic-gate typedef struct ptr_table_s {
59357c478bd9Sstevel@tonic-gate 	voidpf org_ptr;
59367c478bd9Sstevel@tonic-gate 	voidpf new_ptr;
59377c478bd9Sstevel@tonic-gate } ptr_table;
59387c478bd9Sstevel@tonic-gate 
59397c478bd9Sstevel@tonic-gate local ptr_table table[MAX_PTR];
59407c478bd9Sstevel@tonic-gate /*
59417c478bd9Sstevel@tonic-gate  * This table is used to remember the original form of pointers to
59427c478bd9Sstevel@tonic-gate  * large buffers (64K). Such pointers are normalized with a zero
59437c478bd9Sstevel@tonic-gate  * offset.  Since MSDOS is not a preemptive multitasking OS, this
59447c478bd9Sstevel@tonic-gate  * table is not protected from concurrent access. This hack doesn't
59457c478bd9Sstevel@tonic-gate  * work anyway on a protected system like OS/2. Use Microsoft C
59467c478bd9Sstevel@tonic-gate  * instead.
59477c478bd9Sstevel@tonic-gate  */
59487c478bd9Sstevel@tonic-gate 
59497c478bd9Sstevel@tonic-gate voidpf
zcalloc(voidpf opaque,unsigned items,unsigned size)59507c478bd9Sstevel@tonic-gate zcalloc(voidpf opaque, unsigned items, unsigned size)
59517c478bd9Sstevel@tonic-gate {
59527c478bd9Sstevel@tonic-gate 	voidpf buf = opaque;	/* just to make some compilers happy */
59537c478bd9Sstevel@tonic-gate 	ulg bsize = (ulg)items*size;
59547c478bd9Sstevel@tonic-gate 
59557c478bd9Sstevel@tonic-gate 	/*
59567c478bd9Sstevel@tonic-gate 	 * If we allocate less than 65520 bytes, we assume that
59577c478bd9Sstevel@tonic-gate 	 * farmalloc will return a usable pointer which doesn't have
59587c478bd9Sstevel@tonic-gate 	 * to be normalized.
59597c478bd9Sstevel@tonic-gate 	 */
59607c478bd9Sstevel@tonic-gate 	if (bsize < 65520L) {
59617c478bd9Sstevel@tonic-gate 		buf = farmalloc(bsize);
59627c478bd9Sstevel@tonic-gate 		if (*(ush *)&buf != 0)
59637c478bd9Sstevel@tonic-gate 			return (buf);
59647c478bd9Sstevel@tonic-gate 	} else {
59657c478bd9Sstevel@tonic-gate 		buf = farmalloc(bsize + 16L);
59667c478bd9Sstevel@tonic-gate 	}
59677c478bd9Sstevel@tonic-gate 	if (buf == NULL || next_ptr >= MAX_PTR)
59687c478bd9Sstevel@tonic-gate 		return (NULL);
59697c478bd9Sstevel@tonic-gate 	table[next_ptr].org_ptr = buf;
59707c478bd9Sstevel@tonic-gate 
59717c478bd9Sstevel@tonic-gate 	/* Normalize the pointer to seg:0 */
59727c478bd9Sstevel@tonic-gate 	*((ush *)&buf+1) += ((ush)((uch *)buf-0) + 15) >> 4;
59737c478bd9Sstevel@tonic-gate 	*(ush *)&buf = 0;
59747c478bd9Sstevel@tonic-gate 	table[next_ptr++].new_ptr = buf;
59757c478bd9Sstevel@tonic-gate 	return (buf);
59767c478bd9Sstevel@tonic-gate }
59777c478bd9Sstevel@tonic-gate 
59787c478bd9Sstevel@tonic-gate void
zcfree(voidpf opaque,voidpf ptr)59797c478bd9Sstevel@tonic-gate zcfree(voidpf opaque, voidpf ptr)
59807c478bd9Sstevel@tonic-gate {
59817c478bd9Sstevel@tonic-gate 	int n;
59827c478bd9Sstevel@tonic-gate 	if (*(ush*)&ptr != 0) { /* object < 64K */
59837c478bd9Sstevel@tonic-gate 		farfree(ptr);
59847c478bd9Sstevel@tonic-gate 		return;
59857c478bd9Sstevel@tonic-gate 	}
59867c478bd9Sstevel@tonic-gate 	/* Find the original pointer */
59877c478bd9Sstevel@tonic-gate 	for (n = 0; n < next_ptr; n++) {
59887c478bd9Sstevel@tonic-gate 		if (ptr != table[n].new_ptr)
59897c478bd9Sstevel@tonic-gate 			continue;
59907c478bd9Sstevel@tonic-gate 
59917c478bd9Sstevel@tonic-gate 		farfree(table[n].org_ptr);
59927c478bd9Sstevel@tonic-gate 		while (++n < next_ptr) {
59937c478bd9Sstevel@tonic-gate 			table[n-1] = table[n];
59947c478bd9Sstevel@tonic-gate 		}
59957c478bd9Sstevel@tonic-gate 		next_ptr--;
59967c478bd9Sstevel@tonic-gate 		return;
59977c478bd9Sstevel@tonic-gate 	}
59987c478bd9Sstevel@tonic-gate 	ptr = opaque;	/* just to make some compilers happy */
59997c478bd9Sstevel@tonic-gate 	Assert(0, "zcfree: ptr not found");
60007c478bd9Sstevel@tonic-gate }
60017c478bd9Sstevel@tonic-gate #endif
60027c478bd9Sstevel@tonic-gate #endif /* __TURBOC__ */
60037c478bd9Sstevel@tonic-gate 
60047c478bd9Sstevel@tonic-gate 
60057c478bd9Sstevel@tonic-gate #if defined(M_I86) && !defined(__32BIT__)
60067c478bd9Sstevel@tonic-gate /* Microsoft C in 16-bit mode */
60077c478bd9Sstevel@tonic-gate 
60087c478bd9Sstevel@tonic-gate #define	MY_ZCALLOC
60097c478bd9Sstevel@tonic-gate 
60107c478bd9Sstevel@tonic-gate #if (!defined(_MSC_VER) || (_MSC_VER <= 600))
60117c478bd9Sstevel@tonic-gate #define	_halloc  halloc
60127c478bd9Sstevel@tonic-gate #define	_hfree   hfree
60137c478bd9Sstevel@tonic-gate #endif
60147c478bd9Sstevel@tonic-gate 
60157c478bd9Sstevel@tonic-gate voidpf
zcalloc(voidpf opaque,unsigned items,unsigned size)60167c478bd9Sstevel@tonic-gate zcalloc(voidpf opaque, unsigned items, unsigned size)
60177c478bd9Sstevel@tonic-gate {
60187c478bd9Sstevel@tonic-gate 	if (opaque) opaque = 0;	/* to make compiler happy */
60197c478bd9Sstevel@tonic-gate 	return (_halloc((long)items, size));
60207c478bd9Sstevel@tonic-gate }
60217c478bd9Sstevel@tonic-gate 
60227c478bd9Sstevel@tonic-gate void
zcfree(voidpf opaque,voidpf ptr)60237c478bd9Sstevel@tonic-gate zcfree(voidpf opaque, voidpf ptr)
60247c478bd9Sstevel@tonic-gate {
60257c478bd9Sstevel@tonic-gate 	if (opaque) opaque = 0;	/* to make compiler happy */
60267c478bd9Sstevel@tonic-gate 	_hfree(ptr);
60277c478bd9Sstevel@tonic-gate }
60287c478bd9Sstevel@tonic-gate 
60297c478bd9Sstevel@tonic-gate #endif /* MSC */
60307c478bd9Sstevel@tonic-gate 
60317c478bd9Sstevel@tonic-gate 
60327c478bd9Sstevel@tonic-gate #ifndef MY_ZCALLOC /* Any system without a special alloc function */
60337c478bd9Sstevel@tonic-gate 
60347c478bd9Sstevel@tonic-gate #ifndef STDC
60357c478bd9Sstevel@tonic-gate extern voidp  calloc OF((uInt items, uInt size));
60367c478bd9Sstevel@tonic-gate extern void   free   OF((voidpf ptr));
60377c478bd9Sstevel@tonic-gate #endif
60387c478bd9Sstevel@tonic-gate 
60397c478bd9Sstevel@tonic-gate voidpf
zcalloc(opaque,items,size)60407c478bd9Sstevel@tonic-gate zcalloc(opaque, items, size)
60417c478bd9Sstevel@tonic-gate     voidpf opaque;
60427c478bd9Sstevel@tonic-gate     unsigned items;
60437c478bd9Sstevel@tonic-gate     unsigned size;
60447c478bd9Sstevel@tonic-gate {
60457c478bd9Sstevel@tonic-gate 	if (opaque) items += size - size;	/* make compiler happy */
60467c478bd9Sstevel@tonic-gate 	return ((voidpf)calloc(items, size));
60477c478bd9Sstevel@tonic-gate }
60487c478bd9Sstevel@tonic-gate 
60497c478bd9Sstevel@tonic-gate /*ARGSUSED*/
60507c478bd9Sstevel@tonic-gate void
zcfree(opaque,ptr)60517c478bd9Sstevel@tonic-gate zcfree(opaque, ptr)
60527c478bd9Sstevel@tonic-gate     voidpf opaque;
60537c478bd9Sstevel@tonic-gate     voidpf ptr;
60547c478bd9Sstevel@tonic-gate {
60557c478bd9Sstevel@tonic-gate 	free(ptr);
60567c478bd9Sstevel@tonic-gate }
60577c478bd9Sstevel@tonic-gate 
60587c478bd9Sstevel@tonic-gate #endif /* MY_ZCALLOC */
60597c478bd9Sstevel@tonic-gate /* --- zutil.c */
60607c478bd9Sstevel@tonic-gate 
60617c478bd9Sstevel@tonic-gate /* +++ adler32.c */
60627c478bd9Sstevel@tonic-gate /*
60637c478bd9Sstevel@tonic-gate  * adler32.c -- compute the Adler-32 checksum of a data stream
60647c478bd9Sstevel@tonic-gate  * Copyright (C) 1995-1998 Mark Adler
60657c478bd9Sstevel@tonic-gate  * For conditions of distribution and use, see copyright notice in zlib.h
60667c478bd9Sstevel@tonic-gate  */
60677c478bd9Sstevel@tonic-gate 
60687c478bd9Sstevel@tonic-gate /* From: adler32.c,v 1.10 1996/05/22 11:52:18 me Exp $ */
60697c478bd9Sstevel@tonic-gate 
60707c478bd9Sstevel@tonic-gate /* #include "zlib.h" */
60717c478bd9Sstevel@tonic-gate 
60727c478bd9Sstevel@tonic-gate #define	BASE 65521L /* largest prime smaller than 65536 */
60737c478bd9Sstevel@tonic-gate #define	NMAX 5552
60747c478bd9Sstevel@tonic-gate /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
60757c478bd9Sstevel@tonic-gate 
60767c478bd9Sstevel@tonic-gate #define	DO1(buf, i)  {s1 += buf[i]; s2 += s1; }
60777c478bd9Sstevel@tonic-gate #define	DO2(buf, i)  DO1(buf, i); DO1(buf, i+1);
60787c478bd9Sstevel@tonic-gate #define	DO4(buf, i)  DO2(buf, i); DO2(buf, i+2);
60797c478bd9Sstevel@tonic-gate #define	DO8(buf, i)  DO4(buf, i); DO4(buf, i+4);
60807c478bd9Sstevel@tonic-gate #define	DO16(buf)   DO8(buf, 0); DO8(buf, 8);
60817c478bd9Sstevel@tonic-gate 
60827c478bd9Sstevel@tonic-gate /* ========================================================================= */
60837c478bd9Sstevel@tonic-gate uLong
adler32(adler,buf,len)60847c478bd9Sstevel@tonic-gate adler32(adler, buf, len)
60857c478bd9Sstevel@tonic-gate     uLong adler;
60867c478bd9Sstevel@tonic-gate     const Bytef *buf;
60877c478bd9Sstevel@tonic-gate     uInt len;
60887c478bd9Sstevel@tonic-gate {
60897c478bd9Sstevel@tonic-gate 	unsigned long s1 = adler & 0xffff;
60907c478bd9Sstevel@tonic-gate 	unsigned long s2 = (adler >> 16) & 0xffff;
60917c478bd9Sstevel@tonic-gate 	int k;
60927c478bd9Sstevel@tonic-gate 
60937c478bd9Sstevel@tonic-gate 	if (buf == Z_NULL)
60947c478bd9Sstevel@tonic-gate 		return (1L);
60957c478bd9Sstevel@tonic-gate 
60967c478bd9Sstevel@tonic-gate 	while (len > 0) {
60977c478bd9Sstevel@tonic-gate 		k = len < NMAX ? len : NMAX;
60987c478bd9Sstevel@tonic-gate 		len -= k;
60997c478bd9Sstevel@tonic-gate 		while (k >= 16) {
61007c478bd9Sstevel@tonic-gate 			DO16(buf);
61017c478bd9Sstevel@tonic-gate 			buf += 16;
61027c478bd9Sstevel@tonic-gate 			k -= 16;
61037c478bd9Sstevel@tonic-gate 		}
61047c478bd9Sstevel@tonic-gate 		if (k != 0) do {
61057c478bd9Sstevel@tonic-gate 			s1 += *buf++;
61067c478bd9Sstevel@tonic-gate 			s2 += s1;
61077c478bd9Sstevel@tonic-gate 		} while (--k);
61087c478bd9Sstevel@tonic-gate 		s1 %= BASE;
61097c478bd9Sstevel@tonic-gate 		s2 %= BASE;
61107c478bd9Sstevel@tonic-gate 	}
61117c478bd9Sstevel@tonic-gate 	return ((s2 << 16) | s1);
61127c478bd9Sstevel@tonic-gate }
61137c478bd9Sstevel@tonic-gate /* --- adler32.c */
6114