1*96c32821Schristos /* $NetBSD: inflate.c,v 1.8 2024/09/22 19:12:27 christos Exp $ */ 2aaf4ece6Schristos 3aaf4ece6Schristos /* inflate.c -- zlib decompression 43b1253e8Schristos * Copyright (C) 1995-2022 Mark Adler 5aaf4ece6Schristos * For conditions of distribution and use, see copyright notice in zlib.h 6aaf4ece6Schristos */ 7aaf4ece6Schristos 8aaf4ece6Schristos /* 9aaf4ece6Schristos * Change history: 10aaf4ece6Schristos * 11aaf4ece6Schristos * 1.2.beta0 24 Nov 2002 12aaf4ece6Schristos * - First version -- complete rewrite of inflate to simplify code, avoid 13aaf4ece6Schristos * creation of window when not needed, minimize use of window when it is 14aaf4ece6Schristos * needed, make inffast.c even faster, implement gzip decoding, and to 15aaf4ece6Schristos * improve code readability and style over the previous zlib inflate code 16aaf4ece6Schristos * 17aaf4ece6Schristos * 1.2.beta1 25 Nov 2002 18aaf4ece6Schristos * - Use pointers for available input and output checking in inffast.c 19aaf4ece6Schristos * - Remove input and output counters in inffast.c 20aaf4ece6Schristos * - Change inffast.c entry and loop from avail_in >= 7 to >= 6 21aaf4ece6Schristos * - Remove unnecessary second byte pull from length extra in inffast.c 22aaf4ece6Schristos * - Unroll direct copy to three copies per loop in inffast.c 23aaf4ece6Schristos * 24aaf4ece6Schristos * 1.2.beta2 4 Dec 2002 25aaf4ece6Schristos * - Change external routine names to reduce potential conflicts 26aaf4ece6Schristos * - Correct filename to inffixed.h for fixed tables in inflate.c 27aaf4ece6Schristos * - Make hbuf[] unsigned char to match parameter type in inflate.c 28aaf4ece6Schristos * - Change strm->next_out[-state->offset] to *(strm->next_out - state->offset) 29aaf4ece6Schristos * to avoid negation problem on Alphas (64 bit) in inflate.c 30aaf4ece6Schristos * 31aaf4ece6Schristos * 1.2.beta3 22 Dec 2002 32aaf4ece6Schristos * - Add comments on state->bits assertion in inffast.c 33aaf4ece6Schristos * - Add comments on op field in inftrees.h 34aaf4ece6Schristos * - Fix bug in reuse of allocated window after inflateReset() 35aaf4ece6Schristos * - Remove bit fields--back to byte structure for speed 36aaf4ece6Schristos * - Remove distance extra == 0 check in inflate_fast()--only helps for lengths 37aaf4ece6Schristos * - Change post-increments to pre-increments in inflate_fast(), PPC biased? 38aaf4ece6Schristos * - Add compile time option, POSTINC, to use post-increments instead (Intel?) 39aaf4ece6Schristos * - Make MATCH copy in inflate() much faster for when inflate_fast() not used 40aaf4ece6Schristos * - Use local copies of stream next and avail values, as well as local bit 41aaf4ece6Schristos * buffer and bit count in inflate()--for speed when inflate_fast() not used 42aaf4ece6Schristos * 43aaf4ece6Schristos * 1.2.beta4 1 Jan 2003 44aaf4ece6Schristos * - Split ptr - 257 statements in inflate_table() to avoid compiler warnings 45aaf4ece6Schristos * - Move a comment on output buffer sizes from inffast.c to inflate.c 46aaf4ece6Schristos * - Add comments in inffast.c to introduce the inflate_fast() routine 47aaf4ece6Schristos * - Rearrange window copies in inflate_fast() for speed and simplification 48aaf4ece6Schristos * - Unroll last copy for window match in inflate_fast() 49aaf4ece6Schristos * - Use local copies of window variables in inflate_fast() for speed 506db8c6e9Schristos * - Pull out common wnext == 0 case for speed in inflate_fast() 51aaf4ece6Schristos * - Make op and len in inflate_fast() unsigned for consistency 52aaf4ece6Schristos * - Add FAR to lcode and dcode declarations in inflate_fast() 53aaf4ece6Schristos * - Simplified bad distance check in inflate_fast() 54aaf4ece6Schristos * - Added inflateBackInit(), inflateBack(), and inflateBackEnd() in new 55aaf4ece6Schristos * source file infback.c to provide a call-back interface to inflate for 56aaf4ece6Schristos * programs like gzip and unzip -- uses window as output buffer to avoid 57aaf4ece6Schristos * window copying 58aaf4ece6Schristos * 59aaf4ece6Schristos * 1.2.beta5 1 Jan 2003 60aaf4ece6Schristos * - Improved inflateBack() interface to allow the caller to provide initial 61aaf4ece6Schristos * input in strm. 62aaf4ece6Schristos * - Fixed stored blocks bug in inflateBack() 63aaf4ece6Schristos * 64aaf4ece6Schristos * 1.2.beta6 4 Jan 2003 65aaf4ece6Schristos * - Added comments in inffast.c on effectiveness of POSTINC 66aaf4ece6Schristos * - Typecasting all around to reduce compiler warnings 67aaf4ece6Schristos * - Changed loops from while (1) or do {} while (1) to for (;;), again to 68aaf4ece6Schristos * make compilers happy 69aaf4ece6Schristos * - Changed type of window in inflateBackInit() to unsigned char * 70aaf4ece6Schristos * 71aaf4ece6Schristos * 1.2.beta7 27 Jan 2003 72aaf4ece6Schristos * - Changed many types to unsigned or unsigned short to avoid warnings 73aaf4ece6Schristos * - Added inflateCopy() function 74aaf4ece6Schristos * 75aaf4ece6Schristos * 1.2.0 9 Mar 2003 76aaf4ece6Schristos * - Changed inflateBack() interface to provide separate opaque descriptors 77aaf4ece6Schristos * for the in() and out() functions 78aaf4ece6Schristos * - Changed inflateBack() argument and in_func typedef to swap the length 79aaf4ece6Schristos * and buffer address return values for the input function 80aaf4ece6Schristos * - Check next_in and next_out for Z_NULL on entry to inflate() 81aaf4ece6Schristos * 82aaf4ece6Schristos * The history for versions after 1.2.0 are in ChangeLog in zlib distribution. 83aaf4ece6Schristos */ 84aaf4ece6Schristos 85aaf4ece6Schristos #include "zutil.h" 86aaf4ece6Schristos #include "inftrees.h" 87aaf4ece6Schristos #include "inflate.h" 88aaf4ece6Schristos #include "inffast.h" 89aaf4ece6Schristos 90aaf4ece6Schristos #ifdef MAKEFIXED 91aaf4ece6Schristos # ifndef BUILDFIXED 92aaf4ece6Schristos # define BUILDFIXED 93aaf4ece6Schristos # endif 94aaf4ece6Schristos #endif 95aaf4ece6Schristos 96*96c32821Schristos local int inflateStateCheck(z_streamp strm) { 976db8c6e9Schristos struct inflate_state FAR *state; 986db8c6e9Schristos if (strm == Z_NULL || 996db8c6e9Schristos strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) 1006db8c6e9Schristos return 1; 1016db8c6e9Schristos state = (struct inflate_state FAR *)strm->state; 1026db8c6e9Schristos if (state == Z_NULL || state->strm != strm || 1036db8c6e9Schristos state->mode < HEAD || state->mode > SYNC) 1046db8c6e9Schristos return 1; 1056db8c6e9Schristos return 0; 1066db8c6e9Schristos } 1076db8c6e9Schristos 108*96c32821Schristos int ZEXPORT inflateResetKeep(z_streamp strm) { 1096db8c6e9Schristos struct inflate_state FAR *state; 1106db8c6e9Schristos 1116db8c6e9Schristos if (inflateStateCheck(strm)) return Z_STREAM_ERROR; 1126db8c6e9Schristos state = (struct inflate_state FAR *)strm->state; 1136db8c6e9Schristos strm->total_in = strm->total_out = state->total = 0; 1146db8c6e9Schristos strm->msg = Z_NULL; 1156db8c6e9Schristos if (state->wrap) /* to support ill-conceived Java test suite */ 1166db8c6e9Schristos strm->adler = state->wrap & 1; 1176db8c6e9Schristos state->mode = HEAD; 1186db8c6e9Schristos state->last = 0; 1196db8c6e9Schristos state->havedict = 0; 1203b1253e8Schristos state->flags = -1; 1216db8c6e9Schristos state->dmax = 32768U; 1226db8c6e9Schristos state->head = Z_NULL; 1236db8c6e9Schristos state->hold = 0; 1246db8c6e9Schristos state->bits = 0; 1256db8c6e9Schristos state->lencode = state->distcode = state->next = state->codes; 1266db8c6e9Schristos state->sane = 1; 1276db8c6e9Schristos state->back = -1; 1286db8c6e9Schristos Tracev((stderr, "inflate: reset\n")); 1296db8c6e9Schristos return Z_OK; 1306db8c6e9Schristos } 1316db8c6e9Schristos 132*96c32821Schristos int ZEXPORT inflateReset(z_streamp strm) { 133aaf4ece6Schristos struct inflate_state FAR *state; 134aaf4ece6Schristos 1356db8c6e9Schristos if (inflateStateCheck(strm)) return Z_STREAM_ERROR; 136aaf4ece6Schristos state = (struct inflate_state FAR *)strm->state; 137aaf4ece6Schristos state->wsize = 0; 138aaf4ece6Schristos state->whave = 0; 1396db8c6e9Schristos state->wnext = 0; 1406db8c6e9Schristos return inflateResetKeep(strm); 1416db8c6e9Schristos } 1426db8c6e9Schristos 143*96c32821Schristos int ZEXPORT inflateReset2(z_streamp strm, int windowBits) { 1446db8c6e9Schristos int wrap; 1456db8c6e9Schristos struct inflate_state FAR *state; 1466db8c6e9Schristos 1476db8c6e9Schristos /* get the state */ 1486db8c6e9Schristos if (inflateStateCheck(strm)) return Z_STREAM_ERROR; 1496db8c6e9Schristos state = (struct inflate_state FAR *)strm->state; 1506db8c6e9Schristos 1516db8c6e9Schristos /* extract wrap request from windowBits parameter */ 1526db8c6e9Schristos if (windowBits < 0) { 1533b1253e8Schristos if (windowBits < -15) 1543b1253e8Schristos return Z_STREAM_ERROR; 1556db8c6e9Schristos wrap = 0; 1566db8c6e9Schristos windowBits = -windowBits; 1576db8c6e9Schristos } 1586db8c6e9Schristos else { 1596db8c6e9Schristos wrap = (windowBits >> 4) + 5; 1606db8c6e9Schristos #ifdef GUNZIP 1616db8c6e9Schristos if (windowBits < 48) 1626db8c6e9Schristos windowBits &= 15; 1636db8c6e9Schristos #endif 1646db8c6e9Schristos } 1656db8c6e9Schristos 1666db8c6e9Schristos /* set number of window bits, free window if different */ 1676db8c6e9Schristos if (windowBits && (windowBits < 8 || windowBits > 15)) 1686db8c6e9Schristos return Z_STREAM_ERROR; 1696db8c6e9Schristos if (state->window != Z_NULL && state->wbits != (unsigned)windowBits) { 1706db8c6e9Schristos ZFREE(strm, state->window); 1716db8c6e9Schristos state->window = Z_NULL; 1726db8c6e9Schristos } 1736db8c6e9Schristos 1746db8c6e9Schristos /* update state and reset the rest of it */ 1756db8c6e9Schristos state->wrap = wrap; 1766db8c6e9Schristos state->wbits = (unsigned)windowBits; 1776db8c6e9Schristos return inflateReset(strm); 1786db8c6e9Schristos } 1796db8c6e9Schristos 180*96c32821Schristos int ZEXPORT inflateInit2_(z_streamp strm, int windowBits, 181*96c32821Schristos const char *version, int stream_size) { 1826db8c6e9Schristos int ret; 1836db8c6e9Schristos struct inflate_state FAR *state; 1846db8c6e9Schristos 1856db8c6e9Schristos if (version == Z_NULL || version[0] != ZLIB_VERSION[0] || 1866db8c6e9Schristos stream_size != (int)(sizeof(z_stream))) 1876db8c6e9Schristos return Z_VERSION_ERROR; 1886db8c6e9Schristos if (strm == Z_NULL) return Z_STREAM_ERROR; 1896db8c6e9Schristos strm->msg = Z_NULL; /* in case we return an error */ 1906db8c6e9Schristos if (strm->zalloc == (alloc_func)0) { 1916db8c6e9Schristos #ifdef Z_SOLO 1926db8c6e9Schristos return Z_STREAM_ERROR; 1936db8c6e9Schristos #else 1946db8c6e9Schristos strm->zalloc = zcalloc; 1956db8c6e9Schristos strm->opaque = (voidpf)0; 1966db8c6e9Schristos #endif 1976db8c6e9Schristos } 1986db8c6e9Schristos if (strm->zfree == (free_func)0) 1996db8c6e9Schristos #ifdef Z_SOLO 2006db8c6e9Schristos return Z_STREAM_ERROR; 2016db8c6e9Schristos #else 2026db8c6e9Schristos strm->zfree = zcfree; 2036db8c6e9Schristos #endif 2046db8c6e9Schristos state = (struct inflate_state FAR *) 2056db8c6e9Schristos ZALLOC(strm, 1, sizeof(struct inflate_state)); 2066db8c6e9Schristos if (state == Z_NULL) return Z_MEM_ERROR; 2076db8c6e9Schristos Tracev((stderr, "inflate: allocated\n")); 2086db8c6e9Schristos strm->state = (struct internal_state FAR *)state; 2096db8c6e9Schristos state->strm = strm; 2106db8c6e9Schristos state->window = Z_NULL; 2116db8c6e9Schristos state->mode = HEAD; /* to pass state test in inflateReset2() */ 2126db8c6e9Schristos ret = inflateReset2(strm, windowBits); 2136db8c6e9Schristos if (ret != Z_OK) { 2146db8c6e9Schristos ZFREE(strm, state); 2156db8c6e9Schristos strm->state = Z_NULL; 2166db8c6e9Schristos } 2176db8c6e9Schristos return ret; 2186db8c6e9Schristos } 2196db8c6e9Schristos 220*96c32821Schristos int ZEXPORT inflateInit_(z_streamp strm, const char *version, 221*96c32821Schristos int stream_size) { 2226db8c6e9Schristos return inflateInit2_(strm, DEF_WBITS, version, stream_size); 223aaf4ece6Schristos } 224aaf4ece6Schristos 225*96c32821Schristos int ZEXPORT inflatePrime(z_streamp strm, int bits, int value) { 226aaf4ece6Schristos struct inflate_state FAR *state; 227aaf4ece6Schristos 2286db8c6e9Schristos if (inflateStateCheck(strm)) return Z_STREAM_ERROR; 229*96c32821Schristos if (bits == 0) 230*96c32821Schristos return Z_OK; 231aaf4ece6Schristos state = (struct inflate_state FAR *)strm->state; 2326db8c6e9Schristos if (bits < 0) { 2336db8c6e9Schristos state->hold = 0; 2346db8c6e9Schristos state->bits = 0; 235aaf4ece6Schristos return Z_OK; 236aaf4ece6Schristos } 2376db8c6e9Schristos if (bits > 16 || state->bits + (uInt)bits > 32) return Z_STREAM_ERROR; 2386db8c6e9Schristos value &= (1L << bits) - 1; 2396db8c6e9Schristos state->hold += (unsigned)value << state->bits; 2406db8c6e9Schristos state->bits += (uInt)bits; 2416db8c6e9Schristos return Z_OK; 242aaf4ece6Schristos } 243aaf4ece6Schristos 244aaf4ece6Schristos /* 245aaf4ece6Schristos Return state with length and distance decoding tables and index sizes set to 246aaf4ece6Schristos fixed code decoding. Normally this returns fixed tables from inffixed.h. 247aaf4ece6Schristos If BUILDFIXED is defined, then instead this routine builds the tables the 248aaf4ece6Schristos first time it's called, and returns those tables the first time and 249aaf4ece6Schristos thereafter. This reduces the size of the code by about 2K bytes, in 250aaf4ece6Schristos exchange for a little execution time. However, BUILDFIXED should not be 251aaf4ece6Schristos used for threaded applications, since the rewriting of the tables and virgin 252aaf4ece6Schristos may not be thread-safe. 253aaf4ece6Schristos */ 254*96c32821Schristos local void fixedtables(struct inflate_state FAR *state) { 255aaf4ece6Schristos #ifdef BUILDFIXED 256aaf4ece6Schristos static int virgin = 1; 257aaf4ece6Schristos static code *lenfix, *distfix; 258aaf4ece6Schristos static code fixed[544]; 259aaf4ece6Schristos 260aaf4ece6Schristos /* build fixed huffman tables if first call (may not be thread safe) */ 261aaf4ece6Schristos if (virgin) { 262aaf4ece6Schristos unsigned sym, bits; 263aaf4ece6Schristos static code *next; 264aaf4ece6Schristos 265aaf4ece6Schristos /* literal/length table */ 266aaf4ece6Schristos sym = 0; 267aaf4ece6Schristos while (sym < 144) state->lens[sym++] = 8; 268aaf4ece6Schristos while (sym < 256) state->lens[sym++] = 9; 269aaf4ece6Schristos while (sym < 280) state->lens[sym++] = 7; 270aaf4ece6Schristos while (sym < 288) state->lens[sym++] = 8; 271aaf4ece6Schristos next = fixed; 272aaf4ece6Schristos lenfix = next; 273aaf4ece6Schristos bits = 9; 274aaf4ece6Schristos inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work); 275aaf4ece6Schristos 276aaf4ece6Schristos /* distance table */ 277aaf4ece6Schristos sym = 0; 278aaf4ece6Schristos while (sym < 32) state->lens[sym++] = 5; 279aaf4ece6Schristos distfix = next; 280aaf4ece6Schristos bits = 5; 281aaf4ece6Schristos inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work); 282aaf4ece6Schristos 283aaf4ece6Schristos /* do this just once */ 284aaf4ece6Schristos virgin = 0; 285aaf4ece6Schristos } 286aaf4ece6Schristos #else /* !BUILDFIXED */ 287aaf4ece6Schristos # include "inffixed.h" 288aaf4ece6Schristos #endif /* BUILDFIXED */ 289aaf4ece6Schristos state->lencode = lenfix; 290aaf4ece6Schristos state->lenbits = 9; 291aaf4ece6Schristos state->distcode = distfix; 292aaf4ece6Schristos state->distbits = 5; 293aaf4ece6Schristos } 294aaf4ece6Schristos 295aaf4ece6Schristos #ifdef MAKEFIXED 296aaf4ece6Schristos #include <stdio.h> 297aaf4ece6Schristos 298aaf4ece6Schristos /* 299aaf4ece6Schristos Write out the inffixed.h that is #include'd above. Defining MAKEFIXED also 300aaf4ece6Schristos defines BUILDFIXED, so the tables are built on the fly. makefixed() writes 301aaf4ece6Schristos those tables to stdout, which would be piped to inffixed.h. A small program 302aaf4ece6Schristos can simply call makefixed to do this: 303aaf4ece6Schristos 304aaf4ece6Schristos void makefixed(void); 305aaf4ece6Schristos 306aaf4ece6Schristos int main(void) 307aaf4ece6Schristos { 308aaf4ece6Schristos makefixed(); 309aaf4ece6Schristos return 0; 310aaf4ece6Schristos } 311aaf4ece6Schristos 312aaf4ece6Schristos Then that can be linked with zlib built with MAKEFIXED defined and run: 313aaf4ece6Schristos 314aaf4ece6Schristos a.out > inffixed.h 315aaf4ece6Schristos */ 316*96c32821Schristos void makefixed(void) 317aaf4ece6Schristos { 318aaf4ece6Schristos unsigned low, size; 319aaf4ece6Schristos struct inflate_state state; 320aaf4ece6Schristos 321aaf4ece6Schristos fixedtables(&state); 322aaf4ece6Schristos puts(" /* inffixed.h -- table for decoding fixed codes"); 323aaf4ece6Schristos puts(" * Generated automatically by makefixed()."); 324aaf4ece6Schristos puts(" */"); 325aaf4ece6Schristos puts(""); 326aaf4ece6Schristos puts(" /* WARNING: this file should *not* be used by applications."); 327aaf4ece6Schristos puts(" It is part of the implementation of this library and is"); 328aaf4ece6Schristos puts(" subject to change. Applications should only use zlib.h."); 329aaf4ece6Schristos puts(" */"); 330aaf4ece6Schristos puts(""); 331aaf4ece6Schristos size = 1U << 9; 332aaf4ece6Schristos printf(" static const code lenfix[%u] = {", size); 333aaf4ece6Schristos low = 0; 334aaf4ece6Schristos for (;;) { 335aaf4ece6Schristos if ((low % 7) == 0) printf("\n "); 3366db8c6e9Schristos printf("{%u,%u,%d}", (low & 127) == 99 ? 64 : state.lencode[low].op, 3376db8c6e9Schristos state.lencode[low].bits, state.lencode[low].val); 338aaf4ece6Schristos if (++low == size) break; 339aaf4ece6Schristos putchar(','); 340aaf4ece6Schristos } 341aaf4ece6Schristos puts("\n };"); 342aaf4ece6Schristos size = 1U << 5; 343aaf4ece6Schristos printf("\n static const code distfix[%u] = {", size); 344aaf4ece6Schristos low = 0; 345aaf4ece6Schristos for (;;) { 346aaf4ece6Schristos if ((low % 6) == 0) printf("\n "); 347aaf4ece6Schristos printf("{%u,%u,%d}", state.distcode[low].op, state.distcode[low].bits, 348aaf4ece6Schristos state.distcode[low].val); 349aaf4ece6Schristos if (++low == size) break; 350aaf4ece6Schristos putchar(','); 351aaf4ece6Schristos } 352aaf4ece6Schristos puts("\n };"); 353aaf4ece6Schristos } 354aaf4ece6Schristos #endif /* MAKEFIXED */ 355aaf4ece6Schristos 356aaf4ece6Schristos /* 357aaf4ece6Schristos Update the window with the last wsize (normally 32K) bytes written before 358aaf4ece6Schristos returning. If window does not exist yet, create it. This is only called 359aaf4ece6Schristos when a window is already in use, or when output has been written during this 360aaf4ece6Schristos inflate call, but the end of the deflate stream has not been reached yet. 361aaf4ece6Schristos It is also called to create a window for dictionary data when a dictionary 362aaf4ece6Schristos is loaded. 363aaf4ece6Schristos 364aaf4ece6Schristos Providing output buffers larger than 32K to inflate() should provide a speed 365aaf4ece6Schristos advantage, since only the last 32K of output is copied to the sliding window 366aaf4ece6Schristos upon return from inflate(), and since all distances after the first 32K of 367aaf4ece6Schristos output will fall in the output data, making match copies simpler and faster. 368aaf4ece6Schristos The advantage may be dependent on the size of the processor's data caches. 369aaf4ece6Schristos */ 370*96c32821Schristos local int updatewindow(z_streamp strm, const Bytef *end, unsigned copy) { 371aaf4ece6Schristos struct inflate_state FAR *state; 3726db8c6e9Schristos unsigned dist; 373aaf4ece6Schristos 374aaf4ece6Schristos state = (struct inflate_state FAR *)strm->state; 375aaf4ece6Schristos 376aaf4ece6Schristos /* if it hasn't been done already, allocate space for the window */ 377aaf4ece6Schristos if (state->window == Z_NULL) { 378aaf4ece6Schristos state->window = (unsigned char FAR *) 379aaf4ece6Schristos ZALLOC(strm, 1U << state->wbits, 380aaf4ece6Schristos sizeof(unsigned char)); 381aaf4ece6Schristos if (state->window == Z_NULL) return 1; 382aaf4ece6Schristos } 383aaf4ece6Schristos 384aaf4ece6Schristos /* if window not in use yet, initialize */ 385aaf4ece6Schristos if (state->wsize == 0) { 386aaf4ece6Schristos state->wsize = 1U << state->wbits; 3876db8c6e9Schristos state->wnext = 0; 388aaf4ece6Schristos state->whave = 0; 389aaf4ece6Schristos } 390aaf4ece6Schristos 391aaf4ece6Schristos /* copy state->wsize or less output bytes into the circular window */ 392aaf4ece6Schristos if (copy >= state->wsize) { 3936db8c6e9Schristos zmemcpy(state->window, end - state->wsize, state->wsize); 3946db8c6e9Schristos state->wnext = 0; 395aaf4ece6Schristos state->whave = state->wsize; 396aaf4ece6Schristos } 397aaf4ece6Schristos else { 3986db8c6e9Schristos dist = state->wsize - state->wnext; 399aaf4ece6Schristos if (dist > copy) dist = copy; 4006db8c6e9Schristos zmemcpy(state->window + state->wnext, end - copy, dist); 401aaf4ece6Schristos copy -= dist; 402aaf4ece6Schristos if (copy) { 4036db8c6e9Schristos zmemcpy(state->window, end - copy, copy); 4046db8c6e9Schristos state->wnext = copy; 405aaf4ece6Schristos state->whave = state->wsize; 406aaf4ece6Schristos } 407aaf4ece6Schristos else { 4086db8c6e9Schristos state->wnext += dist; 4096db8c6e9Schristos if (state->wnext == state->wsize) state->wnext = 0; 410aaf4ece6Schristos if (state->whave < state->wsize) state->whave += dist; 411aaf4ece6Schristos } 412aaf4ece6Schristos } 413aaf4ece6Schristos return 0; 414aaf4ece6Schristos } 415aaf4ece6Schristos 416aaf4ece6Schristos /* Macros for inflate(): */ 417aaf4ece6Schristos 418aaf4ece6Schristos /* check function to use adler32() for zlib or crc32() for gzip */ 419aaf4ece6Schristos #ifdef GUNZIP 4203b1253e8Schristos # define UPDATE_CHECK(check, buf, len) \ 421aaf4ece6Schristos (state->flags ? crc32(check, buf, len) : adler32(check, buf, len)) 422aaf4ece6Schristos #else 4233b1253e8Schristos # define UPDATE_CHECK(check, buf, len) adler32(check, buf, len) 424aaf4ece6Schristos #endif 425aaf4ece6Schristos 426aaf4ece6Schristos /* check macros for header crc */ 427aaf4ece6Schristos #ifdef GUNZIP 428aaf4ece6Schristos # define CRC2(check, word) \ 429aaf4ece6Schristos do { \ 430aaf4ece6Schristos hbuf[0] = (unsigned char)(word); \ 431aaf4ece6Schristos hbuf[1] = (unsigned char)((word) >> 8); \ 432aaf4ece6Schristos check = crc32(check, hbuf, 2); \ 433aaf4ece6Schristos } while (0) 434aaf4ece6Schristos 435aaf4ece6Schristos # define CRC4(check, word) \ 436aaf4ece6Schristos do { \ 437aaf4ece6Schristos hbuf[0] = (unsigned char)(word); \ 438aaf4ece6Schristos hbuf[1] = (unsigned char)((word) >> 8); \ 439aaf4ece6Schristos hbuf[2] = (unsigned char)((word) >> 16); \ 440aaf4ece6Schristos hbuf[3] = (unsigned char)((word) >> 24); \ 441aaf4ece6Schristos check = crc32(check, hbuf, 4); \ 442aaf4ece6Schristos } while (0) 443aaf4ece6Schristos #endif 444aaf4ece6Schristos 445aaf4ece6Schristos /* Load registers with state in inflate() for speed */ 446aaf4ece6Schristos #define LOAD() \ 447aaf4ece6Schristos do { \ 448aaf4ece6Schristos put = strm->next_out; \ 449aaf4ece6Schristos left = strm->avail_out; \ 450aaf4ece6Schristos next = strm->next_in; \ 451aaf4ece6Schristos have = strm->avail_in; \ 452aaf4ece6Schristos hold = state->hold; \ 453aaf4ece6Schristos bits = state->bits; \ 454aaf4ece6Schristos } while (0) 455aaf4ece6Schristos 456aaf4ece6Schristos /* Restore state from registers in inflate() */ 457aaf4ece6Schristos #define RESTORE() \ 458aaf4ece6Schristos do { \ 459aaf4ece6Schristos strm->next_out = put; \ 460aaf4ece6Schristos strm->avail_out = left; \ 461aaf4ece6Schristos strm->next_in = next; \ 462aaf4ece6Schristos strm->avail_in = have; \ 463aaf4ece6Schristos state->hold = hold; \ 464aaf4ece6Schristos state->bits = bits; \ 465aaf4ece6Schristos } while (0) 466aaf4ece6Schristos 467aaf4ece6Schristos /* Clear the input bit accumulator */ 468aaf4ece6Schristos #define INITBITS() \ 469aaf4ece6Schristos do { \ 470aaf4ece6Schristos hold = 0; \ 471aaf4ece6Schristos bits = 0; \ 472aaf4ece6Schristos } while (0) 473aaf4ece6Schristos 474aaf4ece6Schristos /* Get a byte of input into the bit accumulator, or return from inflate() 475aaf4ece6Schristos if there is no input available. */ 476aaf4ece6Schristos #define PULLBYTE() \ 477aaf4ece6Schristos do { \ 478aaf4ece6Schristos if (have == 0) goto inf_leave; \ 479aaf4ece6Schristos have--; \ 480aaf4ece6Schristos hold += (unsigned long)(*next++) << bits; \ 481aaf4ece6Schristos bits += 8; \ 482aaf4ece6Schristos } while (0) 483aaf4ece6Schristos 484aaf4ece6Schristos /* Assure that there are at least n bits in the bit accumulator. If there is 485aaf4ece6Schristos not enough available input to do that, then return from inflate(). */ 486aaf4ece6Schristos #define NEEDBITS(n) \ 487aaf4ece6Schristos do { \ 488aaf4ece6Schristos while (bits < (unsigned)(n)) \ 489aaf4ece6Schristos PULLBYTE(); \ 490aaf4ece6Schristos } while (0) 491aaf4ece6Schristos 492aaf4ece6Schristos /* Return the low n bits of the bit accumulator (n < 16) */ 493aaf4ece6Schristos #define BITS(n) \ 494aaf4ece6Schristos ((unsigned)hold & ((1U << (n)) - 1)) 495aaf4ece6Schristos 496aaf4ece6Schristos /* Remove n bits from the bit accumulator */ 497aaf4ece6Schristos #define DROPBITS(n) \ 498aaf4ece6Schristos do { \ 499aaf4ece6Schristos hold >>= (n); \ 500aaf4ece6Schristos bits -= (unsigned)(n); \ 501aaf4ece6Schristos } while (0) 502aaf4ece6Schristos 503aaf4ece6Schristos /* Remove zero to seven bits as needed to go to a byte boundary */ 504aaf4ece6Schristos #define BYTEBITS() \ 505aaf4ece6Schristos do { \ 506aaf4ece6Schristos hold >>= bits & 7; \ 507aaf4ece6Schristos bits -= bits & 7; \ 508aaf4ece6Schristos } while (0) 509aaf4ece6Schristos 510aaf4ece6Schristos /* 511aaf4ece6Schristos inflate() uses a state machine to process as much input data and generate as 512aaf4ece6Schristos much output data as possible before returning. The state machine is 513aaf4ece6Schristos structured roughly as follows: 514aaf4ece6Schristos 515aaf4ece6Schristos for (;;) switch (state) { 516aaf4ece6Schristos ... 517aaf4ece6Schristos case STATEn: 518aaf4ece6Schristos if (not enough input data or output space to make progress) 519aaf4ece6Schristos return; 520aaf4ece6Schristos ... make progress ... 521aaf4ece6Schristos state = STATEm; 522aaf4ece6Schristos break; 523aaf4ece6Schristos ... 524aaf4ece6Schristos } 525aaf4ece6Schristos 526aaf4ece6Schristos so when inflate() is called again, the same case is attempted again, and 527aaf4ece6Schristos if the appropriate resources are provided, the machine proceeds to the 528aaf4ece6Schristos next state. The NEEDBITS() macro is usually the way the state evaluates 529aaf4ece6Schristos whether it can proceed or should return. NEEDBITS() does the return if 530aaf4ece6Schristos the requested bits are not available. The typical use of the BITS macros 531aaf4ece6Schristos is: 532aaf4ece6Schristos 533aaf4ece6Schristos NEEDBITS(n); 534aaf4ece6Schristos ... do something with BITS(n) ... 535aaf4ece6Schristos DROPBITS(n); 536aaf4ece6Schristos 537aaf4ece6Schristos where NEEDBITS(n) either returns from inflate() if there isn't enough 538aaf4ece6Schristos input left to load n bits into the accumulator, or it continues. BITS(n) 539aaf4ece6Schristos gives the low n bits in the accumulator. When done, DROPBITS(n) drops 540aaf4ece6Schristos the low n bits off the accumulator. INITBITS() clears the accumulator 541aaf4ece6Schristos and sets the number of available bits to zero. BYTEBITS() discards just 542aaf4ece6Schristos enough bits to put the accumulator on a byte boundary. After BYTEBITS() 543aaf4ece6Schristos and a NEEDBITS(8), then BITS(8) would return the next byte in the stream. 544aaf4ece6Schristos 545aaf4ece6Schristos NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return 546aaf4ece6Schristos if there is no input available. The decoding of variable length codes uses 547aaf4ece6Schristos PULLBYTE() directly in order to pull just enough bytes to decode the next 548aaf4ece6Schristos code, and no more. 549aaf4ece6Schristos 550aaf4ece6Schristos Some states loop until they get enough input, making sure that enough 551aaf4ece6Schristos state information is maintained to continue the loop where it left off 552aaf4ece6Schristos if NEEDBITS() returns in the loop. For example, want, need, and keep 553aaf4ece6Schristos would all have to actually be part of the saved state in case NEEDBITS() 554aaf4ece6Schristos returns: 555aaf4ece6Schristos 556aaf4ece6Schristos case STATEw: 557aaf4ece6Schristos while (want < need) { 558aaf4ece6Schristos NEEDBITS(n); 559aaf4ece6Schristos keep[want++] = BITS(n); 560aaf4ece6Schristos DROPBITS(n); 561aaf4ece6Schristos } 562aaf4ece6Schristos state = STATEx; 563aaf4ece6Schristos case STATEx: 564aaf4ece6Schristos 565aaf4ece6Schristos As shown above, if the next state is also the next case, then the break 566aaf4ece6Schristos is omitted. 567aaf4ece6Schristos 568aaf4ece6Schristos A state may also return if there is not enough output space available to 569aaf4ece6Schristos complete that state. Those states are copying stored data, writing a 570aaf4ece6Schristos literal byte, and copying a matching string. 571aaf4ece6Schristos 572aaf4ece6Schristos When returning, a "goto inf_leave" is used to update the total counters, 573aaf4ece6Schristos update the check value, and determine whether any progress has been made 574aaf4ece6Schristos during that inflate() call in order to return the proper return code. 575aaf4ece6Schristos Progress is defined as a change in either strm->avail_in or strm->avail_out. 576aaf4ece6Schristos When there is a window, goto inf_leave will update the window with the last 577aaf4ece6Schristos output written. If a goto inf_leave occurs in the middle of decompression 578aaf4ece6Schristos and there is no window currently, goto inf_leave will create one and copy 579aaf4ece6Schristos output to the window for the next call of inflate(). 580aaf4ece6Schristos 581aaf4ece6Schristos In this implementation, the flush parameter of inflate() only affects the 582aaf4ece6Schristos return code (per zlib.h). inflate() always writes as much as possible to 583aaf4ece6Schristos strm->next_out, given the space available and the provided input--the effect 584aaf4ece6Schristos documented in zlib.h of Z_SYNC_FLUSH. Furthermore, inflate() always defers 585aaf4ece6Schristos the allocation of and copying into a sliding window until necessary, which 586aaf4ece6Schristos provides the effect documented in zlib.h for Z_FINISH when the entire input 587aaf4ece6Schristos stream available. So the only thing the flush parameter actually does is: 588aaf4ece6Schristos when flush is set to Z_FINISH, inflate() cannot return Z_OK. Instead it 589aaf4ece6Schristos will return Z_BUF_ERROR if it has not reached the end of the stream. 590aaf4ece6Schristos */ 591aaf4ece6Schristos 592*96c32821Schristos int ZEXPORT inflate(z_streamp strm, int flush) { 593aaf4ece6Schristos struct inflate_state FAR *state; 5946db8c6e9Schristos z_const unsigned char FAR *next; /* next input */ 595aaf4ece6Schristos unsigned char FAR *put; /* next output */ 596aaf4ece6Schristos unsigned have, left; /* available input and output */ 597aaf4ece6Schristos unsigned long hold; /* bit buffer */ 598aaf4ece6Schristos unsigned bits; /* bits in bit buffer */ 599aaf4ece6Schristos unsigned in, out; /* save starting available input and output */ 600aaf4ece6Schristos unsigned copy; /* number of stored or match bytes to copy */ 601aaf4ece6Schristos unsigned char FAR *from; /* where to copy match bytes from */ 6026db8c6e9Schristos code here; /* current decoding table entry */ 603aaf4ece6Schristos code last; /* parent table entry */ 604aaf4ece6Schristos unsigned len; /* length to copy for repeats, bits to drop */ 605aaf4ece6Schristos int ret; /* return code */ 606aaf4ece6Schristos #ifdef GUNZIP 607aaf4ece6Schristos unsigned char hbuf[4]; /* buffer for gzip header crc calculation */ 608aaf4ece6Schristos #endif 609aaf4ece6Schristos static const unsigned short order[19] = /* permutation of code lengths */ 610aaf4ece6Schristos {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; 611aaf4ece6Schristos 6126dd94d2aStsutsui #if defined(__NetBSD__) && defined(_STANDALONE) 6136dd94d2aStsutsui /* Some kernels are loaded at address 0x0 so strm->next_out could be NULL */ 6146dd94d2aStsutsui if (inflateStateCheck(strm) || 6156dd94d2aStsutsui (strm->next_in == Z_NULL && strm->avail_in != 0)) 6166dd94d2aStsutsui return Z_STREAM_ERROR; 6176dd94d2aStsutsui #else 6186db8c6e9Schristos if (inflateStateCheck(strm) || strm->next_out == Z_NULL || 6193e9910f3Stsutsui (strm->next_in == Z_NULL && strm->avail_in != 0)) 6203e9910f3Stsutsui return Z_STREAM_ERROR; 6216dd94d2aStsutsui #endif 622aaf4ece6Schristos 623aaf4ece6Schristos state = (struct inflate_state FAR *)strm->state; 624aaf4ece6Schristos if (state->mode == TYPE) state->mode = TYPEDO; /* skip check */ 625aaf4ece6Schristos LOAD(); 626aaf4ece6Schristos in = have; 627aaf4ece6Schristos out = left; 628aaf4ece6Schristos ret = Z_OK; 629aaf4ece6Schristos for (;;) 630aaf4ece6Schristos switch (state->mode) { 631aaf4ece6Schristos case HEAD: 632aaf4ece6Schristos if (state->wrap == 0) { 633aaf4ece6Schristos state->mode = TYPEDO; 634aaf4ece6Schristos break; 635aaf4ece6Schristos } 636aaf4ece6Schristos NEEDBITS(16); 637aaf4ece6Schristos #ifdef GUNZIP 638aaf4ece6Schristos if ((state->wrap & 2) && hold == 0x8b1f) { /* gzip header */ 6396db8c6e9Schristos if (state->wbits == 0) 6406db8c6e9Schristos state->wbits = 15; 641aaf4ece6Schristos state->check = crc32(0L, Z_NULL, 0); 642aaf4ece6Schristos CRC2(state->check, hold); 643aaf4ece6Schristos INITBITS(); 644aaf4ece6Schristos state->mode = FLAGS; 645aaf4ece6Schristos break; 646aaf4ece6Schristos } 647aaf4ece6Schristos if (state->head != Z_NULL) 648aaf4ece6Schristos state->head->done = -1; 649aaf4ece6Schristos if (!(state->wrap & 1) || /* check if zlib header allowed */ 650aaf4ece6Schristos #else 651aaf4ece6Schristos if ( 652aaf4ece6Schristos #endif 653aaf4ece6Schristos ((BITS(8) << 8) + (hold >> 8)) % 31) { 654b85ba082Schristos strm->msg = __UNCONST("incorrect header check"); 655aaf4ece6Schristos state->mode = BAD; 656aaf4ece6Schristos break; 657aaf4ece6Schristos } 658aaf4ece6Schristos if (BITS(4) != Z_DEFLATED) { 659b85ba082Schristos strm->msg = __UNCONST("unknown compression method"); 660aaf4ece6Schristos state->mode = BAD; 661aaf4ece6Schristos break; 662aaf4ece6Schristos } 663aaf4ece6Schristos DROPBITS(4); 664aaf4ece6Schristos len = BITS(4) + 8; 6656db8c6e9Schristos if (state->wbits == 0) 6666db8c6e9Schristos state->wbits = len; 6676db8c6e9Schristos if (len > 15 || len > state->wbits) { 668b85ba082Schristos strm->msg = __UNCONST("invalid window size"); 669aaf4ece6Schristos state->mode = BAD; 670aaf4ece6Schristos break; 671aaf4ece6Schristos } 672aaf4ece6Schristos state->dmax = 1U << len; 6733b1253e8Schristos state->flags = 0; /* indicate zlib header */ 674aaf4ece6Schristos Tracev((stderr, "inflate: zlib header ok\n")); 675aaf4ece6Schristos strm->adler = state->check = adler32(0L, Z_NULL, 0); 676aaf4ece6Schristos state->mode = hold & 0x200 ? DICTID : TYPE; 677aaf4ece6Schristos INITBITS(); 678aaf4ece6Schristos break; 679aaf4ece6Schristos #ifdef GUNZIP 680aaf4ece6Schristos case FLAGS: 681aaf4ece6Schristos NEEDBITS(16); 682aaf4ece6Schristos state->flags = (int)(hold); 683aaf4ece6Schristos if ((state->flags & 0xff) != Z_DEFLATED) { 684b85ba082Schristos strm->msg = __UNCONST("unknown compression method"); 685aaf4ece6Schristos state->mode = BAD; 686aaf4ece6Schristos break; 687aaf4ece6Schristos } 688aaf4ece6Schristos if (state->flags & 0xe000) { 689b85ba082Schristos strm->msg = __UNCONST("unknown header flags set"); 690aaf4ece6Schristos state->mode = BAD; 691aaf4ece6Schristos break; 692aaf4ece6Schristos } 693aaf4ece6Schristos if (state->head != Z_NULL) 694aaf4ece6Schristos state->head->text = (int)((hold >> 8) & 1); 6956db8c6e9Schristos if ((state->flags & 0x0200) && (state->wrap & 4)) 6966db8c6e9Schristos CRC2(state->check, hold); 697aaf4ece6Schristos INITBITS(); 698aaf4ece6Schristos state->mode = TIME; 6993b1253e8Schristos /* fallthrough */ 700aaf4ece6Schristos case TIME: 701aaf4ece6Schristos NEEDBITS(32); 702aaf4ece6Schristos if (state->head != Z_NULL) 703aaf4ece6Schristos state->head->time = hold; 7046db8c6e9Schristos if ((state->flags & 0x0200) && (state->wrap & 4)) 7056db8c6e9Schristos CRC4(state->check, hold); 706aaf4ece6Schristos INITBITS(); 707aaf4ece6Schristos state->mode = OS; 7083b1253e8Schristos /* fallthrough */ 709aaf4ece6Schristos case OS: 710aaf4ece6Schristos NEEDBITS(16); 711aaf4ece6Schristos if (state->head != Z_NULL) { 712aaf4ece6Schristos state->head->xflags = (int)(hold & 0xff); 713aaf4ece6Schristos state->head->os = (int)(hold >> 8); 714aaf4ece6Schristos } 7156db8c6e9Schristos if ((state->flags & 0x0200) && (state->wrap & 4)) 7166db8c6e9Schristos CRC2(state->check, hold); 717aaf4ece6Schristos INITBITS(); 718aaf4ece6Schristos state->mode = EXLEN; 7193b1253e8Schristos /* fallthrough */ 720aaf4ece6Schristos case EXLEN: 721aaf4ece6Schristos if (state->flags & 0x0400) { 722aaf4ece6Schristos NEEDBITS(16); 723aaf4ece6Schristos state->length = (unsigned)(hold); 724aaf4ece6Schristos if (state->head != Z_NULL) 725aaf4ece6Schristos state->head->extra_len = (unsigned)hold; 7266db8c6e9Schristos if ((state->flags & 0x0200) && (state->wrap & 4)) 7276db8c6e9Schristos CRC2(state->check, hold); 728aaf4ece6Schristos INITBITS(); 729aaf4ece6Schristos } 730aaf4ece6Schristos else if (state->head != Z_NULL) 731aaf4ece6Schristos state->head->extra = Z_NULL; 732aaf4ece6Schristos state->mode = EXTRA; 7333b1253e8Schristos /* fallthrough */ 734aaf4ece6Schristos case EXTRA: 735aaf4ece6Schristos if (state->flags & 0x0400) { 736aaf4ece6Schristos copy = state->length; 737aaf4ece6Schristos if (copy > have) copy = have; 738aaf4ece6Schristos if (copy) { 739aaf4ece6Schristos if (state->head != Z_NULL && 7403b1253e8Schristos state->head->extra != Z_NULL && 7413b1253e8Schristos (len = state->head->extra_len - state->length) < 7423b1253e8Schristos state->head->extra_max) { 743aaf4ece6Schristos zmemcpy(state->head->extra + len, next, 744aaf4ece6Schristos len + copy > state->head->extra_max ? 745aaf4ece6Schristos state->head->extra_max - len : copy); 746aaf4ece6Schristos } 7476db8c6e9Schristos if ((state->flags & 0x0200) && (state->wrap & 4)) 748aaf4ece6Schristos state->check = crc32(state->check, next, copy); 749aaf4ece6Schristos have -= copy; 750aaf4ece6Schristos next += copy; 751aaf4ece6Schristos state->length -= copy; 752aaf4ece6Schristos } 753aaf4ece6Schristos if (state->length) goto inf_leave; 754aaf4ece6Schristos } 755aaf4ece6Schristos state->length = 0; 756aaf4ece6Schristos state->mode = NAME; 7573b1253e8Schristos /* fallthrough */ 758aaf4ece6Schristos case NAME: 759aaf4ece6Schristos if (state->flags & 0x0800) { 760aaf4ece6Schristos if (have == 0) goto inf_leave; 761aaf4ece6Schristos copy = 0; 762aaf4ece6Schristos do { 763aaf4ece6Schristos len = (unsigned)(next[copy++]); 764aaf4ece6Schristos if (state->head != Z_NULL && 765aaf4ece6Schristos state->head->name != Z_NULL && 766aaf4ece6Schristos state->length < state->head->name_max) 7676db8c6e9Schristos state->head->name[state->length++] = (Bytef)len; 768aaf4ece6Schristos } while (len && copy < have); 7696db8c6e9Schristos if ((state->flags & 0x0200) && (state->wrap & 4)) 770aaf4ece6Schristos state->check = crc32(state->check, next, copy); 771aaf4ece6Schristos have -= copy; 772aaf4ece6Schristos next += copy; 773aaf4ece6Schristos if (len) goto inf_leave; 774aaf4ece6Schristos } 775aaf4ece6Schristos else if (state->head != Z_NULL) 776aaf4ece6Schristos state->head->name = Z_NULL; 777aaf4ece6Schristos state->length = 0; 778aaf4ece6Schristos state->mode = COMMENT; 7793b1253e8Schristos /* fallthrough */ 780aaf4ece6Schristos case COMMENT: 781aaf4ece6Schristos if (state->flags & 0x1000) { 782aaf4ece6Schristos if (have == 0) goto inf_leave; 783aaf4ece6Schristos copy = 0; 784aaf4ece6Schristos do { 785aaf4ece6Schristos len = (unsigned)(next[copy++]); 786aaf4ece6Schristos if (state->head != Z_NULL && 787aaf4ece6Schristos state->head->comment != Z_NULL && 788aaf4ece6Schristos state->length < state->head->comm_max) 7896db8c6e9Schristos state->head->comment[state->length++] = (Bytef)len; 790aaf4ece6Schristos } while (len && copy < have); 7916db8c6e9Schristos if ((state->flags & 0x0200) && (state->wrap & 4)) 792aaf4ece6Schristos state->check = crc32(state->check, next, copy); 793aaf4ece6Schristos have -= copy; 794aaf4ece6Schristos next += copy; 795aaf4ece6Schristos if (len) goto inf_leave; 796aaf4ece6Schristos } 797aaf4ece6Schristos else if (state->head != Z_NULL) 798aaf4ece6Schristos state->head->comment = Z_NULL; 799aaf4ece6Schristos state->mode = HCRC; 8003b1253e8Schristos /* fallthrough */ 801aaf4ece6Schristos case HCRC: 802aaf4ece6Schristos if (state->flags & 0x0200) { 803aaf4ece6Schristos NEEDBITS(16); 8046db8c6e9Schristos if ((state->wrap & 4) && hold != (state->check & 0xffff)) { 805b85ba082Schristos strm->msg = __UNCONST("header crc mismatch"); 806aaf4ece6Schristos state->mode = BAD; 807aaf4ece6Schristos break; 808aaf4ece6Schristos } 809aaf4ece6Schristos INITBITS(); 810aaf4ece6Schristos } 811aaf4ece6Schristos if (state->head != Z_NULL) { 812aaf4ece6Schristos state->head->hcrc = (int)((state->flags >> 9) & 1); 813aaf4ece6Schristos state->head->done = 1; 814aaf4ece6Schristos } 815aaf4ece6Schristos strm->adler = state->check = crc32(0L, Z_NULL, 0); 816aaf4ece6Schristos state->mode = TYPE; 817aaf4ece6Schristos break; 818aaf4ece6Schristos #endif 819aaf4ece6Schristos case DICTID: 820aaf4ece6Schristos NEEDBITS(32); 8216db8c6e9Schristos strm->adler = state->check = ZSWAP32(hold); 822aaf4ece6Schristos INITBITS(); 823aaf4ece6Schristos state->mode = DICT; 8243b1253e8Schristos /* fallthrough */ 825aaf4ece6Schristos case DICT: 826aaf4ece6Schristos if (state->havedict == 0) { 827aaf4ece6Schristos RESTORE(); 828aaf4ece6Schristos return Z_NEED_DICT; 829aaf4ece6Schristos } 830aaf4ece6Schristos strm->adler = state->check = adler32(0L, Z_NULL, 0); 831aaf4ece6Schristos state->mode = TYPE; 8323b1253e8Schristos /* fallthrough */ 833aaf4ece6Schristos case TYPE: 8346db8c6e9Schristos if (flush == Z_BLOCK || flush == Z_TREES) goto inf_leave; 8353b1253e8Schristos /* fallthrough */ 836aaf4ece6Schristos case TYPEDO: 837aaf4ece6Schristos if (state->last) { 838aaf4ece6Schristos BYTEBITS(); 839aaf4ece6Schristos state->mode = CHECK; 840aaf4ece6Schristos break; 841aaf4ece6Schristos } 842aaf4ece6Schristos NEEDBITS(3); 843aaf4ece6Schristos state->last = BITS(1); 844aaf4ece6Schristos DROPBITS(1); 845aaf4ece6Schristos switch (BITS(2)) { 846aaf4ece6Schristos case 0: /* stored block */ 847aaf4ece6Schristos Tracev((stderr, "inflate: stored block%s\n", 848aaf4ece6Schristos state->last ? " (last)" : "")); 849aaf4ece6Schristos state->mode = STORED; 850aaf4ece6Schristos break; 851aaf4ece6Schristos case 1: /* fixed block */ 852aaf4ece6Schristos fixedtables(state); 853aaf4ece6Schristos Tracev((stderr, "inflate: fixed codes block%s\n", 854aaf4ece6Schristos state->last ? " (last)" : "")); 8556db8c6e9Schristos state->mode = LEN_; /* decode codes */ 8566db8c6e9Schristos if (flush == Z_TREES) { 8576db8c6e9Schristos DROPBITS(2); 8586db8c6e9Schristos goto inf_leave; 8596db8c6e9Schristos } 860aaf4ece6Schristos break; 861aaf4ece6Schristos case 2: /* dynamic block */ 862aaf4ece6Schristos Tracev((stderr, "inflate: dynamic codes block%s\n", 863aaf4ece6Schristos state->last ? " (last)" : "")); 864aaf4ece6Schristos state->mode = TABLE; 865aaf4ece6Schristos break; 866aaf4ece6Schristos case 3: 867b85ba082Schristos strm->msg = __UNCONST("invalid block type"); 868aaf4ece6Schristos state->mode = BAD; 869aaf4ece6Schristos } 870aaf4ece6Schristos DROPBITS(2); 871aaf4ece6Schristos break; 872aaf4ece6Schristos case STORED: 873aaf4ece6Schristos BYTEBITS(); /* go to byte boundary */ 874aaf4ece6Schristos NEEDBITS(32); 875aaf4ece6Schristos if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) { 876b85ba082Schristos strm->msg = __UNCONST("invalid stored block lengths"); 877aaf4ece6Schristos state->mode = BAD; 878aaf4ece6Schristos break; 879aaf4ece6Schristos } 880aaf4ece6Schristos state->length = (unsigned)hold & 0xffff; 881aaf4ece6Schristos Tracev((stderr, "inflate: stored length %u\n", 882aaf4ece6Schristos state->length)); 883aaf4ece6Schristos INITBITS(); 8846db8c6e9Schristos state->mode = COPY_; 8856db8c6e9Schristos if (flush == Z_TREES) goto inf_leave; 8863b1253e8Schristos /* fallthrough */ 8876db8c6e9Schristos case COPY_: 888aaf4ece6Schristos state->mode = COPY; 8893b1253e8Schristos /* fallthrough */ 890aaf4ece6Schristos case COPY: 891aaf4ece6Schristos copy = state->length; 892aaf4ece6Schristos if (copy) { 893aaf4ece6Schristos if (copy > have) copy = have; 894aaf4ece6Schristos if (copy > left) copy = left; 895aaf4ece6Schristos if (copy == 0) goto inf_leave; 896aaf4ece6Schristos zmemcpy(put, next, copy); 897aaf4ece6Schristos have -= copy; 898aaf4ece6Schristos next += copy; 899aaf4ece6Schristos left -= copy; 900aaf4ece6Schristos put += copy; 901aaf4ece6Schristos state->length -= copy; 902aaf4ece6Schristos break; 903aaf4ece6Schristos } 904aaf4ece6Schristos Tracev((stderr, "inflate: stored end\n")); 905aaf4ece6Schristos state->mode = TYPE; 906aaf4ece6Schristos break; 907aaf4ece6Schristos case TABLE: 908aaf4ece6Schristos NEEDBITS(14); 909aaf4ece6Schristos state->nlen = BITS(5) + 257; 910aaf4ece6Schristos DROPBITS(5); 911aaf4ece6Schristos state->ndist = BITS(5) + 1; 912aaf4ece6Schristos DROPBITS(5); 913aaf4ece6Schristos state->ncode = BITS(4) + 4; 914aaf4ece6Schristos DROPBITS(4); 915aaf4ece6Schristos #ifndef PKZIP_BUG_WORKAROUND 916aaf4ece6Schristos if (state->nlen > 286 || state->ndist > 30) { 917b85ba082Schristos strm->msg = __UNCONST("too many length or distance symbols"); 918aaf4ece6Schristos state->mode = BAD; 919aaf4ece6Schristos break; 920aaf4ece6Schristos } 921aaf4ece6Schristos #endif 922aaf4ece6Schristos Tracev((stderr, "inflate: table sizes ok\n")); 923aaf4ece6Schristos state->have = 0; 924aaf4ece6Schristos state->mode = LENLENS; 9253b1253e8Schristos /* fallthrough */ 926aaf4ece6Schristos case LENLENS: 927aaf4ece6Schristos while (state->have < state->ncode) { 928aaf4ece6Schristos NEEDBITS(3); 929aaf4ece6Schristos state->lens[order[state->have++]] = (unsigned short)BITS(3); 930aaf4ece6Schristos DROPBITS(3); 931aaf4ece6Schristos } 932aaf4ece6Schristos while (state->have < 19) 933aaf4ece6Schristos state->lens[order[state->have++]] = 0; 934aaf4ece6Schristos state->next = state->codes; 9356db8c6e9Schristos state->lencode = (const code FAR *)(state->next); 936aaf4ece6Schristos state->lenbits = 7; 937aaf4ece6Schristos ret = inflate_table(CODES, state->lens, 19, &(state->next), 938aaf4ece6Schristos &(state->lenbits), state->work); 939aaf4ece6Schristos if (ret) { 940b85ba082Schristos strm->msg = __UNCONST("invalid code lengths set"); 941aaf4ece6Schristos state->mode = BAD; 942aaf4ece6Schristos break; 943aaf4ece6Schristos } 944aaf4ece6Schristos Tracev((stderr, "inflate: code lengths ok\n")); 945aaf4ece6Schristos state->have = 0; 946aaf4ece6Schristos state->mode = CODELENS; 9473b1253e8Schristos /* fallthrough */ 948aaf4ece6Schristos case CODELENS: 949aaf4ece6Schristos while (state->have < state->nlen + state->ndist) { 950aaf4ece6Schristos for (;;) { 9516db8c6e9Schristos here = state->lencode[BITS(state->lenbits)]; 9526db8c6e9Schristos if ((unsigned)(here.bits) <= bits) break; 953aaf4ece6Schristos PULLBYTE(); 954aaf4ece6Schristos } 9556db8c6e9Schristos if (here.val < 16) { 9566db8c6e9Schristos DROPBITS(here.bits); 9576db8c6e9Schristos state->lens[state->have++] = here.val; 958aaf4ece6Schristos } 959aaf4ece6Schristos else { 9606db8c6e9Schristos if (here.val == 16) { 9616db8c6e9Schristos NEEDBITS(here.bits + 2); 9626db8c6e9Schristos DROPBITS(here.bits); 963aaf4ece6Schristos if (state->have == 0) { 964b85ba082Schristos strm->msg = __UNCONST("invalid bit length repeat"); 965aaf4ece6Schristos state->mode = BAD; 966aaf4ece6Schristos break; 967aaf4ece6Schristos } 968aaf4ece6Schristos len = state->lens[state->have - 1]; 969aaf4ece6Schristos copy = 3 + BITS(2); 970aaf4ece6Schristos DROPBITS(2); 971aaf4ece6Schristos } 9726db8c6e9Schristos else if (here.val == 17) { 9736db8c6e9Schristos NEEDBITS(here.bits + 3); 9746db8c6e9Schristos DROPBITS(here.bits); 975aaf4ece6Schristos len = 0; 976aaf4ece6Schristos copy = 3 + BITS(3); 977aaf4ece6Schristos DROPBITS(3); 978aaf4ece6Schristos } 979aaf4ece6Schristos else { 9806db8c6e9Schristos NEEDBITS(here.bits + 7); 9816db8c6e9Schristos DROPBITS(here.bits); 982aaf4ece6Schristos len = 0; 983aaf4ece6Schristos copy = 11 + BITS(7); 984aaf4ece6Schristos DROPBITS(7); 985aaf4ece6Schristos } 986aaf4ece6Schristos if (state->have + copy > state->nlen + state->ndist) { 987b85ba082Schristos strm->msg = __UNCONST("invalid bit length repeat"); 988aaf4ece6Schristos state->mode = BAD; 989aaf4ece6Schristos break; 990aaf4ece6Schristos } 991aaf4ece6Schristos while (copy--) 992aaf4ece6Schristos state->lens[state->have++] = (unsigned short)len; 993aaf4ece6Schristos } 994aaf4ece6Schristos } 995aaf4ece6Schristos 996aaf4ece6Schristos /* handle error breaks in while */ 997aaf4ece6Schristos if (state->mode == BAD) break; 998aaf4ece6Schristos 9996db8c6e9Schristos /* check for end-of-block code (better have one) */ 10006db8c6e9Schristos if (state->lens[256] == 0) { 10016db8c6e9Schristos strm->msg = __UNCONST("invalid code -- missing end-of-block"); 10026db8c6e9Schristos state->mode = BAD; 10036db8c6e9Schristos break; 10046db8c6e9Schristos } 10056db8c6e9Schristos 10066db8c6e9Schristos /* build code tables -- note: do not change the lenbits or distbits 10076db8c6e9Schristos values here (9 and 6) without reading the comments in inftrees.h 10086db8c6e9Schristos concerning the ENOUGH constants, which depend on those values */ 1009aaf4ece6Schristos state->next = state->codes; 10106db8c6e9Schristos state->lencode = (const code FAR *)(state->next); 1011aaf4ece6Schristos state->lenbits = 9; 1012aaf4ece6Schristos ret = inflate_table(LENS, state->lens, state->nlen, &(state->next), 1013aaf4ece6Schristos &(state->lenbits), state->work); 1014aaf4ece6Schristos if (ret) { 1015b85ba082Schristos strm->msg = __UNCONST("invalid literal/lengths set"); 1016aaf4ece6Schristos state->mode = BAD; 1017aaf4ece6Schristos break; 1018aaf4ece6Schristos } 10196db8c6e9Schristos state->distcode = (const code FAR *)(state->next); 1020aaf4ece6Schristos state->distbits = 6; 1021aaf4ece6Schristos ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist, 1022aaf4ece6Schristos &(state->next), &(state->distbits), state->work); 1023aaf4ece6Schristos if (ret) { 1024b85ba082Schristos strm->msg = __UNCONST("invalid distances set"); 1025aaf4ece6Schristos state->mode = BAD; 1026aaf4ece6Schristos break; 1027aaf4ece6Schristos } 1028aaf4ece6Schristos Tracev((stderr, "inflate: codes ok\n")); 10296db8c6e9Schristos state->mode = LEN_; 10306db8c6e9Schristos if (flush == Z_TREES) goto inf_leave; 10313b1253e8Schristos /* fallthrough */ 10326db8c6e9Schristos case LEN_: 1033aaf4ece6Schristos state->mode = LEN; 10343b1253e8Schristos /* fallthrough */ 1035aaf4ece6Schristos case LEN: 1036aaf4ece6Schristos if (have >= 6 && left >= 258) { 1037aaf4ece6Schristos RESTORE(); 1038aaf4ece6Schristos inflate_fast(strm, out); 1039aaf4ece6Schristos LOAD(); 10406db8c6e9Schristos if (state->mode == TYPE) 10416db8c6e9Schristos state->back = -1; 1042aaf4ece6Schristos break; 1043aaf4ece6Schristos } 10446db8c6e9Schristos state->back = 0; 1045aaf4ece6Schristos for (;;) { 10466db8c6e9Schristos here = state->lencode[BITS(state->lenbits)]; 10476db8c6e9Schristos if ((unsigned)(here.bits) <= bits) break; 1048aaf4ece6Schristos PULLBYTE(); 1049aaf4ece6Schristos } 10506db8c6e9Schristos if (here.op && (here.op & 0xf0) == 0) { 10516db8c6e9Schristos last = here; 1052aaf4ece6Schristos for (;;) { 10536db8c6e9Schristos here = state->lencode[last.val + 1054aaf4ece6Schristos (BITS(last.bits + last.op) >> last.bits)]; 10556db8c6e9Schristos if ((unsigned)(last.bits + here.bits) <= bits) break; 1056aaf4ece6Schristos PULLBYTE(); 1057aaf4ece6Schristos } 1058aaf4ece6Schristos DROPBITS(last.bits); 10596db8c6e9Schristos state->back += last.bits; 1060aaf4ece6Schristos } 10616db8c6e9Schristos DROPBITS(here.bits); 10626db8c6e9Schristos state->back += here.bits; 10636db8c6e9Schristos state->length = (unsigned)here.val; 10646db8c6e9Schristos if ((int)(here.op) == 0) { 10656db8c6e9Schristos Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ? 1066aaf4ece6Schristos "inflate: literal '%c'\n" : 10676db8c6e9Schristos "inflate: literal 0x%02x\n", here.val)); 1068aaf4ece6Schristos state->mode = LIT; 1069aaf4ece6Schristos break; 1070aaf4ece6Schristos } 10716db8c6e9Schristos if (here.op & 32) { 1072aaf4ece6Schristos Tracevv((stderr, "inflate: end of block\n")); 10736db8c6e9Schristos state->back = -1; 1074aaf4ece6Schristos state->mode = TYPE; 1075aaf4ece6Schristos break; 1076aaf4ece6Schristos } 10776db8c6e9Schristos if (here.op & 64) { 1078b85ba082Schristos strm->msg = __UNCONST("invalid literal/length code"); 1079aaf4ece6Schristos state->mode = BAD; 1080aaf4ece6Schristos break; 1081aaf4ece6Schristos } 10826db8c6e9Schristos state->extra = (unsigned)(here.op) & 15; 1083aaf4ece6Schristos state->mode = LENEXT; 10843b1253e8Schristos /* fallthrough */ 1085aaf4ece6Schristos case LENEXT: 1086aaf4ece6Schristos if (state->extra) { 1087aaf4ece6Schristos NEEDBITS(state->extra); 1088aaf4ece6Schristos state->length += BITS(state->extra); 1089aaf4ece6Schristos DROPBITS(state->extra); 10906db8c6e9Schristos state->back += state->extra; 1091aaf4ece6Schristos } 1092aaf4ece6Schristos Tracevv((stderr, "inflate: length %u\n", state->length)); 10936db8c6e9Schristos state->was = state->length; 1094aaf4ece6Schristos state->mode = DIST; 10953b1253e8Schristos /* fallthrough */ 1096aaf4ece6Schristos case DIST: 1097aaf4ece6Schristos for (;;) { 10986db8c6e9Schristos here = state->distcode[BITS(state->distbits)]; 10996db8c6e9Schristos if ((unsigned)(here.bits) <= bits) break; 1100aaf4ece6Schristos PULLBYTE(); 1101aaf4ece6Schristos } 11026db8c6e9Schristos if ((here.op & 0xf0) == 0) { 11036db8c6e9Schristos last = here; 1104aaf4ece6Schristos for (;;) { 11056db8c6e9Schristos here = state->distcode[last.val + 1106aaf4ece6Schristos (BITS(last.bits + last.op) >> last.bits)]; 11076db8c6e9Schristos if ((unsigned)(last.bits + here.bits) <= bits) break; 1108aaf4ece6Schristos PULLBYTE(); 1109aaf4ece6Schristos } 1110aaf4ece6Schristos DROPBITS(last.bits); 11116db8c6e9Schristos state->back += last.bits; 1112aaf4ece6Schristos } 11136db8c6e9Schristos DROPBITS(here.bits); 11146db8c6e9Schristos state->back += here.bits; 11156db8c6e9Schristos if (here.op & 64) { 1116b85ba082Schristos strm->msg = __UNCONST("invalid distance code"); 1117aaf4ece6Schristos state->mode = BAD; 1118aaf4ece6Schristos break; 1119aaf4ece6Schristos } 11206db8c6e9Schristos state->offset = (unsigned)here.val; 11216db8c6e9Schristos state->extra = (unsigned)(here.op) & 15; 1122aaf4ece6Schristos state->mode = DISTEXT; 11233b1253e8Schristos /* fallthrough */ 1124aaf4ece6Schristos case DISTEXT: 1125aaf4ece6Schristos if (state->extra) { 1126aaf4ece6Schristos NEEDBITS(state->extra); 1127aaf4ece6Schristos state->offset += BITS(state->extra); 1128aaf4ece6Schristos DROPBITS(state->extra); 11296db8c6e9Schristos state->back += state->extra; 1130aaf4ece6Schristos } 1131aaf4ece6Schristos #ifdef INFLATE_STRICT 1132aaf4ece6Schristos if (state->offset > state->dmax) { 1133b85ba082Schristos strm->msg = __UNCONST("invalid distance too far back"); 1134aaf4ece6Schristos state->mode = BAD; 1135aaf4ece6Schristos break; 1136aaf4ece6Schristos } 1137aaf4ece6Schristos #endif 1138aaf4ece6Schristos Tracevv((stderr, "inflate: distance %u\n", state->offset)); 1139aaf4ece6Schristos state->mode = MATCH; 11403b1253e8Schristos /* fallthrough */ 1141aaf4ece6Schristos case MATCH: 1142aaf4ece6Schristos if (left == 0) goto inf_leave; 1143aaf4ece6Schristos copy = out - left; 1144aaf4ece6Schristos if (state->offset > copy) { /* copy from window */ 1145aaf4ece6Schristos copy = state->offset - copy; 11466db8c6e9Schristos if (copy > state->whave) { 11476db8c6e9Schristos if (state->sane) { 11486db8c6e9Schristos strm->msg = __UNCONST("invalid distance too far back"); 11496db8c6e9Schristos state->mode = BAD; 11506db8c6e9Schristos break; 11516db8c6e9Schristos } 11526db8c6e9Schristos #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR 11536db8c6e9Schristos Trace((stderr, "inflate.c too far\n")); 11546db8c6e9Schristos copy -= state->whave; 11556db8c6e9Schristos if (copy > state->length) copy = state->length; 11566db8c6e9Schristos if (copy > left) copy = left; 11576db8c6e9Schristos left -= copy; 11586db8c6e9Schristos state->length -= copy; 11596db8c6e9Schristos do { 11606db8c6e9Schristos *put++ = 0; 11616db8c6e9Schristos } while (--copy); 11626db8c6e9Schristos if (state->length == 0) state->mode = LEN; 11636db8c6e9Schristos break; 11646db8c6e9Schristos #endif 11656db8c6e9Schristos } 11666db8c6e9Schristos if (copy > state->wnext) { 11676db8c6e9Schristos copy -= state->wnext; 1168aaf4ece6Schristos from = state->window + (state->wsize - copy); 1169aaf4ece6Schristos } 1170aaf4ece6Schristos else 11716db8c6e9Schristos from = state->window + (state->wnext - copy); 1172aaf4ece6Schristos if (copy > state->length) copy = state->length; 1173aaf4ece6Schristos } 1174aaf4ece6Schristos else { /* copy from output */ 1175aaf4ece6Schristos from = put - state->offset; 1176aaf4ece6Schristos copy = state->length; 1177aaf4ece6Schristos } 1178aaf4ece6Schristos if (copy > left) copy = left; 1179aaf4ece6Schristos left -= copy; 1180aaf4ece6Schristos state->length -= copy; 1181aaf4ece6Schristos do { 1182aaf4ece6Schristos *put++ = *from++; 1183aaf4ece6Schristos } while (--copy); 1184aaf4ece6Schristos if (state->length == 0) state->mode = LEN; 1185aaf4ece6Schristos break; 1186aaf4ece6Schristos case LIT: 1187aaf4ece6Schristos if (left == 0) goto inf_leave; 1188aaf4ece6Schristos *put++ = (unsigned char)(state->length); 1189aaf4ece6Schristos left--; 1190aaf4ece6Schristos state->mode = LEN; 1191aaf4ece6Schristos break; 1192aaf4ece6Schristos case CHECK: 1193aaf4ece6Schristos if (state->wrap) { 1194aaf4ece6Schristos NEEDBITS(32); 1195aaf4ece6Schristos out -= left; 1196aaf4ece6Schristos strm->total_out += out; 1197aaf4ece6Schristos state->total += out; 11986db8c6e9Schristos if ((state->wrap & 4) && out) 1199aaf4ece6Schristos strm->adler = state->check = 12003b1253e8Schristos UPDATE_CHECK(state->check, put - out, out); 1201aaf4ece6Schristos out = left; 12026db8c6e9Schristos if ((state->wrap & 4) && ( 1203aaf4ece6Schristos #ifdef GUNZIP 1204aaf4ece6Schristos state->flags ? hold : 1205aaf4ece6Schristos #endif 12066db8c6e9Schristos ZSWAP32(hold)) != state->check) { 1207b85ba082Schristos strm->msg = __UNCONST("incorrect data check"); 1208aaf4ece6Schristos state->mode = BAD; 1209aaf4ece6Schristos break; 1210aaf4ece6Schristos } 1211aaf4ece6Schristos INITBITS(); 1212aaf4ece6Schristos Tracev((stderr, "inflate: check matches trailer\n")); 1213aaf4ece6Schristos } 1214aaf4ece6Schristos #ifdef GUNZIP 1215aaf4ece6Schristos state->mode = LENGTH; 12163b1253e8Schristos /* fallthrough */ 1217aaf4ece6Schristos case LENGTH: 1218aaf4ece6Schristos if (state->wrap && state->flags) { 1219aaf4ece6Schristos NEEDBITS(32); 12203b1253e8Schristos if ((state->wrap & 4) && hold != (state->total & 0xffffffff)) { 1221b85ba082Schristos strm->msg = __UNCONST("incorrect length check"); 1222aaf4ece6Schristos state->mode = BAD; 1223aaf4ece6Schristos break; 1224aaf4ece6Schristos } 1225aaf4ece6Schristos INITBITS(); 1226aaf4ece6Schristos Tracev((stderr, "inflate: length matches trailer\n")); 1227aaf4ece6Schristos } 1228aaf4ece6Schristos #endif 1229aaf4ece6Schristos state->mode = DONE; 12303b1253e8Schristos /* fallthrough */ 1231aaf4ece6Schristos case DONE: 1232aaf4ece6Schristos ret = Z_STREAM_END; 1233aaf4ece6Schristos goto inf_leave; 1234aaf4ece6Schristos case BAD: 1235aaf4ece6Schristos ret = Z_DATA_ERROR; 1236aaf4ece6Schristos goto inf_leave; 1237aaf4ece6Schristos case MEM: 1238aaf4ece6Schristos return Z_MEM_ERROR; 1239aaf4ece6Schristos case SYNC: 12403b1253e8Schristos /* fallthrough */ 1241aaf4ece6Schristos default: 1242aaf4ece6Schristos return Z_STREAM_ERROR; 1243aaf4ece6Schristos } 1244aaf4ece6Schristos 1245aaf4ece6Schristos /* 1246aaf4ece6Schristos Return from inflate(), updating the total counts and the check value. 1247aaf4ece6Schristos If there was no progress during the inflate() call, return a buffer 1248aaf4ece6Schristos error. Call updatewindow() to create and/or update the window state. 1249aaf4ece6Schristos Note: a memory error from inflate() is non-recoverable. 1250aaf4ece6Schristos */ 1251aaf4ece6Schristos inf_leave: 1252aaf4ece6Schristos RESTORE(); 12536db8c6e9Schristos if (state->wsize || (out != strm->avail_out && state->mode < BAD && 12546db8c6e9Schristos (state->mode < CHECK || flush != Z_FINISH))) 12556db8c6e9Schristos if (updatewindow(strm, strm->next_out, out - strm->avail_out)) { 1256aaf4ece6Schristos state->mode = MEM; 1257aaf4ece6Schristos return Z_MEM_ERROR; 1258aaf4ece6Schristos } 1259aaf4ece6Schristos in -= strm->avail_in; 1260aaf4ece6Schristos out -= strm->avail_out; 1261aaf4ece6Schristos strm->total_in += in; 1262aaf4ece6Schristos strm->total_out += out; 1263aaf4ece6Schristos state->total += out; 12646db8c6e9Schristos if ((state->wrap & 4) && out) 1265aaf4ece6Schristos strm->adler = state->check = 12663b1253e8Schristos UPDATE_CHECK(state->check, strm->next_out - out, out); 12676db8c6e9Schristos strm->data_type = (int)state->bits + (state->last ? 64 : 0) + 12686db8c6e9Schristos (state->mode == TYPE ? 128 : 0) + 12696db8c6e9Schristos (state->mode == LEN_ || state->mode == COPY_ ? 256 : 0); 1270aaf4ece6Schristos if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK) 1271aaf4ece6Schristos ret = Z_BUF_ERROR; 1272aaf4ece6Schristos return ret; 1273aaf4ece6Schristos } 1274aaf4ece6Schristos 1275*96c32821Schristos int ZEXPORT inflateEnd(z_streamp strm) { 1276aaf4ece6Schristos struct inflate_state FAR *state; 12776db8c6e9Schristos if (inflateStateCheck(strm)) 1278aaf4ece6Schristos return Z_STREAM_ERROR; 1279aaf4ece6Schristos state = (struct inflate_state FAR *)strm->state; 1280aaf4ece6Schristos if (state->window != Z_NULL) ZFREE(strm, state->window); 1281aaf4ece6Schristos ZFREE(strm, strm->state); 1282aaf4ece6Schristos strm->state = Z_NULL; 1283aaf4ece6Schristos Tracev((stderr, "inflate: end\n")); 1284aaf4ece6Schristos return Z_OK; 1285aaf4ece6Schristos } 1286aaf4ece6Schristos 1287*96c32821Schristos int ZEXPORT inflateGetDictionary(z_streamp strm, Bytef *dictionary, 1288*96c32821Schristos uInt *dictLength) { 12896db8c6e9Schristos struct inflate_state FAR *state; 12906db8c6e9Schristos 12916db8c6e9Schristos /* check state */ 12926db8c6e9Schristos if (inflateStateCheck(strm)) return Z_STREAM_ERROR; 12936db8c6e9Schristos state = (struct inflate_state FAR *)strm->state; 12946db8c6e9Schristos 12956db8c6e9Schristos /* copy dictionary */ 12966db8c6e9Schristos if (state->whave && dictionary != Z_NULL) { 12976db8c6e9Schristos zmemcpy(dictionary, state->window + state->wnext, 12986db8c6e9Schristos state->whave - state->wnext); 12996db8c6e9Schristos zmemcpy(dictionary + state->whave - state->wnext, 13006db8c6e9Schristos state->window, state->wnext); 13016db8c6e9Schristos } 13026db8c6e9Schristos if (dictLength != Z_NULL) 13036db8c6e9Schristos *dictLength = state->whave; 13046db8c6e9Schristos return Z_OK; 13056db8c6e9Schristos } 13066db8c6e9Schristos 1307*96c32821Schristos int ZEXPORT inflateSetDictionary(z_streamp strm, const Bytef *dictionary, 1308*96c32821Schristos uInt dictLength) { 1309aaf4ece6Schristos struct inflate_state FAR *state; 13106db8c6e9Schristos unsigned long dictid; 13116db8c6e9Schristos int ret; 1312aaf4ece6Schristos 1313aaf4ece6Schristos /* check state */ 13146db8c6e9Schristos if (inflateStateCheck(strm)) return Z_STREAM_ERROR; 1315aaf4ece6Schristos state = (struct inflate_state FAR *)strm->state; 1316aaf4ece6Schristos if (state->wrap != 0 && state->mode != DICT) 1317aaf4ece6Schristos return Z_STREAM_ERROR; 1318aaf4ece6Schristos 13196db8c6e9Schristos /* check for correct dictionary identifier */ 1320aaf4ece6Schristos if (state->mode == DICT) { 13216db8c6e9Schristos dictid = adler32(0L, Z_NULL, 0); 13226db8c6e9Schristos dictid = adler32(dictid, dictionary, dictLength); 13236db8c6e9Schristos if (dictid != state->check) 1324aaf4ece6Schristos return Z_DATA_ERROR; 1325aaf4ece6Schristos } 1326aaf4ece6Schristos 13276db8c6e9Schristos /* copy dictionary to window using updatewindow(), which will amend the 13286db8c6e9Schristos existing dictionary if appropriate */ 13296db8c6e9Schristos ret = updatewindow(strm, dictionary + dictLength, dictLength); 13306db8c6e9Schristos if (ret) { 1331aaf4ece6Schristos state->mode = MEM; 1332aaf4ece6Schristos return Z_MEM_ERROR; 1333aaf4ece6Schristos } 1334aaf4ece6Schristos state->havedict = 1; 1335aaf4ece6Schristos Tracev((stderr, "inflate: dictionary set\n")); 1336aaf4ece6Schristos return Z_OK; 1337aaf4ece6Schristos } 1338aaf4ece6Schristos 1339*96c32821Schristos int ZEXPORT inflateGetHeader(z_streamp strm, gz_headerp head) { 1340aaf4ece6Schristos struct inflate_state FAR *state; 1341aaf4ece6Schristos 1342aaf4ece6Schristos /* check state */ 13436db8c6e9Schristos if (inflateStateCheck(strm)) return Z_STREAM_ERROR; 1344aaf4ece6Schristos state = (struct inflate_state FAR *)strm->state; 1345aaf4ece6Schristos if ((state->wrap & 2) == 0) return Z_STREAM_ERROR; 1346aaf4ece6Schristos 1347aaf4ece6Schristos /* save header structure */ 1348aaf4ece6Schristos state->head = head; 1349aaf4ece6Schristos head->done = 0; 1350aaf4ece6Schristos return Z_OK; 1351aaf4ece6Schristos } 1352aaf4ece6Schristos 1353aaf4ece6Schristos /* 1354aaf4ece6Schristos Search buf[0..len-1] for the pattern: 0, 0, 0xff, 0xff. Return when found 1355aaf4ece6Schristos or when out of input. When called, *have is the number of pattern bytes 1356aaf4ece6Schristos found in order so far, in 0..3. On return *have is updated to the new 1357aaf4ece6Schristos state. If on return *have equals four, then the pattern was found and the 1358aaf4ece6Schristos return value is how many bytes were read including the last byte of the 1359aaf4ece6Schristos pattern. If *have is less than four, then the pattern has not been found 1360aaf4ece6Schristos yet and the return value is len. In the latter case, syncsearch() can be 1361aaf4ece6Schristos called again with more data and the *have state. *have is initialized to 1362aaf4ece6Schristos zero for the first call. 1363aaf4ece6Schristos */ 1364*96c32821Schristos local unsigned syncsearch(unsigned FAR *have, const unsigned char FAR *buf, 1365*96c32821Schristos unsigned len) { 1366aaf4ece6Schristos unsigned got; 1367aaf4ece6Schristos unsigned next; 1368aaf4ece6Schristos 1369aaf4ece6Schristos got = *have; 1370aaf4ece6Schristos next = 0; 1371aaf4ece6Schristos while (next < len && got < 4) { 1372aaf4ece6Schristos if ((int)(buf[next]) == (got < 2 ? 0 : 0xff)) 1373aaf4ece6Schristos got++; 1374aaf4ece6Schristos else if (buf[next]) 1375aaf4ece6Schristos got = 0; 1376aaf4ece6Schristos else 1377aaf4ece6Schristos got = 4 - got; 1378aaf4ece6Schristos next++; 1379aaf4ece6Schristos } 1380aaf4ece6Schristos *have = got; 1381aaf4ece6Schristos return next; 1382aaf4ece6Schristos } 1383aaf4ece6Schristos 1384*96c32821Schristos int ZEXPORT inflateSync(z_streamp strm) { 1385aaf4ece6Schristos unsigned len; /* number of bytes to look at or looked at */ 13863b1253e8Schristos int flags; /* temporary to save header status */ 1387aaf4ece6Schristos unsigned long in, out; /* temporary to save total_in and total_out */ 1388aaf4ece6Schristos unsigned char buf[4]; /* to restore bit buffer to byte string */ 1389aaf4ece6Schristos struct inflate_state FAR *state; 1390aaf4ece6Schristos 1391aaf4ece6Schristos /* check parameters */ 13926db8c6e9Schristos if (inflateStateCheck(strm)) return Z_STREAM_ERROR; 1393aaf4ece6Schristos state = (struct inflate_state FAR *)strm->state; 1394aaf4ece6Schristos if (strm->avail_in == 0 && state->bits < 8) return Z_BUF_ERROR; 1395aaf4ece6Schristos 1396aaf4ece6Schristos /* if first time, start search in bit buffer */ 1397aaf4ece6Schristos if (state->mode != SYNC) { 1398aaf4ece6Schristos state->mode = SYNC; 1399*96c32821Schristos state->hold >>= state->bits & 7; 1400aaf4ece6Schristos state->bits -= state->bits & 7; 1401aaf4ece6Schristos len = 0; 1402aaf4ece6Schristos while (state->bits >= 8) { 1403aaf4ece6Schristos buf[len++] = (unsigned char)(state->hold); 1404aaf4ece6Schristos state->hold >>= 8; 1405aaf4ece6Schristos state->bits -= 8; 1406aaf4ece6Schristos } 1407aaf4ece6Schristos state->have = 0; 1408aaf4ece6Schristos syncsearch(&(state->have), buf, len); 1409aaf4ece6Schristos } 1410aaf4ece6Schristos 1411aaf4ece6Schristos /* search available input */ 1412aaf4ece6Schristos len = syncsearch(&(state->have), strm->next_in, strm->avail_in); 1413aaf4ece6Schristos strm->avail_in -= len; 1414aaf4ece6Schristos strm->next_in += len; 1415aaf4ece6Schristos strm->total_in += len; 1416aaf4ece6Schristos 1417aaf4ece6Schristos /* return no joy or set up to restart inflate() on a new block */ 1418aaf4ece6Schristos if (state->have != 4) return Z_DATA_ERROR; 14193b1253e8Schristos if (state->flags == -1) 14203b1253e8Schristos state->wrap = 0; /* if no header yet, treat as raw */ 14213b1253e8Schristos else 14223b1253e8Schristos state->wrap &= ~4; /* no point in computing a check value now */ 14233b1253e8Schristos flags = state->flags; 1424aaf4ece6Schristos in = strm->total_in; out = strm->total_out; 1425aaf4ece6Schristos inflateReset(strm); 1426aaf4ece6Schristos strm->total_in = in; strm->total_out = out; 14273b1253e8Schristos state->flags = flags; 1428aaf4ece6Schristos state->mode = TYPE; 1429aaf4ece6Schristos return Z_OK; 1430aaf4ece6Schristos } 1431aaf4ece6Schristos 1432aaf4ece6Schristos /* 1433aaf4ece6Schristos Returns true if inflate is currently at the end of a block generated by 1434aaf4ece6Schristos Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP 1435aaf4ece6Schristos implementation to provide an additional safety check. PPP uses 1436aaf4ece6Schristos Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored 1437aaf4ece6Schristos block. When decompressing, PPP checks that at the end of input packet, 1438aaf4ece6Schristos inflate is waiting for these length bytes. 1439aaf4ece6Schristos */ 1440*96c32821Schristos int ZEXPORT inflateSyncPoint(z_streamp strm) { 1441aaf4ece6Schristos struct inflate_state FAR *state; 1442aaf4ece6Schristos 14436db8c6e9Schristos if (inflateStateCheck(strm)) return Z_STREAM_ERROR; 1444aaf4ece6Schristos state = (struct inflate_state FAR *)strm->state; 1445aaf4ece6Schristos return state->mode == STORED && state->bits == 0; 1446aaf4ece6Schristos } 1447aaf4ece6Schristos 1448*96c32821Schristos int ZEXPORT inflateCopy(z_streamp dest, z_streamp source) { 1449aaf4ece6Schristos struct inflate_state FAR *state; 1450aaf4ece6Schristos struct inflate_state FAR *copy; 1451aaf4ece6Schristos unsigned char FAR *window; 1452aaf4ece6Schristos unsigned wsize; 1453aaf4ece6Schristos 1454aaf4ece6Schristos /* check input */ 14556db8c6e9Schristos if (inflateStateCheck(source) || dest == Z_NULL) 1456aaf4ece6Schristos return Z_STREAM_ERROR; 1457aaf4ece6Schristos state = (struct inflate_state FAR *)source->state; 1458aaf4ece6Schristos 1459aaf4ece6Schristos /* allocate space */ 1460aaf4ece6Schristos copy = (struct inflate_state FAR *) 1461aaf4ece6Schristos ZALLOC(source, 1, sizeof(struct inflate_state)); 1462aaf4ece6Schristos if (copy == Z_NULL) return Z_MEM_ERROR; 1463aaf4ece6Schristos window = Z_NULL; 1464aaf4ece6Schristos if (state->window != Z_NULL) { 1465aaf4ece6Schristos window = (unsigned char FAR *) 1466aaf4ece6Schristos ZALLOC(source, 1U << state->wbits, sizeof(unsigned char)); 1467aaf4ece6Schristos if (window == Z_NULL) { 1468aaf4ece6Schristos ZFREE(source, copy); 1469aaf4ece6Schristos return Z_MEM_ERROR; 1470aaf4ece6Schristos } 1471aaf4ece6Schristos } 1472aaf4ece6Schristos 1473aaf4ece6Schristos /* copy state */ 14746db8c6e9Schristos zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream)); 14756db8c6e9Schristos zmemcpy((voidpf)copy, (voidpf)state, sizeof(struct inflate_state)); 14766db8c6e9Schristos copy->strm = dest; 1477aaf4ece6Schristos if (state->lencode >= state->codes && 1478aaf4ece6Schristos state->lencode <= state->codes + ENOUGH - 1) { 1479aaf4ece6Schristos copy->lencode = copy->codes + (state->lencode - state->codes); 1480aaf4ece6Schristos copy->distcode = copy->codes + (state->distcode - state->codes); 1481aaf4ece6Schristos } 1482aaf4ece6Schristos copy->next = copy->codes + (state->next - state->codes); 1483aaf4ece6Schristos if (window != Z_NULL) { 1484aaf4ece6Schristos wsize = 1U << state->wbits; 1485aaf4ece6Schristos zmemcpy(window, state->window, wsize); 1486aaf4ece6Schristos } 1487aaf4ece6Schristos copy->window = window; 1488aaf4ece6Schristos dest->state = (struct internal_state FAR *)copy; 1489aaf4ece6Schristos return Z_OK; 1490aaf4ece6Schristos } 14916db8c6e9Schristos 1492*96c32821Schristos int ZEXPORT inflateUndermine(z_streamp strm, int subvert) { 14936db8c6e9Schristos struct inflate_state FAR *state; 14946db8c6e9Schristos 14956db8c6e9Schristos if (inflateStateCheck(strm)) return Z_STREAM_ERROR; 14966db8c6e9Schristos state = (struct inflate_state FAR *)strm->state; 14976db8c6e9Schristos #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR 14986db8c6e9Schristos state->sane = !subvert; 14996db8c6e9Schristos return Z_OK; 15006db8c6e9Schristos #else 15016db8c6e9Schristos (void)subvert; 15026db8c6e9Schristos state->sane = 1; 15036db8c6e9Schristos return Z_DATA_ERROR; 15046db8c6e9Schristos #endif 15056db8c6e9Schristos } 15066db8c6e9Schristos 1507*96c32821Schristos int ZEXPORT inflateValidate(z_streamp strm, int check) { 15086db8c6e9Schristos struct inflate_state FAR *state; 15096db8c6e9Schristos 15106db8c6e9Schristos if (inflateStateCheck(strm)) return Z_STREAM_ERROR; 15116db8c6e9Schristos state = (struct inflate_state FAR *)strm->state; 15123b1253e8Schristos if (check && state->wrap) 15136db8c6e9Schristos state->wrap |= 4; 15146db8c6e9Schristos else 15156db8c6e9Schristos state->wrap &= ~4; 15166db8c6e9Schristos return Z_OK; 15176db8c6e9Schristos } 15186db8c6e9Schristos 1519*96c32821Schristos long ZEXPORT inflateMark(z_streamp strm) { 15206db8c6e9Schristos struct inflate_state FAR *state; 15216db8c6e9Schristos 15226db8c6e9Schristos if (inflateStateCheck(strm)) 15236db8c6e9Schristos return -(1L << 16); 15246db8c6e9Schristos state = (struct inflate_state FAR *)strm->state; 15256db8c6e9Schristos return (long)(((unsigned long)((long)state->back)) << 16) + 15266db8c6e9Schristos (state->mode == COPY ? state->length : 15276db8c6e9Schristos (state->mode == MATCH ? state->was - state->length : 0)); 15286db8c6e9Schristos } 15296db8c6e9Schristos 1530*96c32821Schristos unsigned long ZEXPORT inflateCodesUsed(z_streamp strm) { 15316db8c6e9Schristos struct inflate_state FAR *state; 15326db8c6e9Schristos if (inflateStateCheck(strm)) return (unsigned long)-1; 15336db8c6e9Schristos state = (struct inflate_state FAR *)strm->state; 15346db8c6e9Schristos return (unsigned long)(state->next - state->codes); 15356db8c6e9Schristos } 1536