annotate packages/net/ppp/current/src/zlib.c @ 1586:e0d88dc6a92f

Added PPP network support.
author nickg
date Wed, 14 Apr 2004 15:08:56 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1586
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1 //==========================================================================
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2 //
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3 // src/zlib.c
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4 //
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5 //==========================================================================
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
6 //####ECOSGPLCOPYRIGHTBEGIN####
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
7 // -------------------------------------------
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
8 // This file is part of eCos, the Embedded Configurable Operating System.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
9 // Portions created by Nick Garnett are
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
10 // Copyright (C) 2003 eCosCentric Ltd.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
11 //
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
12 // eCos is free software; you can redistribute it and/or modify it under
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
13 // the terms of the GNU General Public License as published by the Free
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
14 // Software Foundation; either version 2 or (at your option) any later version.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
15 //
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
16 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
17 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
18 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
19 // for more details.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
20 //
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
21 // You should have received a copy of the GNU General Public License along
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
22 // with eCos; if not, write to the Free Software Foundation, Inc.,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
23 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
24 //
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
25 // As a special exception, if other files instantiate templates or use macros
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
26 // or inline functions from this file, or you compile this file and link it
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
27 // with other works to produce a work based on this file, this file does not
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
28 // by itself cause the resulting work to be covered by the GNU General Public
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
29 // License. However the source code for this file must still be made available
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
30 // in accordance with section (3) of the GNU General Public License.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
31 //
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
32 // This exception does not invalidate any other reasons why a work based on
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
33 // this file might be covered by the GNU General Public License.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
34 //
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
35 // -------------------------------------------
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
36 //####ECOSGPLCOPYRIGHTEND####
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
37 //####BSDCOPYRIGHTBEGIN####
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
38 //
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
39 // -------------------------------------------
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
40 //
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
41 // Portions of this software may have been derived from OpenBSD,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
42 // FreeBSD or other sources, and are covered by the appropriate
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
43 // copyright disclaimers included herein.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
44 //
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
45 // -------------------------------------------
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
46 //
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
47 //####BSDCOPYRIGHTEND####
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
48 //==========================================================================
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
49
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
50 /*
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
51 * This file is derived from various .h and .c files from the zlib-1.0.4
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
52 * distribution by Jean-loup Gailly and Mark Adler, with some additions
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
53 * by Paul Mackerras to aid in implementing Deflate compression and
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
54 * decompression for PPP packets. See zlib.h for conditions of
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
55 * distribution and use.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
56 *
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
57 * Changes that have been made include:
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
58 * - added Z_PACKET_FLUSH (see zlib.h for details)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
59 * - added inflateIncomp and deflateOutputPending
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
60 * - allow strm->next_out to be NULL, meaning discard the output
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
61 *
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
62 * $FreeBSD: src/sys/net/zlib.c,v 1.10.6.2 2002/03/24 23:23:58 jedgar Exp $
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
63 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
64
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
65 /*
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
66 * ==FILEVERSION 971210==
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
67 *
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
68 * This marker is used by the Linux installation script to determine
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
69 * whether an up-to-date version of this file is already installed.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
70 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
71
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
72 #define _KERNEL
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
73
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
74 #define NO_DUMMY_DECL
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
75 #define NO_ZCFUNCS
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
76 #define MY_ZCALLOC
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
77
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
78 #if 1 //defined(__FreeBSD__) && defined(_KERNEL)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
79 #define inflate inflate_ppp /* FreeBSD already has an inflate :-( */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
80 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
81
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
82
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
83 /* +++ zutil.h */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
84 /* zutil.h -- internal interface and configuration of the compression library
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
85 * Copyright (C) 1995-1996 Jean-loup Gailly.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
86 * For conditions of distribution and use, see copyright notice in zlib.h
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
87 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
88
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
89 /* WARNING: this file should *not* be used by applications. It is
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
90 part of the implementation of the compression library and is
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
91 subject to change. Applications should only use zlib.h.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
92 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
93
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
94 /* From: zutil.h,v 1.16 1996/07/24 13:41:13 me Exp $ */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
95
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
96 #ifndef _Z_UTIL_H
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
97 #define _Z_UTIL_H
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
98
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
99 #ifdef _KERNEL
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
100 #include <cyg/ppp/net/zlib.h>
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
101 #else
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
102 #include "zlib.h"
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
103 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
104
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
105 #ifdef _KERNEL
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
106 /* Assume this is a *BSD or SVR4 kernel */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
107 #include <sys/param.h>
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
108 #include <sys/types.h>
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
109 #include <sys/time.h>
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
110 //#include <sys/systm.h>
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
111 #ifndef __ECOS
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
112 # define HAVE_MEMCPY
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
113 # define memcpy(d, s, n) bcopy((s), (d), (n))
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
114 # define memset(d, v, n) bzero((d), (n))
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
115 # define memcmp bcmp
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
116 #else
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
117 # include <string.h>
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
118 //# include <stdlib.h>
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
119 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
120
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
121 #else
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
122 #if defined(__KERNEL__)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
123 /* Assume this is a Linux kernel */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
124 #include <linux/string.h>
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
125 #define HAVE_MEMCPY
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
126
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
127 #else /* not kernel */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
128
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
129 #if defined(MSDOS)||defined(VMS)||defined(CRAY)||defined(WIN32)||defined(RISCOS)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
130 # include <stddef.h>
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
131 # include <errno.h>
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
132 #else
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
133 extern int errno;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
134 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
135 #ifdef STDC
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
136 # include <string.h>
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
137 # include <stdlib.h>
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
138 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
139 #endif /* __KERNEL__ */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
140 #endif /* _KERNEL */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
141
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
142 #ifndef local
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
143 # define local static
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
144 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
145 /* compile with -Dlocal if your debugger can't find static symbols */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
146
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
147 typedef unsigned char uch;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
148 typedef uch FAR uchf;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
149 typedef unsigned short ush;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
150 typedef ush FAR ushf;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
151 typedef unsigned long ulg;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
152
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
153 extern const char *z_errmsg[10]; /* indexed by 2-zlib_error */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
154 /* (size given to avoid silly warnings with Visual C++) */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
155
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
156 #define ERR_MSG(err) z_errmsg[Z_NEED_DICT-(err)]
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
157
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
158 #define ERR_RETURN(strm,err) \
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
159 return (strm->msg = (const char*)ERR_MSG(err), (err))
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
160 /* To be used only when the state is known to be valid */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
161
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
162 /* common constants */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
163
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
164 #ifndef DEF_WBITS
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
165 # define DEF_WBITS MAX_WBITS
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
166 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
167 /* default windowBits for decompression. MAX_WBITS is for compression only */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
168
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
169 #if MAX_MEM_LEVEL >= 8
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
170 # define DEF_MEM_LEVEL 8
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
171 #else
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
172 # define DEF_MEM_LEVEL MAX_MEM_LEVEL
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
173 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
174 /* default memLevel */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
175
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
176 #define STORED_BLOCK 0
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
177 #define STATIC_TREES 1
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
178 #define DYN_TREES 2
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
179 /* The three kinds of block type */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
180
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
181 #define MIN_MATCH 3
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
182 #define MAX_MATCH 258
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
183 /* The minimum and maximum match lengths */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
184
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
185 #define PRESET_DICT 0x20 /* preset dictionary flag in zlib header */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
186
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
187 /* target dependencies */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
188
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
189 #ifdef MSDOS
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
190 # define OS_CODE 0x00
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
191 # ifdef __TURBOC__
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
192 # include <alloc.h>
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
193 # else /* MSC or DJGPP */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
194 # include <malloc.h>
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
195 # endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
196 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
197
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
198 #ifdef OS2
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
199 # define OS_CODE 0x06
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
200 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
201
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
202 #ifdef WIN32 /* Window 95 & Windows NT */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
203 # define OS_CODE 0x0b
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
204 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
205
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
206 #if defined(VAXC) || defined(VMS)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
207 # define OS_CODE 0x02
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
208 # define FOPEN(name, mode) \
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
209 fopen((name), (mode), "mbc=60", "ctx=stm", "rfm=fix", "mrs=512")
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
210 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
211
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
212 #ifdef AMIGA
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
213 # define OS_CODE 0x01
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
214 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
215
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
216 #if defined(ATARI) || defined(atarist)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
217 # define OS_CODE 0x05
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
218 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
219
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
220 #ifdef MACOS
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
221 # define OS_CODE 0x07
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
222 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
223
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
224 #ifdef __50SERIES /* Prime/PRIMOS */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
225 # define OS_CODE 0x0F
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
226 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
227
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
228 #ifdef TOPS20
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
229 # define OS_CODE 0x0a
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
230 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
231
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
232 #if defined(_BEOS_) || defined(RISCOS)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
233 # define fdopen(fd,mode) NULL /* No fdopen() */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
234 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
235
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
236 /* Common defaults */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
237
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
238 #ifndef OS_CODE
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
239 # define OS_CODE 0x03 /* assume Unix */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
240 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
241
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
242 #ifndef FOPEN
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
243 # define FOPEN(name, mode) fopen((name), (mode))
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
244 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
245
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
246 /* functions */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
247
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
248 #ifdef HAVE_STRERROR
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
249 extern char *strerror OF((int));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
250 # define zstrerror(errnum) strerror(errnum)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
251 #else
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
252 # define zstrerror(errnum) ""
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
253 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
254
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
255 #if defined(pyr)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
256 # define NO_MEMCPY
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
257 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
258 #if (defined(M_I86SM) || defined(M_I86MM)) && !defined(_MSC_VER)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
259 /* Use our own functions for small and medium model with MSC <= 5.0.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
260 * You may have to use the same strategy for Borland C (untested).
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
261 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
262 # define NO_MEMCPY
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
263 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
264 #if defined(STDC) && !defined(HAVE_MEMCPY) && !defined(NO_MEMCPY)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
265 # define HAVE_MEMCPY
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
266 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
267 #ifdef HAVE_MEMCPY
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
268 # ifdef SMALL_MEDIUM /* MSDOS small or medium model */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
269 # define zmemcpy _fmemcpy
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
270 # define zmemcmp _fmemcmp
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
271 # define zmemzero(dest, len) _fmemset(dest, 0, len)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
272 # else
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
273 # define zmemcpy memcpy
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
274 # define zmemcmp memcmp
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
275 # define zmemzero(dest, len) memset(dest, 0, len)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
276 # endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
277 #else
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
278 extern void zmemcpy OF((Bytef* dest, Bytef* source, uInt len));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
279 extern int zmemcmp OF((Bytef* s1, Bytef* s2, uInt len));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
280 extern void zmemzero OF((Bytef* dest, uInt len));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
281 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
282
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
283 /* Diagnostic functions */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
284 #ifdef DEBUG_ZLIB
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
285 # include <stdio.h>
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
286 # ifndef verbose
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
287 # define verbose 0
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
288 # endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
289 extern void z_error OF((char *m));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
290 # define Assert(cond,msg) {if(!(cond)) z_error(msg);}
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
291 # define Trace(x) fprintf x
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
292 # define Tracev(x) {if (verbose) fprintf x ;}
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
293 # define Tracevv(x) {if (verbose>1) fprintf x ;}
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
294 # define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
295 # define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
296 #else
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
297 # define Assert(cond,msg)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
298 # define Trace(x)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
299 # define Tracev(x)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
300 # define Tracevv(x)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
301 # define Tracec(c,x)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
302 # define Tracecv(c,x)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
303 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
304
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
305
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
306 typedef uLong (*check_func) OF((uLong check, const Bytef *buf, uInt len));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
307
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
308 voidpf zcalloc OF((voidpf opaque, unsigned items, unsigned size));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
309 void zcfree OF((voidpf opaque, voidpf ptr));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
310
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
311 #define ZALLOC(strm, items, size) \
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
312 (*((strm)->zalloc))((strm)->opaque, (items), (size))
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
313 #define ZFREE(strm, addr) (*((strm)->zfree))((strm)->opaque, (voidpf)(addr))
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
314 #define TRY_FREE(s, p) {if (p) ZFREE(s, p);}
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
315
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
316 #endif /* _Z_UTIL_H */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
317 /* --- zutil.h */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
318
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
319 /* +++ deflate.h */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
320 /* deflate.h -- internal compression state
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
321 * Copyright (C) 1995-1996 Jean-loup Gailly
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
322 * For conditions of distribution and use, see copyright notice in zlib.h
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
323 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
324
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
325 /* WARNING: this file should *not* be used by applications. It is
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
326 part of the implementation of the compression library and is
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
327 subject to change. Applications should only use zlib.h.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
328 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
329
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
330 /* From: deflate.h,v 1.10 1996/07/02 12:41:00 me Exp $ */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
331
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
332 #ifndef _DEFLATE_H
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
333 #define _DEFLATE_H
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
334
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
335 /* #include "zutil.h" */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
336
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
337 /* ===========================================================================
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
338 * Internal compression state.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
339 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
340
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
341 #define LENGTH_CODES 29
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
342 /* number of length codes, not counting the special END_BLOCK code */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
343
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
344 #define LITERALS 256
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
345 /* number of literal bytes 0..255 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
346
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
347 #define L_CODES (LITERALS+1+LENGTH_CODES)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
348 /* number of Literal or Length codes, including the END_BLOCK code */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
349
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
350 #define D_CODES 30
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
351 /* number of distance codes */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
352
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
353 #define BL_CODES 19
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
354 /* number of codes used to transfer the bit lengths */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
355
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
356 #define HEAP_SIZE (2*L_CODES+1)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
357 /* maximum heap size */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
358
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
359 #define MAX_BITS 15
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
360 /* All codes must not exceed MAX_BITS bits */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
361
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
362 #define INIT_STATE 42
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
363 #define BUSY_STATE 113
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
364 #define FINISH_STATE 666
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
365 /* Stream status */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
366
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
367
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
368 /* Data structure describing a single value and its code string. */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
369 typedef struct ct_data_s {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
370 union {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
371 ush freq; /* frequency count */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
372 ush code; /* bit string */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
373 } fc;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
374 union {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
375 ush dad; /* father node in Huffman tree */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
376 ush len; /* length of bit string */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
377 } dl;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
378 } FAR ct_data;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
379
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
380 #define Freq fc.freq
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
381 #define Code fc.code
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
382 #define Dad dl.dad
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
383 #define Len dl.len
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
384
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
385 typedef struct static_tree_desc_s static_tree_desc;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
386
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
387 typedef struct tree_desc_s {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
388 ct_data *dyn_tree; /* the dynamic tree */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
389 int max_code; /* largest code with non zero frequency */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
390 static_tree_desc *stat_desc; /* the corresponding static tree */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
391 } FAR tree_desc;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
392
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
393 typedef ush Pos;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
394 typedef Pos FAR Posf;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
395 typedef unsigned IPos;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
396
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
397 /* A Pos is an index in the character window. We use short instead of int to
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
398 * save space in the various tables. IPos is used only for parameter passing.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
399 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
400
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
401 typedef struct deflate_state {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
402 z_streamp strm; /* pointer back to this zlib stream */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
403 int status; /* as the name implies */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
404 Bytef *pending_buf; /* output still pending */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
405 ulg pending_buf_size; /* size of pending_buf */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
406 Bytef *pending_out; /* next pending byte to output to the stream */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
407 int pending; /* nb of bytes in the pending buffer */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
408 int noheader; /* suppress zlib header and adler32 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
409 Byte data_type; /* UNKNOWN, BINARY or ASCII */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
410 Byte method; /* STORED (for zip only) or DEFLATED */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
411 int last_flush; /* value of flush param for previous deflate call */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
412
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
413 /* used by deflate.c: */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
414
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
415 uInt w_size; /* LZ77 window size (32K by default) */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
416 uInt w_bits; /* log2(w_size) (8..16) */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
417 uInt w_mask; /* w_size - 1 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
418
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
419 Bytef *window;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
420 /* Sliding window. Input bytes are read into the second half of the window,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
421 * and move to the first half later to keep a dictionary of at least wSize
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
422 * bytes. With this organization, matches are limited to a distance of
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
423 * wSize-MAX_MATCH bytes, but this ensures that IO is always
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
424 * performed with a length multiple of the block size. Also, it limits
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
425 * the window size to 64K, which is quite useful on MSDOS.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
426 * To do: use the user input buffer as sliding window.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
427 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
428
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
429 ulg window_size;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
430 /* Actual size of window: 2*wSize, except when the user input buffer
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
431 * is directly used as sliding window.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
432 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
433
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
434 Posf *prev;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
435 /* Link to older string with same hash index. To limit the size of this
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
436 * array to 64K, this link is maintained only for the last 32K strings.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
437 * An index in this array is thus a window index modulo 32K.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
438 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
439
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
440 Posf *head; /* Heads of the hash chains or NIL. */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
441
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
442 uInt ins_h; /* hash index of string to be inserted */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
443 uInt hash_size; /* number of elements in hash table */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
444 uInt hash_bits; /* log2(hash_size) */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
445 uInt hash_mask; /* hash_size-1 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
446
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
447 uInt hash_shift;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
448 /* Number of bits by which ins_h must be shifted at each input
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
449 * step. It must be such that after MIN_MATCH steps, the oldest
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
450 * byte no longer takes part in the hash key, that is:
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
451 * hash_shift * MIN_MATCH >= hash_bits
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
452 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
453
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
454 long block_start;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
455 /* Window position at the beginning of the current output block. Gets
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
456 * negative when the window is moved backwards.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
457 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
458
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
459 uInt match_length; /* length of best match */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
460 IPos prev_match; /* previous match */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
461 int match_available; /* set if previous match exists */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
462 uInt strstart; /* start of string to insert */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
463 uInt match_start; /* start of matching string */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
464 uInt lookahead; /* number of valid bytes ahead in window */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
465
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
466 uInt prev_length;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
467 /* Length of the best match at previous step. Matches not greater than this
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
468 * are discarded. This is used in the lazy match evaluation.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
469 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
470
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
471 uInt max_chain_length;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
472 /* To speed up deflation, hash chains are never searched beyond this
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
473 * length. A higher limit improves compression ratio but degrades the
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
474 * speed.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
475 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
476
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
477 uInt max_lazy_match;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
478 /* Attempt to find a better match only when the current match is strictly
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
479 * smaller than this value. This mechanism is used only for compression
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
480 * levels >= 4.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
481 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
482 # define max_insert_length max_lazy_match
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
483 /* Insert new strings in the hash table only if the match length is not
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
484 * greater than this length. This saves time but degrades compression.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
485 * max_insert_length is used only for compression levels <= 3.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
486 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
487
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
488 int level; /* compression level (1..9) */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
489 int strategy; /* favor or force Huffman coding*/
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
490
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
491 uInt good_match;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
492 /* Use a faster search when the previous match is longer than this */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
493
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
494 int nice_match; /* Stop searching when current match exceeds this */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
495
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
496 /* used by trees.c: */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
497 /* Didn't use ct_data typedef below to supress compiler warning */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
498 struct ct_data_s dyn_ltree[HEAP_SIZE]; /* literal and length tree */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
499 struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
500 struct ct_data_s bl_tree[2*BL_CODES+1]; /* Huffman tree for bit lengths */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
501
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
502 struct tree_desc_s l_desc; /* desc. for literal tree */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
503 struct tree_desc_s d_desc; /* desc. for distance tree */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
504 struct tree_desc_s bl_desc; /* desc. for bit length tree */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
505
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
506 ush bl_count[MAX_BITS+1];
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
507 /* number of codes at each bit length for an optimal tree */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
508
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
509 int heap[2*L_CODES+1]; /* heap used to build the Huffman trees */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
510 int heap_len; /* number of elements in the heap */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
511 int heap_max; /* element of largest frequency */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
512 /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
513 * The same heap array is used to build all trees.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
514 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
515
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
516 uch depth[2*L_CODES+1];
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
517 /* Depth of each subtree used as tie breaker for trees of equal frequency
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
518 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
519
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
520 uchf *l_buf; /* buffer for literals or lengths */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
521
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
522 uInt lit_bufsize;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
523 /* Size of match buffer for literals/lengths. There are 4 reasons for
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
524 * limiting lit_bufsize to 64K:
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
525 * - frequencies can be kept in 16 bit counters
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
526 * - if compression is not successful for the first block, all input
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
527 * data is still in the window so we can still emit a stored block even
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
528 * when input comes from standard input. (This can also be done for
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
529 * all blocks if lit_bufsize is not greater than 32K.)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
530 * - if compression is not successful for a file smaller than 64K, we can
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
531 * even emit a stored file instead of a stored block (saving 5 bytes).
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
532 * This is applicable only for zip (not gzip or zlib).
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
533 * - creating new Huffman trees less frequently may not provide fast
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
534 * adaptation to changes in the input data statistics. (Take for
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
535 * example a binary file with poorly compressible code followed by
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
536 * a highly compressible string table.) Smaller buffer sizes give
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
537 * fast adaptation but have of course the overhead of transmitting
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
538 * trees more frequently.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
539 * - I can't count above 4
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
540 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
541
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
542 uInt last_lit; /* running index in l_buf */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
543
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
544 ushf *d_buf;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
545 /* Buffer for distances. To simplify the code, d_buf and l_buf have
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
546 * the same number of elements. To use different lengths, an extra flag
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
547 * array would be necessary.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
548 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
549
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
550 ulg opt_len; /* bit length of current block with optimal trees */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
551 ulg static_len; /* bit length of current block with static trees */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
552 ulg compressed_len; /* total bit length of compressed file */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
553 uInt matches; /* number of string matches in current block */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
554 int last_eob_len; /* bit length of EOB code for last block */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
555
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
556 #ifdef DEBUG_ZLIB
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
557 ulg bits_sent; /* bit length of the compressed data */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
558 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
559
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
560 ush bi_buf;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
561 /* Output buffer. bits are inserted starting at the bottom (least
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
562 * significant bits).
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
563 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
564 int bi_valid;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
565 /* Number of valid bits in bi_buf. All bits above the last valid bit
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
566 * are always zero.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
567 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
568
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
569 } FAR deflate_state;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
570
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
571 /* Output a byte on the stream.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
572 * IN assertion: there is enough room in pending_buf.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
573 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
574 #define put_byte(s, c) {s->pending_buf[s->pending++] = (c);}
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
575
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
576
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
577 #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
578 /* Minimum amount of lookahead, except at the end of the input file.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
579 * See deflate.c for comments about the MIN_MATCH+1.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
580 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
581
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
582 #define MAX_DIST(s) ((s)->w_size-MIN_LOOKAHEAD)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
583 /* In order to simplify the code, particularly on 16 bit machines, match
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
584 * distances are limited to MAX_DIST instead of WSIZE.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
585 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
586
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
587 /* in trees.c */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
588 void _tr_init OF((deflate_state *s));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
589 int _tr_tally OF((deflate_state *s, unsigned dist, unsigned lc));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
590 ulg _tr_flush_block OF((deflate_state *s, charf *buf, ulg stored_len,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
591 int eof));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
592 void _tr_align OF((deflate_state *s));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
593 void _tr_stored_block OF((deflate_state *s, charf *buf, ulg stored_len,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
594 int eof));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
595 void _tr_stored_type_only OF((deflate_state *));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
596
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
597 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
598 /* --- deflate.h */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
599
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
600 /* +++ deflate.c */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
601 /* deflate.c -- compress data using the deflation algorithm
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
602 * Copyright (C) 1995-1996 Jean-loup Gailly.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
603 * For conditions of distribution and use, see copyright notice in zlib.h
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
604 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
605
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
606 /*
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
607 * ALGORITHM
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
608 *
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
609 * The "deflation" process depends on being able to identify portions
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
610 * of the input text which are identical to earlier input (within a
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
611 * sliding window trailing behind the input currently being processed).
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
612 *
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
613 * The most straightforward technique turns out to be the fastest for
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
614 * most input files: try all possible matches and select the longest.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
615 * The key feature of this algorithm is that insertions into the string
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
616 * dictionary are very simple and thus fast, and deletions are avoided
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
617 * completely. Insertions are performed at each input character, whereas
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
618 * string matches are performed only when the previous match ends. So it
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
619 * is preferable to spend more time in matches to allow very fast string
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
620 * insertions and avoid deletions. The matching algorithm for small
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
621 * strings is inspired from that of Rabin & Karp. A brute force approach
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
622 * is used to find longer strings when a small match has been found.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
623 * A similar algorithm is used in comic (by Jan-Mark Wams) and freeze
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
624 * (by Leonid Broukhis).
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
625 * A previous version of this file used a more sophisticated algorithm
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
626 * (by Fiala and Greene) which is guaranteed to run in linear amortized
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
627 * time, but has a larger average cost, uses more memory and is patented.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
628 * However the F&G algorithm may be faster for some highly redundant
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
629 * files if the parameter max_chain_length (described below) is too large.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
630 *
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
631 * ACKNOWLEDGEMENTS
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
632 *
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
633 * The idea of lazy evaluation of matches is due to Jan-Mark Wams, and
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
634 * I found it in 'freeze' written by Leonid Broukhis.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
635 * Thanks to many people for bug reports and testing.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
636 *
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
637 * REFERENCES
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
638 *
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
639 * Deutsch, L.P.,"DEFLATE Compressed Data Format Specification".
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
640 * Available in ftp://ds.internic.net/rfc/rfc1951.txt
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
641 *
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
642 * A description of the Rabin and Karp algorithm is given in the book
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
643 * "Algorithms" by R. Sedgewick, Addison-Wesley, p252.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
644 *
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
645 * Fiala,E.R., and Greene,D.H.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
646 * Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
647 *
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
648 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
649
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
650 /* From: deflate.c,v 1.15 1996/07/24 13:40:58 me Exp $ */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
651
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
652 /* #include "deflate.h" */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
653
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
654 char deflate_copyright[] = " deflate 1.0.4 Copyright 1995-1996 Jean-loup Gailly ";
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
655 /*
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
656 If you use the zlib library in a product, an acknowledgment is welcome
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
657 in the documentation of your product. If for some reason you cannot
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
658 include such an acknowledgment, I would appreciate that you keep this
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
659 copyright string in the executable of your product.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
660 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
661
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
662 /* ===========================================================================
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
663 * Function prototypes.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
664 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
665 typedef enum {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
666 need_more, /* block not completed, need more input or more output */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
667 block_done, /* block flush performed */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
668 finish_started, /* finish started, need only more output at next deflate */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
669 finish_done /* finish done, accept no more input or output */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
670 } block_state;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
671
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
672 typedef block_state (*compress_func) OF((deflate_state *s, int flush));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
673 /* Compression function. Returns the block state after the call. */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
674
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
675 local void fill_window OF((deflate_state *s));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
676 local block_state deflate_stored OF((deflate_state *s, int flush));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
677 local block_state deflate_fast OF((deflate_state *s, int flush));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
678 local block_state deflate_slow OF((deflate_state *s, int flush));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
679 local void lm_init OF((deflate_state *s));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
680 local void putShortMSB OF((deflate_state *s, uInt b));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
681 local void flush_pending OF((z_streamp strm));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
682 local int read_buf OF((z_streamp strm, charf *buf, unsigned size));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
683 #ifdef ASMV
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
684 void match_init OF((void)); /* asm code initialization */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
685 uInt longest_match OF((deflate_state *s, IPos cur_match));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
686 #else
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
687 local uInt longest_match OF((deflate_state *s, IPos cur_match));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
688 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
689
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
690 #ifdef DEBUG_ZLIB
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
691 local void check_match OF((deflate_state *s, IPos start, IPos match,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
692 int length));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
693 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
694
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
695 /* ===========================================================================
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
696 * Local data
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
697 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
698
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
699 #define NIL 0
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
700 /* Tail of hash chains */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
701
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
702 #ifndef TOO_FAR
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
703 # define TOO_FAR 4096
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
704 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
705 /* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
706
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
707 #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
708 /* Minimum amount of lookahead, except at the end of the input file.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
709 * See deflate.c for comments about the MIN_MATCH+1.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
710 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
711
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
712 /* Values for max_lazy_match, good_match and max_chain_length, depending on
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
713 * the desired pack level (0..9). The values given below have been tuned to
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
714 * exclude worst case performance for pathological files. Better values may be
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
715 * found for specific files.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
716 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
717 typedef struct config_s {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
718 ush good_length; /* reduce lazy search above this match length */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
719 ush max_lazy; /* do not perform lazy search above this match length */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
720 ush nice_length; /* quit search above this match length */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
721 ush max_chain;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
722 compress_func func;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
723 } config;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
724
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
725 local config configuration_table[10] = {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
726 /* good lazy nice chain */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
727 /* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
728 /* 1 */ {4, 4, 8, 4, deflate_fast}, /* maximum speed, no lazy matches */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
729 /* 2 */ {4, 5, 16, 8, deflate_fast},
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
730 /* 3 */ {4, 6, 32, 32, deflate_fast},
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
731
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
732 /* 4 */ {4, 4, 16, 16, deflate_slow}, /* lazy matches */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
733 /* 5 */ {8, 16, 32, 32, deflate_slow},
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
734 /* 6 */ {8, 16, 128, 128, deflate_slow},
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
735 /* 7 */ {8, 32, 128, 256, deflate_slow},
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
736 /* 8 */ {32, 128, 258, 1024, deflate_slow},
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
737 /* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* maximum compression */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
738
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
739 /* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
740 * For deflate_fast() (levels <= 3) good is ignored and lazy has a different
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
741 * meaning.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
742 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
743
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
744 #define EQUAL 0
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
745 /* result of memcmp for equal strings */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
746
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
747 #ifndef NO_DUMMY_DECL
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
748 struct static_tree_desc_s {int dummy;}; /* for buggy compilers */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
749 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
750
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
751 /* ===========================================================================
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
752 * Update a hash value with the given input byte
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
753 * IN assertion: all calls to to UPDATE_HASH are made with consecutive
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
754 * input characters, so that a running hash key can be computed from the
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
755 * previous key instead of complete recalculation each time.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
756 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
757 #define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
758
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
759
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
760 /* ===========================================================================
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
761 * Insert string str in the dictionary and set match_head to the previous head
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
762 * of the hash chain (the most recent string with same hash key). Return
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
763 * the previous length of the hash chain.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
764 * IN assertion: all calls to to INSERT_STRING are made with consecutive
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
765 * input characters and the first MIN_MATCH bytes of str are valid
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
766 * (except for the last MIN_MATCH-1 bytes of the input file).
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
767 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
768 #define INSERT_STRING(s, str, match_head) \
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
769 (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
770 s->prev[(str) & s->w_mask] = match_head = s->head[s->ins_h], \
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
771 s->head[s->ins_h] = (Pos)(str))
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
772
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
773 /* ===========================================================================
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
774 * Initialize the hash table (avoiding 64K overflow for 16 bit systems).
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
775 * prev[] will be initialized on the fly.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
776 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
777 #define CLEAR_HASH(s) \
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
778 s->head[s->hash_size-1] = NIL; \
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
779 zmemzero((charf *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
780
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
781 /* ========================================================================= */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
782 int deflateInit_(strm, level, version, stream_size)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
783 z_streamp strm;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
784 int level;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
785 const char *version;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
786 int stream_size;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
787 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
788 return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
789 Z_DEFAULT_STRATEGY, version, stream_size);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
790 /* To do: ignore strm->next_in if we use it as window */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
791 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
792
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
793 /* ========================================================================= */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
794 int deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
795 version, stream_size)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
796 z_streamp strm;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
797 int level;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
798 int method;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
799 int windowBits;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
800 int memLevel;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
801 int strategy;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
802 const char *version;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
803 int stream_size;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
804 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
805 deflate_state *s;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
806 int noheader = 0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
807 static char* my_version = ZLIB_VERSION;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
808
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
809 ushf *overlay;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
810 /* We overlay pending_buf and d_buf+l_buf. This works since the average
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
811 * output size for (length,distance) codes is <= 24 bits.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
812 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
813
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
814 if (version == Z_NULL || version[0] != my_version[0] ||
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
815 stream_size != sizeof(z_stream)) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
816 return Z_VERSION_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
817 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
818 if (strm == Z_NULL) return Z_STREAM_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
819
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
820 strm->msg = Z_NULL;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
821 #ifndef NO_ZCFUNCS
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
822 if (strm->zalloc == Z_NULL) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
823 strm->zalloc = zcalloc;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
824 strm->opaque = (voidpf)0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
825 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
826 if (strm->zfree == Z_NULL) strm->zfree = zcfree;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
827 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
828
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
829 if (level == Z_DEFAULT_COMPRESSION) level = 6;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
830
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
831 if (windowBits < 0) { /* undocumented feature: suppress zlib header */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
832 noheader = 1;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
833 windowBits = -windowBits;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
834 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
835 if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
836 windowBits < 9 || windowBits > 15 || level < 0 || level > 9 ||
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
837 strategy < 0 || strategy > Z_HUFFMAN_ONLY) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
838 return Z_STREAM_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
839 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
840 s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
841 if (s == Z_NULL) return Z_MEM_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
842 strm->state = (struct internal_state FAR *)s;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
843 s->strm = strm;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
844
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
845 s->noheader = noheader;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
846 s->w_bits = windowBits;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
847 s->w_size = 1 << s->w_bits;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
848 s->w_mask = s->w_size - 1;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
849
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
850 s->hash_bits = memLevel + 7;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
851 s->hash_size = 1 << s->hash_bits;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
852 s->hash_mask = s->hash_size - 1;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
853 s->hash_shift = ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
854
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
855 s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
856 s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
857 s->head = (Posf *) ZALLOC(strm, s->hash_size, sizeof(Pos));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
858
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
859 s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
860
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
861 overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
862 s->pending_buf = (uchf *) overlay;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
863 s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
864
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
865 if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
866 s->pending_buf == Z_NULL) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
867 strm->msg = (const char*)ERR_MSG(Z_MEM_ERROR);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
868 deflateEnd (strm);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
869 return Z_MEM_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
870 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
871 s->d_buf = overlay + s->lit_bufsize/sizeof(ush);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
872 s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
873
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
874 s->level = level;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
875 s->strategy = strategy;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
876 s->method = (Byte)method;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
877
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
878 return deflateReset(strm);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
879 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
880
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
881 /* ========================================================================= */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
882 int deflateSetDictionary (strm, dictionary, dictLength)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
883 z_streamp strm;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
884 const Bytef *dictionary;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
885 uInt dictLength;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
886 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
887 deflate_state *s;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
888 uInt length = dictLength;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
889 uInt n;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
890 IPos hash_head = 0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
891
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
892 if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
893 return Z_STREAM_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
894
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
895 s = (deflate_state *) strm->state;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
896 if (s->status != INIT_STATE) return Z_STREAM_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
897
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
898 strm->adler = adler32(strm->adler, dictionary, dictLength);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
899
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
900 if (length < MIN_MATCH) return Z_OK;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
901 if (length > MAX_DIST(s)) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
902 length = MAX_DIST(s);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
903 #ifndef USE_DICT_HEAD
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
904 dictionary += dictLength - length; /* use the tail of the dictionary */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
905 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
906 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
907 zmemcpy((charf *)s->window, dictionary, length);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
908 s->strstart = length;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
909 s->block_start = (long)length;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
910
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
911 /* Insert all strings in the hash table (except for the last two bytes).
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
912 * s->lookahead stays null, so s->ins_h will be recomputed at the next
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
913 * call of fill_window.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
914 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
915 s->ins_h = s->window[0];
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
916 UPDATE_HASH(s, s->ins_h, s->window[1]);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
917 for (n = 0; n <= length - MIN_MATCH; n++) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
918 INSERT_STRING(s, n, hash_head);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
919 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
920 if (hash_head) hash_head = 0; /* to make compiler happy */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
921 return Z_OK;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
922 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
923
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
924 /* ========================================================================= */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
925 int deflateReset (strm)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
926 z_streamp strm;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
927 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
928 deflate_state *s;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
929
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
930 if (strm == Z_NULL || strm->state == Z_NULL ||
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
931 strm->zalloc == Z_NULL || strm->zfree == Z_NULL) return Z_STREAM_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
932
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
933 strm->total_in = strm->total_out = 0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
934 strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
935 strm->data_type = Z_UNKNOWN;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
936
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
937 s = (deflate_state *)strm->state;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
938 s->pending = 0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
939 s->pending_out = s->pending_buf;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
940
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
941 if (s->noheader < 0) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
942 s->noheader = 0; /* was set to -1 by deflate(..., Z_FINISH); */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
943 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
944 s->status = s->noheader ? BUSY_STATE : INIT_STATE;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
945 strm->adler = 1;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
946 s->last_flush = Z_NO_FLUSH;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
947
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
948 _tr_init(s);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
949 lm_init(s);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
950
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
951 return Z_OK;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
952 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
953
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
954 /* ========================================================================= */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
955 int deflateParams(strm, level, strategy)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
956 z_streamp strm;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
957 int level;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
958 int strategy;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
959 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
960 deflate_state *s;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
961 compress_func func;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
962 int err = Z_OK;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
963
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
964 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
965 s = (deflate_state *) strm->state;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
966
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
967 if (level == Z_DEFAULT_COMPRESSION) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
968 level = 6;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
969 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
970 if (level < 0 || level > 9 || strategy < 0 || strategy > Z_HUFFMAN_ONLY) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
971 return Z_STREAM_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
972 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
973 func = configuration_table[s->level].func;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
974
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
975 if (func != configuration_table[level].func && strm->total_in != 0) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
976 /* Flush the last buffer: */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
977 err = deflate(strm, Z_PARTIAL_FLUSH);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
978 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
979 if (s->level != level) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
980 s->level = level;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
981 s->max_lazy_match = configuration_table[level].max_lazy;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
982 s->good_match = configuration_table[level].good_length;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
983 s->nice_match = configuration_table[level].nice_length;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
984 s->max_chain_length = configuration_table[level].max_chain;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
985 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
986 s->strategy = strategy;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
987 return err;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
988 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
989
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
990 /* =========================================================================
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
991 * Put a short in the pending buffer. The 16-bit value is put in MSB order.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
992 * IN assertion: the stream state is correct and there is enough room in
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
993 * pending_buf.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
994 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
995 local void putShortMSB (s, b)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
996 deflate_state *s;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
997 uInt b;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
998 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
999 put_byte(s, (Byte)(b >> 8));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1000 put_byte(s, (Byte)(b & 0xff));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1001 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1002
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1003 /* =========================================================================
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1004 * Flush as much pending output as possible. All deflate() output goes
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1005 * through this function so some applications may wish to modify it
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1006 * to avoid allocating a large strm->next_out buffer and copying into it.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1007 * (See also read_buf()).
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1008 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1009 local void flush_pending(strm)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1010 z_streamp strm;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1011 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1012 deflate_state *s = (deflate_state *) strm->state;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1013 unsigned len = s->pending;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1014
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1015 if (len > strm->avail_out) len = strm->avail_out;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1016 if (len == 0) return;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1017
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1018 if (strm->next_out != Z_NULL) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1019 zmemcpy(strm->next_out, s->pending_out, len);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1020 strm->next_out += len;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1021 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1022 s->pending_out += len;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1023 strm->total_out += len;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1024 strm->avail_out -= len;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1025 s->pending -= len;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1026 if (s->pending == 0) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1027 s->pending_out = s->pending_buf;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1028 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1029 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1030
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1031 /* ========================================================================= */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1032 int deflate (strm, flush)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1033 z_streamp strm;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1034 int flush;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1035 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1036 int old_flush; /* value of flush param for previous deflate call */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1037 deflate_state *s;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1038
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1039 if (strm == Z_NULL || strm->state == Z_NULL ||
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1040 flush > Z_FINISH || flush < 0) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1041 return Z_STREAM_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1042 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1043 s = (deflate_state *) strm->state;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1044
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1045 if ((strm->next_in == Z_NULL && strm->avail_in != 0) ||
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1046 (s->status == FINISH_STATE && flush != Z_FINISH)) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1047 ERR_RETURN(strm, Z_STREAM_ERROR);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1048 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1049 if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1050
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1051 s->strm = strm; /* just in case */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1052 old_flush = s->last_flush;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1053 s->last_flush = flush;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1054
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1055 /* Write the zlib header */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1056 if (s->status == INIT_STATE) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1057
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1058 uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1059 uInt level_flags = (s->level-1) >> 1;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1060
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1061 if (level_flags > 3) level_flags = 3;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1062 header |= (level_flags << 6);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1063 if (s->strstart != 0) header |= PRESET_DICT;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1064 header += 31 - (header % 31);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1065
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1066 s->status = BUSY_STATE;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1067 putShortMSB(s, header);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1068
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1069 /* Save the adler32 of the preset dictionary: */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1070 if (s->strstart != 0) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1071 putShortMSB(s, (uInt)(strm->adler >> 16));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1072 putShortMSB(s, (uInt)(strm->adler & 0xffff));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1073 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1074 strm->adler = 1L;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1075 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1076
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1077 /* Flush as much pending output as possible */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1078 if (s->pending != 0) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1079 flush_pending(strm);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1080 if (strm->avail_out == 0) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1081 /* Since avail_out is 0, deflate will be called again with
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1082 * more output space, but possibly with both pending and
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1083 * avail_in equal to zero. There won't be anything to do,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1084 * but this is not an error situation so make sure we
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1085 * return OK instead of BUF_ERROR at next call of deflate:
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1086 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1087 s->last_flush = -1;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1088 return Z_OK;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1089 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1090
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1091 /* Make sure there is something to do and avoid duplicate consecutive
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1092 * flushes. For repeated and useless calls with Z_FINISH, we keep
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1093 * returning Z_STREAM_END instead of Z_BUFF_ERROR.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1094 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1095 } else if (strm->avail_in == 0 && flush <= old_flush &&
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1096 flush != Z_FINISH) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1097 ERR_RETURN(strm, Z_BUF_ERROR);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1098 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1099
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1100 /* User must not provide more input after the first FINISH: */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1101 if (s->status == FINISH_STATE && strm->avail_in != 0) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1102 ERR_RETURN(strm, Z_BUF_ERROR);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1103 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1104
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1105 /* Start a new block or continue the current one.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1106 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1107 if (strm->avail_in != 0 || s->lookahead != 0 ||
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1108 (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1109 block_state bstate;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1110
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1111 bstate = (*(configuration_table[s->level].func))(s, flush);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1112
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1113 if (bstate == finish_started || bstate == finish_done) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1114 s->status = FINISH_STATE;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1115 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1116 if (bstate == need_more || bstate == finish_started) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1117 if (strm->avail_out == 0) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1118 s->last_flush = -1; /* avoid BUF_ERROR next call, see above */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1119 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1120 return Z_OK;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1121 /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1122 * of deflate should use the same flush parameter to make sure
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1123 * that the flush is complete. So we don't have to output an
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1124 * empty block here, this will be done at next call. This also
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1125 * ensures that for a very small output buffer, we emit at most
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1126 * one empty block.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1127 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1128 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1129 if (bstate == block_done) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1130 if (flush == Z_PARTIAL_FLUSH) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1131 _tr_align(s);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1132 } else if (flush == Z_PACKET_FLUSH) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1133 /* Output just the 3-bit `stored' block type value,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1134 but not a zero length. */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1135 _tr_stored_type_only(s);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1136 } else { /* FULL_FLUSH or SYNC_FLUSH */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1137 _tr_stored_block(s, (char*)0, 0L, 0);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1138 /* For a full flush, this empty block will be recognized
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1139 * as a special marker by inflate_sync().
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1140 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1141 if (flush == Z_FULL_FLUSH) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1142 CLEAR_HASH(s); /* forget history */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1143 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1144 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1145 flush_pending(strm);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1146 if (strm->avail_out == 0) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1147 s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1148 return Z_OK;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1149 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1150 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1151 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1152 Assert(strm->avail_out > 0, "bug2");
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1153
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1154 if (flush != Z_FINISH) return Z_OK;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1155 if (s->noheader) return Z_STREAM_END;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1156
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1157 /* Write the zlib trailer (adler32) */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1158 putShortMSB(s, (uInt)(strm->adler >> 16));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1159 putShortMSB(s, (uInt)(strm->adler & 0xffff));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1160 flush_pending(strm);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1161 /* If avail_out is zero, the application will call deflate again
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1162 * to flush the rest.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1163 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1164 s->noheader = -1; /* write the trailer only once! */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1165 return s->pending != 0 ? Z_OK : Z_STREAM_END;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1166 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1167
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1168 /* ========================================================================= */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1169 int deflateEnd (strm)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1170 z_streamp strm;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1171 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1172 int status;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1173 deflate_state *s;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1174
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1175 if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1176 s = (deflate_state *) strm->state;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1177
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1178 status = s->status;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1179 if (status != INIT_STATE && status != BUSY_STATE &&
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1180 status != FINISH_STATE) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1181 return Z_STREAM_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1182 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1183
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1184 /* Deallocate in reverse order of allocations: */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1185 TRY_FREE(strm, s->pending_buf);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1186 TRY_FREE(strm, s->head);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1187 TRY_FREE(strm, s->prev);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1188 TRY_FREE(strm, s->window);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1189
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1190 ZFREE(strm, s);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1191 strm->state = Z_NULL;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1192
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1193 return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1194 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1195
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1196 /* =========================================================================
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1197 * Copy the source state to the destination state.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1198 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1199 int deflateCopy (dest, source)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1200 z_streamp dest;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1201 z_streamp source;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1202 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1203 deflate_state *ds;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1204 deflate_state *ss;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1205 ushf *overlay;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1206
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1207 if (source == Z_NULL || dest == Z_NULL || source->state == Z_NULL)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1208 return Z_STREAM_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1209 ss = (deflate_state *) source->state;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1210
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1211 zmemcpy(dest, source, sizeof(*dest));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1212
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1213 ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1214 if (ds == Z_NULL) return Z_MEM_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1215 dest->state = (struct internal_state FAR *) ds;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1216 zmemcpy(ds, ss, sizeof(*ds));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1217 ds->strm = dest;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1218
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1219 ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1220 ds->prev = (Posf *) ZALLOC(dest, ds->w_size, sizeof(Pos));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1221 ds->head = (Posf *) ZALLOC(dest, ds->hash_size, sizeof(Pos));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1222 overlay = (ushf *) ZALLOC(dest, ds->lit_bufsize, sizeof(ush)+2);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1223 ds->pending_buf = (uchf *) overlay;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1224
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1225 if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL ||
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1226 ds->pending_buf == Z_NULL) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1227 deflateEnd (dest);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1228 return Z_MEM_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1229 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1230 /* ??? following zmemcpy doesn't work for 16-bit MSDOS */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1231 zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1232 zmemcpy(ds->prev, ss->prev, ds->w_size * sizeof(Pos));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1233 zmemcpy(ds->head, ss->head, ds->hash_size * sizeof(Pos));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1234 zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1235
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1236 ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1237 ds->d_buf = overlay + ds->lit_bufsize/sizeof(ush);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1238 ds->l_buf = ds->pending_buf + (1+sizeof(ush))*ds->lit_bufsize;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1239
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1240 ds->l_desc.dyn_tree = ds->dyn_ltree;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1241 ds->d_desc.dyn_tree = ds->dyn_dtree;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1242 ds->bl_desc.dyn_tree = ds->bl_tree;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1243
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1244 return Z_OK;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1245 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1246
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1247 /* ===========================================================================
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1248 * Return the number of bytes of output which are immediately available
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1249 * for output from the decompressor.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1250 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1251 int deflateOutputPending (strm)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1252 z_streamp strm;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1253 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1254 if (strm == Z_NULL || strm->state == Z_NULL) return 0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1255
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1256 return ((deflate_state *)(strm->state))->pending;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1257 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1258
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1259 /* ===========================================================================
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1260 * Read a new buffer from the current input stream, update the adler32
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1261 * and total number of bytes read. All deflate() input goes through
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1262 * this function so some applications may wish to modify it to avoid
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1263 * allocating a large strm->next_in buffer and copying from it.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1264 * (See also flush_pending()).
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1265 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1266 local int read_buf(strm, buf, size)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1267 z_streamp strm;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1268 charf *buf;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1269 unsigned size;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1270 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1271 unsigned len = strm->avail_in;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1272
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1273 if (len > size) len = size;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1274 if (len == 0) return 0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1275
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1276 strm->avail_in -= len;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1277
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1278 if (!((deflate_state *)(strm->state))->noheader) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1279 strm->adler = adler32(strm->adler, strm->next_in, len);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1280 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1281 zmemcpy(buf, strm->next_in, len);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1282 strm->next_in += len;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1283 strm->total_in += len;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1284
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1285 return (int)len;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1286 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1287
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1288 /* ===========================================================================
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1289 * Initialize the "longest match" routines for a new zlib stream
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1290 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1291 local void lm_init (s)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1292 deflate_state *s;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1293 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1294 s->window_size = (ulg)2L*s->w_size;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1295
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1296 CLEAR_HASH(s);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1297
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1298 /* Set the default configuration parameters:
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1299 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1300 s->max_lazy_match = configuration_table[s->level].max_lazy;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1301 s->good_match = configuration_table[s->level].good_length;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1302 s->nice_match = configuration_table[s->level].nice_length;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1303 s->max_chain_length = configuration_table[s->level].max_chain;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1304
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1305 s->strstart = 0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1306 s->block_start = 0L;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1307 s->lookahead = 0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1308 s->match_length = s->prev_length = MIN_MATCH-1;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1309 s->match_available = 0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1310 s->ins_h = 0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1311 #ifdef ASMV
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1312 match_init(); /* initialize the asm code */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1313 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1314 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1315
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1316 /* ===========================================================================
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1317 * Set match_start to the longest match starting at the given string and
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1318 * return its length. Matches shorter or equal to prev_length are discarded,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1319 * in which case the result is equal to prev_length and match_start is
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1320 * garbage.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1321 * IN assertions: cur_match is the head of the hash chain for the current
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1322 * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1323 * OUT assertion: the match length is not greater than s->lookahead.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1324 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1325 #ifndef ASMV
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1326 /* For 80x86 and 680x0, an optimized version will be provided in match.asm or
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1327 * match.S. The code will be functionally equivalent.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1328 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1329 local uInt longest_match(s, cur_match)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1330 deflate_state *s;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1331 IPos cur_match; /* current match */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1332 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1333 unsigned chain_length = s->max_chain_length;/* max hash chain length */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1334 register Bytef *scan = s->window + s->strstart; /* current string */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1335 register Bytef *match; /* matched string */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1336 register int len; /* length of current match */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1337 int best_len = s->prev_length; /* best match length so far */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1338 int nice_match = s->nice_match; /* stop if match long enough */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1339 IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1340 s->strstart - (IPos)MAX_DIST(s) : NIL;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1341 /* Stop when cur_match becomes <= limit. To simplify the code,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1342 * we prevent matches with the string of window index 0.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1343 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1344 Posf *prev = s->prev;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1345 uInt wmask = s->w_mask;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1346
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1347 #ifdef UNALIGNED_OK
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1348 /* Compare two bytes at a time. Note: this is not always beneficial.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1349 * Try with and without -DUNALIGNED_OK to check.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1350 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1351 register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1352 register ush scan_start = *(ushf*)scan;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1353 register ush scan_end = *(ushf*)(scan+best_len-1);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1354 #else
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1355 register Bytef *strend = s->window + s->strstart + MAX_MATCH;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1356 register Byte scan_end1 = scan[best_len-1];
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1357 register Byte scan_end = scan[best_len];
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1358 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1359
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1360 /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1361 * It is easy to get rid of this optimization if necessary.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1362 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1363 Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1364
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1365 /* Do not waste too much time if we already have a good match: */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1366 if (s->prev_length >= s->good_match) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1367 chain_length >>= 2;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1368 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1369 /* Do not look for matches beyond the end of the input. This is necessary
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1370 * to make deflate deterministic.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1371 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1372 if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1373
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1374 Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1375
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1376 do {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1377 Assert(cur_match < s->strstart, "no future");
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1378 match = s->window + cur_match;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1379
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1380 /* Skip to next match if the match length cannot increase
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1381 * or if the match length is less than 2:
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1382 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1383 #if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1384 /* This code assumes sizeof(unsigned short) == 2. Do not use
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1385 * UNALIGNED_OK if your compiler uses a different size.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1386 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1387 if (*(ushf*)(match+best_len-1) != scan_end ||
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1388 *(ushf*)match != scan_start) continue;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1389
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1390 /* It is not necessary to compare scan[2] and match[2] since they are
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1391 * always equal when the other bytes match, given that the hash keys
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1392 * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1393 * strstart+3, +5, ... up to strstart+257. We check for insufficient
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1394 * lookahead only every 4th comparison; the 128th check will be made
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1395 * at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1396 * necessary to put more guard bytes at the end of the window, or
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1397 * to check more often for insufficient lookahead.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1398 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1399 Assert(scan[2] == match[2], "scan[2]?");
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1400 scan++, match++;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1401 do {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1402 } while (*(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1403 *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1404 *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1405 *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1406 scan < strend);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1407 /* The funny "do {}" generates better code on most compilers */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1408
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1409 /* Here, scan <= window+strstart+257 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1410 Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1411 if (*scan == *match) scan++;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1412
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1413 len = (MAX_MATCH - 1) - (int)(strend-scan);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1414 scan = strend - (MAX_MATCH-1);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1415
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1416 #else /* UNALIGNED_OK */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1417
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1418 if (match[best_len] != scan_end ||
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1419 match[best_len-1] != scan_end1 ||
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1420 *match != *scan ||
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1421 *++match != scan[1]) continue;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1422
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1423 /* The check at best_len-1 can be removed because it will be made
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1424 * again later. (This heuristic is not always a win.)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1425 * It is not necessary to compare scan[2] and match[2] since they
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1426 * are always equal when the other bytes match, given that
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1427 * the hash keys are equal and that HASH_BITS >= 8.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1428 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1429 scan += 2, match++;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1430 Assert(*scan == *match, "match[2]?");
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1431
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1432 /* We check for insufficient lookahead only every 8th comparison;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1433 * the 256th check will be made at strstart+258.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1434 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1435 do {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1436 } while (*++scan == *++match && *++scan == *++match &&
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1437 *++scan == *++match && *++scan == *++match &&
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1438 *++scan == *++match && *++scan == *++match &&
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1439 *++scan == *++match && *++scan == *++match &&
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1440 scan < strend);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1441
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1442 Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1443
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1444 len = MAX_MATCH - (int)(strend - scan);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1445 scan = strend - MAX_MATCH;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1446
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1447 #endif /* UNALIGNED_OK */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1448
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1449 if (len > best_len) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1450 s->match_start = cur_match;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1451 best_len = len;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1452 if (len >= nice_match) break;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1453 #ifdef UNALIGNED_OK
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1454 scan_end = *(ushf*)(scan+best_len-1);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1455 #else
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1456 scan_end1 = scan[best_len-1];
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1457 scan_end = scan[best_len];
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1458 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1459 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1460 } while ((cur_match = prev[cur_match & wmask]) > limit
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1461 && --chain_length != 0);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1462
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1463 if ((uInt)best_len <= s->lookahead) return best_len;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1464 return s->lookahead;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1465 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1466 #endif /* ASMV */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1467
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1468 #ifdef DEBUG_ZLIB
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1469 /* ===========================================================================
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1470 * Check that the match at match_start is indeed a match.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1471 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1472 local void check_match(s, start, match, length)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1473 deflate_state *s;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1474 IPos start, match;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1475 int length;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1476 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1477 /* check that the match is indeed a match */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1478 if (zmemcmp((charf *)s->window + match,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1479 (charf *)s->window + start, length) != EQUAL) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1480 fprintf(stderr, " start %u, match %u, length %d\n",
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1481 start, match, length);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1482 do {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1483 fprintf(stderr, "%c%c", s->window[match++], s->window[start++]);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1484 } while (--length != 0);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1485 z_error("invalid match");
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1486 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1487 if (z_verbose > 1) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1488 fprintf(stderr,"\\[%d,%d]", start-match, length);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1489 do { putc(s->window[start++], stderr); } while (--length != 0);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1490 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1491 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1492 #else
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1493 # define check_match(s, start, match, length)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1494 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1495
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1496 /* ===========================================================================
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1497 * Fill the window when the lookahead becomes insufficient.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1498 * Updates strstart and lookahead.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1499 *
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1500 * IN assertion: lookahead < MIN_LOOKAHEAD
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1501 * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1502 * At least one byte has been read, or avail_in == 0; reads are
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1503 * performed for at least two bytes (required for the zip translate_eol
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1504 * option -- not supported here).
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1505 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1506 local void fill_window(s)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1507 deflate_state *s;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1508 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1509 register unsigned n, m;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1510 register Posf *p;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1511 unsigned more; /* Amount of free space at the end of the window. */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1512 uInt wsize = s->w_size;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1513
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1514 do {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1515 more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1516
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1517 /* Deal with !@#$% 64K limit: */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1518 if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1519 more = wsize;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1520
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1521 } else if (more == (unsigned)(-1)) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1522 /* Very unlikely, but possible on 16 bit machine if strstart == 0
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1523 * and lookahead == 1 (input done one byte at time)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1524 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1525 more--;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1526
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1527 /* If the window is almost full and there is insufficient lookahead,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1528 * move the upper half to the lower one to make room in the upper half.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1529 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1530 } else if (s->strstart >= wsize+MAX_DIST(s)) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1531
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1532 zmemcpy((charf *)s->window, (charf *)s->window+wsize,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1533 (unsigned)wsize);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1534 s->match_start -= wsize;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1535 s->strstart -= wsize; /* we now have strstart >= MAX_DIST */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1536 s->block_start -= (long) wsize;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1537
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1538 /* Slide the hash table (could be avoided with 32 bit values
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1539 at the expense of memory usage). We slide even when level == 0
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1540 to keep the hash table consistent if we switch back to level > 0
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1541 later. (Using level 0 permanently is not an optimal usage of
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1542 zlib, so we don't care about this pathological case.)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1543 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1544 n = s->hash_size;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1545 p = &s->head[n];
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1546 do {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1547 m = *--p;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1548 *p = (Pos)(m >= wsize ? m-wsize : NIL);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1549 } while (--n);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1550
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1551 n = wsize;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1552 p = &s->prev[n];
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1553 do {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1554 m = *--p;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1555 *p = (Pos)(m >= wsize ? m-wsize : NIL);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1556 /* If n is not on any hash chain, prev[n] is garbage but
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1557 * its value will never be used.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1558 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1559 } while (--n);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1560 more += wsize;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1561 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1562 if (s->strm->avail_in == 0) return;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1563
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1564 /* If there was no sliding:
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1565 * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1566 * more == window_size - lookahead - strstart
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1567 * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1568 * => more >= window_size - 2*WSIZE + 2
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1569 * In the BIG_MEM or MMAP case (not yet supported),
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1570 * window_size == input_size + MIN_LOOKAHEAD &&
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1571 * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1572 * Otherwise, window_size == 2*WSIZE so more >= 2.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1573 * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1574 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1575 Assert(more >= 2, "more < 2");
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1576
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1577 n = read_buf(s->strm, (charf *)s->window + s->strstart + s->lookahead,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1578 more);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1579 s->lookahead += n;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1580
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1581 /* Initialize the hash value now that we have some input: */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1582 if (s->lookahead >= MIN_MATCH) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1583 s->ins_h = s->window[s->strstart];
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1584 UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1585 #if MIN_MATCH != 3
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1586 Call UPDATE_HASH() MIN_MATCH-3 more times
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1587 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1588 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1589 /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1590 * but this is not important since only literal bytes will be emitted.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1591 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1592
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1593 } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1594 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1595
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1596 /* ===========================================================================
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1597 * Flush the current block, with given end-of-file flag.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1598 * IN assertion: strstart is set to the end of the current match.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1599 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1600 #define FLUSH_BLOCK_ONLY(s, eof) { \
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1601 _tr_flush_block(s, (s->block_start >= 0L ? \
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1602 (charf *)&s->window[(unsigned)s->block_start] : \
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1603 (charf *)Z_NULL), \
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1604 (ulg)((long)s->strstart - s->block_start), \
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1605 (eof)); \
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1606 s->block_start = s->strstart; \
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1607 flush_pending(s->strm); \
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1608 Tracev((stderr,"[FLUSH]")); \
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1609 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1610
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1611 /* Same but force premature exit if necessary. */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1612 #define FLUSH_BLOCK(s, eof) { \
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1613 FLUSH_BLOCK_ONLY(s, eof); \
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1614 if (s->strm->avail_out == 0) return (eof) ? finish_started : need_more; \
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1615 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1616
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1617 /* ===========================================================================
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1618 * Copy without compression as much as possible from the input stream, return
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1619 * the current block state.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1620 * This function does not insert new strings in the dictionary since
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1621 * uncompressible data is probably not useful. This function is used
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1622 * only for the level=0 compression option.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1623 * NOTE: this function should be optimized to avoid extra copying from
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1624 * window to pending_buf.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1625 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1626 local block_state deflate_stored(s, flush)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1627 deflate_state *s;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1628 int flush;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1629 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1630 /* Stored blocks are limited to 0xffff bytes, pending_buf is limited
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1631 * to pending_buf_size, and each stored block has a 5 byte header:
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1632 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1633 ulg max_block_size = 0xffff;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1634 ulg max_start;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1635
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1636 if (max_block_size > s->pending_buf_size - 5) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1637 max_block_size = s->pending_buf_size - 5;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1638 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1639
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1640 /* Copy as much as possible from input to output: */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1641 for (;;) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1642 /* Fill the window as much as possible: */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1643 if (s->lookahead <= 1) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1644
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1645 Assert(s->strstart < s->w_size+MAX_DIST(s) ||
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1646 s->block_start >= (long)s->w_size, "slide too late");
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1647
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1648 fill_window(s);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1649 if (s->lookahead == 0 && flush == Z_NO_FLUSH) return need_more;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1650
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1651 if (s->lookahead == 0) break; /* flush the current block */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1652 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1653 Assert(s->block_start >= 0L, "block gone");
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1654
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1655 s->strstart += s->lookahead;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1656 s->lookahead = 0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1657
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1658 /* Emit a stored block if pending_buf will be full: */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1659 max_start = s->block_start + max_block_size;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1660 if (s->strstart == 0 || (ulg)s->strstart >= max_start) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1661 /* strstart == 0 is possible when wraparound on 16-bit machine */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1662 s->lookahead = (uInt)(s->strstart - max_start);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1663 s->strstart = (uInt)max_start;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1664 FLUSH_BLOCK(s, 0);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1665 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1666 /* Flush if we may have to slide, otherwise block_start may become
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1667 * negative and the data will be gone:
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1668 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1669 if (s->strstart - (uInt)s->block_start >= MAX_DIST(s)) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1670 FLUSH_BLOCK(s, 0);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1671 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1672 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1673 FLUSH_BLOCK(s, flush == Z_FINISH);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1674 return flush == Z_FINISH ? finish_done : block_done;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1675 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1676
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1677 /* ===========================================================================
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1678 * Compress as much as possible from the input stream, return the current
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1679 * block state.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1680 * This function does not perform lazy evaluation of matches and inserts
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1681 * new strings in the dictionary only for unmatched strings or for short
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1682 * matches. It is used only for the fast compression options.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1683 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1684 local block_state deflate_fast(s, flush)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1685 deflate_state *s;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1686 int flush;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1687 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1688 IPos hash_head = NIL; /* head of the hash chain */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1689 int bflush; /* set if current block must be flushed */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1690
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1691 for (;;) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1692 /* Make sure that we always have enough lookahead, except
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1693 * at the end of the input file. We need MAX_MATCH bytes
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1694 * for the next match, plus MIN_MATCH bytes to insert the
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1695 * string following the next match.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1696 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1697 if (s->lookahead < MIN_LOOKAHEAD) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1698 fill_window(s);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1699 if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1700 return need_more;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1701 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1702 if (s->lookahead == 0) break; /* flush the current block */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1703 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1704
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1705 /* Insert the string window[strstart .. strstart+2] in the
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1706 * dictionary, and set hash_head to the head of the hash chain:
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1707 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1708 if (s->lookahead >= MIN_MATCH) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1709 INSERT_STRING(s, s->strstart, hash_head);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1710 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1711
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1712 /* Find the longest match, discarding those <= prev_length.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1713 * At this point we have always match_length < MIN_MATCH
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1714 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1715 if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1716 /* To simplify the code, we prevent matches with the string
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1717 * of window index 0 (in particular we have to avoid a match
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1718 * of the string with itself at the start of the input file).
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1719 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1720 if (s->strategy != Z_HUFFMAN_ONLY) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1721 s->match_length = longest_match (s, hash_head);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1722 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1723 /* longest_match() sets match_start */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1724 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1725 if (s->match_length >= MIN_MATCH) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1726 check_match(s, s->strstart, s->match_start, s->match_length);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1727
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1728 bflush = _tr_tally(s, s->strstart - s->match_start,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1729 s->match_length - MIN_MATCH);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1730
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1731 s->lookahead -= s->match_length;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1732
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1733 /* Insert new strings in the hash table only if the match length
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1734 * is not too large. This saves time but degrades compression.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1735 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1736 if (s->match_length <= s->max_insert_length &&
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1737 s->lookahead >= MIN_MATCH) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1738 s->match_length--; /* string at strstart already in hash table */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1739 do {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1740 s->strstart++;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1741 INSERT_STRING(s, s->strstart, hash_head);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1742 /* strstart never exceeds WSIZE-MAX_MATCH, so there are
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1743 * always MIN_MATCH bytes ahead.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1744 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1745 } while (--s->match_length != 0);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1746 s->strstart++;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1747 } else {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1748 s->strstart += s->match_length;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1749 s->match_length = 0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1750 s->ins_h = s->window[s->strstart];
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1751 UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1752 #if MIN_MATCH != 3
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1753 Call UPDATE_HASH() MIN_MATCH-3 more times
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1754 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1755 /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1756 * matter since it will be recomputed at next deflate call.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1757 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1758 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1759 } else {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1760 /* No match, output a literal byte */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1761 Tracevv((stderr,"%c", s->window[s->strstart]));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1762 bflush = _tr_tally (s, 0, s->window[s->strstart]);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1763 s->lookahead--;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1764 s->strstart++;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1765 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1766 if (bflush) FLUSH_BLOCK(s, 0);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1767 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1768 FLUSH_BLOCK(s, flush == Z_FINISH);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1769 return flush == Z_FINISH ? finish_done : block_done;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1770 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1771
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1772 /* ===========================================================================
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1773 * Same as above, but achieves better compression. We use a lazy
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1774 * evaluation for matches: a match is finally adopted only if there is
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1775 * no better match at the next window position.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1776 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1777 local block_state deflate_slow(s, flush)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1778 deflate_state *s;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1779 int flush;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1780 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1781 IPos hash_head = NIL; /* head of hash chain */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1782 int bflush; /* set if current block must be flushed */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1783
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1784 /* Process the input block. */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1785 for (;;) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1786 /* Make sure that we always have enough lookahead, except
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1787 * at the end of the input file. We need MAX_MATCH bytes
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1788 * for the next match, plus MIN_MATCH bytes to insert the
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1789 * string following the next match.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1790 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1791 if (s->lookahead < MIN_LOOKAHEAD) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1792 fill_window(s);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1793 if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1794 return need_more;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1795 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1796 if (s->lookahead == 0) break; /* flush the current block */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1797 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1798
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1799 /* Insert the string window[strstart .. strstart+2] in the
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1800 * dictionary, and set hash_head to the head of the hash chain:
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1801 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1802 if (s->lookahead >= MIN_MATCH) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1803 INSERT_STRING(s, s->strstart, hash_head);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1804 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1805
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1806 /* Find the longest match, discarding those <= prev_length.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1807 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1808 s->prev_length = s->match_length, s->prev_match = s->match_start;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1809 s->match_length = MIN_MATCH-1;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1810
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1811 if (hash_head != NIL && s->prev_length < s->max_lazy_match &&
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1812 s->strstart - hash_head <= MAX_DIST(s)) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1813 /* To simplify the code, we prevent matches with the string
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1814 * of window index 0 (in particular we have to avoid a match
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1815 * of the string with itself at the start of the input file).
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1816 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1817 if (s->strategy != Z_HUFFMAN_ONLY) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1818 s->match_length = longest_match (s, hash_head);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1819 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1820 /* longest_match() sets match_start */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1821
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1822 if (s->match_length <= 5 && (s->strategy == Z_FILTERED ||
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1823 (s->match_length == MIN_MATCH &&
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1824 s->strstart - s->match_start > TOO_FAR))) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1825
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1826 /* If prev_match is also MIN_MATCH, match_start is garbage
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1827 * but we will ignore the current match anyway.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1828 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1829 s->match_length = MIN_MATCH-1;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1830 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1831 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1832 /* If there was a match at the previous step and the current
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1833 * match is not better, output the previous match:
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1834 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1835 if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1836 uInt max_insert = s->strstart + s->lookahead - MIN_MATCH;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1837 /* Do not insert strings in hash table beyond this. */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1838
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1839 check_match(s, s->strstart-1, s->prev_match, s->prev_length);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1840
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1841 bflush = _tr_tally(s, s->strstart -1 - s->prev_match,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1842 s->prev_length - MIN_MATCH);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1843
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1844 /* Insert in hash table all strings up to the end of the match.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1845 * strstart-1 and strstart are already inserted. If there is not
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1846 * enough lookahead, the last two strings are not inserted in
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1847 * the hash table.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1848 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1849 s->lookahead -= s->prev_length-1;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1850 s->prev_length -= 2;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1851 do {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1852 if (++s->strstart <= max_insert) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1853 INSERT_STRING(s, s->strstart, hash_head);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1854 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1855 } while (--s->prev_length != 0);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1856 s->match_available = 0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1857 s->match_length = MIN_MATCH-1;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1858 s->strstart++;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1859
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1860 if (bflush) FLUSH_BLOCK(s, 0);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1861
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1862 } else if (s->match_available) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1863 /* If there was no match at the previous position, output a
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1864 * single literal. If there was a match but the current match
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1865 * is longer, truncate the previous match to a single literal.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1866 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1867 Tracevv((stderr,"%c", s->window[s->strstart-1]));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1868 if (_tr_tally (s, 0, s->window[s->strstart-1])) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1869 FLUSH_BLOCK_ONLY(s, 0);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1870 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1871 s->strstart++;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1872 s->lookahead--;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1873 if (s->strm->avail_out == 0) return need_more;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1874 } else {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1875 /* There is no previous match to compare with, wait for
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1876 * the next step to decide.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1877 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1878 s->match_available = 1;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1879 s->strstart++;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1880 s->lookahead--;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1881 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1882 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1883 Assert (flush != Z_NO_FLUSH, "no flush?");
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1884 if (s->match_available) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1885 Tracevv((stderr,"%c", s->window[s->strstart-1]));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1886 _tr_tally (s, 0, s->window[s->strstart-1]);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1887 s->match_available = 0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1888 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1889 FLUSH_BLOCK(s, flush == Z_FINISH);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1890 return flush == Z_FINISH ? finish_done : block_done;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1891 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1892 /* --- deflate.c */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1893
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1894 /* +++ trees.c */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1895 /* trees.c -- output deflated data using Huffman coding
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1896 * Copyright (C) 1995-1996 Jean-loup Gailly
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1897 * For conditions of distribution and use, see copyright notice in zlib.h
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1898 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1899
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1900 /*
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1901 * ALGORITHM
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1902 *
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1903 * The "deflation" process uses several Huffman trees. The more
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1904 * common source values are represented by shorter bit sequences.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1905 *
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1906 * Each code tree is stored in a compressed form which is itself
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1907 * a Huffman encoding of the lengths of all the code strings (in
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1908 * ascending order by source values). The actual code strings are
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1909 * reconstructed from the lengths in the inflate process, as described
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1910 * in the deflate specification.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1911 *
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1912 * REFERENCES
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1913 *
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1914 * Deutsch, L.P.,"'Deflate' Compressed Data Format Specification".
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1915 * Available in ftp.uu.net:/pub/archiving/zip/doc/deflate-1.1.doc
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1916 *
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1917 * Storer, James A.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1918 * Data Compression: Methods and Theory, pp. 49-50.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1919 * Computer Science Press, 1988. ISBN 0-7167-8156-5.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1920 *
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1921 * Sedgewick, R.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1922 * Algorithms, p290.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1923 * Addison-Wesley, 1983. ISBN 0-201-06672-6.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1924 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1925
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1926 /* From: trees.c,v 1.11 1996/07/24 13:41:06 me Exp $ */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1927
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1928 /* #include "deflate.h" */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1929
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1930 #ifdef DEBUG_ZLIB
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1931 # include <ctype.h>
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1932 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1933
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1934 /* ===========================================================================
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1935 * Constants
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1936 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1937
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1938 #define MAX_BL_BITS 7
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1939 /* Bit length codes must not exceed MAX_BL_BITS bits */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1940
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1941 #define END_BLOCK 256
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1942 /* end of block literal code */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1943
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1944 #define REP_3_6 16
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1945 /* repeat previous bit length 3-6 times (2 bits of repeat count) */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1946
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1947 #define REPZ_3_10 17
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1948 /* repeat a zero length 3-10 times (3 bits of repeat count) */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1949
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1950 #define REPZ_11_138 18
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1951 /* repeat a zero length 11-138 times (7 bits of repeat count) */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1952
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1953 local int extra_lbits[LENGTH_CODES] /* extra bits for each length code */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1954 = {0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0};
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1955
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1956 local int extra_dbits[D_CODES] /* extra bits for each distance code */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1957 = {0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13};
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1958
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1959 local int extra_blbits[BL_CODES]/* extra bits for each bit length code */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1960 = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7};
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1961
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1962 local uch bl_order[BL_CODES]
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1963 = {16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15};
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1964 /* The lengths of the bit length codes are sent in order of decreasing
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1965 * probability, to avoid transmitting the lengths for unused bit length codes.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1966 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1967
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1968 #define Buf_size (8 * 2*sizeof(char))
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1969 /* Number of bits used within bi_buf. (bi_buf might be implemented on
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1970 * more than 16 bits on some systems.)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1971 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1972
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1973 /* ===========================================================================
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1974 * Local data. These are initialized only once.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1975 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1976
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1977 local ct_data static_ltree[L_CODES+2];
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1978 /* The static literal tree. Since the bit lengths are imposed, there is no
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1979 * need for the L_CODES extra codes used during heap construction. However
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1980 * The codes 286 and 287 are needed to build a canonical tree (see _tr_init
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1981 * below).
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1982 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1983
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1984 local ct_data static_dtree[D_CODES];
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1985 /* The static distance tree. (Actually a trivial tree since all codes use
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1986 * 5 bits.)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1987 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1988
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1989 local uch dist_code[512];
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1990 /* distance codes. The first 256 values correspond to the distances
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1991 * 3 .. 258, the last 256 values correspond to the top 8 bits of
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1992 * the 15 bit distances.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1993 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1994
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1995 local uch length_code[MAX_MATCH-MIN_MATCH+1];
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1996 /* length code for each normalized match length (0 == MIN_MATCH) */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1997
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1998 local int base_length[LENGTH_CODES];
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
1999 /* First normalized length for each code (0 = MIN_MATCH) */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2000
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2001 local int base_dist[D_CODES];
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2002 /* First normalized distance for each code (0 = distance of 1) */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2003
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2004 struct static_tree_desc_s {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2005 ct_data *static_tree; /* static tree or NULL */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2006 intf *extra_bits; /* extra bits for each code or NULL */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2007 int extra_base; /* base index for extra_bits */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2008 int elems; /* max number of elements in the tree */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2009 int max_length; /* max bit length for the codes */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2010 };
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2011
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2012 local static_tree_desc static_l_desc =
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2013 {static_ltree, extra_lbits, LITERALS+1, L_CODES, MAX_BITS};
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2014
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2015 local static_tree_desc static_d_desc =
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2016 {static_dtree, extra_dbits, 0, D_CODES, MAX_BITS};
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2017
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2018 local static_tree_desc static_bl_desc =
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2019 {(ct_data *)0, extra_blbits, 0, BL_CODES, MAX_BL_BITS};
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2020
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2021 /* ===========================================================================
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2022 * Local (static) routines in this file.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2023 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2024
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2025 local void tr_static_init OF((void));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2026 local void init_block OF((deflate_state *s));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2027 local void pqdownheap OF((deflate_state *s, ct_data *tree, int k));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2028 local void gen_bitlen OF((deflate_state *s, tree_desc *desc));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2029 local void gen_codes OF((ct_data *tree, int max_code, ushf *bl_count));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2030 local void build_tree OF((deflate_state *s, tree_desc *desc));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2031 local void scan_tree OF((deflate_state *s, ct_data *tree, int max_code));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2032 local void send_tree OF((deflate_state *s, ct_data *tree, int max_code));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2033 local int build_bl_tree OF((deflate_state *s));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2034 local void send_all_trees OF((deflate_state *s, int lcodes, int dcodes,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2035 int blcodes));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2036 local void compress_block OF((deflate_state *s, ct_data *ltree,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2037 ct_data *dtree));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2038 local void set_data_type OF((deflate_state *s));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2039 local unsigned bi_reverse OF((unsigned value, int length));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2040 local void bi_windup OF((deflate_state *s));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2041 local void bi_flush OF((deflate_state *s));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2042 local void copy_block OF((deflate_state *s, charf *buf, unsigned len,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2043 int header));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2044
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2045 #ifndef DEBUG_ZLIB
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2046 # define send_code(s, c, tree) send_bits(s, tree[c].Code, tree[c].Len)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2047 /* Send a code of the given tree. c and tree must not have side effects */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2048
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2049 #else /* DEBUG_ZLIB */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2050 # define send_code(s, c, tree) \
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2051 { if (verbose>2) fprintf(stderr,"\ncd %3d ",(c)); \
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2052 send_bits(s, tree[c].Code, tree[c].Len); }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2053 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2054
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2055 #define d_code(dist) \
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2056 ((dist) < 256 ? dist_code[dist] : dist_code[256+((dist)>>7)])
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2057 /* Mapping from a distance to a distance code. dist is the distance - 1 and
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2058 * must not have side effects. dist_code[256] and dist_code[257] are never
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2059 * used.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2060 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2061
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2062 /* ===========================================================================
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2063 * Output a short LSB first on the stream.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2064 * IN assertion: there is enough room in pendingBuf.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2065 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2066 #define put_short(s, w) { \
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2067 put_byte(s, (uch)((w) & 0xff)); \
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2068 put_byte(s, (uch)((ush)(w) >> 8)); \
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2069 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2070
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2071 /* ===========================================================================
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2072 * Send a value on a given number of bits.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2073 * IN assertion: length <= 16 and value fits in length bits.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2074 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2075 #ifdef DEBUG_ZLIB
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2076 local void send_bits OF((deflate_state *s, int value, int length));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2077
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2078 local void send_bits(s, value, length)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2079 deflate_state *s;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2080 int value; /* value to send */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2081 int length; /* number of bits */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2082 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2083 Tracevv((stderr," l %2d v %4x ", length, value));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2084 Assert(length > 0 && length <= 15, "invalid length");
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2085 s->bits_sent += (ulg)length;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2086
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2087 /* If not enough room in bi_buf, use (valid) bits from bi_buf and
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2088 * (16 - bi_valid) bits from value, leaving (width - (16-bi_valid))
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2089 * unused bits in value.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2090 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2091 if (s->bi_valid > (int)Buf_size - length) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2092 s->bi_buf |= (value << s->bi_valid);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2093 put_short(s, s->bi_buf);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2094 s->bi_buf = (ush)value >> (Buf_size - s->bi_valid);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2095 s->bi_valid += length - Buf_size;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2096 } else {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2097 s->bi_buf |= value << s->bi_valid;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2098 s->bi_valid += length;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2099 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2100 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2101 #else /* !DEBUG_ZLIB */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2102
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2103 #define send_bits(s, value, length) \
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2104 { int len = length;\
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2105 if (s->bi_valid > (int)Buf_size - len) {\
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2106 int val = value;\
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2107 s->bi_buf |= (val << s->bi_valid);\
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2108 put_short(s, s->bi_buf);\
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2109 s->bi_buf = (ush)val >> (Buf_size - s->bi_valid);\
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2110 s->bi_valid += len - Buf_size;\
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2111 } else {\
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2112 s->bi_buf |= (value) << s->bi_valid;\
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2113 s->bi_valid += len;\
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2114 }\
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2115 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2116 #endif /* DEBUG_ZLIB */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2117
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2118
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2119 #define MAX(a,b) (a >= b ? a : b)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2120 /* the arguments must not have side effects */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2121
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2122 /* ===========================================================================
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2123 * Initialize the various 'constant' tables. In a multi-threaded environment,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2124 * this function may be called by two threads concurrently, but this is
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2125 * harmless since both invocations do exactly the same thing.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2126 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2127 local void tr_static_init()
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2128 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2129 static int static_init_done = 0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2130 int n; /* iterates over tree elements */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2131 int bits; /* bit counter */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2132 int length; /* length value */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2133 int code; /* code value */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2134 int dist; /* distance index */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2135 ush bl_count[MAX_BITS+1];
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2136 /* number of codes at each bit length for an optimal tree */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2137
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2138 if (static_init_done) return;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2139
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2140 /* Initialize the mapping length (0..255) -> length code (0..28) */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2141 length = 0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2142 for (code = 0; code < LENGTH_CODES-1; code++) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2143 base_length[code] = length;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2144 for (n = 0; n < (1<<extra_lbits[code]); n++) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2145 length_code[length++] = (uch)code;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2146 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2147 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2148 Assert (length == 256, "tr_static_init: length != 256");
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2149 /* Note that the length 255 (match length 258) can be represented
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2150 * in two different ways: code 284 + 5 bits or code 285, so we
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2151 * overwrite length_code[255] to use the best encoding:
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2152 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2153 length_code[length-1] = (uch)code;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2154
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2155 /* Initialize the mapping dist (0..32K) -> dist code (0..29) */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2156 dist = 0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2157 for (code = 0 ; code < 16; code++) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2158 base_dist[code] = dist;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2159 for (n = 0; n < (1<<extra_dbits[code]); n++) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2160 dist_code[dist++] = (uch)code;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2161 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2162 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2163 Assert (dist == 256, "tr_static_init: dist != 256");
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2164 dist >>= 7; /* from now on, all distances are divided by 128 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2165 for ( ; code < D_CODES; code++) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2166 base_dist[code] = dist << 7;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2167 for (n = 0; n < (1<<(extra_dbits[code]-7)); n++) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2168 dist_code[256 + dist++] = (uch)code;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2169 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2170 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2171 Assert (dist == 256, "tr_static_init: 256+dist != 512");
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2172
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2173 /* Construct the codes of the static literal tree */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2174 for (bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2175 n = 0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2176 while (n <= 143) static_ltree[n++].Len = 8, bl_count[8]++;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2177 while (n <= 255) static_ltree[n++].Len = 9, bl_count[9]++;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2178 while (n <= 279) static_ltree[n++].Len = 7, bl_count[7]++;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2179 while (n <= 287) static_ltree[n++].Len = 8, bl_count[8]++;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2180 /* Codes 286 and 287 do not exist, but we must include them in the
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2181 * tree construction to get a canonical Huffman tree (longest code
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2182 * all ones)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2183 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2184 gen_codes((ct_data *)static_ltree, L_CODES+1, bl_count);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2185
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2186 /* The static distance tree is trivial: */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2187 for (n = 0; n < D_CODES; n++) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2188 static_dtree[n].Len = 5;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2189 static_dtree[n].Code = bi_reverse((unsigned)n, 5);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2190 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2191 static_init_done = 1;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2192 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2193
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2194 /* ===========================================================================
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2195 * Initialize the tree data structures for a new zlib stream.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2196 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2197 void _tr_init(s)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2198 deflate_state *s;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2199 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2200 tr_static_init();
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2201
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2202 s->compressed_len = 0L;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2203
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2204 s->l_desc.dyn_tree = s->dyn_ltree;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2205 s->l_desc.stat_desc = &static_l_desc;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2206
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2207 s->d_desc.dyn_tree = s->dyn_dtree;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2208 s->d_desc.stat_desc = &static_d_desc;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2209
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2210 s->bl_desc.dyn_tree = s->bl_tree;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2211 s->bl_desc.stat_desc = &static_bl_desc;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2212
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2213 s->bi_buf = 0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2214 s->bi_valid = 0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2215 s->last_eob_len = 8; /* enough lookahead for inflate */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2216 #ifdef DEBUG_ZLIB
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2217 s->bits_sent = 0L;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2218 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2219
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2220 /* Initialize the first block of the first file: */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2221 init_block(s);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2222 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2223
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2224 /* ===========================================================================
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2225 * Initialize a new block.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2226 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2227 local void init_block(s)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2228 deflate_state *s;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2229 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2230 int n; /* iterates over tree elements */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2231
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2232 /* Initialize the trees. */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2233 for (n = 0; n < L_CODES; n++) s->dyn_ltree[n].Freq = 0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2234 for (n = 0; n < D_CODES; n++) s->dyn_dtree[n].Freq = 0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2235 for (n = 0; n < BL_CODES; n++) s->bl_tree[n].Freq = 0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2236
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2237 s->dyn_ltree[END_BLOCK].Freq = 1;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2238 s->opt_len = s->static_len = 0L;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2239 s->last_lit = s->matches = 0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2240 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2241
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2242 #define SMALLEST 1
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2243 /* Index within the heap array of least frequent node in the Huffman tree */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2244
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2245
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2246 /* ===========================================================================
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2247 * Remove the smallest element from the heap and recreate the heap with
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2248 * one less element. Updates heap and heap_len.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2249 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2250 #define pqremove(s, tree, top) \
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2251 {\
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2252 top = s->heap[SMALLEST]; \
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2253 s->heap[SMALLEST] = s->heap[s->heap_len--]; \
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2254 pqdownheap(s, tree, SMALLEST); \
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2255 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2256
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2257 /* ===========================================================================
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2258 * Compares to subtrees, using the tree depth as tie breaker when
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2259 * the subtrees have equal frequency. This minimizes the worst case length.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2260 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2261 #define smaller(tree, n, m, depth) \
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2262 (tree[n].Freq < tree[m].Freq || \
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2263 (tree[n].Freq == tree[m].Freq && depth[n] <= depth[m]))
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2264
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2265 /* ===========================================================================
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2266 * Restore the heap property by moving down the tree starting at node k,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2267 * exchanging a node with the smallest of its two sons if necessary, stopping
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2268 * when the heap property is re-established (each father smaller than its
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2269 * two sons).
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2270 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2271 local void pqdownheap(s, tree, k)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2272 deflate_state *s;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2273 ct_data *tree; /* the tree to restore */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2274 int k; /* node to move down */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2275 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2276 int v = s->heap[k];
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2277 int j = k << 1; /* left son of k */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2278 while (j <= s->heap_len) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2279 /* Set j to the smallest of the two sons: */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2280 if (j < s->heap_len &&
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2281 smaller(tree, s->heap[j+1], s->heap[j], s->depth)) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2282 j++;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2283 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2284 /* Exit if v is smaller than both sons */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2285 if (smaller(tree, v, s->heap[j], s->depth)) break;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2286
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2287 /* Exchange v with the smallest son */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2288 s->heap[k] = s->heap[j]; k = j;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2289
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2290 /* And continue down the tree, setting j to the left son of k */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2291 j <<= 1;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2292 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2293 s->heap[k] = v;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2294 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2295
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2296 /* ===========================================================================
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2297 * Compute the optimal bit lengths for a tree and update the total bit length
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2298 * for the current block.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2299 * IN assertion: the fields freq and dad are set, heap[heap_max] and
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2300 * above are the tree nodes sorted by increasing frequency.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2301 * OUT assertions: the field len is set to the optimal bit length, the
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2302 * array bl_count contains the frequencies for each bit length.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2303 * The length opt_len is updated; static_len is also updated if stree is
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2304 * not null.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2305 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2306 local void gen_bitlen(s, desc)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2307 deflate_state *s;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2308 tree_desc *desc; /* the tree descriptor */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2309 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2310 ct_data *tree = desc->dyn_tree;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2311 int max_code = desc->max_code;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2312 ct_data *stree = desc->stat_desc->static_tree;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2313 intf *extra = desc->stat_desc->extra_bits;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2314 int base = desc->stat_desc->extra_base;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2315 int max_length = desc->stat_desc->max_length;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2316 int h; /* heap index */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2317 int n, m; /* iterate over the tree elements */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2318 int bits; /* bit length */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2319 int xbits; /* extra bits */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2320 ush f; /* frequency */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2321 int overflow = 0; /* number of elements with bit length too large */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2322
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2323 for (bits = 0; bits <= MAX_BITS; bits++) s->bl_count[bits] = 0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2324
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2325 /* In a first pass, compute the optimal bit lengths (which may
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2326 * overflow in the case of the bit length tree).
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2327 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2328 tree[s->heap[s->heap_max]].Len = 0; /* root of the heap */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2329
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2330 for (h = s->heap_max+1; h < HEAP_SIZE; h++) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2331 n = s->heap[h];
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2332 bits = tree[tree[n].Dad].Len + 1;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2333 if (bits > max_length) bits = max_length, overflow++;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2334 tree[n].Len = (ush)bits;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2335 /* We overwrite tree[n].Dad which is no longer needed */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2336
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2337 if (n > max_code) continue; /* not a leaf node */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2338
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2339 s->bl_count[bits]++;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2340 xbits = 0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2341 if (n >= base) xbits = extra[n-base];
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2342 f = tree[n].Freq;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2343 s->opt_len += (ulg)f * (bits + xbits);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2344 if (stree) s->static_len += (ulg)f * (stree[n].Len + xbits);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2345 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2346 if (overflow == 0) return;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2347
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2348 Trace((stderr,"\nbit length overflow\n"));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2349 /* This happens for example on obj2 and pic of the Calgary corpus */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2350
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2351 /* Find the first bit length which could increase: */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2352 do {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2353 bits = max_length-1;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2354 while (s->bl_count[bits] == 0) bits--;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2355 s->bl_count[bits]--; /* move one leaf down the tree */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2356 s->bl_count[bits+1] += 2; /* move one overflow item as its brother */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2357 s->bl_count[max_length]--;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2358 /* The brother of the overflow item also moves one step up,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2359 * but this does not affect bl_count[max_length]
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2360 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2361 overflow -= 2;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2362 } while (overflow > 0);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2363
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2364 /* Now recompute all bit lengths, scanning in increasing frequency.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2365 * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2366 * lengths instead of fixing only the wrong ones. This idea is taken
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2367 * from 'ar' written by Haruhiko Okumura.)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2368 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2369 for (bits = max_length; bits != 0; bits--) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2370 n = s->bl_count[bits];
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2371 while (n != 0) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2372 m = s->heap[--h];
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2373 if (m > max_code) continue;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2374 if (tree[m].Len != (unsigned) bits) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2375 Trace((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2376 s->opt_len += ((long)bits - (long)tree[m].Len)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2377 *(long)tree[m].Freq;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2378 tree[m].Len = (ush)bits;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2379 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2380 n--;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2381 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2382 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2383 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2384
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2385 /* ===========================================================================
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2386 * Generate the codes for a given tree and bit counts (which need not be
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2387 * optimal).
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2388 * IN assertion: the array bl_count contains the bit length statistics for
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2389 * the given tree and the field len is set for all tree elements.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2390 * OUT assertion: the field code is set for all tree elements of non
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2391 * zero code length.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2392 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2393 local void gen_codes (tree, max_code, bl_count)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2394 ct_data *tree; /* the tree to decorate */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2395 int max_code; /* largest code with non zero frequency */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2396 ushf *bl_count; /* number of codes at each bit length */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2397 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2398 ush next_code[MAX_BITS+1]; /* next code value for each bit length */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2399 ush code = 0; /* running code value */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2400 int bits; /* bit index */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2401 int n; /* code index */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2402
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2403 /* The distribution counts are first used to generate the code values
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2404 * without bit reversal.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2405 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2406 for (bits = 1; bits <= MAX_BITS; bits++) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2407 next_code[bits] = code = (code + bl_count[bits-1]) << 1;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2408 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2409 /* Check that the bit counts in bl_count are consistent. The last code
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2410 * must be all ones.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2411 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2412 Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2413 "inconsistent bit counts");
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2414 Tracev((stderr,"\ngen_codes: max_code %d ", max_code));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2415
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2416 for (n = 0; n <= max_code; n++) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2417 int len = tree[n].Len;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2418 if (len == 0) continue;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2419 /* Now reverse the bits */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2420 tree[n].Code = bi_reverse(next_code[len]++, len);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2421
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2422 Tracecv(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ",
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2423 n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len]-1));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2424 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2425 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2426
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2427 /* ===========================================================================
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2428 * Construct one Huffman tree and assigns the code bit strings and lengths.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2429 * Update the total bit length for the current block.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2430 * IN assertion: the field freq is set for all tree elements.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2431 * OUT assertions: the fields len and code are set to the optimal bit length
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2432 * and corresponding code. The length opt_len is updated; static_len is
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2433 * also updated if stree is not null. The field max_code is set.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2434 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2435 local void build_tree(s, desc)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2436 deflate_state *s;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2437 tree_desc *desc; /* the tree descriptor */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2438 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2439 ct_data *tree = desc->dyn_tree;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2440 ct_data *stree = desc->stat_desc->static_tree;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2441 int elems = desc->stat_desc->elems;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2442 int n, m; /* iterate over heap elements */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2443 int max_code = -1; /* largest code with non zero frequency */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2444 int node; /* new node being created */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2445
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2446 /* Construct the initial heap, with least frequent element in
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2447 * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2448 * heap[0] is not used.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2449 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2450 s->heap_len = 0, s->heap_max = HEAP_SIZE;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2451
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2452 for (n = 0; n < elems; n++) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2453 if (tree[n].Freq != 0) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2454 s->heap[++(s->heap_len)] = max_code = n;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2455 s->depth[n] = 0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2456 } else {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2457 tree[n].Len = 0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2458 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2459 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2460
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2461 /* The pkzip format requires that at least one distance code exists,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2462 * and that at least one bit should be sent even if there is only one
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2463 * possible code. So to avoid special checks later on we force at least
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2464 * two codes of non zero frequency.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2465 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2466 while (s->heap_len < 2) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2467 node = s->heap[++(s->heap_len)] = (max_code < 2 ? ++max_code : 0);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2468 tree[node].Freq = 1;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2469 s->depth[node] = 0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2470 s->opt_len--; if (stree) s->static_len -= stree[node].Len;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2471 /* node is 0 or 1 so it does not have extra bits */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2472 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2473 desc->max_code = max_code;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2474
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2475 /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2476 * establish sub-heaps of increasing lengths:
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2477 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2478 for (n = s->heap_len/2; n >= 1; n--) pqdownheap(s, tree, n);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2479
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2480 /* Construct the Huffman tree by repeatedly combining the least two
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2481 * frequent nodes.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2482 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2483 node = elems; /* next internal node of the tree */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2484 do {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2485 pqremove(s, tree, n); /* n = node of least frequency */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2486 m = s->heap[SMALLEST]; /* m = node of next least frequency */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2487
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2488 s->heap[--(s->heap_max)] = n; /* keep the nodes sorted by frequency */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2489 s->heap[--(s->heap_max)] = m;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2490
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2491 /* Create a new node father of n and m */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2492 tree[node].Freq = tree[n].Freq + tree[m].Freq;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2493 s->depth[node] = (uch) (MAX(s->depth[n], s->depth[m]) + 1);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2494 tree[n].Dad = tree[m].Dad = (ush)node;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2495 #ifdef DUMP_BL_TREE
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2496 if (tree == s->bl_tree) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2497 fprintf(stderr,"\nnode %d(%d), sons %d(%d) %d(%d)",
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2498 node, tree[node].Freq, n, tree[n].Freq, m, tree[m].Freq);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2499 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2500 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2501 /* and insert the new node in the heap */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2502 s->heap[SMALLEST] = node++;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2503 pqdownheap(s, tree, SMALLEST);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2504
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2505 } while (s->heap_len >= 2);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2506
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2507 s->heap[--(s->heap_max)] = s->heap[SMALLEST];
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2508
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2509 /* At this point, the fields freq and dad are set. We can now
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2510 * generate the bit lengths.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2511 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2512 gen_bitlen(s, (tree_desc *)desc);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2513
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2514 /* The field len is now set, we can generate the bit codes */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2515 gen_codes ((ct_data *)tree, max_code, s->bl_count);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2516 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2517
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2518 /* ===========================================================================
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2519 * Scan a literal or distance tree to determine the frequencies of the codes
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2520 * in the bit length tree.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2521 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2522 local void scan_tree (s, tree, max_code)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2523 deflate_state *s;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2524 ct_data *tree; /* the tree to be scanned */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2525 int max_code; /* and its largest code of non zero frequency */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2526 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2527 int n; /* iterates over all tree elements */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2528 int prevlen = -1; /* last emitted length */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2529 int curlen; /* length of current code */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2530 int nextlen = tree[0].Len; /* length of next code */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2531 int count = 0; /* repeat count of the current code */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2532 int max_count = 7; /* max repeat count */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2533 int min_count = 4; /* min repeat count */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2534
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2535 if (nextlen == 0) max_count = 138, min_count = 3;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2536 tree[max_code+1].Len = (ush)0xffff; /* guard */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2537
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2538 for (n = 0; n <= max_code; n++) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2539 curlen = nextlen; nextlen = tree[n+1].Len;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2540 if (++count < max_count && curlen == nextlen) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2541 continue;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2542 } else if (count < min_count) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2543 s->bl_tree[curlen].Freq += count;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2544 } else if (curlen != 0) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2545 if (curlen != prevlen) s->bl_tree[curlen].Freq++;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2546 s->bl_tree[REP_3_6].Freq++;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2547 } else if (count <= 10) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2548 s->bl_tree[REPZ_3_10].Freq++;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2549 } else {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2550 s->bl_tree[REPZ_11_138].Freq++;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2551 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2552 count = 0; prevlen = curlen;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2553 if (nextlen == 0) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2554 max_count = 138, min_count = 3;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2555 } else if (curlen == nextlen) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2556 max_count = 6, min_count = 3;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2557 } else {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2558 max_count = 7, min_count = 4;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2559 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2560 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2561 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2562
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2563 /* ===========================================================================
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2564 * Send a literal or distance tree in compressed form, using the codes in
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2565 * bl_tree.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2566 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2567 local void send_tree (s, tree, max_code)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2568 deflate_state *s;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2569 ct_data *tree; /* the tree to be scanned */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2570 int max_code; /* and its largest code of non zero frequency */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2571 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2572 int n; /* iterates over all tree elements */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2573 int prevlen = -1; /* last emitted length */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2574 int curlen; /* length of current code */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2575 int nextlen = tree[0].Len; /* length of next code */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2576 int count = 0; /* repeat count of the current code */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2577 int max_count = 7; /* max repeat count */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2578 int min_count = 4; /* min repeat count */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2579
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2580 /* tree[max_code+1].Len = -1; */ /* guard already set */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2581 if (nextlen == 0) max_count = 138, min_count = 3;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2582
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2583 for (n = 0; n <= max_code; n++) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2584 curlen = nextlen; nextlen = tree[n+1].Len;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2585 if (++count < max_count && curlen == nextlen) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2586 continue;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2587 } else if (count < min_count) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2588 do { send_code(s, curlen, s->bl_tree); } while (--count != 0);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2589
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2590 } else if (curlen != 0) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2591 if (curlen != prevlen) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2592 send_code(s, curlen, s->bl_tree); count--;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2593 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2594 Assert(count >= 3 && count <= 6, " 3_6?");
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2595 send_code(s, REP_3_6, s->bl_tree); send_bits(s, count-3, 2);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2596
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2597 } else if (count <= 10) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2598 send_code(s, REPZ_3_10, s->bl_tree); send_bits(s, count-3, 3);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2599
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2600 } else {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2601 send_code(s, REPZ_11_138, s->bl_tree); send_bits(s, count-11, 7);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2602 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2603 count = 0; prevlen = curlen;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2604 if (nextlen == 0) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2605 max_count = 138, min_count = 3;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2606 } else if (curlen == nextlen) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2607 max_count = 6, min_count = 3;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2608 } else {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2609 max_count = 7, min_count = 4;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2610 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2611 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2612 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2613
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2614 /* ===========================================================================
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2615 * Construct the Huffman tree for the bit lengths and return the index in
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2616 * bl_order of the last bit length code to send.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2617 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2618 local int build_bl_tree(s)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2619 deflate_state *s;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2620 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2621 int max_blindex; /* index of last bit length code of non zero freq */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2622
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2623 /* Determine the bit length frequencies for literal and distance trees */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2624 scan_tree(s, (ct_data *)s->dyn_ltree, s->l_desc.max_code);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2625 scan_tree(s, (ct_data *)s->dyn_dtree, s->d_desc.max_code);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2626
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2627 /* Build the bit length tree: */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2628 build_tree(s, (tree_desc *)(&(s->bl_desc)));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2629 /* opt_len now includes the length of the tree representations, except
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2630 * the lengths of the bit lengths codes and the 5+5+4 bits for the counts.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2631 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2632
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2633 /* Determine the number of bit length codes to send. The pkzip format
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2634 * requires that at least 4 bit length codes be sent. (appnote.txt says
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2635 * 3 but the actual value used is 4.)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2636 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2637 for (max_blindex = BL_CODES-1; max_blindex >= 3; max_blindex--) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2638 if (s->bl_tree[bl_order[max_blindex]].Len != 0) break;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2639 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2640 /* Update opt_len to include the bit length tree and counts */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2641 s->opt_len += 3*(max_blindex+1) + 5+5+4;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2642 Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld",
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2643 s->opt_len, s->static_len));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2644
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2645 return max_blindex;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2646 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2647
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2648 /* ===========================================================================
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2649 * Send the header for a block using dynamic Huffman trees: the counts, the
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2650 * lengths of the bit length codes, the literal tree and the distance tree.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2651 * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2652 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2653 local void send_all_trees(s, lcodes, dcodes, blcodes)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2654 deflate_state *s;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2655 int lcodes, dcodes, blcodes; /* number of codes for each tree */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2656 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2657 int rank; /* index in bl_order */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2658
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2659 Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes");
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2660 Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2661 "too many codes");
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2662 Tracev((stderr, "\nbl counts: "));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2663 send_bits(s, lcodes-257, 5); /* not +255 as stated in appnote.txt */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2664 send_bits(s, dcodes-1, 5);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2665 send_bits(s, blcodes-4, 4); /* not -3 as stated in appnote.txt */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2666 for (rank = 0; rank < blcodes; rank++) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2667 Tracev((stderr, "\nbl code %2d ", bl_order[rank]));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2668 send_bits(s, s->bl_tree[bl_order[rank]].Len, 3);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2669 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2670 Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2671
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2672 send_tree(s, (ct_data *)s->dyn_ltree, lcodes-1); /* literal tree */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2673 Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2674
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2675 send_tree(s, (ct_data *)s->dyn_dtree, dcodes-1); /* distance tree */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2676 Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2677 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2678
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2679 /* ===========================================================================
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2680 * Send a stored block
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2681 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2682 void _tr_stored_block(s, buf, stored_len, eof)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2683 deflate_state *s;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2684 charf *buf; /* input block */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2685 ulg stored_len; /* length of input block */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2686 int eof; /* true if this is the last block for a file */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2687 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2688 send_bits(s, (STORED_BLOCK<<1)+eof, 3); /* send block type */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2689 s->compressed_len = (s->compressed_len + 3 + 7) & (ulg)~7L;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2690 s->compressed_len += (stored_len + 4) << 3;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2691
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2692 copy_block(s, buf, (unsigned)stored_len, 1); /* with header */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2693 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2694
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2695 /* Send just the `stored block' type code without any length bytes or data.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2696 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2697 void _tr_stored_type_only(s)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2698 deflate_state *s;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2699 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2700 send_bits(s, (STORED_BLOCK << 1), 3);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2701 bi_windup(s);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2702 s->compressed_len = (s->compressed_len + 3) & ~7L;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2703 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2704
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2705
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2706 /* ===========================================================================
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2707 * Send one empty static block to give enough lookahead for inflate.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2708 * This takes 10 bits, of which 7 may remain in the bit buffer.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2709 * The current inflate code requires 9 bits of lookahead. If the
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2710 * last two codes for the previous block (real code plus EOB) were coded
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2711 * on 5 bits or less, inflate may have only 5+3 bits of lookahead to decode
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2712 * the last real code. In this case we send two empty static blocks instead
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2713 * of one. (There are no problems if the previous block is stored or fixed.)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2714 * To simplify the code, we assume the worst case of last real code encoded
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2715 * on one bit only.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2716 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2717 void _tr_align(s)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2718 deflate_state *s;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2719 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2720 send_bits(s, STATIC_TREES<<1, 3);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2721 send_code(s, END_BLOCK, static_ltree);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2722 s->compressed_len += 10L; /* 3 for block type, 7 for EOB */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2723 bi_flush(s);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2724 /* Of the 10 bits for the empty block, we have already sent
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2725 * (10 - bi_valid) bits. The lookahead for the last real code (before
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2726 * the EOB of the previous block) was thus at least one plus the length
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2727 * of the EOB plus what we have just sent of the empty static block.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2728 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2729 if (1 + s->last_eob_len + 10 - s->bi_valid < 9) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2730 send_bits(s, STATIC_TREES<<1, 3);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2731 send_code(s, END_BLOCK, static_ltree);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2732 s->compressed_len += 10L;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2733 bi_flush(s);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2734 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2735 s->last_eob_len = 7;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2736 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2737
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2738 /* ===========================================================================
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2739 * Determine the best encoding for the current block: dynamic trees, static
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2740 * trees or store, and output the encoded block to the zip file. This function
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2741 * returns the total compressed length for the file so far.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2742 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2743 ulg _tr_flush_block(s, buf, stored_len, eof)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2744 deflate_state *s;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2745 charf *buf; /* input block, or NULL if too old */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2746 ulg stored_len; /* length of input block */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2747 int eof; /* true if this is the last block for a file */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2748 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2749 ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2750 int max_blindex = 0; /* index of last bit length code of non zero freq */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2751
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2752 /* Build the Huffman trees unless a stored block is forced */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2753 if (s->level > 0) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2754
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2755 /* Check if the file is ascii or binary */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2756 if (s->data_type == Z_UNKNOWN) set_data_type(s);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2757
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2758 /* Construct the literal and distance trees */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2759 build_tree(s, (tree_desc *)(&(s->l_desc)));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2760 Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2761 s->static_len));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2762
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2763 build_tree(s, (tree_desc *)(&(s->d_desc)));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2764 Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2765 s->static_len));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2766 /* At this point, opt_len and static_len are the total bit lengths of
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2767 * the compressed block data, excluding the tree representations.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2768 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2769
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2770 /* Build the bit length tree for the above two trees, and get the index
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2771 * in bl_order of the last bit length code to send.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2772 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2773 max_blindex = build_bl_tree(s);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2774
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2775 /* Determine the best encoding. Compute first the block length in bytes*/
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2776 opt_lenb = (s->opt_len+3+7)>>3;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2777 static_lenb = (s->static_len+3+7)>>3;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2778
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2779 Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ",
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2780 opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2781 s->last_lit));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2782
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2783 if (static_lenb <= opt_lenb) opt_lenb = static_lenb;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2784
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2785 } else {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2786 Assert(buf != (char*)0, "lost buf");
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2787 opt_lenb = static_lenb = stored_len + 5; /* force a stored block */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2788 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2789
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2790 /* If compression failed and this is the first and last block,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2791 * and if the .zip file can be seeked (to rewrite the local header),
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2792 * the whole file is transformed into a stored file:
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2793 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2794 #ifdef STORED_FILE_OK
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2795 # ifdef FORCE_STORED_FILE
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2796 if (eof && s->compressed_len == 0L) { /* force stored file */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2797 # else
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2798 if (stored_len <= opt_lenb && eof && s->compressed_len==0L && seekable()) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2799 # endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2800 /* Since LIT_BUFSIZE <= 2*WSIZE, the input data must be there: */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2801 if (buf == (charf*)0) error ("block vanished");
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2802
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2803 copy_block(s, buf, (unsigned)stored_len, 0); /* without header */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2804 s->compressed_len = stored_len << 3;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2805 s->method = STORED;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2806 } else
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2807 #endif /* STORED_FILE_OK */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2808
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2809 #ifdef FORCE_STORED
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2810 if (buf != (char*)0) { /* force stored block */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2811 #else
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2812 if (stored_len+4 <= opt_lenb && buf != (char*)0) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2813 /* 4: two words for the lengths */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2814 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2815 /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2816 * Otherwise we can't have processed more than WSIZE input bytes since
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2817 * the last block flush, because compression would have been
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2818 * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2819 * transform a block into a stored block.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2820 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2821 _tr_stored_block(s, buf, stored_len, eof);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2822
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2823 #ifdef FORCE_STATIC
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2824 } else if (static_lenb >= 0) { /* force static trees */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2825 #else
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2826 } else if (static_lenb == opt_lenb) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2827 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2828 send_bits(s, (STATIC_TREES<<1)+eof, 3);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2829 compress_block(s, (ct_data *)static_ltree, (ct_data *)static_dtree);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2830 s->compressed_len += 3 + s->static_len;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2831 } else {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2832 send_bits(s, (DYN_TREES<<1)+eof, 3);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2833 send_all_trees(s, s->l_desc.max_code+1, s->d_desc.max_code+1,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2834 max_blindex+1);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2835 compress_block(s, (ct_data *)s->dyn_ltree, (ct_data *)s->dyn_dtree);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2836 s->compressed_len += 3 + s->opt_len;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2837 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2838 Assert (s->compressed_len == s->bits_sent, "bad compressed size");
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2839 init_block(s);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2840
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2841 if (eof) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2842 bi_windup(s);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2843 s->compressed_len += 7; /* align on byte boundary */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2844 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2845 Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2846 s->compressed_len-7*eof));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2847
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2848 return s->compressed_len >> 3;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2849 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2850
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2851 /* ===========================================================================
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2852 * Save the match info and tally the frequency counts. Return true if
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2853 * the current block must be flushed.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2854 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2855 int _tr_tally (s, dist, lc)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2856 deflate_state *s;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2857 unsigned dist; /* distance of matched string */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2858 unsigned lc; /* match length-MIN_MATCH or unmatched char (if dist==0) */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2859 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2860 s->d_buf[s->last_lit] = (ush)dist;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2861 s->l_buf[s->last_lit++] = (uch)lc;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2862 if (dist == 0) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2863 /* lc is the unmatched char */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2864 s->dyn_ltree[lc].Freq++;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2865 } else {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2866 s->matches++;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2867 /* Here, lc is the match length - MIN_MATCH */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2868 dist--; /* dist = match distance - 1 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2869 Assert((ush)dist < (ush)MAX_DIST(s) &&
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2870 (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) &&
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2871 (ush)d_code(dist) < (ush)D_CODES, "_tr_tally: bad match");
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2872
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2873 s->dyn_ltree[length_code[lc]+LITERALS+1].Freq++;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2874 s->dyn_dtree[d_code(dist)].Freq++;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2875 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2876
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2877 /* Try to guess if it is profitable to stop the current block here */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2878 if (s->level > 2 && (s->last_lit & 0xfff) == 0) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2879 /* Compute an upper bound for the compressed length */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2880 ulg out_length = (ulg)s->last_lit*8L;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2881 ulg in_length = (ulg)((long)s->strstart - s->block_start);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2882 int dcode;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2883 for (dcode = 0; dcode < D_CODES; dcode++) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2884 out_length += (ulg)s->dyn_dtree[dcode].Freq *
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2885 (5L+extra_dbits[dcode]);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2886 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2887 out_length >>= 3;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2888 Tracev((stderr,"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ",
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2889 s->last_lit, in_length, out_length,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2890 100L - out_length*100L/in_length));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2891 if (s->matches < s->last_lit/2 && out_length < in_length/2) return 1;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2892 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2893 return (s->last_lit == s->lit_bufsize-1);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2894 /* We avoid equality with lit_bufsize because of wraparound at 64K
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2895 * on 16 bit machines and because stored blocks are restricted to
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2896 * 64K-1 bytes.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2897 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2898 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2899
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2900 /* ===========================================================================
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2901 * Send the block data compressed using the given Huffman trees
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2902 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2903 local void compress_block(s, ltree, dtree)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2904 deflate_state *s;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2905 ct_data *ltree; /* literal tree */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2906 ct_data *dtree; /* distance tree */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2907 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2908 unsigned dist; /* distance of matched string */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2909 int lc; /* match length or unmatched char (if dist == 0) */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2910 unsigned lx = 0; /* running index in l_buf */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2911 unsigned code; /* the code to send */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2912 int extra; /* number of extra bits to send */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2913
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2914 if (s->last_lit != 0) do {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2915 dist = s->d_buf[lx];
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2916 lc = s->l_buf[lx++];
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2917 if (dist == 0) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2918 send_code(s, lc, ltree); /* send a literal byte */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2919 Tracecv(isgraph(lc), (stderr," '%c' ", lc));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2920 } else {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2921 /* Here, lc is the match length - MIN_MATCH */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2922 code = length_code[lc];
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2923 send_code(s, code+LITERALS+1, ltree); /* send the length code */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2924 extra = extra_lbits[code];
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2925 if (extra != 0) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2926 lc -= base_length[code];
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2927 send_bits(s, lc, extra); /* send the extra length bits */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2928 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2929 dist--; /* dist is now the match distance - 1 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2930 code = d_code(dist);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2931 Assert (code < D_CODES, "bad d_code");
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2932
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2933 send_code(s, code, dtree); /* send the distance code */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2934 extra = extra_dbits[code];
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2935 if (extra != 0) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2936 dist -= base_dist[code];
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2937 send_bits(s, dist, extra); /* send the extra distance bits */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2938 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2939 } /* literal or match pair ? */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2940
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2941 /* Check that the overlay between pending_buf and d_buf+l_buf is ok: */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2942 Assert(s->pending < s->lit_bufsize + 2*lx, "pendingBuf overflow");
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2943
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2944 } while (lx < s->last_lit);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2945
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2946 send_code(s, END_BLOCK, ltree);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2947 s->last_eob_len = ltree[END_BLOCK].Len;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2948 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2949
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2950 /* ===========================================================================
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2951 * Set the data type to ASCII or BINARY, using a crude approximation:
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2952 * binary if more than 20% of the bytes are <= 6 or >= 128, ascii otherwise.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2953 * IN assertion: the fields freq of dyn_ltree are set and the total of all
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2954 * frequencies does not exceed 64K (to fit in an int on 16 bit machines).
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2955 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2956 local void set_data_type(s)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2957 deflate_state *s;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2958 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2959 int n = 0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2960 unsigned ascii_freq = 0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2961 unsigned bin_freq = 0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2962 while (n < 7) bin_freq += s->dyn_ltree[n++].Freq;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2963 while (n < 128) ascii_freq += s->dyn_ltree[n++].Freq;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2964 while (n < LITERALS) bin_freq += s->dyn_ltree[n++].Freq;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2965 s->data_type = (Byte)(bin_freq > (ascii_freq >> 2) ? Z_BINARY : Z_ASCII);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2966 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2967
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2968 /* ===========================================================================
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2969 * Reverse the first len bits of a code, using straightforward code (a faster
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2970 * method would use a table)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2971 * IN assertion: 1 <= len <= 15
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2972 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2973 local unsigned bi_reverse(code, len)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2974 unsigned code; /* the value to invert */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2975 int len; /* its bit length */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2976 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2977 register unsigned res = 0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2978 do {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2979 res |= code & 1;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2980 code >>= 1, res <<= 1;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2981 } while (--len > 0);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2982 return res >> 1;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2983 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2984
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2985 /* ===========================================================================
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2986 * Flush the bit buffer, keeping at most 7 bits in it.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2987 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2988 local void bi_flush(s)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2989 deflate_state *s;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2990 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2991 if (s->bi_valid == 16) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2992 put_short(s, s->bi_buf);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2993 s->bi_buf = 0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2994 s->bi_valid = 0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2995 } else if (s->bi_valid >= 8) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2996 put_byte(s, (Byte)s->bi_buf);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2997 s->bi_buf >>= 8;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2998 s->bi_valid -= 8;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
2999 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3000 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3001
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3002 /* ===========================================================================
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3003 * Flush the bit buffer and align the output on a byte boundary
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3004 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3005 local void bi_windup(s)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3006 deflate_state *s;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3007 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3008 if (s->bi_valid > 8) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3009 put_short(s, s->bi_buf);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3010 } else if (s->bi_valid > 0) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3011 put_byte(s, (Byte)s->bi_buf);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3012 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3013 s->bi_buf = 0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3014 s->bi_valid = 0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3015 #ifdef DEBUG_ZLIB
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3016 s->bits_sent = (s->bits_sent+7) & ~7;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3017 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3018 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3019
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3020 /* ===========================================================================
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3021 * Copy a stored block, storing first the length and its
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3022 * one's complement if requested.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3023 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3024 local void copy_block(s, buf, len, header)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3025 deflate_state *s;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3026 charf *buf; /* the input data */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3027 unsigned len; /* its length */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3028 int header; /* true if block header must be written */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3029 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3030 bi_windup(s); /* align on byte boundary */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3031 s->last_eob_len = 8; /* enough lookahead for inflate */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3032
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3033 if (header) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3034 put_short(s, (ush)len);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3035 put_short(s, (ush)~len);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3036 #ifdef DEBUG_ZLIB
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3037 s->bits_sent += 2*16;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3038 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3039 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3040 #ifdef DEBUG_ZLIB
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3041 s->bits_sent += (ulg)len<<3;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3042 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3043 /* bundle up the put_byte(s, *buf++) calls */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3044 zmemcpy(&s->pending_buf[s->pending], buf, len);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3045 s->pending += len;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3046 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3047 /* --- trees.c */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3048
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3049 /* +++ inflate.c */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3050 /* inflate.c -- zlib interface to inflate modules
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3051 * Copyright (C) 1995-1996 Mark Adler
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3052 * For conditions of distribution and use, see copyright notice in zlib.h
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3053 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3054
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3055 /* #include "zutil.h" */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3056
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3057 /* +++ infblock.h */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3058 /* infblock.h -- header to use infblock.c
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3059 * Copyright (C) 1995-1996 Mark Adler
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3060 * For conditions of distribution and use, see copyright notice in zlib.h
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3061 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3062
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3063 /* WARNING: this file should *not* be used by applications. It is
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3064 part of the implementation of the compression library and is
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3065 subject to change. Applications should only use zlib.h.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3066 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3067
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3068 struct inflate_blocks_state;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3069 typedef struct inflate_blocks_state FAR inflate_blocks_statef;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3070
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3071 extern inflate_blocks_statef * inflate_blocks_new OF((
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3072 z_streamp z,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3073 check_func c, /* check function */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3074 uInt w)); /* window size */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3075
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3076 extern int inflate_blocks OF((
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3077 inflate_blocks_statef *,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3078 z_streamp ,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3079 int)); /* initial return code */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3080
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3081 extern void inflate_blocks_reset OF((
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3082 inflate_blocks_statef *,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3083 z_streamp ,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3084 uLongf *)); /* check value on output */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3085
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3086 extern int inflate_blocks_free OF((
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3087 inflate_blocks_statef *,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3088 z_streamp ,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3089 uLongf *)); /* check value on output */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3090
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3091 extern void inflate_set_dictionary OF((
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3092 inflate_blocks_statef *s,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3093 const Bytef *d, /* dictionary */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3094 uInt n)); /* dictionary length */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3095
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3096 extern int inflate_addhistory OF((
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3097 inflate_blocks_statef *,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3098 z_streamp));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3099
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3100 extern int inflate_packet_flush OF((
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3101 inflate_blocks_statef *));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3102 /* --- infblock.h */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3103
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3104 #ifndef NO_DUMMY_DECL
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3105 struct inflate_blocks_state {int dummy;}; /* for buggy compilers */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3106 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3107
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3108 /* inflate private state */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3109 struct internal_state {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3110
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3111 /* mode */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3112 enum {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3113 METHOD, /* waiting for method byte */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3114 FLAG, /* waiting for flag byte */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3115 DICT4, /* four dictionary check bytes to go */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3116 DICT3, /* three dictionary check bytes to go */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3117 DICT2, /* two dictionary check bytes to go */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3118 DICT1, /* one dictionary check byte to go */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3119 DICT0, /* waiting for inflateSetDictionary */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3120 BLOCKS, /* decompressing blocks */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3121 CHECK4, /* four check bytes to go */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3122 CHECK3, /* three check bytes to go */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3123 CHECK2, /* two check bytes to go */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3124 CHECK1, /* one check byte to go */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3125 DONE, /* finished check, done */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3126 BAD} /* got an error--stay here */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3127 mode; /* current inflate mode */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3128
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3129 /* mode dependent information */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3130 union {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3131 uInt method; /* if FLAGS, method byte */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3132 struct {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3133 uLong was; /* computed check value */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3134 uLong need; /* stream check value */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3135 } check; /* if CHECK, check values to compare */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3136 uInt marker; /* if BAD, inflateSync's marker bytes count */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3137 } sub; /* submode */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3138
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3139 /* mode independent information */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3140 int nowrap; /* flag for no wrapper */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3141 uInt wbits; /* log2(window size) (8..15, defaults to 15) */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3142 inflate_blocks_statef
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3143 *blocks; /* current inflate_blocks state */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3144
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3145 };
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3146
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3147
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3148 int inflateReset(z)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3149 z_streamp z;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3150 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3151 uLong c;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3152
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3153 if (z == Z_NULL || z->state == Z_NULL)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3154 return Z_STREAM_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3155 z->total_in = z->total_out = 0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3156 z->msg = Z_NULL;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3157 z->state->mode = z->state->nowrap ? BLOCKS : METHOD;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3158 inflate_blocks_reset(z->state->blocks, z, &c);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3159 Trace((stderr, "inflate: reset\n"));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3160 return Z_OK;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3161 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3162
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3163
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3164 int inflateEnd(z)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3165 z_streamp z;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3166 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3167 uLong c;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3168
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3169 if (z == Z_NULL || z->state == Z_NULL || z->zfree == Z_NULL)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3170 return Z_STREAM_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3171 if (z->state->blocks != Z_NULL)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3172 inflate_blocks_free(z->state->blocks, z, &c);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3173 ZFREE(z, z->state);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3174 z->state = Z_NULL;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3175 Trace((stderr, "inflate: end\n"));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3176 return Z_OK;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3177 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3178
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3179
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3180 int inflateInit2_(z, w, version, stream_size)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3181 z_streamp z;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3182 int w;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3183 const char *version;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3184 int stream_size;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3185 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3186 if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3187 stream_size != sizeof(z_stream))
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3188 return Z_VERSION_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3189
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3190 /* initialize state */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3191 if (z == Z_NULL)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3192 return Z_STREAM_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3193 z->msg = Z_NULL;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3194 #ifndef NO_ZCFUNCS
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3195 if (z->zalloc == Z_NULL)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3196 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3197 z->zalloc = zcalloc;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3198 z->opaque = (voidpf)0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3199 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3200 if (z->zfree == Z_NULL) z->zfree = zcfree;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3201 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3202 if ((z->state = (struct internal_state FAR *)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3203 ZALLOC(z,1,sizeof(struct internal_state))) == Z_NULL)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3204 return Z_MEM_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3205 z->state->blocks = Z_NULL;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3206
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3207 /* handle undocumented nowrap option (no zlib header or check) */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3208 z->state->nowrap = 0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3209 if (w < 0)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3210 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3211 w = - w;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3212 z->state->nowrap = 1;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3213 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3214
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3215 /* set window size */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3216 if (w < 8 || w > 15)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3217 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3218 inflateEnd(z);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3219 return Z_STREAM_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3220 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3221 z->state->wbits = (uInt)w;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3222
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3223 /* create inflate_blocks state */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3224 if ((z->state->blocks =
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3225 inflate_blocks_new(z, z->state->nowrap ? Z_NULL : adler32, (uInt)1 << w))
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3226 == Z_NULL)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3227 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3228 inflateEnd(z);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3229 return Z_MEM_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3230 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3231 Trace((stderr, "inflate: allocated\n"));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3232
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3233 /* reset state */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3234 inflateReset(z);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3235 return Z_OK;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3236 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3237
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3238
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3239 int inflateInit_(z, version, stream_size)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3240 z_streamp z;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3241 const char *version;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3242 int stream_size;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3243 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3244 return inflateInit2_(z, DEF_WBITS, version, stream_size);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3245 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3246
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3247
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3248 #define NEEDBYTE {if(z->avail_in==0)goto empty;r=Z_OK;}
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3249 #define NEXTBYTE (z->avail_in--,z->total_in++,*z->next_in++)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3250
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3251 int inflate(z, f)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3252 z_streamp z;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3253 int f;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3254 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3255 int r;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3256 uInt b;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3257
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3258 if (z == Z_NULL || z->state == Z_NULL || z->next_in == Z_NULL || f < 0)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3259 return Z_STREAM_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3260 r = Z_BUF_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3261 while (1) switch (z->state->mode)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3262 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3263 case METHOD:
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3264 NEEDBYTE
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3265 if (((z->state->sub.method = NEXTBYTE) & 0xf) != Z_DEFLATED)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3266 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3267 z->state->mode = BAD;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3268 z->msg = (char*)"unknown compression method";
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3269 z->state->sub.marker = 5; /* can't try inflateSync */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3270 break;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3271 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3272 if ((z->state->sub.method >> 4) + 8 > z->state->wbits)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3273 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3274 z->state->mode = BAD;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3275 z->msg = (char*)"invalid window size";
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3276 z->state->sub.marker = 5; /* can't try inflateSync */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3277 break;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3278 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3279 z->state->mode = FLAG;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3280 case FLAG:
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3281 NEEDBYTE
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3282 b = NEXTBYTE;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3283 if (((z->state->sub.method << 8) + b) % 31)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3284 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3285 z->state->mode = BAD;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3286 z->msg = (char*)"incorrect header check";
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3287 z->state->sub.marker = 5; /* can't try inflateSync */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3288 break;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3289 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3290 Trace((stderr, "inflate: zlib header ok\n"));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3291 if (!(b & PRESET_DICT))
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3292 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3293 z->state->mode = BLOCKS;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3294 break;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3295 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3296 z->state->mode = DICT4;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3297 case DICT4:
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3298 NEEDBYTE
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3299 z->state->sub.check.need = (uLong)NEXTBYTE << 24;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3300 z->state->mode = DICT3;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3301 case DICT3:
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3302 NEEDBYTE
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3303 z->state->sub.check.need += (uLong)NEXTBYTE << 16;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3304 z->state->mode = DICT2;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3305 case DICT2:
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3306 NEEDBYTE
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3307 z->state->sub.check.need += (uLong)NEXTBYTE << 8;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3308 z->state->mode = DICT1;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3309 case DICT1:
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3310 NEEDBYTE
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3311 z->state->sub.check.need += (uLong)NEXTBYTE;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3312 z->adler = z->state->sub.check.need;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3313 z->state->mode = DICT0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3314 return Z_NEED_DICT;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3315 case DICT0:
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3316 z->state->mode = BAD;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3317 z->msg = (char*)"need dictionary";
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3318 z->state->sub.marker = 0; /* can try inflateSync */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3319 return Z_STREAM_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3320 case BLOCKS:
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3321 r = inflate_blocks(z->state->blocks, z, r);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3322 if (f == Z_PACKET_FLUSH && z->avail_in == 0 && z->avail_out != 0)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3323 r = inflate_packet_flush(z->state->blocks);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3324 if (r == Z_DATA_ERROR)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3325 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3326 z->state->mode = BAD;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3327 z->state->sub.marker = 0; /* can try inflateSync */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3328 break;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3329 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3330 if (r != Z_STREAM_END)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3331 return r;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3332 r = Z_OK;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3333 inflate_blocks_reset(z->state->blocks, z, &z->state->sub.check.was);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3334 if (z->state->nowrap)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3335 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3336 z->state->mode = DONE;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3337 break;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3338 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3339 z->state->mode = CHECK4;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3340 case CHECK4:
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3341 NEEDBYTE
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3342 z->state->sub.check.need = (uLong)NEXTBYTE << 24;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3343 z->state->mode = CHECK3;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3344 case CHECK3:
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3345 NEEDBYTE
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3346 z->state->sub.check.need += (uLong)NEXTBYTE << 16;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3347 z->state->mode = CHECK2;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3348 case CHECK2:
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3349 NEEDBYTE
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3350 z->state->sub.check.need += (uLong)NEXTBYTE << 8;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3351 z->state->mode = CHECK1;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3352 case CHECK1:
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3353 NEEDBYTE
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3354 z->state->sub.check.need += (uLong)NEXTBYTE;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3355
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3356 if (z->state->sub.check.was != z->state->sub.check.need)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3357 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3358 z->state->mode = BAD;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3359 z->msg = (char*)"incorrect data check";
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3360 z->state->sub.marker = 5; /* can't try inflateSync */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3361 break;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3362 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3363 Trace((stderr, "inflate: zlib check ok\n"));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3364 z->state->mode = DONE;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3365 case DONE:
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3366 return Z_STREAM_END;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3367 case BAD:
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3368 return Z_DATA_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3369 default:
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3370 return Z_STREAM_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3371 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3372
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3373 empty:
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3374 if (f != Z_PACKET_FLUSH)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3375 return r;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3376 z->state->mode = BAD;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3377 z->msg = (char *)"need more for packet flush";
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3378 z->state->sub.marker = 0; /* can try inflateSync */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3379 return Z_DATA_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3380 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3381
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3382
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3383 int inflateSetDictionary(z, dictionary, dictLength)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3384 z_streamp z;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3385 const Bytef *dictionary;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3386 uInt dictLength;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3387 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3388 uInt length = dictLength;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3389
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3390 if (z == Z_NULL || z->state == Z_NULL || z->state->mode != DICT0)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3391 return Z_STREAM_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3392
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3393 if (adler32(1L, dictionary, dictLength) != z->adler) return Z_DATA_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3394 z->adler = 1L;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3395
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3396 if (length >= ((uInt)1<<z->state->wbits))
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3397 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3398 length = (1<<z->state->wbits)-1;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3399 dictionary += dictLength - length;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3400 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3401 inflate_set_dictionary(z->state->blocks, dictionary, length);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3402 z->state->mode = BLOCKS;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3403 return Z_OK;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3404 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3405
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3406 /*
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3407 * This subroutine adds the data at next_in/avail_in to the output history
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3408 * without performing any output. The output buffer must be "caught up";
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3409 * i.e. no pending output (hence s->read equals s->write), and the state must
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3410 * be BLOCKS (i.e. we should be willing to see the start of a series of
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3411 * BLOCKS). On exit, the output will also be caught up, and the checksum
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3412 * will have been updated if need be.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3413 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3414
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3415 int inflateIncomp(z)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3416 z_stream *z;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3417 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3418 if (z->state->mode != BLOCKS)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3419 return Z_DATA_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3420 return inflate_addhistory(z->state->blocks, z);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3421 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3422
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3423
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3424 int inflateSync(z)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3425 z_streamp z;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3426 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3427 uInt n; /* number of bytes to look at */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3428 Bytef *p; /* pointer to bytes */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3429 uInt m; /* number of marker bytes found in a row */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3430 uLong r, w; /* temporaries to save total_in and total_out */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3431
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3432 /* set up */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3433 if (z == Z_NULL || z->state == Z_NULL)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3434 return Z_STREAM_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3435 if (z->state->mode != BAD)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3436 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3437 z->state->mode = BAD;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3438 z->state->sub.marker = 0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3439 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3440 if ((n = z->avail_in) == 0)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3441 return Z_BUF_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3442 p = z->next_in;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3443 m = z->state->sub.marker;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3444
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3445 /* search */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3446 while (n && m < 4)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3447 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3448 if (*p == (Byte)(m < 2 ? 0 : 0xff))
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3449 m++;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3450 else if (*p)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3451 m = 0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3452 else
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3453 m = 4 - m;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3454 p++, n--;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3455 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3456
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3457 /* restore */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3458 z->total_in += p - z->next_in;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3459 z->next_in = p;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3460 z->avail_in = n;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3461 z->state->sub.marker = m;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3462
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3463 /* return no joy or set up to restart on a new block */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3464 if (m != 4)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3465 return Z_DATA_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3466 r = z->total_in; w = z->total_out;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3467 inflateReset(z);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3468 z->total_in = r; z->total_out = w;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3469 z->state->mode = BLOCKS;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3470 return Z_OK;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3471 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3472
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3473 #undef NEEDBYTE
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3474 #undef NEXTBYTE
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3475 /* --- inflate.c */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3476
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3477 /* +++ infblock.c */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3478 /* infblock.c -- interpret and process block types to last block
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3479 * Copyright (C) 1995-1996 Mark Adler
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3480 * For conditions of distribution and use, see copyright notice in zlib.h
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3481 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3482
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3483 /* #include "zutil.h" */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3484 /* #include "infblock.h" */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3485
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3486 /* +++ inftrees.h */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3487 /* inftrees.h -- header to use inftrees.c
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3488 * Copyright (C) 1995-1996 Mark Adler
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3489 * For conditions of distribution and use, see copyright notice in zlib.h
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3490 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3491
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3492 /* WARNING: this file should *not* be used by applications. It is
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3493 part of the implementation of the compression library and is
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3494 subject to change. Applications should only use zlib.h.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3495 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3496
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3497 /* Huffman code lookup table entry--this entry is four bytes for machines
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3498 that have 16-bit pointers (e.g. PC's in the small or medium model). */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3499
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3500 typedef struct inflate_huft_s FAR inflate_huft;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3501
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3502 struct inflate_huft_s {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3503 union {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3504 struct {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3505 Byte Exop; /* number of extra bits or operation */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3506 Byte Bits; /* number of bits in this code or subcode */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3507 } what;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3508 Bytef *pad; /* pad structure to a power of 2 (4 bytes for */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3509 } word; /* 16-bit, 8 bytes for 32-bit machines) */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3510 union {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3511 uInt Base; /* literal, length base, or distance base */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3512 inflate_huft *Next; /* pointer to next level of table */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3513 } more;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3514 };
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3515
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3516 #ifdef DEBUG_ZLIB
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3517 extern uInt inflate_hufts;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3518 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3519
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3520 extern int inflate_trees_bits OF((
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3521 uIntf *, /* 19 code lengths */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3522 uIntf *, /* bits tree desired/actual depth */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3523 inflate_huft * FAR *, /* bits tree result */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3524 z_streamp )); /* for zalloc, zfree functions */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3525
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3526 extern int inflate_trees_dynamic OF((
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3527 uInt, /* number of literal/length codes */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3528 uInt, /* number of distance codes */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3529 uIntf *, /* that many (total) code lengths */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3530 uIntf *, /* literal desired/actual bit depth */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3531 uIntf *, /* distance desired/actual bit depth */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3532 inflate_huft * FAR *, /* literal/length tree result */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3533 inflate_huft * FAR *, /* distance tree result */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3534 z_streamp )); /* for zalloc, zfree functions */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3535
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3536 extern int inflate_trees_fixed OF((
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3537 uIntf *, /* literal desired/actual bit depth */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3538 uIntf *, /* distance desired/actual bit depth */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3539 inflate_huft * FAR *, /* literal/length tree result */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3540 inflate_huft * FAR *)); /* distance tree result */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3541
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3542 extern int inflate_trees_free OF((
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3543 inflate_huft *, /* tables to free */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3544 z_streamp )); /* for zfree function */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3545
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3546 /* --- inftrees.h */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3547
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3548 /* +++ infcodes.h */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3549 /* infcodes.h -- header to use infcodes.c
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3550 * Copyright (C) 1995-1996 Mark Adler
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3551 * For conditions of distribution and use, see copyright notice in zlib.h
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3552 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3553
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3554 /* WARNING: this file should *not* be used by applications. It is
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3555 part of the implementation of the compression library and is
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3556 subject to change. Applications should only use zlib.h.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3557 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3558
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3559 struct inflate_codes_state;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3560 typedef struct inflate_codes_state FAR inflate_codes_statef;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3561
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3562 extern inflate_codes_statef *inflate_codes_new OF((
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3563 uInt, uInt,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3564 inflate_huft *, inflate_huft *,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3565 z_streamp ));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3566
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3567 extern int inflate_codes OF((
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3568 inflate_blocks_statef *,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3569 z_streamp ,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3570 int));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3571
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3572 extern void inflate_codes_free OF((
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3573 inflate_codes_statef *,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3574 z_streamp ));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3575
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3576 /* --- infcodes.h */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3577
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3578 /* +++ infutil.h */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3579 /* infutil.h -- types and macros common to blocks and codes
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3580 * Copyright (C) 1995-1996 Mark Adler
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3581 * For conditions of distribution and use, see copyright notice in zlib.h
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3582 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3583
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3584 /* WARNING: this file should *not* be used by applications. It is
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3585 part of the implementation of the compression library and is
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3586 subject to change. Applications should only use zlib.h.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3587 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3588
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3589 #ifndef _INFUTIL_H
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3590 #define _INFUTIL_H
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3591
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3592 typedef enum {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3593 TYPE, /* get type bits (3, including end bit) */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3594 LENS, /* get lengths for stored */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3595 STORED, /* processing stored block */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3596 TABLE, /* get table lengths */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3597 BTREE, /* get bit lengths tree for a dynamic block */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3598 DTREE, /* get length, distance trees for a dynamic block */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3599 CODES, /* processing fixed or dynamic block */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3600 DRY, /* output remaining window bytes */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3601 DONEB, /* finished last block, done */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3602 BADB} /* got a data error--stuck here */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3603 inflate_block_mode;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3604
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3605 /* inflate blocks semi-private state */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3606 struct inflate_blocks_state {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3607
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3608 /* mode */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3609 inflate_block_mode mode; /* current inflate_block mode */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3610
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3611 /* mode dependent information */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3612 union {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3613 uInt left; /* if STORED, bytes left to copy */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3614 struct {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3615 uInt table; /* table lengths (14 bits) */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3616 uInt index; /* index into blens (or border) */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3617 uIntf *blens; /* bit lengths of codes */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3618 uInt bb; /* bit length tree depth */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3619 inflate_huft *tb; /* bit length decoding tree */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3620 } trees; /* if DTREE, decoding info for trees */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3621 struct {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3622 inflate_huft *tl;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3623 inflate_huft *td; /* trees to free */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3624 inflate_codes_statef
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3625 *codes;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3626 } decode; /* if CODES, current state */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3627 } sub; /* submode */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3628 uInt last; /* true if this block is the last block */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3629
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3630 /* mode independent information */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3631 uInt bitk; /* bits in bit buffer */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3632 uLong bitb; /* bit buffer */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3633 Bytef *window; /* sliding window */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3634 Bytef *end; /* one byte after sliding window */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3635 Bytef *read; /* window read pointer */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3636 Bytef *write; /* window write pointer */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3637 check_func checkfn; /* check function */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3638 uLong check; /* check on output */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3639
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3640 };
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3641
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3642
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3643 /* defines for inflate input/output */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3644 /* update pointers and return */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3645 #define UPDBITS {s->bitb=b;s->bitk=k;}
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3646 #define UPDIN {z->avail_in=n;z->total_in+=p-z->next_in;z->next_in=p;}
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3647 #define UPDOUT {s->write=q;}
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3648 #define UPDATE {UPDBITS UPDIN UPDOUT}
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3649 #define LEAVE {UPDATE return inflate_flush(s,z,r);}
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3650 /* get bytes and bits */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3651 #define LOADIN {p=z->next_in;n=z->avail_in;b=s->bitb;k=s->bitk;}
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3652 #define NEEDBYTE {if(n)r=Z_OK;else LEAVE}
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3653 #define NEXTBYTE (n--,*p++)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3654 #define NEEDBITS(j) {while(k<(j)){NEEDBYTE;b|=((uLong)NEXTBYTE)<<k;k+=8;}}
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3655 #define DUMPBITS(j) {b>>=(j);k-=(j);}
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3656 /* output bytes */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3657 #define WAVAIL (uInt)(q<s->read?s->read-q-1:s->end-q)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3658 #define LOADOUT {q=s->write;m=(uInt)WAVAIL;}
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3659 #define WWRAP {if(q==s->end&&s->read!=s->window){q=s->window;m=(uInt)WAVAIL;}}
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3660 #define FLUSH {UPDOUT r=inflate_flush(s,z,r); LOADOUT}
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3661 #define NEEDOUT {if(m==0){WWRAP if(m==0){FLUSH WWRAP if(m==0) LEAVE}}r=Z_OK;}
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3662 #define OUTBYTE(a) {*q++=(Byte)(a);m--;}
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3663 /* load local pointers */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3664 #define LOAD {LOADIN LOADOUT}
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3665
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3666 /* masks for lower bits (size given to avoid silly warnings with Visual C++) */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3667 extern uInt inflate_mask[17];
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3668
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3669 /* copy as much as possible from the sliding window to the output area */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3670 extern int inflate_flush OF((
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3671 inflate_blocks_statef *,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3672 z_streamp ,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3673 int));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3674
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3675 #ifndef NO_DUMMY_DECL
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3676 struct internal_state {int dummy;}; /* for buggy compilers */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3677 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3678
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3679 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3680 /* --- infutil.h */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3681
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3682 #ifndef NO_DUMMY_DECL
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3683 struct inflate_codes_state {int dummy;}; /* for buggy compilers */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3684 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3685
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3686 /* Table for deflate from PKZIP's appnote.txt. */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3687 local const uInt border[] = { /* Order of the bit length code lengths */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3688 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3689
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3690 /*
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3691 Notes beyond the 1.93a appnote.txt:
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3692
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3693 1. Distance pointers never point before the beginning of the output
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3694 stream.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3695 2. Distance pointers can point back across blocks, up to 32k away.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3696 3. There is an implied maximum of 7 bits for the bit length table and
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3697 15 bits for the actual data.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3698 4. If only one code exists, then it is encoded using one bit. (Zero
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3699 would be more efficient, but perhaps a little confusing.) If two
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3700 codes exist, they are coded using one bit each (0 and 1).
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3701 5. There is no way of sending zero distance codes--a dummy must be
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3702 sent if there are none. (History: a pre 2.0 version of PKZIP would
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3703 store blocks with no distance codes, but this was discovered to be
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3704 too harsh a criterion.) Valid only for 1.93a. 2.04c does allow
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3705 zero distance codes, which is sent as one code of zero bits in
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3706 length.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3707 6. There are up to 286 literal/length codes. Code 256 represents the
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3708 end-of-block. Note however that the static length tree defines
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3709 288 codes just to fill out the Huffman codes. Codes 286 and 287
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3710 cannot be used though, since there is no length base or extra bits
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3711 defined for them. Similarily, there are up to 30 distance codes.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3712 However, static trees define 32 codes (all 5 bits) to fill out the
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3713 Huffman codes, but the last two had better not show up in the data.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3714 7. Unzip can check dynamic Huffman blocks for complete code sets.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3715 The exception is that a single code would not be complete (see #4).
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3716 8. The five bits following the block type is really the number of
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3717 literal codes sent minus 257.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3718 9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3719 (1+6+6). Therefore, to output three times the length, you output
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3720 three codes (1+1+1), whereas to output four times the same length,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3721 you only need two codes (1+3). Hmm.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3722 10. In the tree reconstruction algorithm, Code = Code + Increment
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3723 only if BitLength(i) is not zero. (Pretty obvious.)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3724 11. Correction: 4 Bits: # of Bit Length codes - 4 (4 - 19)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3725 12. Note: length code 284 can represent 227-258, but length code 285
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3726 really is 258. The last length deserves its own, short code
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3727 since it gets used a lot in very redundant files. The length
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3728 258 is special since 258 - 3 (the min match length) is 255.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3729 13. The literal/length and distance code bit lengths are read as a
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3730 single stream of lengths. It is possible (and advantageous) for
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3731 a repeat code (16, 17, or 18) to go across the boundary between
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3732 the two sets of lengths.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3733 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3734
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3735
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3736 void inflate_blocks_reset(s, z, c)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3737 inflate_blocks_statef *s;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3738 z_streamp z;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3739 uLongf *c;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3740 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3741 if (s->checkfn != Z_NULL)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3742 *c = s->check;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3743 if (s->mode == BTREE || s->mode == DTREE)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3744 ZFREE(z, s->sub.trees.blens);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3745 if (s->mode == CODES)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3746 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3747 inflate_codes_free(s->sub.decode.codes, z);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3748 inflate_trees_free(s->sub.decode.td, z);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3749 inflate_trees_free(s->sub.decode.tl, z);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3750 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3751 s->mode = TYPE;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3752 s->bitk = 0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3753 s->bitb = 0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3754 s->read = s->write = s->window;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3755 if (s->checkfn != Z_NULL)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3756 z->adler = s->check = (*s->checkfn)(0L, Z_NULL, 0);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3757 Trace((stderr, "inflate: blocks reset\n"));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3758 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3759
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3760
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3761 inflate_blocks_statef *inflate_blocks_new(z, c, w)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3762 z_streamp z;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3763 check_func c;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3764 uInt w;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3765 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3766 inflate_blocks_statef *s;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3767
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3768 if ((s = (inflate_blocks_statef *)ZALLOC
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3769 (z,1,sizeof(struct inflate_blocks_state))) == Z_NULL)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3770 return s;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3771 if ((s->window = (Bytef *)ZALLOC(z, 1, w)) == Z_NULL)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3772 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3773 ZFREE(z, s);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3774 return Z_NULL;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3775 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3776 s->end = s->window + w;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3777 s->checkfn = c;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3778 s->mode = TYPE;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3779 Trace((stderr, "inflate: blocks allocated\n"));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3780 inflate_blocks_reset(s, z, &s->check);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3781 return s;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3782 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3783
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3784
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3785 #ifdef DEBUG_ZLIB
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3786 extern uInt inflate_hufts;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3787 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3788 int inflate_blocks(s, z, r)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3789 inflate_blocks_statef *s;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3790 z_streamp z;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3791 int r;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3792 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3793 uInt t; /* temporary storage */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3794 uLong b; /* bit buffer */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3795 uInt k; /* bits in bit buffer */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3796 Bytef *p; /* input data pointer */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3797 uInt n; /* bytes available there */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3798 Bytef *q; /* output window write pointer */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3799 uInt m; /* bytes to end of window or read pointer */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3800
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3801 /* copy input/output information to locals (UPDATE macro restores) */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3802 LOAD
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3803
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3804 /* process input based on current state */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3805 while (1) switch (s->mode)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3806 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3807 case TYPE:
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3808 NEEDBITS(3)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3809 t = (uInt)b & 7;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3810 s->last = t & 1;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3811 switch (t >> 1)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3812 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3813 case 0: /* stored */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3814 Trace((stderr, "inflate: stored block%s\n",
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3815 s->last ? " (last)" : ""));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3816 DUMPBITS(3)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3817 t = k & 7; /* go to byte boundary */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3818 DUMPBITS(t)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3819 s->mode = LENS; /* get length of stored block */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3820 break;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3821 case 1: /* fixed */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3822 Trace((stderr, "inflate: fixed codes block%s\n",
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3823 s->last ? " (last)" : ""));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3824 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3825 uInt bl, bd;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3826 inflate_huft *tl, *td;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3827
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3828 inflate_trees_fixed(&bl, &bd, &tl, &td);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3829 s->sub.decode.codes = inflate_codes_new(bl, bd, tl, td, z);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3830 if (s->sub.decode.codes == Z_NULL)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3831 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3832 r = Z_MEM_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3833 LEAVE
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3834 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3835 s->sub.decode.tl = Z_NULL; /* don't try to free these */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3836 s->sub.decode.td = Z_NULL;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3837 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3838 DUMPBITS(3)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3839 s->mode = CODES;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3840 break;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3841 case 2: /* dynamic */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3842 Trace((stderr, "inflate: dynamic codes block%s\n",
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3843 s->last ? " (last)" : ""));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3844 DUMPBITS(3)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3845 s->mode = TABLE;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3846 break;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3847 case 3: /* illegal */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3848 DUMPBITS(3)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3849 s->mode = BADB;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3850 z->msg = (char*)"invalid block type";
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3851 r = Z_DATA_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3852 LEAVE
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3853 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3854 break;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3855 case LENS:
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3856 NEEDBITS(32)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3857 if ((((~b) >> 16) & 0xffff) != (b & 0xffff))
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3858 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3859 s->mode = BADB;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3860 z->msg = (char*)"invalid stored block lengths";
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3861 r = Z_DATA_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3862 LEAVE
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3863 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3864 s->sub.left = (uInt)b & 0xffff;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3865 b = k = 0; /* dump bits */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3866 Tracev((stderr, "inflate: stored length %u\n", s->sub.left));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3867 s->mode = s->sub.left ? STORED : (s->last ? DRY : TYPE);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3868 break;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3869 case STORED:
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3870 if (n == 0)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3871 LEAVE
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3872 NEEDOUT
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3873 t = s->sub.left;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3874 if (t > n) t = n;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3875 if (t > m) t = m;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3876 zmemcpy(q, p, t);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3877 p += t; n -= t;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3878 q += t; m -= t;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3879 if ((s->sub.left -= t) != 0)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3880 break;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3881 Tracev((stderr, "inflate: stored end, %lu total out\n",
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3882 z->total_out + (q >= s->read ? q - s->read :
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3883 (s->end - s->read) + (q - s->window))));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3884 s->mode = s->last ? DRY : TYPE;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3885 break;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3886 case TABLE:
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3887 NEEDBITS(14)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3888 s->sub.trees.table = t = (uInt)b & 0x3fff;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3889 #ifndef PKZIP_BUG_WORKAROUND
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3890 if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3891 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3892 s->mode = BADB;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3893 z->msg = (char*)"too many length or distance symbols";
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3894 r = Z_DATA_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3895 LEAVE
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3896 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3897 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3898 t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3899 if (t < 19)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3900 t = 19;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3901 if ((s->sub.trees.blens = (uIntf*)ZALLOC(z, t, sizeof(uInt))) == Z_NULL)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3902 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3903 r = Z_MEM_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3904 LEAVE
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3905 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3906 DUMPBITS(14)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3907 s->sub.trees.index = 0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3908 Tracev((stderr, "inflate: table sizes ok\n"));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3909 s->mode = BTREE;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3910 case BTREE:
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3911 while (s->sub.trees.index < 4 + (s->sub.trees.table >> 10))
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3912 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3913 NEEDBITS(3)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3914 s->sub.trees.blens[border[s->sub.trees.index++]] = (uInt)b & 7;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3915 DUMPBITS(3)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3916 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3917 while (s->sub.trees.index < 19)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3918 s->sub.trees.blens[border[s->sub.trees.index++]] = 0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3919 s->sub.trees.bb = 7;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3920 t = inflate_trees_bits(s->sub.trees.blens, &s->sub.trees.bb,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3921 &s->sub.trees.tb, z);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3922 if (t != Z_OK)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3923 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3924 r = t;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3925 if (r == Z_DATA_ERROR) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3926 ZFREE(z, s->sub.trees.blens);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3927 s->mode = BADB;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3928 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3929 LEAVE
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3930 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3931 s->sub.trees.index = 0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3932 Tracev((stderr, "inflate: bits tree ok\n"));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3933 s->mode = DTREE;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3934 case DTREE:
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3935 while (t = s->sub.trees.table,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3936 s->sub.trees.index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f))
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3937 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3938 inflate_huft *h;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3939 uInt i, j, c;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3940
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3941 t = s->sub.trees.bb;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3942 NEEDBITS(t)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3943 h = s->sub.trees.tb + ((uInt)b & inflate_mask[t]);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3944 t = h->word.what.Bits;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3945 c = h->more.Base;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3946 if (c < 16)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3947 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3948 DUMPBITS(t)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3949 s->sub.trees.blens[s->sub.trees.index++] = c;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3950 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3951 else /* c == 16..18 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3952 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3953 i = c == 18 ? 7 : c - 14;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3954 j = c == 18 ? 11 : 3;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3955 NEEDBITS(t + i)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3956 DUMPBITS(t)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3957 j += (uInt)b & inflate_mask[i];
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3958 DUMPBITS(i)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3959 i = s->sub.trees.index;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3960 t = s->sub.trees.table;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3961 if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) ||
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3962 (c == 16 && i < 1))
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3963 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3964 inflate_trees_free(s->sub.trees.tb, z);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3965 ZFREE(z, s->sub.trees.blens);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3966 s->mode = BADB;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3967 z->msg = (char*)"invalid bit length repeat";
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3968 r = Z_DATA_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3969 LEAVE
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3970 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3971 c = c == 16 ? s->sub.trees.blens[i - 1] : 0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3972 do {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3973 s->sub.trees.blens[i++] = c;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3974 } while (--j);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3975 s->sub.trees.index = i;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3976 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3977 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3978 inflate_trees_free(s->sub.trees.tb, z);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3979 s->sub.trees.tb = Z_NULL;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3980 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3981 uInt bl, bd;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3982 inflate_huft *tl, *td;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3983 inflate_codes_statef *c;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3984
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3985 bl = 9; /* must be <= 9 for lookahead assumptions */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3986 bd = 6; /* must be <= 9 for lookahead assumptions */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3987 t = s->sub.trees.table;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3988 #ifdef DEBUG_ZLIB
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3989 inflate_hufts = 0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3990 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3991 t = inflate_trees_dynamic(257 + (t & 0x1f), 1 + ((t >> 5) & 0x1f),
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3992 s->sub.trees.blens, &bl, &bd, &tl, &td, z);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3993 if (t != Z_OK)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3994 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3995 if (t == (uInt)Z_DATA_ERROR) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3996 ZFREE(z, s->sub.trees.blens);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3997 s->mode = BADB;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3998 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
3999 r = t;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4000 LEAVE
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4001 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4002 Tracev((stderr, "inflate: trees ok, %d * %d bytes used\n",
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4003 inflate_hufts, sizeof(inflate_huft)));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4004 if ((c = inflate_codes_new(bl, bd, tl, td, z)) == Z_NULL)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4005 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4006 inflate_trees_free(td, z);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4007 inflate_trees_free(tl, z);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4008 r = Z_MEM_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4009 LEAVE
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4010 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4011 /*
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4012 * this ZFREE must occur *BEFORE* we mess with sub.decode, because
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4013 * sub.trees is union'd with sub.decode.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4014 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4015 ZFREE(z, s->sub.trees.blens);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4016 s->sub.decode.codes = c;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4017 s->sub.decode.tl = tl;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4018 s->sub.decode.td = td;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4019 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4020 s->mode = CODES;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4021 case CODES:
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4022 UPDATE
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4023 if ((r = inflate_codes(s, z, r)) != Z_STREAM_END)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4024 return inflate_flush(s, z, r);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4025 r = Z_OK;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4026 inflate_codes_free(s->sub.decode.codes, z);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4027 inflate_trees_free(s->sub.decode.td, z);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4028 inflate_trees_free(s->sub.decode.tl, z);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4029 LOAD
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4030 Tracev((stderr, "inflate: codes end, %lu total out\n",
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4031 z->total_out + (q >= s->read ? q - s->read :
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4032 (s->end - s->read) + (q - s->window))));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4033 if (!s->last)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4034 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4035 s->mode = TYPE;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4036 break;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4037 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4038 if (k > 7) /* return unused byte, if any */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4039 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4040 Assert(k < 16, "inflate_codes grabbed too many bytes")
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4041 k -= 8;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4042 n++;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4043 p--; /* can always return one */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4044 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4045 s->mode = DRY;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4046 case DRY:
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4047 FLUSH
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4048 if (s->read != s->write)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4049 LEAVE
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4050 s->mode = DONEB;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4051 case DONEB:
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4052 r = Z_STREAM_END;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4053 LEAVE
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4054 case BADB:
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4055 r = Z_DATA_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4056 LEAVE
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4057 default:
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4058 r = Z_STREAM_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4059 LEAVE
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4060 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4061 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4062
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4063
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4064 int inflate_blocks_free(s, z, c)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4065 inflate_blocks_statef *s;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4066 z_streamp z;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4067 uLongf *c;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4068 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4069 inflate_blocks_reset(s, z, c);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4070 ZFREE(z, s->window);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4071 ZFREE(z, s);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4072 Trace((stderr, "inflate: blocks freed\n"));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4073 return Z_OK;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4074 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4075
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4076
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4077 void inflate_set_dictionary(s, d, n)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4078 inflate_blocks_statef *s;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4079 const Bytef *d;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4080 uInt n;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4081 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4082 zmemcpy((charf *)s->window, d, n);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4083 s->read = s->write = s->window + n;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4084 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4085
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4086 /*
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4087 * This subroutine adds the data at next_in/avail_in to the output history
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4088 * without performing any output. The output buffer must be "caught up";
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4089 * i.e. no pending output (hence s->read equals s->write), and the state must
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4090 * be BLOCKS (i.e. we should be willing to see the start of a series of
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4091 * BLOCKS). On exit, the output will also be caught up, and the checksum
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4092 * will have been updated if need be.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4093 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4094 int inflate_addhistory(s, z)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4095 inflate_blocks_statef *s;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4096 z_stream *z;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4097 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4098 uLong b; /* bit buffer */ /* NOT USED HERE */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4099 uInt k; /* bits in bit buffer */ /* NOT USED HERE */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4100 uInt t; /* temporary storage */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4101 Bytef *p; /* input data pointer */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4102 uInt n; /* bytes available there */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4103 Bytef *q; /* output window write pointer */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4104 uInt m; /* bytes to end of window or read pointer */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4105
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4106 if (s->read != s->write)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4107 return Z_STREAM_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4108 if (s->mode != TYPE)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4109 return Z_DATA_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4110
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4111 /* we're ready to rock */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4112 LOAD
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4113 /* while there is input ready, copy to output buffer, moving
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4114 * pointers as needed.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4115 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4116 while (n) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4117 t = n; /* how many to do */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4118 /* is there room until end of buffer? */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4119 if (t > m) t = m;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4120 /* update check information */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4121 if (s->checkfn != Z_NULL)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4122 s->check = (*s->checkfn)(s->check, q, t);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4123 zmemcpy(q, p, t);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4124 q += t;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4125 p += t;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4126 n -= t;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4127 z->total_out += t;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4128 s->read = q; /* drag read pointer forward */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4129 /* WWRAP */ /* expand WWRAP macro by hand to handle s->read */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4130 if (q == s->end) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4131 s->read = q = s->window;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4132 m = WAVAIL;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4133 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4134 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4135 UPDATE
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4136 return Z_OK;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4137 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4138
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4139
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4140 /*
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4141 * At the end of a Deflate-compressed PPP packet, we expect to have seen
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4142 * a `stored' block type value but not the (zero) length bytes.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4143 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4144 int inflate_packet_flush(s)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4145 inflate_blocks_statef *s;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4146 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4147 if (s->mode != LENS)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4148 return Z_DATA_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4149 s->mode = TYPE;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4150 return Z_OK;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4151 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4152 /* --- infblock.c */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4153
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4154 /* +++ inftrees.c */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4155 /* inftrees.c -- generate Huffman trees for efficient decoding
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4156 * Copyright (C) 1995-1996 Mark Adler
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4157 * For conditions of distribution and use, see copyright notice in zlib.h
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4158 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4159
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4160 /* #include "zutil.h" */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4161 /* #include "inftrees.h" */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4162
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4163 char inflate_copyright[] = " inflate 1.0.4 Copyright 1995-1996 Mark Adler ";
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4164 /*
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4165 If you use the zlib library in a product, an acknowledgment is welcome
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4166 in the documentation of your product. If for some reason you cannot
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4167 include such an acknowledgment, I would appreciate that you keep this
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4168 copyright string in the executable of your product.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4169 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4170
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4171 #ifndef NO_DUMMY_DECL
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4172 struct internal_state {int dummy;}; /* for buggy compilers */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4173 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4174
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4175 /* simplify the use of the inflate_huft type with some defines */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4176 #define base more.Base
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4177 #define next more.Next
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4178 #define exop word.what.Exop
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4179 #define bits word.what.Bits
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4180
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4181
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4182 local int huft_build OF((
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4183 uIntf *, /* code lengths in bits */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4184 uInt, /* number of codes */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4185 uInt, /* number of "simple" codes */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4186 const uIntf *, /* list of base values for non-simple codes */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4187 const uIntf *, /* list of extra bits for non-simple codes */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4188 inflate_huft * FAR*,/* result: starting table */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4189 uIntf *, /* maximum lookup bits (returns actual) */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4190 z_streamp )); /* for zalloc function */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4191
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4192 local voidpf falloc OF((
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4193 voidpf, /* opaque pointer (not used) */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4194 uInt, /* number of items */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4195 uInt)); /* size of item */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4196
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4197 /* Tables for deflate from PKZIP's appnote.txt. */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4198 local const uInt cplens[31] = { /* Copy lengths for literal codes 257..285 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4199 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4200 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4201 /* see note #13 above about 258 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4202 local const uInt cplext[31] = { /* Extra bits for literal codes 257..285 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4203 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4204 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 112, 112}; /* 112==invalid */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4205 local const uInt cpdist[30] = { /* Copy offsets for distance codes 0..29 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4206 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4207 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4208 8193, 12289, 16385, 24577};
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4209 local const uInt cpdext[30] = { /* Extra bits for distance codes */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4210 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4211 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4212 12, 12, 13, 13};
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4213
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4214 /*
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4215 Huffman code decoding is performed using a multi-level table lookup.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4216 The fastest way to decode is to simply build a lookup table whose
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4217 size is determined by the longest code. However, the time it takes
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4218 to build this table can also be a factor if the data being decoded
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4219 is not very long. The most common codes are necessarily the
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4220 shortest codes, so those codes dominate the decoding time, and hence
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4221 the speed. The idea is you can have a shorter table that decodes the
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4222 shorter, more probable codes, and then point to subsidiary tables for
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4223 the longer codes. The time it costs to decode the longer codes is
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4224 then traded against the time it takes to make longer tables.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4225
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4226 This results of this trade are in the variables lbits and dbits
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4227 below. lbits is the number of bits the first level table for literal/
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4228 length codes can decode in one step, and dbits is the same thing for
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4229 the distance codes. Subsequent tables are also less than or equal to
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4230 those sizes. These values may be adjusted either when all of the
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4231 codes are shorter than that, in which case the longest code length in
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4232 bits is used, or when the shortest code is *longer* than the requested
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4233 table size, in which case the length of the shortest code in bits is
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4234 used.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4235
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4236 There are two different values for the two tables, since they code a
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4237 different number of possibilities each. The literal/length table
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4238 codes 286 possible values, or in a flat code, a little over eight
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4239 bits. The distance table codes 30 possible values, or a little less
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4240 than five bits, flat. The optimum values for speed end up being
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4241 about one bit more than those, so lbits is 8+1 and dbits is 5+1.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4242 The optimum values may differ though from machine to machine, and
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4243 possibly even between compilers. Your mileage may vary.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4244 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4245
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4246
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4247 /* If BMAX needs to be larger than 16, then h and x[] should be uLong. */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4248 #define BMAX 15 /* maximum bit length of any code */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4249 #define N_MAX 288 /* maximum number of codes in any set */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4250
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4251 #ifdef DEBUG_ZLIB
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4252 uInt inflate_hufts;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4253 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4254
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4255 local int huft_build(b, n, s, d, e, t, m, zs)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4256 uIntf *b; /* code lengths in bits (all assumed <= BMAX) */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4257 uInt n; /* number of codes (assumed <= N_MAX) */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4258 uInt s; /* number of simple-valued codes (0..s-1) */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4259 const uIntf *d; /* list of base values for non-simple codes */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4260 const uIntf *e; /* list of extra bits for non-simple codes */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4261 inflate_huft * FAR *t; /* result: starting table */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4262 uIntf *m; /* maximum lookup bits, returns actual */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4263 z_streamp zs; /* for zalloc function */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4264 /* Given a list of code lengths and a maximum table size, make a set of
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4265 tables to decode that set of codes. Return Z_OK on success, Z_BUF_ERROR
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4266 if the given code set is incomplete (the tables are still built in this
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4267 case), Z_DATA_ERROR if the input is invalid (an over-subscribed set of
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4268 lengths), or Z_MEM_ERROR if not enough memory. */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4269 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4270
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4271 uInt a; /* counter for codes of length k */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4272 uInt c[BMAX+1]; /* bit length count table */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4273 uInt f; /* i repeats in table every f entries */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4274 int g; /* maximum code length */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4275 int h; /* table level */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4276 register uInt i; /* counter, current code */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4277 register uInt j; /* counter */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4278 register int k; /* number of bits in current code */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4279 int l; /* bits per table (returned in m) */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4280 register uIntf *p; /* pointer into c[], b[], or v[] */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4281 inflate_huft *q; /* points to current table */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4282 struct inflate_huft_s r; /* table entry for structure assignment */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4283 inflate_huft *u[BMAX]; /* table stack */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4284 uInt v[N_MAX]; /* values in order of bit length */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4285 register int w; /* bits before this table == (l * h) */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4286 uInt x[BMAX+1]; /* bit offsets, then code stack */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4287 uIntf *xp; /* pointer into x */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4288 int y; /* number of dummy codes added */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4289 uInt z; /* number of entries in current table */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4290
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4291
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4292 /* Generate counts for each bit length */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4293 p = c;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4294 #define C0 *p++ = 0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4295 #define C2 C0 C0 C0 C0
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4296 #define C4 C2 C2 C2 C2
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4297 C4 /* clear c[]--assume BMAX+1 is 16 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4298 p = b; i = n;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4299 do {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4300 c[*p++]++; /* assume all entries <= BMAX */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4301 } while (--i);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4302 if (c[0] == n) /* null input--all zero length codes */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4303 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4304 *t = (inflate_huft *)Z_NULL;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4305 *m = 0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4306 return Z_OK;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4307 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4308
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4309
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4310 /* Find minimum and maximum length, bound *m by those */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4311 l = *m;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4312 for (j = 1; j <= BMAX; j++)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4313 if (c[j])
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4314 break;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4315 k = j; /* minimum code length */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4316 if ((uInt)l < j)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4317 l = j;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4318 for (i = BMAX; i; i--)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4319 if (c[i])
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4320 break;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4321 g = i; /* maximum code length */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4322 if ((uInt)l > i)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4323 l = i;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4324 *m = l;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4325
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4326
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4327 /* Adjust last length count to fill out codes, if needed */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4328 for (y = 1 << j; j < i; j++, y <<= 1)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4329 if ((y -= c[j]) < 0)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4330 return Z_DATA_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4331 if ((y -= c[i]) < 0)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4332 return Z_DATA_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4333 c[i] += y;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4334
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4335
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4336 /* Generate starting offsets into the value table for each length */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4337 x[1] = j = 0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4338 p = c + 1; xp = x + 2;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4339 while (--i) { /* note that i == g from above */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4340 *xp++ = (j += *p++);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4341 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4342
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4343
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4344 /* Make a table of values in order of bit lengths */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4345 p = b; i = 0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4346 do {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4347 if ((j = *p++) != 0)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4348 v[x[j]++] = i;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4349 } while (++i < n);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4350 n = x[g]; /* set n to length of v */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4351
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4352
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4353 /* Generate the Huffman codes and for each, make the table entries */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4354 x[0] = i = 0; /* first Huffman code is zero */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4355 p = v; /* grab values in bit order */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4356 h = -1; /* no tables yet--level -1 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4357 w = -l; /* bits decoded == (l * h) */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4358 u[0] = (inflate_huft *)Z_NULL; /* just to keep compilers happy */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4359 q = (inflate_huft *)Z_NULL; /* ditto */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4360 z = 0; /* ditto */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4361
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4362 /* go through the bit lengths (k already is bits in shortest code) */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4363 for (; k <= g; k++)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4364 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4365 a = c[k];
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4366 while (a--)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4367 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4368 /* here i is the Huffman code of length k bits for value *p */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4369 /* make tables up to required level */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4370 while (k > w + l)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4371 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4372 h++;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4373 w += l; /* previous table always l bits */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4374
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4375 /* compute minimum size table less than or equal to l bits */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4376 z = g - w;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4377 z = z > (uInt)l ? l : z; /* table size upper limit */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4378 if ((f = 1 << (j = k - w)) > a + 1) /* try a k-w bit table */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4379 { /* too few codes for k-w bit table */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4380 f -= a + 1; /* deduct codes from patterns left */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4381 xp = c + k;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4382 if (j < z)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4383 while (++j < z) /* try smaller tables up to z bits */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4384 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4385 if ((f <<= 1) <= *++xp)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4386 break; /* enough codes to use up j bits */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4387 f -= *xp; /* else deduct codes from patterns */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4388 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4389 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4390 z = 1 << j; /* table entries for j-bit table */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4391
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4392 /* allocate and link in new table */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4393 if ((q = (inflate_huft *)ZALLOC
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4394 (zs,z + 1,sizeof(inflate_huft))) == Z_NULL)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4395 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4396 if (h)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4397 inflate_trees_free(u[0], zs);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4398 return Z_MEM_ERROR; /* not enough memory */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4399 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4400 #ifdef DEBUG_ZLIB
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4401 inflate_hufts += z + 1;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4402 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4403 *t = q + 1; /* link to list for huft_free() */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4404 *(t = &(q->next)) = Z_NULL;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4405 u[h] = ++q; /* table starts after link */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4406
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4407 /* connect to last table, if there is one */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4408 if (h)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4409 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4410 x[h] = i; /* save pattern for backing up */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4411 r.bits = (Byte)l; /* bits to dump before this table */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4412 r.exop = (Byte)j; /* bits in this table */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4413 r.next = q; /* pointer to this table */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4414 j = i >> (w - l); /* (get around Turbo C bug) */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4415 u[h-1][j] = r; /* connect to last table */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4416 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4417 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4418
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4419 /* set up table entry in r */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4420 r.bits = (Byte)(k - w);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4421 if (p >= v + n)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4422 r.exop = 128 + 64; /* out of values--invalid code */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4423 else if (*p < s)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4424 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4425 r.exop = (Byte)(*p < 256 ? 0 : 32 + 64); /* 256 is end-of-block */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4426 r.base = *p++; /* simple code is just the value */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4427 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4428 else
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4429 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4430 r.exop = (Byte)(e[*p - s] + 16 + 64);/* non-simple--look up in lists */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4431 r.base = d[*p++ - s];
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4432 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4433
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4434 /* fill code-like entries with r */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4435 f = 1 << (k - w);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4436 for (j = i >> w; j < z; j += f)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4437 q[j] = r;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4438
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4439 /* backwards increment the k-bit code i */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4440 for (j = 1 << (k - 1); i & j; j >>= 1)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4441 i ^= j;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4442 i ^= j;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4443
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4444 /* backup over finished tables */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4445 while ((i & ((1 << w) - 1)) != x[h])
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4446 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4447 h--; /* don't need to update q */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4448 w -= l;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4449 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4450 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4451 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4452
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4453
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4454 /* Return Z_BUF_ERROR if we were given an incomplete table */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4455 return y != 0 && g != 1 ? Z_BUF_ERROR : Z_OK;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4456 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4457
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4458
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4459 int inflate_trees_bits(c, bb, tb, z)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4460 uIntf *c; /* 19 code lengths */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4461 uIntf *bb; /* bits tree desired/actual depth */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4462 inflate_huft * FAR *tb; /* bits tree result */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4463 z_streamp z; /* for zfree function */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4464 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4465 int r;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4466
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4467 r = huft_build(c, 19, 19, (uIntf*)Z_NULL, (uIntf*)Z_NULL, tb, bb, z);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4468 if (r == Z_DATA_ERROR)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4469 z->msg = (char*)"oversubscribed dynamic bit lengths tree";
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4470 else if (r == Z_BUF_ERROR || *bb == 0)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4471 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4472 inflate_trees_free(*tb, z);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4473 z->msg = (char*)"incomplete dynamic bit lengths tree";
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4474 r = Z_DATA_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4475 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4476 return r;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4477 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4478
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4479
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4480 int inflate_trees_dynamic(nl, nd, c, bl, bd, tl, td, z)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4481 uInt nl; /* number of literal/length codes */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4482 uInt nd; /* number of distance codes */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4483 uIntf *c; /* that many (total) code lengths */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4484 uIntf *bl; /* literal desired/actual bit depth */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4485 uIntf *bd; /* distance desired/actual bit depth */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4486 inflate_huft * FAR *tl; /* literal/length tree result */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4487 inflate_huft * FAR *td; /* distance tree result */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4488 z_streamp z; /* for zfree function */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4489 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4490 int r;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4491
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4492 /* build literal/length tree */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4493 r = huft_build(c, nl, 257, cplens, cplext, tl, bl, z);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4494 if (r != Z_OK || *bl == 0)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4495 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4496 if (r == Z_DATA_ERROR)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4497 z->msg = (char*)"oversubscribed literal/length tree";
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4498 else if (r != Z_MEM_ERROR)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4499 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4500 inflate_trees_free(*tl, z);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4501 z->msg = (char*)"incomplete literal/length tree";
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4502 r = Z_DATA_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4503 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4504 return r;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4505 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4506
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4507 /* build distance tree */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4508 r = huft_build(c + nl, nd, 0, cpdist, cpdext, td, bd, z);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4509 if (r != Z_OK || (*bd == 0 && nl > 257))
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4510 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4511 if (r == Z_DATA_ERROR)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4512 z->msg = (char*)"oversubscribed distance tree";
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4513 else if (r == Z_BUF_ERROR) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4514 #ifdef PKZIP_BUG_WORKAROUND
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4515 r = Z_OK;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4516 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4517 #else
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4518 inflate_trees_free(*td, z);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4519 z->msg = (char*)"incomplete distance tree";
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4520 r = Z_DATA_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4521 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4522 else if (r != Z_MEM_ERROR)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4523 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4524 z->msg = (char*)"empty distance tree with lengths";
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4525 r = Z_DATA_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4526 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4527 inflate_trees_free(*tl, z);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4528 return r;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4529 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4530 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4531
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4532 /* done */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4533 return Z_OK;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4534 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4535
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4536
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4537 /* build fixed tables only once--keep them here */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4538 local int fixed_built = 0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4539 #define FIXEDH 530 /* number of hufts used by fixed tables */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4540 local inflate_huft fixed_mem[FIXEDH];
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4541 local uInt fixed_bl;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4542 local uInt fixed_bd;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4543 local inflate_huft *fixed_tl;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4544 local inflate_huft *fixed_td;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4545
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4546
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4547 local voidpf falloc(q, n, s)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4548 voidpf q; /* opaque pointer */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4549 uInt n; /* number of items */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4550 uInt s; /* size of item */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4551 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4552 Assert(s == sizeof(inflate_huft) && n <= *(intf *)q,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4553 "inflate_trees falloc overflow");
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4554 *(intf *)q -= n+s-s; /* s-s to avoid warning */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4555 return (voidpf)(fixed_mem + *(intf *)q);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4556 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4557
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4558
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4559 int inflate_trees_fixed(bl, bd, tl, td)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4560 uIntf *bl; /* literal desired/actual bit depth */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4561 uIntf *bd; /* distance desired/actual bit depth */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4562 inflate_huft * FAR *tl; /* literal/length tree result */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4563 inflate_huft * FAR *td; /* distance tree result */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4564 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4565 /* build fixed tables if not already (multiple overlapped executions ok) */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4566 if (!fixed_built)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4567 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4568 int k; /* temporary variable */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4569 unsigned c[288]; /* length list for huft_build */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4570 z_stream z; /* for falloc function */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4571 int f = FIXEDH; /* number of hufts left in fixed_mem */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4572
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4573 /* set up fake z_stream for memory routines */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4574 z.zalloc = falloc;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4575 z.zfree = Z_NULL;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4576 z.opaque = (voidpf)&f;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4577
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4578 /* literal table */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4579 for (k = 0; k < 144; k++)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4580 c[k] = 8;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4581 for (; k < 256; k++)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4582 c[k] = 9;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4583 for (; k < 280; k++)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4584 c[k] = 7;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4585 for (; k < 288; k++)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4586 c[k] = 8;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4587 fixed_bl = 7;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4588 huft_build(c, 288, 257, cplens, cplext, &fixed_tl, &fixed_bl, &z);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4589
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4590 /* distance table */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4591 for (k = 0; k < 30; k++)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4592 c[k] = 5;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4593 fixed_bd = 5;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4594 huft_build(c, 30, 0, cpdist, cpdext, &fixed_td, &fixed_bd, &z);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4595
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4596 /* done */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4597 Assert(f == 0, "invalid build of fixed tables");
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4598 fixed_built = 1;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4599 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4600 *bl = fixed_bl;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4601 *bd = fixed_bd;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4602 *tl = fixed_tl;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4603 *td = fixed_td;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4604 return Z_OK;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4605 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4606
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4607
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4608 int inflate_trees_free(t, z)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4609 inflate_huft *t; /* table to free */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4610 z_streamp z; /* for zfree function */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4611 /* Free the malloc'ed tables built by huft_build(), which makes a linked
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4612 list of the tables it made, with the links in a dummy first entry of
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4613 each table. */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4614 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4615 register inflate_huft *p, *q, *r;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4616
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4617 /* Reverse linked list */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4618 p = Z_NULL;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4619 q = t;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4620 while (q != Z_NULL)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4621 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4622 r = (q - 1)->next;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4623 (q - 1)->next = p;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4624 p = q;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4625 q = r;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4626 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4627 /* Go through linked list, freeing from the malloced (t[-1]) address. */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4628 while (p != Z_NULL)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4629 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4630 q = (--p)->next;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4631 ZFREE(z,p);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4632 p = q;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4633 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4634 return Z_OK;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4635 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4636 /* --- inftrees.c */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4637
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4638 /* +++ infcodes.c */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4639 /* infcodes.c -- process literals and length/distance pairs
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4640 * Copyright (C) 1995-1996 Mark Adler
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4641 * For conditions of distribution and use, see copyright notice in zlib.h
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4642 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4643
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4644 /* #include "zutil.h" */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4645 /* #include "inftrees.h" */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4646 /* #include "infblock.h" */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4647 /* #include "infcodes.h" */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4648 /* #include "infutil.h" */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4649
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4650 /* +++ inffast.h */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4651 /* inffast.h -- header to use inffast.c
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4652 * Copyright (C) 1995-1996 Mark Adler
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4653 * For conditions of distribution and use, see copyright notice in zlib.h
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4654 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4655
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4656 /* WARNING: this file should *not* be used by applications. It is
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4657 part of the implementation of the compression library and is
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4658 subject to change. Applications should only use zlib.h.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4659 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4660
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4661 extern int inflate_fast OF((
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4662 uInt,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4663 uInt,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4664 inflate_huft *,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4665 inflate_huft *,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4666 inflate_blocks_statef *,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4667 z_streamp ));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4668 /* --- inffast.h */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4669
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4670 /* simplify the use of the inflate_huft type with some defines */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4671 #define base more.Base
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4672 #define next more.Next
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4673 #define exop word.what.Exop
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4674 #define bits word.what.Bits
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4675
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4676 /* inflate codes private state */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4677 struct inflate_codes_state {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4678
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4679 /* mode */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4680 enum { /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4681 START, /* x: set up for LEN */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4682 LEN, /* i: get length/literal/eob next */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4683 LENEXT, /* i: getting length extra (have base) */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4684 DIST, /* i: get distance next */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4685 DISTEXT, /* i: getting distance extra */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4686 COPY, /* o: copying bytes in window, waiting for space */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4687 LIT, /* o: got literal, waiting for output space */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4688 WASH, /* o: got eob, possibly still output waiting */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4689 END, /* x: got eob and all data flushed */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4690 BADCODE} /* x: got error */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4691 mode; /* current inflate_codes mode */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4692
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4693 /* mode dependent information */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4694 uInt len;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4695 union {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4696 struct {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4697 inflate_huft *tree; /* pointer into tree */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4698 uInt need; /* bits needed */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4699 } code; /* if LEN or DIST, where in tree */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4700 uInt lit; /* if LIT, literal */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4701 struct {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4702 uInt get; /* bits to get for extra */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4703 uInt dist; /* distance back to copy from */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4704 } copy; /* if EXT or COPY, where and how much */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4705 } sub; /* submode */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4706
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4707 /* mode independent information */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4708 Byte lbits; /* ltree bits decoded per branch */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4709 Byte dbits; /* dtree bits decoder per branch */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4710 inflate_huft *ltree; /* literal/length/eob tree */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4711 inflate_huft *dtree; /* distance tree */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4712
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4713 };
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4714
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4715
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4716 inflate_codes_statef *inflate_codes_new(bl, bd, tl, td, z)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4717 uInt bl, bd;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4718 inflate_huft *tl;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4719 inflate_huft *td; /* need separate declaration for Borland C++ */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4720 z_streamp z;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4721 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4722 inflate_codes_statef *c;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4723
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4724 if ((c = (inflate_codes_statef *)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4725 ZALLOC(z,1,sizeof(struct inflate_codes_state))) != Z_NULL)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4726 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4727 c->mode = START;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4728 c->lbits = (Byte)bl;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4729 c->dbits = (Byte)bd;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4730 c->ltree = tl;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4731 c->dtree = td;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4732 Tracev((stderr, "inflate: codes new\n"));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4733 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4734 return c;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4735 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4736
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4737
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4738 int inflate_codes(s, z, r)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4739 inflate_blocks_statef *s;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4740 z_streamp z;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4741 int r;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4742 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4743 uInt j; /* temporary storage */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4744 inflate_huft *t; /* temporary pointer */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4745 uInt e; /* extra bits or operation */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4746 uLong b; /* bit buffer */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4747 uInt k; /* bits in bit buffer */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4748 Bytef *p; /* input data pointer */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4749 uInt n; /* bytes available there */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4750 Bytef *q; /* output window write pointer */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4751 uInt m; /* bytes to end of window or read pointer */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4752 Bytef *f; /* pointer to copy strings from */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4753 inflate_codes_statef *c = s->sub.decode.codes; /* codes state */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4754
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4755 /* copy input/output information to locals (UPDATE macro restores) */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4756 LOAD
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4757
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4758 /* process input and output based on current state */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4759 while (1) switch (c->mode)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4760 { /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4761 case START: /* x: set up for LEN */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4762 #ifndef SLOW
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4763 if (m >= 258 && n >= 10)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4764 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4765 UPDATE
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4766 r = inflate_fast(c->lbits, c->dbits, c->ltree, c->dtree, s, z);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4767 LOAD
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4768 if (r != Z_OK)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4769 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4770 c->mode = r == Z_STREAM_END ? WASH : BADCODE;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4771 break;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4772 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4773 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4774 #endif /* !SLOW */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4775 c->sub.code.need = c->lbits;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4776 c->sub.code.tree = c->ltree;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4777 c->mode = LEN;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4778 case LEN: /* i: get length/literal/eob next */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4779 j = c->sub.code.need;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4780 NEEDBITS(j)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4781 t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4782 DUMPBITS(t->bits)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4783 e = (uInt)(t->exop);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4784 if (e == 0) /* literal */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4785 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4786 c->sub.lit = t->base;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4787 Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4788 "inflate: literal '%c'\n" :
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4789 "inflate: literal 0x%02x\n", t->base));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4790 c->mode = LIT;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4791 break;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4792 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4793 if (e & 16) /* length */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4794 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4795 c->sub.copy.get = e & 15;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4796 c->len = t->base;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4797 c->mode = LENEXT;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4798 break;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4799 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4800 if ((e & 64) == 0) /* next table */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4801 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4802 c->sub.code.need = e;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4803 c->sub.code.tree = t->next;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4804 break;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4805 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4806 if (e & 32) /* end of block */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4807 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4808 Tracevv((stderr, "inflate: end of block\n"));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4809 c->mode = WASH;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4810 break;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4811 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4812 c->mode = BADCODE; /* invalid code */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4813 z->msg = (char*)"invalid literal/length code";
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4814 r = Z_DATA_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4815 LEAVE
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4816 case LENEXT: /* i: getting length extra (have base) */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4817 j = c->sub.copy.get;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4818 NEEDBITS(j)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4819 c->len += (uInt)b & inflate_mask[j];
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4820 DUMPBITS(j)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4821 c->sub.code.need = c->dbits;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4822 c->sub.code.tree = c->dtree;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4823 Tracevv((stderr, "inflate: length %u\n", c->len));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4824 c->mode = DIST;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4825 case DIST: /* i: get distance next */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4826 j = c->sub.code.need;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4827 NEEDBITS(j)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4828 t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4829 DUMPBITS(t->bits)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4830 e = (uInt)(t->exop);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4831 if (e & 16) /* distance */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4832 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4833 c->sub.copy.get = e & 15;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4834 c->sub.copy.dist = t->base;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4835 c->mode = DISTEXT;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4836 break;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4837 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4838 if ((e & 64) == 0) /* next table */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4839 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4840 c->sub.code.need = e;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4841 c->sub.code.tree = t->next;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4842 break;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4843 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4844 c->mode = BADCODE; /* invalid code */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4845 z->msg = (char*)"invalid distance code";
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4846 r = Z_DATA_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4847 LEAVE
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4848 case DISTEXT: /* i: getting distance extra */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4849 j = c->sub.copy.get;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4850 NEEDBITS(j)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4851 c->sub.copy.dist += (uInt)b & inflate_mask[j];
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4852 DUMPBITS(j)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4853 Tracevv((stderr, "inflate: distance %u\n", c->sub.copy.dist));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4854 c->mode = COPY;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4855 case COPY: /* o: copying bytes in window, waiting for space */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4856 #ifndef __TURBOC__ /* Turbo C bug for following expression */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4857 f = (uInt)(q - s->window) < c->sub.copy.dist ?
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4858 s->end - (c->sub.copy.dist - (q - s->window)) :
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4859 q - c->sub.copy.dist;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4860 #else
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4861 f = q - c->sub.copy.dist;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4862 if ((uInt)(q - s->window) < c->sub.copy.dist)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4863 f = s->end - (c->sub.copy.dist - (uInt)(q - s->window));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4864 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4865 while (c->len)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4866 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4867 NEEDOUT
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4868 OUTBYTE(*f++)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4869 if (f == s->end)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4870 f = s->window;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4871 c->len--;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4872 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4873 c->mode = START;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4874 break;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4875 case LIT: /* o: got literal, waiting for output space */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4876 NEEDOUT
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4877 OUTBYTE(c->sub.lit)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4878 c->mode = START;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4879 break;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4880 case WASH: /* o: got eob, possibly more output */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4881 FLUSH
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4882 if (s->read != s->write)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4883 LEAVE
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4884 c->mode = END;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4885 case END:
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4886 r = Z_STREAM_END;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4887 LEAVE
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4888 case BADCODE: /* x: got error */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4889 r = Z_DATA_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4890 LEAVE
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4891 default:
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4892 r = Z_STREAM_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4893 LEAVE
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4894 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4895 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4896
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4897
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4898 void inflate_codes_free(c, z)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4899 inflate_codes_statef *c;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4900 z_streamp z;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4901 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4902 ZFREE(z, c);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4903 Tracev((stderr, "inflate: codes free\n"));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4904 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4905 /* --- infcodes.c */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4906
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4907 /* +++ infutil.c */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4908 /* inflate_util.c -- data and routines common to blocks and codes
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4909 * Copyright (C) 1995-1996 Mark Adler
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4910 * For conditions of distribution and use, see copyright notice in zlib.h
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4911 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4912
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4913 /* #include "zutil.h" */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4914 /* #include "infblock.h" */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4915 /* #include "inftrees.h" */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4916 /* #include "infcodes.h" */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4917 /* #include "infutil.h" */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4918
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4919 #ifndef NO_DUMMY_DECL
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4920 struct inflate_codes_state {int dummy;}; /* for buggy compilers */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4921 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4922
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4923 /* And'ing with mask[n] masks the lower n bits */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4924 uInt inflate_mask[17] = {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4925 0x0000,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4926 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4927 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4928 };
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4929
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4930
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4931 /* copy as much as possible from the sliding window to the output area */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4932 int inflate_flush(s, z, r)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4933 inflate_blocks_statef *s;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4934 z_streamp z;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4935 int r;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4936 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4937 uInt n;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4938 Bytef *p;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4939 Bytef *q;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4940
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4941 /* local copies of source and destination pointers */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4942 p = z->next_out;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4943 q = s->read;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4944
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4945 /* compute number of bytes to copy as far as end of window */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4946 n = (uInt)((q <= s->write ? s->write : s->end) - q);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4947 if (n > z->avail_out) n = z->avail_out;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4948 if (n && r == Z_BUF_ERROR) r = Z_OK;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4949
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4950 /* update counters */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4951 z->avail_out -= n;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4952 z->total_out += n;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4953
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4954 /* update check information */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4955 if (s->checkfn != Z_NULL)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4956 z->adler = s->check = (*s->checkfn)(s->check, q, n);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4957
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4958 /* copy as far as end of window */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4959 if (p != Z_NULL) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4960 zmemcpy(p, q, n);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4961 p += n;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4962 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4963 q += n;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4964
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4965 /* see if more to copy at beginning of window */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4966 if (q == s->end)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4967 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4968 /* wrap pointers */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4969 q = s->window;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4970 if (s->write == s->end)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4971 s->write = s->window;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4972
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4973 /* compute bytes to copy */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4974 n = (uInt)(s->write - q);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4975 if (n > z->avail_out) n = z->avail_out;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4976 if (n && r == Z_BUF_ERROR) r = Z_OK;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4977
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4978 /* update counters */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4979 z->avail_out -= n;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4980 z->total_out += n;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4981
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4982 /* update check information */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4983 if (s->checkfn != Z_NULL)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4984 z->adler = s->check = (*s->checkfn)(s->check, q, n);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4985
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4986 /* copy */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4987 if (p != Z_NULL) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4988 zmemcpy(p, q, n);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4989 p += n;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4990 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4991 q += n;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4992 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4993
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4994 /* update pointers */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4995 z->next_out = p;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4996 s->read = q;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4997
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4998 /* done */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
4999 return r;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5000 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5001 /* --- infutil.c */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5002
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5003 /* +++ inffast.c */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5004 /* inffast.c -- process literals and length/distance pairs fast
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5005 * Copyright (C) 1995-1996 Mark Adler
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5006 * For conditions of distribution and use, see copyright notice in zlib.h
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5007 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5008
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5009 /* #include "zutil.h" */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5010 /* #include "inftrees.h" */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5011 /* #include "infblock.h" */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5012 /* #include "infcodes.h" */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5013 /* #include "infutil.h" */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5014 /* #include "inffast.h" */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5015
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5016 #ifndef NO_DUMMY_DECL
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5017 struct inflate_codes_state {int dummy;}; /* for buggy compilers */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5018 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5019
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5020 /* simplify the use of the inflate_huft type with some defines */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5021 #define base more.Base
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5022 #define next more.Next
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5023 #define exop word.what.Exop
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5024 #define bits word.what.Bits
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5025
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5026 /* macros for bit input with no checking and for returning unused bytes */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5027 #define GRABBITS(j) {while(k<(j)){b|=((uLong)NEXTBYTE)<<k;k+=8;}}
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5028 #define UNGRAB {n+=(c=k>>3);p-=c;k&=7;}
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5029
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5030 /* Called with number of bytes left to write in window at least 258
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5031 (the maximum string length) and number of input bytes available
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5032 at least ten. The ten bytes are six bytes for the longest length/
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5033 distance pair plus four bytes for overloading the bit buffer. */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5034
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5035 int inflate_fast(bl, bd, tl, td, s, z)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5036 uInt bl, bd;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5037 inflate_huft *tl;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5038 inflate_huft *td; /* need separate declaration for Borland C++ */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5039 inflate_blocks_statef *s;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5040 z_streamp z;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5041 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5042 inflate_huft *t; /* temporary pointer */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5043 uInt e; /* extra bits or operation */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5044 uLong b; /* bit buffer */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5045 uInt k; /* bits in bit buffer */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5046 Bytef *p; /* input data pointer */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5047 uInt n; /* bytes available there */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5048 Bytef *q; /* output window write pointer */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5049 uInt m; /* bytes to end of window or read pointer */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5050 uInt ml; /* mask for literal/length tree */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5051 uInt md; /* mask for distance tree */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5052 uInt c; /* bytes to copy */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5053 uInt d; /* distance back to copy from */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5054 Bytef *r; /* copy source pointer */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5055
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5056 /* load input, output, bit values */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5057 LOAD
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5058
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5059 /* initialize masks */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5060 ml = inflate_mask[bl];
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5061 md = inflate_mask[bd];
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5062
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5063 /* do until not enough input or output space for fast loop */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5064 do { /* assume called with m >= 258 && n >= 10 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5065 /* get literal/length code */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5066 GRABBITS(20) /* max bits for literal/length code */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5067 if ((e = (t = tl + ((uInt)b & ml))->exop) == 0)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5068 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5069 DUMPBITS(t->bits)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5070 Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5071 "inflate: * literal '%c'\n" :
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5072 "inflate: * literal 0x%02x\n", t->base));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5073 *q++ = (Byte)t->base;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5074 m--;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5075 continue;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5076 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5077 do {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5078 DUMPBITS(t->bits)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5079 if (e & 16)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5080 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5081 /* get extra bits for length */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5082 e &= 15;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5083 c = t->base + ((uInt)b & inflate_mask[e]);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5084 DUMPBITS(e)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5085 Tracevv((stderr, "inflate: * length %u\n", c));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5086
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5087 /* decode distance base of block to copy */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5088 GRABBITS(15); /* max bits for distance code */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5089 e = (t = td + ((uInt)b & md))->exop;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5090 do {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5091 DUMPBITS(t->bits)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5092 if (e & 16)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5093 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5094 /* get extra bits to add to distance base */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5095 e &= 15;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5096 GRABBITS(e) /* get extra bits (up to 13) */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5097 d = t->base + ((uInt)b & inflate_mask[e]);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5098 DUMPBITS(e)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5099 Tracevv((stderr, "inflate: * distance %u\n", d));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5100
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5101 /* do the copy */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5102 m -= c;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5103 if ((uInt)(q - s->window) >= d) /* offset before dest */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5104 { /* just copy */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5105 r = q - d;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5106 *q++ = *r++; c--; /* minimum count is three, */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5107 *q++ = *r++; c--; /* so unroll loop a little */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5108 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5109 else /* else offset after destination */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5110 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5111 e = d - (uInt)(q - s->window); /* bytes from offset to end */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5112 r = s->end - e; /* pointer to offset */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5113 if (c > e) /* if source crosses, */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5114 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5115 c -= e; /* copy to end of window */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5116 do {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5117 *q++ = *r++;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5118 } while (--e);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5119 r = s->window; /* copy rest from start of window */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5120 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5121 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5122 do { /* copy all or what's left */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5123 *q++ = *r++;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5124 } while (--c);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5125 break;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5126 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5127 else if ((e & 64) == 0)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5128 e = (t = t->next + ((uInt)b & inflate_mask[e]))->exop;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5129 else
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5130 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5131 z->msg = (char*)"invalid distance code";
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5132 UNGRAB
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5133 UPDATE
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5134 return Z_DATA_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5135 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5136 } while (1);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5137 break;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5138 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5139 if ((e & 64) == 0)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5140 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5141 if ((e = (t = t->next + ((uInt)b & inflate_mask[e]))->exop) == 0)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5142 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5143 DUMPBITS(t->bits)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5144 Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5145 "inflate: * literal '%c'\n" :
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5146 "inflate: * literal 0x%02x\n", t->base));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5147 *q++ = (Byte)t->base;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5148 m--;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5149 break;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5150 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5151 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5152 else if (e & 32)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5153 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5154 Tracevv((stderr, "inflate: * end of block\n"));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5155 UNGRAB
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5156 UPDATE
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5157 return Z_STREAM_END;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5158 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5159 else
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5160 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5161 z->msg = (char*)"invalid literal/length code";
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5162 UNGRAB
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5163 UPDATE
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5164 return Z_DATA_ERROR;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5165 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5166 } while (1);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5167 } while (m >= 258 && n >= 10);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5168
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5169 /* not enough input or output--restore pointers and return */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5170 UNGRAB
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5171 UPDATE
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5172 return Z_OK;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5173 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5174 /* --- inffast.c */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5175
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5176 /* +++ zutil.c */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5177 /* zutil.c -- target dependent utility functions for the compression library
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5178 * Copyright (C) 1995-1996 Jean-loup Gailly.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5179 * For conditions of distribution and use, see copyright notice in zlib.h
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5180 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5181
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5182 /* From: zutil.c,v 1.17 1996/07/24 13:41:12 me Exp $ */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5183
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5184 #ifdef DEBUG_ZLIB
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5185 #include <stdio.h>
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5186 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5187
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5188 /* #include "zutil.h" */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5189
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5190 #ifndef NO_DUMMY_DECL
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5191 struct internal_state {int dummy;}; /* for buggy compilers */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5192 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5193
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5194 #ifndef STDC
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5195 extern void exit OF((int));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5196 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5197
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5198 static const char *z_errmsg[10] = {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5199 "need dictionary", /* Z_NEED_DICT 2 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5200 "stream end", /* Z_STREAM_END 1 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5201 "", /* Z_OK 0 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5202 "file error", /* Z_ERRNO (-1) */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5203 "stream error", /* Z_STREAM_ERROR (-2) */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5204 "data error", /* Z_DATA_ERROR (-3) */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5205 "insufficient memory", /* Z_MEM_ERROR (-4) */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5206 "buffer error", /* Z_BUF_ERROR (-5) */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5207 "incompatible version",/* Z_VERSION_ERROR (-6) */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5208 ""};
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5209
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5210
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5211 const char *zlibVersion()
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5212 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5213 return ZLIB_VERSION;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5214 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5215
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5216 #ifdef DEBUG_ZLIB
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5217 void z_error (m)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5218 char *m;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5219 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5220 fprintf(stderr, "%s\n", m);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5221 exit(1);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5222 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5223 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5224
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5225 #ifndef HAVE_MEMCPY
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5226
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5227 void zmemcpy(dest, source, len)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5228 Bytef* dest;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5229 Bytef* source;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5230 uInt len;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5231 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5232 if (len == 0) return;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5233 do {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5234 *dest++ = *source++; /* ??? to be unrolled */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5235 } while (--len != 0);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5236 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5237
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5238 int zmemcmp(s1, s2, len)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5239 Bytef* s1;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5240 Bytef* s2;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5241 uInt len;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5242 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5243 uInt j;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5244
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5245 for (j = 0; j < len; j++) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5246 if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5247 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5248 return 0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5249 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5250
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5251 void zmemzero(dest, len)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5252 Bytef* dest;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5253 uInt len;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5254 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5255 if (len == 0) return;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5256 do {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5257 *dest++ = 0; /* ??? to be unrolled */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5258 } while (--len != 0);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5259 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5260 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5261
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5262 #ifdef __TURBOC__
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5263 #if (defined( __BORLANDC__) || !defined(SMALL_MEDIUM)) && !defined(__32BIT__)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5264 /* Small and medium model in Turbo C are for now limited to near allocation
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5265 * with reduced MAX_WBITS and MAX_MEM_LEVEL
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5266 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5267 # define MY_ZCALLOC
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5268
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5269 /* Turbo C malloc() does not allow dynamic allocation of 64K bytes
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5270 * and farmalloc(64K) returns a pointer with an offset of 8, so we
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5271 * must fix the pointer. Warning: the pointer must be put back to its
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5272 * original form in order to free it, use zcfree().
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5273 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5274
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5275 #define MAX_PTR 10
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5276 /* 10*64K = 640K */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5277
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5278 local int next_ptr = 0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5279
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5280 typedef struct ptr_table_s {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5281 voidpf org_ptr;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5282 voidpf new_ptr;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5283 } ptr_table;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5284
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5285 local ptr_table table[MAX_PTR];
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5286 /* This table is used to remember the original form of pointers
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5287 * to large buffers (64K). Such pointers are normalized with a zero offset.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5288 * Since MSDOS is not a preemptive multitasking OS, this table is not
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5289 * protected from concurrent access. This hack doesn't work anyway on
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5290 * a protected system like OS/2. Use Microsoft C instead.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5291 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5292
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5293 voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5294 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5295 voidpf buf = opaque; /* just to make some compilers happy */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5296 ulg bsize = (ulg)items*size;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5297
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5298 /* If we allocate less than 65520 bytes, we assume that farmalloc
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5299 * will return a usable pointer which doesn't have to be normalized.
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5300 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5301 if (bsize < 65520L) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5302 buf = farmalloc(bsize);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5303 if (*(ush*)&buf != 0) return buf;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5304 } else {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5305 buf = farmalloc(bsize + 16L);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5306 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5307 if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5308 table[next_ptr].org_ptr = buf;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5309
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5310 /* Normalize the pointer to seg:0 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5311 *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5312 *(ush*)&buf = 0;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5313 table[next_ptr++].new_ptr = buf;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5314 return buf;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5315 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5316
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5317 void zcfree (voidpf opaque, voidpf ptr)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5318 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5319 int n;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5320 if (*(ush*)&ptr != 0) { /* object < 64K */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5321 farfree(ptr);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5322 return;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5323 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5324 /* Find the original pointer */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5325 for (n = 0; n < next_ptr; n++) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5326 if (ptr != table[n].new_ptr) continue;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5327
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5328 farfree(table[n].org_ptr);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5329 while (++n < next_ptr) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5330 table[n-1] = table[n];
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5331 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5332 next_ptr--;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5333 return;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5334 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5335 ptr = opaque; /* just to make some compilers happy */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5336 Assert(0, "zcfree: ptr not found");
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5337 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5338 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5339 #endif /* __TURBOC__ */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5340
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5341
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5342 #if defined(M_I86) && !defined(__32BIT__)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5343 /* Microsoft C in 16-bit mode */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5344
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5345 # define MY_ZCALLOC
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5346
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5347 #if (!defined(_MSC_VER) || (_MSC_VER < 600))
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5348 # define _halloc halloc
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5349 # define _hfree hfree
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5350 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5351
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5352 voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5353 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5354 if (opaque) opaque = 0; /* to make compiler happy */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5355 return _halloc((long)items, size);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5356 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5357
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5358 void zcfree (voidpf opaque, voidpf ptr)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5359 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5360 if (opaque) opaque = 0; /* to make compiler happy */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5361 _hfree(ptr);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5362 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5363
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5364 #endif /* MSC */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5365
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5366
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5367 #ifndef MY_ZCALLOC /* Any system without a special alloc function */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5368
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5369 #ifndef STDC
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5370 extern voidp calloc OF((uInt items, uInt size));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5371 extern void free OF((voidpf ptr));
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5372 #endif
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5373
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5374 voidpf zcalloc (opaque, items, size)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5375 voidpf opaque;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5376 unsigned items;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5377 unsigned size;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5378 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5379 if (opaque) items += size - size; /* make compiler happy */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5380 return (voidpf)calloc(items, size);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5381 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5382
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5383 void zcfree (opaque, ptr)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5384 voidpf opaque;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5385 voidpf ptr;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5386 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5387 free(ptr);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5388 if (opaque) return; /* make compiler happy */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5389 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5390
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5391 #endif /* MY_ZCALLOC */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5392 /* --- zutil.c */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5393
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5394 /* +++ adler32.c */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5395 /* adler32.c -- compute the Adler-32 checksum of a data stream
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5396 * Copyright (C) 1995-1996 Mark Adler
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5397 * For conditions of distribution and use, see copyright notice in zlib.h
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5398 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5399
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5400 /* From: adler32.c,v 1.10 1996/05/22 11:52:18 me Exp $ */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5401
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5402 /* #include "zlib.h" */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5403
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5404 #define BASE 65521L /* largest prime smaller than 65536 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5405 #define NMAX 5552
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5406 /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5407
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5408 #define DO1(buf,i) {s1 += buf[i]; s2 += s1;}
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5409 #define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5410 #define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5411 #define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5412 #define DO16(buf) DO8(buf,0); DO8(buf,8);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5413
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5414 /* ========================================================================= */
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5415 uLong adler32(adler, buf, len)
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5416 uLong adler;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5417 const Bytef *buf;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5418 uInt len;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5419 {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5420 unsigned long s1 = adler & 0xffff;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5421 unsigned long s2 = (adler >> 16) & 0xffff;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5422 int k;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5423
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5424 if (buf == Z_NULL) return 1L;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5425
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5426 while (len > 0) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5427 k = len < NMAX ? len : NMAX;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5428 len -= k;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5429 while (k >= 16) {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5430 DO16(buf);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5431 buf += 16;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5432 k -= 16;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5433 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5434 if (k != 0) do {
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5435 s1 += *buf++;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5436 s2 += s1;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5437 } while (--k);
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5438 s1 %= BASE;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5439 s2 %= BASE;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5440 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5441 return (s2 << 16) | s1;
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5442 }
e0d88dc6a92f Added PPP network support.
nickg
parents:
diff changeset
5443 /* --- adler32.c */