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