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