1*4882a593Smuzhiyun /* inflate.c -- zlib decompression
2*4882a593Smuzhiyun * Copyright (C) 1995-2005 Mark Adler
3*4882a593Smuzhiyun * For conditions of distribution and use, see copyright notice in zlib.h
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Based on zlib 1.2.3 but modified for the Linux Kernel by
6*4882a593Smuzhiyun * Richard Purdie <richard@openedhand.com>
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * Changes mainly for static instead of dynamic memory allocation
9*4882a593Smuzhiyun *
10*4882a593Smuzhiyun */
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun #include <linux/zutil.h>
13*4882a593Smuzhiyun #include "inftrees.h"
14*4882a593Smuzhiyun #include "inflate.h"
15*4882a593Smuzhiyun #include "inffast.h"
16*4882a593Smuzhiyun #include "infutil.h"
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun /* architecture-specific bits */
19*4882a593Smuzhiyun #ifdef CONFIG_ZLIB_DFLTCC
20*4882a593Smuzhiyun # include "../zlib_dfltcc/dfltcc.h"
21*4882a593Smuzhiyun #else
22*4882a593Smuzhiyun #define INFLATE_RESET_HOOK(strm) do {} while (0)
23*4882a593Smuzhiyun #define INFLATE_TYPEDO_HOOK(strm, flush) do {} while (0)
24*4882a593Smuzhiyun #define INFLATE_NEED_UPDATEWINDOW(strm) 1
25*4882a593Smuzhiyun #define INFLATE_NEED_CHECKSUM(strm) 1
26*4882a593Smuzhiyun #endif
27*4882a593Smuzhiyun
zlib_inflate_workspacesize(void)28*4882a593Smuzhiyun int zlib_inflate_workspacesize(void)
29*4882a593Smuzhiyun {
30*4882a593Smuzhiyun return sizeof(struct inflate_workspace);
31*4882a593Smuzhiyun }
32*4882a593Smuzhiyun
zlib_inflateReset(z_streamp strm)33*4882a593Smuzhiyun int zlib_inflateReset(z_streamp strm)
34*4882a593Smuzhiyun {
35*4882a593Smuzhiyun struct inflate_state *state;
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun if (strm == NULL || strm->state == NULL) return Z_STREAM_ERROR;
38*4882a593Smuzhiyun state = (struct inflate_state *)strm->state;
39*4882a593Smuzhiyun strm->total_in = strm->total_out = state->total = 0;
40*4882a593Smuzhiyun strm->msg = NULL;
41*4882a593Smuzhiyun strm->adler = 1; /* to support ill-conceived Java test suite */
42*4882a593Smuzhiyun state->mode = HEAD;
43*4882a593Smuzhiyun state->last = 0;
44*4882a593Smuzhiyun state->havedict = 0;
45*4882a593Smuzhiyun state->dmax = 32768U;
46*4882a593Smuzhiyun state->hold = 0;
47*4882a593Smuzhiyun state->bits = 0;
48*4882a593Smuzhiyun state->lencode = state->distcode = state->next = state->codes;
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun /* Initialise Window */
51*4882a593Smuzhiyun state->wsize = 1U << state->wbits;
52*4882a593Smuzhiyun state->write = 0;
53*4882a593Smuzhiyun state->whave = 0;
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun INFLATE_RESET_HOOK(strm);
56*4882a593Smuzhiyun return Z_OK;
57*4882a593Smuzhiyun }
58*4882a593Smuzhiyun
zlib_inflateInit2(z_streamp strm,int windowBits)59*4882a593Smuzhiyun int zlib_inflateInit2(z_streamp strm, int windowBits)
60*4882a593Smuzhiyun {
61*4882a593Smuzhiyun struct inflate_state *state;
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun if (strm == NULL) return Z_STREAM_ERROR;
64*4882a593Smuzhiyun strm->msg = NULL; /* in case we return an error */
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun state = &WS(strm)->inflate_state;
67*4882a593Smuzhiyun strm->state = (struct internal_state *)state;
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun if (windowBits < 0) {
70*4882a593Smuzhiyun state->wrap = 0;
71*4882a593Smuzhiyun windowBits = -windowBits;
72*4882a593Smuzhiyun }
73*4882a593Smuzhiyun else {
74*4882a593Smuzhiyun state->wrap = (windowBits >> 4) + 1;
75*4882a593Smuzhiyun }
76*4882a593Smuzhiyun if (windowBits < 8 || windowBits > 15) {
77*4882a593Smuzhiyun return Z_STREAM_ERROR;
78*4882a593Smuzhiyun }
79*4882a593Smuzhiyun state->wbits = (unsigned)windowBits;
80*4882a593Smuzhiyun #ifdef CONFIG_ZLIB_DFLTCC
81*4882a593Smuzhiyun /*
82*4882a593Smuzhiyun * DFLTCC requires the window to be page aligned.
83*4882a593Smuzhiyun * Thus, we overallocate and take the aligned portion of the buffer.
84*4882a593Smuzhiyun */
85*4882a593Smuzhiyun state->window = PTR_ALIGN(&WS(strm)->working_window[0], PAGE_SIZE);
86*4882a593Smuzhiyun #else
87*4882a593Smuzhiyun state->window = &WS(strm)->working_window[0];
88*4882a593Smuzhiyun #endif
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun return zlib_inflateReset(strm);
91*4882a593Smuzhiyun }
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun /*
94*4882a593Smuzhiyun Return state with length and distance decoding tables and index sizes set to
95*4882a593Smuzhiyun fixed code decoding. This returns fixed tables from inffixed.h.
96*4882a593Smuzhiyun */
zlib_fixedtables(struct inflate_state * state)97*4882a593Smuzhiyun static void zlib_fixedtables(struct inflate_state *state)
98*4882a593Smuzhiyun {
99*4882a593Smuzhiyun # include "inffixed.h"
100*4882a593Smuzhiyun state->lencode = lenfix;
101*4882a593Smuzhiyun state->lenbits = 9;
102*4882a593Smuzhiyun state->distcode = distfix;
103*4882a593Smuzhiyun state->distbits = 5;
104*4882a593Smuzhiyun }
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun /*
108*4882a593Smuzhiyun Update the window with the last wsize (normally 32K) bytes written before
109*4882a593Smuzhiyun returning. This is only called when a window is already in use, or when
110*4882a593Smuzhiyun output has been written during this inflate call, but the end of the deflate
111*4882a593Smuzhiyun stream has not been reached yet. It is also called to window dictionary data
112*4882a593Smuzhiyun when a dictionary is loaded.
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun Providing output buffers larger than 32K to inflate() should provide a speed
115*4882a593Smuzhiyun advantage, since only the last 32K of output is copied to the sliding window
116*4882a593Smuzhiyun upon return from inflate(), and since all distances after the first 32K of
117*4882a593Smuzhiyun output will fall in the output data, making match copies simpler and faster.
118*4882a593Smuzhiyun The advantage may be dependent on the size of the processor's data caches.
119*4882a593Smuzhiyun */
zlib_updatewindow(z_streamp strm,unsigned out)120*4882a593Smuzhiyun static void zlib_updatewindow(z_streamp strm, unsigned out)
121*4882a593Smuzhiyun {
122*4882a593Smuzhiyun struct inflate_state *state;
123*4882a593Smuzhiyun unsigned copy, dist;
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun state = (struct inflate_state *)strm->state;
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun /* copy state->wsize or less output bytes into the circular window */
128*4882a593Smuzhiyun copy = out - strm->avail_out;
129*4882a593Smuzhiyun if (copy >= state->wsize) {
130*4882a593Smuzhiyun memcpy(state->window, strm->next_out - state->wsize, state->wsize);
131*4882a593Smuzhiyun state->write = 0;
132*4882a593Smuzhiyun state->whave = state->wsize;
133*4882a593Smuzhiyun }
134*4882a593Smuzhiyun else {
135*4882a593Smuzhiyun dist = state->wsize - state->write;
136*4882a593Smuzhiyun if (dist > copy) dist = copy;
137*4882a593Smuzhiyun memcpy(state->window + state->write, strm->next_out - copy, dist);
138*4882a593Smuzhiyun copy -= dist;
139*4882a593Smuzhiyun if (copy) {
140*4882a593Smuzhiyun memcpy(state->window, strm->next_out - copy, copy);
141*4882a593Smuzhiyun state->write = copy;
142*4882a593Smuzhiyun state->whave = state->wsize;
143*4882a593Smuzhiyun }
144*4882a593Smuzhiyun else {
145*4882a593Smuzhiyun state->write += dist;
146*4882a593Smuzhiyun if (state->write == state->wsize) state->write = 0;
147*4882a593Smuzhiyun if (state->whave < state->wsize) state->whave += dist;
148*4882a593Smuzhiyun }
149*4882a593Smuzhiyun }
150*4882a593Smuzhiyun }
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun /*
154*4882a593Smuzhiyun * At the end of a Deflate-compressed PPP packet, we expect to have seen
155*4882a593Smuzhiyun * a `stored' block type value but not the (zero) length bytes.
156*4882a593Smuzhiyun */
157*4882a593Smuzhiyun /*
158*4882a593Smuzhiyun Returns true if inflate is currently at the end of a block generated by
159*4882a593Smuzhiyun Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
160*4882a593Smuzhiyun implementation to provide an additional safety check. PPP uses
161*4882a593Smuzhiyun Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored
162*4882a593Smuzhiyun block. When decompressing, PPP checks that at the end of input packet,
163*4882a593Smuzhiyun inflate is waiting for these length bytes.
164*4882a593Smuzhiyun */
zlib_inflateSyncPacket(z_streamp strm)165*4882a593Smuzhiyun static int zlib_inflateSyncPacket(z_streamp strm)
166*4882a593Smuzhiyun {
167*4882a593Smuzhiyun struct inflate_state *state;
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun if (strm == NULL || strm->state == NULL) return Z_STREAM_ERROR;
170*4882a593Smuzhiyun state = (struct inflate_state *)strm->state;
171*4882a593Smuzhiyun
172*4882a593Smuzhiyun if (state->mode == STORED && state->bits == 0) {
173*4882a593Smuzhiyun state->mode = TYPE;
174*4882a593Smuzhiyun return Z_OK;
175*4882a593Smuzhiyun }
176*4882a593Smuzhiyun return Z_DATA_ERROR;
177*4882a593Smuzhiyun }
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun /* Macros for inflate(): */
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun /* check function to use adler32() for zlib or crc32() for gzip */
182*4882a593Smuzhiyun #define UPDATE(check, buf, len) zlib_adler32(check, buf, len)
183*4882a593Smuzhiyun
184*4882a593Smuzhiyun /* Load registers with state in inflate() for speed */
185*4882a593Smuzhiyun #define LOAD() \
186*4882a593Smuzhiyun do { \
187*4882a593Smuzhiyun put = strm->next_out; \
188*4882a593Smuzhiyun left = strm->avail_out; \
189*4882a593Smuzhiyun next = strm->next_in; \
190*4882a593Smuzhiyun have = strm->avail_in; \
191*4882a593Smuzhiyun hold = state->hold; \
192*4882a593Smuzhiyun bits = state->bits; \
193*4882a593Smuzhiyun } while (0)
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun /* Restore state from registers in inflate() */
196*4882a593Smuzhiyun #define RESTORE() \
197*4882a593Smuzhiyun do { \
198*4882a593Smuzhiyun strm->next_out = put; \
199*4882a593Smuzhiyun strm->avail_out = left; \
200*4882a593Smuzhiyun strm->next_in = next; \
201*4882a593Smuzhiyun strm->avail_in = have; \
202*4882a593Smuzhiyun state->hold = hold; \
203*4882a593Smuzhiyun state->bits = bits; \
204*4882a593Smuzhiyun } while (0)
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun /* Clear the input bit accumulator */
207*4882a593Smuzhiyun #define INITBITS() \
208*4882a593Smuzhiyun do { \
209*4882a593Smuzhiyun hold = 0; \
210*4882a593Smuzhiyun bits = 0; \
211*4882a593Smuzhiyun } while (0)
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun /* Get a byte of input into the bit accumulator, or return from inflate()
214*4882a593Smuzhiyun if there is no input available. */
215*4882a593Smuzhiyun #define PULLBYTE() \
216*4882a593Smuzhiyun do { \
217*4882a593Smuzhiyun if (have == 0) goto inf_leave; \
218*4882a593Smuzhiyun have--; \
219*4882a593Smuzhiyun hold += (unsigned long)(*next++) << bits; \
220*4882a593Smuzhiyun bits += 8; \
221*4882a593Smuzhiyun } while (0)
222*4882a593Smuzhiyun
223*4882a593Smuzhiyun /* Assure that there are at least n bits in the bit accumulator. If there is
224*4882a593Smuzhiyun not enough available input to do that, then return from inflate(). */
225*4882a593Smuzhiyun #define NEEDBITS(n) \
226*4882a593Smuzhiyun do { \
227*4882a593Smuzhiyun while (bits < (unsigned)(n)) \
228*4882a593Smuzhiyun PULLBYTE(); \
229*4882a593Smuzhiyun } while (0)
230*4882a593Smuzhiyun
231*4882a593Smuzhiyun /* Return the low n bits of the bit accumulator (n < 16) */
232*4882a593Smuzhiyun #define BITS(n) \
233*4882a593Smuzhiyun ((unsigned)hold & ((1U << (n)) - 1))
234*4882a593Smuzhiyun
235*4882a593Smuzhiyun /* Remove n bits from the bit accumulator */
236*4882a593Smuzhiyun #define DROPBITS(n) \
237*4882a593Smuzhiyun do { \
238*4882a593Smuzhiyun hold >>= (n); \
239*4882a593Smuzhiyun bits -= (unsigned)(n); \
240*4882a593Smuzhiyun } while (0)
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun /* Remove zero to seven bits as needed to go to a byte boundary */
243*4882a593Smuzhiyun #define BYTEBITS() \
244*4882a593Smuzhiyun do { \
245*4882a593Smuzhiyun hold >>= bits & 7; \
246*4882a593Smuzhiyun bits -= bits & 7; \
247*4882a593Smuzhiyun } while (0)
248*4882a593Smuzhiyun
249*4882a593Smuzhiyun /*
250*4882a593Smuzhiyun inflate() uses a state machine to process as much input data and generate as
251*4882a593Smuzhiyun much output data as possible before returning. The state machine is
252*4882a593Smuzhiyun structured roughly as follows:
253*4882a593Smuzhiyun
254*4882a593Smuzhiyun for (;;) switch (state) {
255*4882a593Smuzhiyun ...
256*4882a593Smuzhiyun case STATEn:
257*4882a593Smuzhiyun if (not enough input data or output space to make progress)
258*4882a593Smuzhiyun return;
259*4882a593Smuzhiyun ... make progress ...
260*4882a593Smuzhiyun state = STATEm;
261*4882a593Smuzhiyun break;
262*4882a593Smuzhiyun ...
263*4882a593Smuzhiyun }
264*4882a593Smuzhiyun
265*4882a593Smuzhiyun so when inflate() is called again, the same case is attempted again, and
266*4882a593Smuzhiyun if the appropriate resources are provided, the machine proceeds to the
267*4882a593Smuzhiyun next state. The NEEDBITS() macro is usually the way the state evaluates
268*4882a593Smuzhiyun whether it can proceed or should return. NEEDBITS() does the return if
269*4882a593Smuzhiyun the requested bits are not available. The typical use of the BITS macros
270*4882a593Smuzhiyun is:
271*4882a593Smuzhiyun
272*4882a593Smuzhiyun NEEDBITS(n);
273*4882a593Smuzhiyun ... do something with BITS(n) ...
274*4882a593Smuzhiyun DROPBITS(n);
275*4882a593Smuzhiyun
276*4882a593Smuzhiyun where NEEDBITS(n) either returns from inflate() if there isn't enough
277*4882a593Smuzhiyun input left to load n bits into the accumulator, or it continues. BITS(n)
278*4882a593Smuzhiyun gives the low n bits in the accumulator. When done, DROPBITS(n) drops
279*4882a593Smuzhiyun the low n bits off the accumulator. INITBITS() clears the accumulator
280*4882a593Smuzhiyun and sets the number of available bits to zero. BYTEBITS() discards just
281*4882a593Smuzhiyun enough bits to put the accumulator on a byte boundary. After BYTEBITS()
282*4882a593Smuzhiyun and a NEEDBITS(8), then BITS(8) would return the next byte in the stream.
283*4882a593Smuzhiyun
284*4882a593Smuzhiyun NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return
285*4882a593Smuzhiyun if there is no input available. The decoding of variable length codes uses
286*4882a593Smuzhiyun PULLBYTE() directly in order to pull just enough bytes to decode the next
287*4882a593Smuzhiyun code, and no more.
288*4882a593Smuzhiyun
289*4882a593Smuzhiyun Some states loop until they get enough input, making sure that enough
290*4882a593Smuzhiyun state information is maintained to continue the loop where it left off
291*4882a593Smuzhiyun if NEEDBITS() returns in the loop. For example, want, need, and keep
292*4882a593Smuzhiyun would all have to actually be part of the saved state in case NEEDBITS()
293*4882a593Smuzhiyun returns:
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun case STATEw:
296*4882a593Smuzhiyun while (want < need) {
297*4882a593Smuzhiyun NEEDBITS(n);
298*4882a593Smuzhiyun keep[want++] = BITS(n);
299*4882a593Smuzhiyun DROPBITS(n);
300*4882a593Smuzhiyun }
301*4882a593Smuzhiyun state = STATEx;
302*4882a593Smuzhiyun case STATEx:
303*4882a593Smuzhiyun
304*4882a593Smuzhiyun As shown above, if the next state is also the next case, then the break
305*4882a593Smuzhiyun is omitted.
306*4882a593Smuzhiyun
307*4882a593Smuzhiyun A state may also return if there is not enough output space available to
308*4882a593Smuzhiyun complete that state. Those states are copying stored data, writing a
309*4882a593Smuzhiyun literal byte, and copying a matching string.
310*4882a593Smuzhiyun
311*4882a593Smuzhiyun When returning, a "goto inf_leave" is used to update the total counters,
312*4882a593Smuzhiyun update the check value, and determine whether any progress has been made
313*4882a593Smuzhiyun during that inflate() call in order to return the proper return code.
314*4882a593Smuzhiyun Progress is defined as a change in either strm->avail_in or strm->avail_out.
315*4882a593Smuzhiyun When there is a window, goto inf_leave will update the window with the last
316*4882a593Smuzhiyun output written. If a goto inf_leave occurs in the middle of decompression
317*4882a593Smuzhiyun and there is no window currently, goto inf_leave will create one and copy
318*4882a593Smuzhiyun output to the window for the next call of inflate().
319*4882a593Smuzhiyun
320*4882a593Smuzhiyun In this implementation, the flush parameter of inflate() only affects the
321*4882a593Smuzhiyun return code (per zlib.h). inflate() always writes as much as possible to
322*4882a593Smuzhiyun strm->next_out, given the space available and the provided input--the effect
323*4882a593Smuzhiyun documented in zlib.h of Z_SYNC_FLUSH. Furthermore, inflate() always defers
324*4882a593Smuzhiyun the allocation of and copying into a sliding window until necessary, which
325*4882a593Smuzhiyun provides the effect documented in zlib.h for Z_FINISH when the entire input
326*4882a593Smuzhiyun stream available. So the only thing the flush parameter actually does is:
327*4882a593Smuzhiyun when flush is set to Z_FINISH, inflate() cannot return Z_OK. Instead it
328*4882a593Smuzhiyun will return Z_BUF_ERROR if it has not reached the end of the stream.
329*4882a593Smuzhiyun */
330*4882a593Smuzhiyun
zlib_inflate(z_streamp strm,int flush)331*4882a593Smuzhiyun int zlib_inflate(z_streamp strm, int flush)
332*4882a593Smuzhiyun {
333*4882a593Smuzhiyun struct inflate_state *state;
334*4882a593Smuzhiyun const unsigned char *next; /* next input */
335*4882a593Smuzhiyun unsigned char *put; /* next output */
336*4882a593Smuzhiyun unsigned have, left; /* available input and output */
337*4882a593Smuzhiyun unsigned long hold; /* bit buffer */
338*4882a593Smuzhiyun unsigned bits; /* bits in bit buffer */
339*4882a593Smuzhiyun unsigned in, out; /* save starting available input and output */
340*4882a593Smuzhiyun unsigned copy; /* number of stored or match bytes to copy */
341*4882a593Smuzhiyun unsigned char *from; /* where to copy match bytes from */
342*4882a593Smuzhiyun code this; /* current decoding table entry */
343*4882a593Smuzhiyun code last; /* parent table entry */
344*4882a593Smuzhiyun unsigned len; /* length to copy for repeats, bits to drop */
345*4882a593Smuzhiyun int ret; /* return code */
346*4882a593Smuzhiyun static const unsigned short order[19] = /* permutation of code lengths */
347*4882a593Smuzhiyun {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
348*4882a593Smuzhiyun
349*4882a593Smuzhiyun /* Do not check for strm->next_out == NULL here as ppc zImage
350*4882a593Smuzhiyun inflates to strm->next_out = 0 */
351*4882a593Smuzhiyun
352*4882a593Smuzhiyun if (strm == NULL || strm->state == NULL ||
353*4882a593Smuzhiyun (strm->next_in == NULL && strm->avail_in != 0))
354*4882a593Smuzhiyun return Z_STREAM_ERROR;
355*4882a593Smuzhiyun
356*4882a593Smuzhiyun state = (struct inflate_state *)strm->state;
357*4882a593Smuzhiyun
358*4882a593Smuzhiyun if (state->mode == TYPE) state->mode = TYPEDO; /* skip check */
359*4882a593Smuzhiyun LOAD();
360*4882a593Smuzhiyun in = have;
361*4882a593Smuzhiyun out = left;
362*4882a593Smuzhiyun ret = Z_OK;
363*4882a593Smuzhiyun for (;;)
364*4882a593Smuzhiyun switch (state->mode) {
365*4882a593Smuzhiyun case HEAD:
366*4882a593Smuzhiyun if (state->wrap == 0) {
367*4882a593Smuzhiyun state->mode = TYPEDO;
368*4882a593Smuzhiyun break;
369*4882a593Smuzhiyun }
370*4882a593Smuzhiyun NEEDBITS(16);
371*4882a593Smuzhiyun if (
372*4882a593Smuzhiyun ((BITS(8) << 8) + (hold >> 8)) % 31) {
373*4882a593Smuzhiyun strm->msg = (char *)"incorrect header check";
374*4882a593Smuzhiyun state->mode = BAD;
375*4882a593Smuzhiyun break;
376*4882a593Smuzhiyun }
377*4882a593Smuzhiyun if (BITS(4) != Z_DEFLATED) {
378*4882a593Smuzhiyun strm->msg = (char *)"unknown compression method";
379*4882a593Smuzhiyun state->mode = BAD;
380*4882a593Smuzhiyun break;
381*4882a593Smuzhiyun }
382*4882a593Smuzhiyun DROPBITS(4);
383*4882a593Smuzhiyun len = BITS(4) + 8;
384*4882a593Smuzhiyun if (len > state->wbits) {
385*4882a593Smuzhiyun strm->msg = (char *)"invalid window size";
386*4882a593Smuzhiyun state->mode = BAD;
387*4882a593Smuzhiyun break;
388*4882a593Smuzhiyun }
389*4882a593Smuzhiyun state->dmax = 1U << len;
390*4882a593Smuzhiyun strm->adler = state->check = zlib_adler32(0L, NULL, 0);
391*4882a593Smuzhiyun state->mode = hold & 0x200 ? DICTID : TYPE;
392*4882a593Smuzhiyun INITBITS();
393*4882a593Smuzhiyun break;
394*4882a593Smuzhiyun case DICTID:
395*4882a593Smuzhiyun NEEDBITS(32);
396*4882a593Smuzhiyun strm->adler = state->check = REVERSE(hold);
397*4882a593Smuzhiyun INITBITS();
398*4882a593Smuzhiyun state->mode = DICT;
399*4882a593Smuzhiyun /* fall through */
400*4882a593Smuzhiyun case DICT:
401*4882a593Smuzhiyun if (state->havedict == 0) {
402*4882a593Smuzhiyun RESTORE();
403*4882a593Smuzhiyun return Z_NEED_DICT;
404*4882a593Smuzhiyun }
405*4882a593Smuzhiyun strm->adler = state->check = zlib_adler32(0L, NULL, 0);
406*4882a593Smuzhiyun state->mode = TYPE;
407*4882a593Smuzhiyun /* fall through */
408*4882a593Smuzhiyun case TYPE:
409*4882a593Smuzhiyun if (flush == Z_BLOCK) goto inf_leave;
410*4882a593Smuzhiyun /* fall through */
411*4882a593Smuzhiyun case TYPEDO:
412*4882a593Smuzhiyun INFLATE_TYPEDO_HOOK(strm, flush);
413*4882a593Smuzhiyun if (state->last) {
414*4882a593Smuzhiyun BYTEBITS();
415*4882a593Smuzhiyun state->mode = CHECK;
416*4882a593Smuzhiyun break;
417*4882a593Smuzhiyun }
418*4882a593Smuzhiyun NEEDBITS(3);
419*4882a593Smuzhiyun state->last = BITS(1);
420*4882a593Smuzhiyun DROPBITS(1);
421*4882a593Smuzhiyun switch (BITS(2)) {
422*4882a593Smuzhiyun case 0: /* stored block */
423*4882a593Smuzhiyun state->mode = STORED;
424*4882a593Smuzhiyun break;
425*4882a593Smuzhiyun case 1: /* fixed block */
426*4882a593Smuzhiyun zlib_fixedtables(state);
427*4882a593Smuzhiyun state->mode = LEN; /* decode codes */
428*4882a593Smuzhiyun break;
429*4882a593Smuzhiyun case 2: /* dynamic block */
430*4882a593Smuzhiyun state->mode = TABLE;
431*4882a593Smuzhiyun break;
432*4882a593Smuzhiyun case 3:
433*4882a593Smuzhiyun strm->msg = (char *)"invalid block type";
434*4882a593Smuzhiyun state->mode = BAD;
435*4882a593Smuzhiyun }
436*4882a593Smuzhiyun DROPBITS(2);
437*4882a593Smuzhiyun break;
438*4882a593Smuzhiyun case STORED:
439*4882a593Smuzhiyun BYTEBITS(); /* go to byte boundary */
440*4882a593Smuzhiyun NEEDBITS(32);
441*4882a593Smuzhiyun if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
442*4882a593Smuzhiyun strm->msg = (char *)"invalid stored block lengths";
443*4882a593Smuzhiyun state->mode = BAD;
444*4882a593Smuzhiyun break;
445*4882a593Smuzhiyun }
446*4882a593Smuzhiyun state->length = (unsigned)hold & 0xffff;
447*4882a593Smuzhiyun INITBITS();
448*4882a593Smuzhiyun state->mode = COPY;
449*4882a593Smuzhiyun /* fall through */
450*4882a593Smuzhiyun case COPY:
451*4882a593Smuzhiyun copy = state->length;
452*4882a593Smuzhiyun if (copy) {
453*4882a593Smuzhiyun if (copy > have) copy = have;
454*4882a593Smuzhiyun if (copy > left) copy = left;
455*4882a593Smuzhiyun if (copy == 0) goto inf_leave;
456*4882a593Smuzhiyun memcpy(put, next, copy);
457*4882a593Smuzhiyun have -= copy;
458*4882a593Smuzhiyun next += copy;
459*4882a593Smuzhiyun left -= copy;
460*4882a593Smuzhiyun put += copy;
461*4882a593Smuzhiyun state->length -= copy;
462*4882a593Smuzhiyun break;
463*4882a593Smuzhiyun }
464*4882a593Smuzhiyun state->mode = TYPE;
465*4882a593Smuzhiyun break;
466*4882a593Smuzhiyun case TABLE:
467*4882a593Smuzhiyun NEEDBITS(14);
468*4882a593Smuzhiyun state->nlen = BITS(5) + 257;
469*4882a593Smuzhiyun DROPBITS(5);
470*4882a593Smuzhiyun state->ndist = BITS(5) + 1;
471*4882a593Smuzhiyun DROPBITS(5);
472*4882a593Smuzhiyun state->ncode = BITS(4) + 4;
473*4882a593Smuzhiyun DROPBITS(4);
474*4882a593Smuzhiyun #ifndef PKZIP_BUG_WORKAROUND
475*4882a593Smuzhiyun if (state->nlen > 286 || state->ndist > 30) {
476*4882a593Smuzhiyun strm->msg = (char *)"too many length or distance symbols";
477*4882a593Smuzhiyun state->mode = BAD;
478*4882a593Smuzhiyun break;
479*4882a593Smuzhiyun }
480*4882a593Smuzhiyun #endif
481*4882a593Smuzhiyun state->have = 0;
482*4882a593Smuzhiyun state->mode = LENLENS;
483*4882a593Smuzhiyun /* fall through */
484*4882a593Smuzhiyun case LENLENS:
485*4882a593Smuzhiyun while (state->have < state->ncode) {
486*4882a593Smuzhiyun NEEDBITS(3);
487*4882a593Smuzhiyun state->lens[order[state->have++]] = (unsigned short)BITS(3);
488*4882a593Smuzhiyun DROPBITS(3);
489*4882a593Smuzhiyun }
490*4882a593Smuzhiyun while (state->have < 19)
491*4882a593Smuzhiyun state->lens[order[state->have++]] = 0;
492*4882a593Smuzhiyun state->next = state->codes;
493*4882a593Smuzhiyun state->lencode = (code const *)(state->next);
494*4882a593Smuzhiyun state->lenbits = 7;
495*4882a593Smuzhiyun ret = zlib_inflate_table(CODES, state->lens, 19, &(state->next),
496*4882a593Smuzhiyun &(state->lenbits), state->work);
497*4882a593Smuzhiyun if (ret) {
498*4882a593Smuzhiyun strm->msg = (char *)"invalid code lengths set";
499*4882a593Smuzhiyun state->mode = BAD;
500*4882a593Smuzhiyun break;
501*4882a593Smuzhiyun }
502*4882a593Smuzhiyun state->have = 0;
503*4882a593Smuzhiyun state->mode = CODELENS;
504*4882a593Smuzhiyun /* fall through */
505*4882a593Smuzhiyun case CODELENS:
506*4882a593Smuzhiyun while (state->have < state->nlen + state->ndist) {
507*4882a593Smuzhiyun for (;;) {
508*4882a593Smuzhiyun this = state->lencode[BITS(state->lenbits)];
509*4882a593Smuzhiyun if ((unsigned)(this.bits) <= bits) break;
510*4882a593Smuzhiyun PULLBYTE();
511*4882a593Smuzhiyun }
512*4882a593Smuzhiyun if (this.val < 16) {
513*4882a593Smuzhiyun NEEDBITS(this.bits);
514*4882a593Smuzhiyun DROPBITS(this.bits);
515*4882a593Smuzhiyun state->lens[state->have++] = this.val;
516*4882a593Smuzhiyun }
517*4882a593Smuzhiyun else {
518*4882a593Smuzhiyun if (this.val == 16) {
519*4882a593Smuzhiyun NEEDBITS(this.bits + 2);
520*4882a593Smuzhiyun DROPBITS(this.bits);
521*4882a593Smuzhiyun if (state->have == 0) {
522*4882a593Smuzhiyun strm->msg = (char *)"invalid bit length repeat";
523*4882a593Smuzhiyun state->mode = BAD;
524*4882a593Smuzhiyun break;
525*4882a593Smuzhiyun }
526*4882a593Smuzhiyun len = state->lens[state->have - 1];
527*4882a593Smuzhiyun copy = 3 + BITS(2);
528*4882a593Smuzhiyun DROPBITS(2);
529*4882a593Smuzhiyun }
530*4882a593Smuzhiyun else if (this.val == 17) {
531*4882a593Smuzhiyun NEEDBITS(this.bits + 3);
532*4882a593Smuzhiyun DROPBITS(this.bits);
533*4882a593Smuzhiyun len = 0;
534*4882a593Smuzhiyun copy = 3 + BITS(3);
535*4882a593Smuzhiyun DROPBITS(3);
536*4882a593Smuzhiyun }
537*4882a593Smuzhiyun else {
538*4882a593Smuzhiyun NEEDBITS(this.bits + 7);
539*4882a593Smuzhiyun DROPBITS(this.bits);
540*4882a593Smuzhiyun len = 0;
541*4882a593Smuzhiyun copy = 11 + BITS(7);
542*4882a593Smuzhiyun DROPBITS(7);
543*4882a593Smuzhiyun }
544*4882a593Smuzhiyun if (state->have + copy > state->nlen + state->ndist) {
545*4882a593Smuzhiyun strm->msg = (char *)"invalid bit length repeat";
546*4882a593Smuzhiyun state->mode = BAD;
547*4882a593Smuzhiyun break;
548*4882a593Smuzhiyun }
549*4882a593Smuzhiyun while (copy--)
550*4882a593Smuzhiyun state->lens[state->have++] = (unsigned short)len;
551*4882a593Smuzhiyun }
552*4882a593Smuzhiyun }
553*4882a593Smuzhiyun
554*4882a593Smuzhiyun /* handle error breaks in while */
555*4882a593Smuzhiyun if (state->mode == BAD) break;
556*4882a593Smuzhiyun
557*4882a593Smuzhiyun /* build code tables */
558*4882a593Smuzhiyun state->next = state->codes;
559*4882a593Smuzhiyun state->lencode = (code const *)(state->next);
560*4882a593Smuzhiyun state->lenbits = 9;
561*4882a593Smuzhiyun ret = zlib_inflate_table(LENS, state->lens, state->nlen, &(state->next),
562*4882a593Smuzhiyun &(state->lenbits), state->work);
563*4882a593Smuzhiyun if (ret) {
564*4882a593Smuzhiyun strm->msg = (char *)"invalid literal/lengths set";
565*4882a593Smuzhiyun state->mode = BAD;
566*4882a593Smuzhiyun break;
567*4882a593Smuzhiyun }
568*4882a593Smuzhiyun state->distcode = (code const *)(state->next);
569*4882a593Smuzhiyun state->distbits = 6;
570*4882a593Smuzhiyun ret = zlib_inflate_table(DISTS, state->lens + state->nlen, state->ndist,
571*4882a593Smuzhiyun &(state->next), &(state->distbits), state->work);
572*4882a593Smuzhiyun if (ret) {
573*4882a593Smuzhiyun strm->msg = (char *)"invalid distances set";
574*4882a593Smuzhiyun state->mode = BAD;
575*4882a593Smuzhiyun break;
576*4882a593Smuzhiyun }
577*4882a593Smuzhiyun state->mode = LEN;
578*4882a593Smuzhiyun /* fall through */
579*4882a593Smuzhiyun case LEN:
580*4882a593Smuzhiyun if (have >= 6 && left >= 258) {
581*4882a593Smuzhiyun RESTORE();
582*4882a593Smuzhiyun inflate_fast(strm, out);
583*4882a593Smuzhiyun LOAD();
584*4882a593Smuzhiyun break;
585*4882a593Smuzhiyun }
586*4882a593Smuzhiyun for (;;) {
587*4882a593Smuzhiyun this = state->lencode[BITS(state->lenbits)];
588*4882a593Smuzhiyun if ((unsigned)(this.bits) <= bits) break;
589*4882a593Smuzhiyun PULLBYTE();
590*4882a593Smuzhiyun }
591*4882a593Smuzhiyun if (this.op && (this.op & 0xf0) == 0) {
592*4882a593Smuzhiyun last = this;
593*4882a593Smuzhiyun for (;;) {
594*4882a593Smuzhiyun this = state->lencode[last.val +
595*4882a593Smuzhiyun (BITS(last.bits + last.op) >> last.bits)];
596*4882a593Smuzhiyun if ((unsigned)(last.bits + this.bits) <= bits) break;
597*4882a593Smuzhiyun PULLBYTE();
598*4882a593Smuzhiyun }
599*4882a593Smuzhiyun DROPBITS(last.bits);
600*4882a593Smuzhiyun }
601*4882a593Smuzhiyun DROPBITS(this.bits);
602*4882a593Smuzhiyun state->length = (unsigned)this.val;
603*4882a593Smuzhiyun if ((int)(this.op) == 0) {
604*4882a593Smuzhiyun state->mode = LIT;
605*4882a593Smuzhiyun break;
606*4882a593Smuzhiyun }
607*4882a593Smuzhiyun if (this.op & 32) {
608*4882a593Smuzhiyun state->mode = TYPE;
609*4882a593Smuzhiyun break;
610*4882a593Smuzhiyun }
611*4882a593Smuzhiyun if (this.op & 64) {
612*4882a593Smuzhiyun strm->msg = (char *)"invalid literal/length code";
613*4882a593Smuzhiyun state->mode = BAD;
614*4882a593Smuzhiyun break;
615*4882a593Smuzhiyun }
616*4882a593Smuzhiyun state->extra = (unsigned)(this.op) & 15;
617*4882a593Smuzhiyun state->mode = LENEXT;
618*4882a593Smuzhiyun /* fall through */
619*4882a593Smuzhiyun case LENEXT:
620*4882a593Smuzhiyun if (state->extra) {
621*4882a593Smuzhiyun NEEDBITS(state->extra);
622*4882a593Smuzhiyun state->length += BITS(state->extra);
623*4882a593Smuzhiyun DROPBITS(state->extra);
624*4882a593Smuzhiyun }
625*4882a593Smuzhiyun state->mode = DIST;
626*4882a593Smuzhiyun /* fall through */
627*4882a593Smuzhiyun case DIST:
628*4882a593Smuzhiyun for (;;) {
629*4882a593Smuzhiyun this = state->distcode[BITS(state->distbits)];
630*4882a593Smuzhiyun if ((unsigned)(this.bits) <= bits) break;
631*4882a593Smuzhiyun PULLBYTE();
632*4882a593Smuzhiyun }
633*4882a593Smuzhiyun if ((this.op & 0xf0) == 0) {
634*4882a593Smuzhiyun last = this;
635*4882a593Smuzhiyun for (;;) {
636*4882a593Smuzhiyun this = state->distcode[last.val +
637*4882a593Smuzhiyun (BITS(last.bits + last.op) >> last.bits)];
638*4882a593Smuzhiyun if ((unsigned)(last.bits + this.bits) <= bits) break;
639*4882a593Smuzhiyun PULLBYTE();
640*4882a593Smuzhiyun }
641*4882a593Smuzhiyun DROPBITS(last.bits);
642*4882a593Smuzhiyun }
643*4882a593Smuzhiyun DROPBITS(this.bits);
644*4882a593Smuzhiyun if (this.op & 64) {
645*4882a593Smuzhiyun strm->msg = (char *)"invalid distance code";
646*4882a593Smuzhiyun state->mode = BAD;
647*4882a593Smuzhiyun break;
648*4882a593Smuzhiyun }
649*4882a593Smuzhiyun state->offset = (unsigned)this.val;
650*4882a593Smuzhiyun state->extra = (unsigned)(this.op) & 15;
651*4882a593Smuzhiyun state->mode = DISTEXT;
652*4882a593Smuzhiyun /* fall through */
653*4882a593Smuzhiyun case DISTEXT:
654*4882a593Smuzhiyun if (state->extra) {
655*4882a593Smuzhiyun NEEDBITS(state->extra);
656*4882a593Smuzhiyun state->offset += BITS(state->extra);
657*4882a593Smuzhiyun DROPBITS(state->extra);
658*4882a593Smuzhiyun }
659*4882a593Smuzhiyun #ifdef INFLATE_STRICT
660*4882a593Smuzhiyun if (state->offset > state->dmax) {
661*4882a593Smuzhiyun strm->msg = (char *)"invalid distance too far back";
662*4882a593Smuzhiyun state->mode = BAD;
663*4882a593Smuzhiyun break;
664*4882a593Smuzhiyun }
665*4882a593Smuzhiyun #endif
666*4882a593Smuzhiyun if (state->offset > state->whave + out - left) {
667*4882a593Smuzhiyun strm->msg = (char *)"invalid distance too far back";
668*4882a593Smuzhiyun state->mode = BAD;
669*4882a593Smuzhiyun break;
670*4882a593Smuzhiyun }
671*4882a593Smuzhiyun state->mode = MATCH;
672*4882a593Smuzhiyun /* fall through */
673*4882a593Smuzhiyun case MATCH:
674*4882a593Smuzhiyun if (left == 0) goto inf_leave;
675*4882a593Smuzhiyun copy = out - left;
676*4882a593Smuzhiyun if (state->offset > copy) { /* copy from window */
677*4882a593Smuzhiyun copy = state->offset - copy;
678*4882a593Smuzhiyun if (copy > state->write) {
679*4882a593Smuzhiyun copy -= state->write;
680*4882a593Smuzhiyun from = state->window + (state->wsize - copy);
681*4882a593Smuzhiyun }
682*4882a593Smuzhiyun else
683*4882a593Smuzhiyun from = state->window + (state->write - copy);
684*4882a593Smuzhiyun if (copy > state->length) copy = state->length;
685*4882a593Smuzhiyun }
686*4882a593Smuzhiyun else { /* copy from output */
687*4882a593Smuzhiyun from = put - state->offset;
688*4882a593Smuzhiyun copy = state->length;
689*4882a593Smuzhiyun }
690*4882a593Smuzhiyun if (copy > left) copy = left;
691*4882a593Smuzhiyun left -= copy;
692*4882a593Smuzhiyun state->length -= copy;
693*4882a593Smuzhiyun do {
694*4882a593Smuzhiyun *put++ = *from++;
695*4882a593Smuzhiyun } while (--copy);
696*4882a593Smuzhiyun if (state->length == 0) state->mode = LEN;
697*4882a593Smuzhiyun break;
698*4882a593Smuzhiyun case LIT:
699*4882a593Smuzhiyun if (left == 0) goto inf_leave;
700*4882a593Smuzhiyun *put++ = (unsigned char)(state->length);
701*4882a593Smuzhiyun left--;
702*4882a593Smuzhiyun state->mode = LEN;
703*4882a593Smuzhiyun break;
704*4882a593Smuzhiyun case CHECK:
705*4882a593Smuzhiyun if (state->wrap) {
706*4882a593Smuzhiyun NEEDBITS(32);
707*4882a593Smuzhiyun out -= left;
708*4882a593Smuzhiyun strm->total_out += out;
709*4882a593Smuzhiyun state->total += out;
710*4882a593Smuzhiyun if (INFLATE_NEED_CHECKSUM(strm) && out)
711*4882a593Smuzhiyun strm->adler = state->check =
712*4882a593Smuzhiyun UPDATE(state->check, put - out, out);
713*4882a593Smuzhiyun out = left;
714*4882a593Smuzhiyun if ((
715*4882a593Smuzhiyun REVERSE(hold)) != state->check) {
716*4882a593Smuzhiyun strm->msg = (char *)"incorrect data check";
717*4882a593Smuzhiyun state->mode = BAD;
718*4882a593Smuzhiyun break;
719*4882a593Smuzhiyun }
720*4882a593Smuzhiyun INITBITS();
721*4882a593Smuzhiyun }
722*4882a593Smuzhiyun state->mode = DONE;
723*4882a593Smuzhiyun /* fall through */
724*4882a593Smuzhiyun case DONE:
725*4882a593Smuzhiyun ret = Z_STREAM_END;
726*4882a593Smuzhiyun goto inf_leave;
727*4882a593Smuzhiyun case BAD:
728*4882a593Smuzhiyun ret = Z_DATA_ERROR;
729*4882a593Smuzhiyun goto inf_leave;
730*4882a593Smuzhiyun case MEM:
731*4882a593Smuzhiyun return Z_MEM_ERROR;
732*4882a593Smuzhiyun case SYNC:
733*4882a593Smuzhiyun default:
734*4882a593Smuzhiyun return Z_STREAM_ERROR;
735*4882a593Smuzhiyun }
736*4882a593Smuzhiyun
737*4882a593Smuzhiyun /*
738*4882a593Smuzhiyun Return from inflate(), updating the total counts and the check value.
739*4882a593Smuzhiyun If there was no progress during the inflate() call, return a buffer
740*4882a593Smuzhiyun error. Call zlib_updatewindow() to create and/or update the window state.
741*4882a593Smuzhiyun */
742*4882a593Smuzhiyun inf_leave:
743*4882a593Smuzhiyun RESTORE();
744*4882a593Smuzhiyun if (INFLATE_NEED_UPDATEWINDOW(strm) &&
745*4882a593Smuzhiyun (state->wsize || (state->mode < CHECK && out != strm->avail_out)))
746*4882a593Smuzhiyun zlib_updatewindow(strm, out);
747*4882a593Smuzhiyun
748*4882a593Smuzhiyun in -= strm->avail_in;
749*4882a593Smuzhiyun out -= strm->avail_out;
750*4882a593Smuzhiyun strm->total_in += in;
751*4882a593Smuzhiyun strm->total_out += out;
752*4882a593Smuzhiyun state->total += out;
753*4882a593Smuzhiyun if (INFLATE_NEED_CHECKSUM(strm) && state->wrap && out)
754*4882a593Smuzhiyun strm->adler = state->check =
755*4882a593Smuzhiyun UPDATE(state->check, strm->next_out - out, out);
756*4882a593Smuzhiyun
757*4882a593Smuzhiyun strm->data_type = state->bits + (state->last ? 64 : 0) +
758*4882a593Smuzhiyun (state->mode == TYPE ? 128 : 0);
759*4882a593Smuzhiyun
760*4882a593Smuzhiyun if (flush == Z_PACKET_FLUSH && ret == Z_OK &&
761*4882a593Smuzhiyun strm->avail_out != 0 && strm->avail_in == 0)
762*4882a593Smuzhiyun return zlib_inflateSyncPacket(strm);
763*4882a593Smuzhiyun
764*4882a593Smuzhiyun if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
765*4882a593Smuzhiyun ret = Z_BUF_ERROR;
766*4882a593Smuzhiyun
767*4882a593Smuzhiyun return ret;
768*4882a593Smuzhiyun }
769*4882a593Smuzhiyun
zlib_inflateEnd(z_streamp strm)770*4882a593Smuzhiyun int zlib_inflateEnd(z_streamp strm)
771*4882a593Smuzhiyun {
772*4882a593Smuzhiyun if (strm == NULL || strm->state == NULL)
773*4882a593Smuzhiyun return Z_STREAM_ERROR;
774*4882a593Smuzhiyun return Z_OK;
775*4882a593Smuzhiyun }
776*4882a593Smuzhiyun
777*4882a593Smuzhiyun /*
778*4882a593Smuzhiyun * This subroutine adds the data at next_in/avail_in to the output history
779*4882a593Smuzhiyun * without performing any output. The output buffer must be "caught up";
780*4882a593Smuzhiyun * i.e. no pending output but this should always be the case. The state must
781*4882a593Smuzhiyun * be waiting on the start of a block (i.e. mode == TYPE or HEAD). On exit,
782*4882a593Smuzhiyun * the output will also be caught up, and the checksum will have been updated
783*4882a593Smuzhiyun * if need be.
784*4882a593Smuzhiyun */
zlib_inflateIncomp(z_stream * z)785*4882a593Smuzhiyun int zlib_inflateIncomp(z_stream *z)
786*4882a593Smuzhiyun {
787*4882a593Smuzhiyun struct inflate_state *state = (struct inflate_state *)z->state;
788*4882a593Smuzhiyun Byte *saved_no = z->next_out;
789*4882a593Smuzhiyun uInt saved_ao = z->avail_out;
790*4882a593Smuzhiyun
791*4882a593Smuzhiyun if (state->mode != TYPE && state->mode != HEAD)
792*4882a593Smuzhiyun return Z_DATA_ERROR;
793*4882a593Smuzhiyun
794*4882a593Smuzhiyun /* Setup some variables to allow misuse of updateWindow */
795*4882a593Smuzhiyun z->avail_out = 0;
796*4882a593Smuzhiyun z->next_out = (unsigned char*)z->next_in + z->avail_in;
797*4882a593Smuzhiyun
798*4882a593Smuzhiyun zlib_updatewindow(z, z->avail_in);
799*4882a593Smuzhiyun
800*4882a593Smuzhiyun /* Restore saved variables */
801*4882a593Smuzhiyun z->avail_out = saved_ao;
802*4882a593Smuzhiyun z->next_out = saved_no;
803*4882a593Smuzhiyun
804*4882a593Smuzhiyun z->adler = state->check =
805*4882a593Smuzhiyun UPDATE(state->check, z->next_in, z->avail_in);
806*4882a593Smuzhiyun
807*4882a593Smuzhiyun z->total_out += z->avail_in;
808*4882a593Smuzhiyun z->total_in += z->avail_in;
809*4882a593Smuzhiyun z->next_in += z->avail_in;
810*4882a593Smuzhiyun state->total += z->avail_in;
811*4882a593Smuzhiyun z->avail_in = 0;
812*4882a593Smuzhiyun
813*4882a593Smuzhiyun return Z_OK;
814*4882a593Smuzhiyun }
815