1131377Stjr/* infback.c -- inflate using a call-back interface 2237410Sdelphij * Copyright (C) 1995-2011 Mark Adler 3131377Stjr * For conditions of distribution and use, see copyright notice in zlib.h 4131377Stjr */ 5131377Stjr 6131377Stjr/* 7131377Stjr This code is largely copied from inflate.c. Normally either infback.o or 8131377Stjr inflate.o would be linked into an application--not both. The interface 9131377Stjr with inffast.c is retained so that optimized assembler-coded versions of 10131377Stjr inflate_fast() can be used with either inflate.c or infback.c. 11131377Stjr */ 12131377Stjr 13131377Stjr#include "zutil.h" 14131377Stjr#include "inftrees.h" 15131377Stjr#include "inflate.h" 16131377Stjr#include "inffast.h" 17131377Stjr 18131377Stjr/* function prototypes */ 19131377Stjrlocal void fixedtables OF((struct inflate_state FAR *state)); 20131377Stjr 21131377Stjr/* 22131377Stjr strm provides memory allocation functions in zalloc and zfree, or 23131377Stjr Z_NULL to use the library memory allocation functions. 24131377Stjr 25131377Stjr windowBits is in the range 8..15, and window is a user-supplied 26131377Stjr window and output buffer that is 2**windowBits bytes. 27131377Stjr */ 28131377Stjrint ZEXPORT inflateBackInit_(strm, windowBits, window, version, stream_size) 29157046Sdesz_streamp strm; 30131377Stjrint windowBits; 31131377Stjrunsigned char FAR *window; 32131377Stjrconst char *version; 33131377Stjrint stream_size; 34131377Stjr{ 35131377Stjr struct inflate_state FAR *state; 36131377Stjr 37131377Stjr if (version == Z_NULL || version[0] != ZLIB_VERSION[0] || 38131377Stjr stream_size != (int)(sizeof(z_stream))) 39131377Stjr return Z_VERSION_ERROR; 40131377Stjr if (strm == Z_NULL || window == Z_NULL || 41131377Stjr windowBits < 8 || windowBits > 15) 42131377Stjr return Z_STREAM_ERROR; 43131377Stjr strm->msg = Z_NULL; /* in case we return an error */ 44131377Stjr if (strm->zalloc == (alloc_func)0) { 45237410Sdelphij#ifdef Z_SOLO 46237410Sdelphij return Z_STREAM_ERROR; 47237410Sdelphij#else 48131377Stjr strm->zalloc = zcalloc; 49131377Stjr strm->opaque = (voidpf)0; 50237410Sdelphij#endif 51131377Stjr } 52237410Sdelphij if (strm->zfree == (free_func)0) 53237410Sdelphij#ifdef Z_SOLO 54237410Sdelphij return Z_STREAM_ERROR; 55237410Sdelphij#else 56237410Sdelphij strm->zfree = zcfree; 57237410Sdelphij#endif 58131377Stjr state = (struct inflate_state FAR *)ZALLOC(strm, 1, 59131377Stjr sizeof(struct inflate_state)); 60131377Stjr if (state == Z_NULL) return Z_MEM_ERROR; 61131377Stjr Tracev((stderr, "inflate: allocated\n")); 62157046Sdes strm->state = (struct internal_state FAR *)state; 63157046Sdes state->dmax = 32768U; 64131377Stjr state->wbits = windowBits; 65131377Stjr state->wsize = 1U << windowBits; 66131377Stjr state->window = window; 67205471Sdelphij state->wnext = 0; 68131377Stjr state->whave = 0; 69131377Stjr return Z_OK; 70131377Stjr} 71131377Stjr 72131377Stjr/* 73131377Stjr Return state with length and distance decoding tables and index sizes set to 74131377Stjr fixed code decoding. Normally this returns fixed tables from inffixed.h. 75131377Stjr If BUILDFIXED is defined, then instead this routine builds the tables the 76131377Stjr first time it's called, and returns those tables the first time and 77131377Stjr thereafter. This reduces the size of the code by about 2K bytes, in 78131377Stjr exchange for a little execution time. However, BUILDFIXED should not be 79131377Stjr used for threaded applications, since the rewriting of the tables and virgin 80131377Stjr may not be thread-safe. 81131377Stjr */ 82131377Stjrlocal void fixedtables(state) 83131377Stjrstruct inflate_state FAR *state; 84131377Stjr{ 85131377Stjr#ifdef BUILDFIXED 86131377Stjr static int virgin = 1; 87131377Stjr static code *lenfix, *distfix; 88131377Stjr static code fixed[544]; 89131377Stjr 90131377Stjr /* build fixed huffman tables if first call (may not be thread safe) */ 91131377Stjr if (virgin) { 92131377Stjr unsigned sym, bits; 93131377Stjr static code *next; 94131377Stjr 95131377Stjr /* literal/length table */ 96131377Stjr sym = 0; 97131377Stjr while (sym < 144) state->lens[sym++] = 8; 98131377Stjr while (sym < 256) state->lens[sym++] = 9; 99131377Stjr while (sym < 280) state->lens[sym++] = 7; 100131377Stjr while (sym < 288) state->lens[sym++] = 8; 101131377Stjr next = fixed; 102131377Stjr lenfix = next; 103131377Stjr bits = 9; 104131377Stjr inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work); 105131377Stjr 106131377Stjr /* distance table */ 107131377Stjr sym = 0; 108131377Stjr while (sym < 32) state->lens[sym++] = 5; 109131377Stjr distfix = next; 110131377Stjr bits = 5; 111131377Stjr inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work); 112131377Stjr 113131377Stjr /* do this just once */ 114131377Stjr virgin = 0; 115131377Stjr } 116131377Stjr#else /* !BUILDFIXED */ 117131377Stjr# include "inffixed.h" 118131377Stjr#endif /* BUILDFIXED */ 119131377Stjr state->lencode = lenfix; 120131377Stjr state->lenbits = 9; 121131377Stjr state->distcode = distfix; 122131377Stjr state->distbits = 5; 123131377Stjr} 124131377Stjr 125131377Stjr/* Macros for inflateBack(): */ 126131377Stjr 127131377Stjr/* Load returned state from inflate_fast() */ 128131377Stjr#define LOAD() \ 129131377Stjr do { \ 130131377Stjr put = strm->next_out; \ 131131377Stjr left = strm->avail_out; \ 132131377Stjr next = strm->next_in; \ 133131377Stjr have = strm->avail_in; \ 134131377Stjr hold = state->hold; \ 135131377Stjr bits = state->bits; \ 136131377Stjr } while (0) 137131377Stjr 138131377Stjr/* Set state from registers for inflate_fast() */ 139131377Stjr#define RESTORE() \ 140131377Stjr do { \ 141131377Stjr strm->next_out = put; \ 142131377Stjr strm->avail_out = left; \ 143131377Stjr strm->next_in = next; \ 144131377Stjr strm->avail_in = have; \ 145131377Stjr state->hold = hold; \ 146131377Stjr state->bits = bits; \ 147131377Stjr } while (0) 148131377Stjr 149131377Stjr/* Clear the input bit accumulator */ 150131377Stjr#define INITBITS() \ 151131377Stjr do { \ 152131377Stjr hold = 0; \ 153131377Stjr bits = 0; \ 154131377Stjr } while (0) 155131377Stjr 156131377Stjr/* Assure that some input is available. If input is requested, but denied, 157131377Stjr then return a Z_BUF_ERROR from inflateBack(). */ 158131377Stjr#define PULL() \ 159131377Stjr do { \ 160131377Stjr if (have == 0) { \ 161131377Stjr have = in(in_desc, &next); \ 162131377Stjr if (have == 0) { \ 163131377Stjr next = Z_NULL; \ 164131377Stjr ret = Z_BUF_ERROR; \ 165131377Stjr goto inf_leave; \ 166131377Stjr } \ 167131377Stjr } \ 168131377Stjr } while (0) 169131377Stjr 170131377Stjr/* Get a byte of input into the bit accumulator, or return from inflateBack() 171131377Stjr with an error if there is no input available. */ 172131377Stjr#define PULLBYTE() \ 173131377Stjr do { \ 174131377Stjr PULL(); \ 175131377Stjr have--; \ 176131377Stjr hold += (unsigned long)(*next++) << bits; \ 177131377Stjr bits += 8; \ 178131377Stjr } while (0) 179131377Stjr 180131377Stjr/* Assure that there are at least n bits in the bit accumulator. If there is 181131377Stjr not enough available input to do that, then return from inflateBack() with 182131377Stjr an error. */ 183131377Stjr#define NEEDBITS(n) \ 184131377Stjr do { \ 185131377Stjr while (bits < (unsigned)(n)) \ 186131377Stjr PULLBYTE(); \ 187131377Stjr } while (0) 188131377Stjr 189131377Stjr/* Return the low n bits of the bit accumulator (n < 16) */ 190131377Stjr#define BITS(n) \ 191131377Stjr ((unsigned)hold & ((1U << (n)) - 1)) 192131377Stjr 193131377Stjr/* Remove n bits from the bit accumulator */ 194131377Stjr#define DROPBITS(n) \ 195131377Stjr do { \ 196131377Stjr hold >>= (n); \ 197131377Stjr bits -= (unsigned)(n); \ 198131377Stjr } while (0) 199131377Stjr 200131377Stjr/* Remove zero to seven bits as needed to go to a byte boundary */ 201131377Stjr#define BYTEBITS() \ 202131377Stjr do { \ 203131377Stjr hold >>= bits & 7; \ 204131377Stjr bits -= bits & 7; \ 205131377Stjr } while (0) 206131377Stjr 207131377Stjr/* Assure that some output space is available, by writing out the window 208131377Stjr if it's full. If the write fails, return from inflateBack() with a 209131377Stjr Z_BUF_ERROR. */ 210131377Stjr#define ROOM() \ 211131377Stjr do { \ 212131377Stjr if (left == 0) { \ 213131377Stjr put = state->window; \ 214131377Stjr left = state->wsize; \ 215131377Stjr state->whave = left; \ 216131377Stjr if (out(out_desc, put, left)) { \ 217131377Stjr ret = Z_BUF_ERROR; \ 218131377Stjr goto inf_leave; \ 219131377Stjr } \ 220131377Stjr } \ 221131377Stjr } while (0) 222131377Stjr 223131377Stjr/* 224131377Stjr strm provides the memory allocation functions and window buffer on input, 225131377Stjr and provides information on the unused input on return. For Z_DATA_ERROR 226131377Stjr returns, strm will also provide an error message. 227131377Stjr 228131377Stjr in() and out() are the call-back input and output functions. When 229131377Stjr inflateBack() needs more input, it calls in(). When inflateBack() has 230131377Stjr filled the window with output, or when it completes with data in the 231131377Stjr window, it calls out() to write out the data. The application must not 232131377Stjr change the provided input until in() is called again or inflateBack() 233131377Stjr returns. The application must not change the window/output buffer until 234131377Stjr inflateBack() returns. 235131377Stjr 236131377Stjr in() and out() are called with a descriptor parameter provided in the 237131377Stjr inflateBack() call. This parameter can be a structure that provides the 238131377Stjr information required to do the read or write, as well as accumulated 239131377Stjr information on the input and output such as totals and check values. 240131377Stjr 241131377Stjr in() should return zero on failure. out() should return non-zero on 242131377Stjr failure. If either in() or out() fails, than inflateBack() returns a 243131377Stjr Z_BUF_ERROR. strm->next_in can be checked for Z_NULL to see whether it 244131377Stjr was in() or out() that caused in the error. Otherwise, inflateBack() 245131377Stjr returns Z_STREAM_END on success, Z_DATA_ERROR for an deflate format 246131377Stjr error, or Z_MEM_ERROR if it could not allocate memory for the state. 247131377Stjr inflateBack() can also return Z_STREAM_ERROR if the input parameters 248131377Stjr are not correct, i.e. strm is Z_NULL or the state was not initialized. 249131377Stjr */ 250131377Stjrint ZEXPORT inflateBack(strm, in, in_desc, out, out_desc) 251157046Sdesz_streamp strm; 252131377Stjrin_func in; 253131377Stjrvoid FAR *in_desc; 254131377Stjrout_func out; 255131377Stjrvoid FAR *out_desc; 256131377Stjr{ 257131377Stjr struct inflate_state FAR *state; 258250261Sdelphij z_const unsigned char FAR *next; /* next input */ 259131377Stjr unsigned char FAR *put; /* next output */ 260131377Stjr unsigned have, left; /* available input and output */ 261131377Stjr unsigned long hold; /* bit buffer */ 262131377Stjr unsigned bits; /* bits in bit buffer */ 263131377Stjr unsigned copy; /* number of stored or match bytes to copy */ 264131377Stjr unsigned char FAR *from; /* where to copy match bytes from */ 265205471Sdelphij code here; /* current decoding table entry */ 266131377Stjr code last; /* parent table entry */ 267131377Stjr unsigned len; /* length to copy for repeats, bits to drop */ 268131377Stjr int ret; /* return code */ 269131377Stjr static const unsigned short order[19] = /* permutation of code lengths */ 270131377Stjr {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; 271131377Stjr 272131377Stjr /* Check that the strm exists and that the state was initialized */ 273131377Stjr if (strm == Z_NULL || strm->state == Z_NULL) 274131377Stjr return Z_STREAM_ERROR; 275131377Stjr state = (struct inflate_state FAR *)strm->state; 276131377Stjr 277131377Stjr /* Reset the state */ 278131377Stjr strm->msg = Z_NULL; 279131377Stjr state->mode = TYPE; 280131377Stjr state->last = 0; 281131377Stjr state->whave = 0; 282131377Stjr next = strm->next_in; 283131377Stjr have = next != Z_NULL ? strm->avail_in : 0; 284131377Stjr hold = 0; 285131377Stjr bits = 0; 286131377Stjr put = state->window; 287131377Stjr left = state->wsize; 288131377Stjr 289131377Stjr /* Inflate until end of block marked as last */ 290131377Stjr for (;;) 291131377Stjr switch (state->mode) { 292131377Stjr case TYPE: 293131377Stjr /* determine and dispatch block type */ 294131377Stjr if (state->last) { 295131377Stjr BYTEBITS(); 296131377Stjr state->mode = DONE; 297131377Stjr break; 298131377Stjr } 299131377Stjr NEEDBITS(3); 300131377Stjr state->last = BITS(1); 301131377Stjr DROPBITS(1); 302131377Stjr switch (BITS(2)) { 303131377Stjr case 0: /* stored block */ 304131377Stjr Tracev((stderr, "inflate: stored block%s\n", 305131377Stjr state->last ? " (last)" : "")); 306131377Stjr state->mode = STORED; 307131377Stjr break; 308131377Stjr case 1: /* fixed block */ 309131377Stjr fixedtables(state); 310131377Stjr Tracev((stderr, "inflate: fixed codes block%s\n", 311131377Stjr state->last ? " (last)" : "")); 312131377Stjr state->mode = LEN; /* decode codes */ 313131377Stjr break; 314131377Stjr case 2: /* dynamic block */ 315131377Stjr Tracev((stderr, "inflate: dynamic codes block%s\n", 316131377Stjr state->last ? " (last)" : "")); 317131377Stjr state->mode = TABLE; 318131377Stjr break; 319131377Stjr case 3: 320131377Stjr strm->msg = (char *)"invalid block type"; 321131377Stjr state->mode = BAD; 322131377Stjr } 323131377Stjr DROPBITS(2); 324131377Stjr break; 325131377Stjr 326131377Stjr case STORED: 327131377Stjr /* get and verify stored block length */ 328131377Stjr BYTEBITS(); /* go to byte boundary */ 329131377Stjr NEEDBITS(32); 330131377Stjr if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) { 331131377Stjr strm->msg = (char *)"invalid stored block lengths"; 332131377Stjr state->mode = BAD; 333131377Stjr break; 334131377Stjr } 335131377Stjr state->length = (unsigned)hold & 0xffff; 336131377Stjr Tracev((stderr, "inflate: stored length %u\n", 337131377Stjr state->length)); 338131377Stjr INITBITS(); 339131377Stjr 340131377Stjr /* copy stored block from input to output */ 341131377Stjr while (state->length != 0) { 342131377Stjr copy = state->length; 343131377Stjr PULL(); 344131377Stjr ROOM(); 345131377Stjr if (copy > have) copy = have; 346131377Stjr if (copy > left) copy = left; 347131377Stjr zmemcpy(put, next, copy); 348131377Stjr have -= copy; 349131377Stjr next += copy; 350131377Stjr left -= copy; 351131377Stjr put += copy; 352131377Stjr state->length -= copy; 353131377Stjr } 354131377Stjr Tracev((stderr, "inflate: stored end\n")); 355131377Stjr state->mode = TYPE; 356131377Stjr break; 357131377Stjr 358131377Stjr case TABLE: 359131377Stjr /* get dynamic table entries descriptor */ 360131377Stjr NEEDBITS(14); 361131377Stjr state->nlen = BITS(5) + 257; 362131377Stjr DROPBITS(5); 363131377Stjr state->ndist = BITS(5) + 1; 364131377Stjr DROPBITS(5); 365131377Stjr state->ncode = BITS(4) + 4; 366131377Stjr DROPBITS(4); 367131377Stjr#ifndef PKZIP_BUG_WORKAROUND 368131377Stjr if (state->nlen > 286 || state->ndist > 30) { 369131377Stjr strm->msg = (char *)"too many length or distance symbols"; 370131377Stjr state->mode = BAD; 371131377Stjr break; 372131377Stjr } 373131377Stjr#endif 374131377Stjr Tracev((stderr, "inflate: table sizes ok\n")); 375131377Stjr 376131377Stjr /* get code length code lengths (not a typo) */ 377131377Stjr state->have = 0; 378131377Stjr while (state->have < state->ncode) { 379131377Stjr NEEDBITS(3); 380131377Stjr state->lens[order[state->have++]] = (unsigned short)BITS(3); 381131377Stjr DROPBITS(3); 382131377Stjr } 383131377Stjr while (state->have < 19) 384131377Stjr state->lens[order[state->have++]] = 0; 385131377Stjr state->next = state->codes; 386131377Stjr state->lencode = (code const FAR *)(state->next); 387131377Stjr state->lenbits = 7; 388131377Stjr ret = inflate_table(CODES, state->lens, 19, &(state->next), 389131377Stjr &(state->lenbits), state->work); 390131377Stjr if (ret) { 391131377Stjr strm->msg = (char *)"invalid code lengths set"; 392131377Stjr state->mode = BAD; 393131377Stjr break; 394131377Stjr } 395131377Stjr Tracev((stderr, "inflate: code lengths ok\n")); 396131377Stjr 397131377Stjr /* get length and distance code code lengths */ 398131377Stjr state->have = 0; 399131377Stjr while (state->have < state->nlen + state->ndist) { 400131377Stjr for (;;) { 401205471Sdelphij here = state->lencode[BITS(state->lenbits)]; 402205471Sdelphij if ((unsigned)(here.bits) <= bits) break; 403131377Stjr PULLBYTE(); 404131377Stjr } 405205471Sdelphij if (here.val < 16) { 406205471Sdelphij DROPBITS(here.bits); 407205471Sdelphij state->lens[state->have++] = here.val; 408131377Stjr } 409131377Stjr else { 410205471Sdelphij if (here.val == 16) { 411205471Sdelphij NEEDBITS(here.bits + 2); 412205471Sdelphij DROPBITS(here.bits); 413131377Stjr if (state->have == 0) { 414131377Stjr strm->msg = (char *)"invalid bit length repeat"; 415131377Stjr state->mode = BAD; 416131377Stjr break; 417131377Stjr } 418131377Stjr len = (unsigned)(state->lens[state->have - 1]); 419131377Stjr copy = 3 + BITS(2); 420131377Stjr DROPBITS(2); 421131377Stjr } 422205471Sdelphij else if (here.val == 17) { 423205471Sdelphij NEEDBITS(here.bits + 3); 424205471Sdelphij DROPBITS(here.bits); 425131377Stjr len = 0; 426131377Stjr copy = 3 + BITS(3); 427131377Stjr DROPBITS(3); 428131377Stjr } 429131377Stjr else { 430205471Sdelphij NEEDBITS(here.bits + 7); 431205471Sdelphij DROPBITS(here.bits); 432131377Stjr len = 0; 433131377Stjr copy = 11 + BITS(7); 434131377Stjr DROPBITS(7); 435131377Stjr } 436131377Stjr if (state->have + copy > state->nlen + state->ndist) { 437131377Stjr strm->msg = (char *)"invalid bit length repeat"; 438131377Stjr state->mode = BAD; 439131377Stjr break; 440131377Stjr } 441131377Stjr while (copy--) 442131377Stjr state->lens[state->have++] = (unsigned short)len; 443131377Stjr } 444131377Stjr } 445131377Stjr 446146081Skientzle /* handle error breaks in while */ 447146081Skientzle if (state->mode == BAD) break; 448134354Snectar 449205471Sdelphij /* check for end-of-block code (better have one) */ 450205471Sdelphij if (state->lens[256] == 0) { 451205471Sdelphij strm->msg = (char *)"invalid code -- missing end-of-block"; 452205471Sdelphij state->mode = BAD; 453205471Sdelphij break; 454205471Sdelphij } 455205471Sdelphij 456205471Sdelphij /* build code tables -- note: do not change the lenbits or distbits 457205471Sdelphij values here (9 and 6) without reading the comments in inftrees.h 458205471Sdelphij concerning the ENOUGH constants, which depend on those values */ 459131377Stjr state->next = state->codes; 460131377Stjr state->lencode = (code const FAR *)(state->next); 461131377Stjr state->lenbits = 9; 462131377Stjr ret = inflate_table(LENS, state->lens, state->nlen, &(state->next), 463131377Stjr &(state->lenbits), state->work); 464131377Stjr if (ret) { 465131377Stjr strm->msg = (char *)"invalid literal/lengths set"; 466131377Stjr state->mode = BAD; 467131377Stjr break; 468131377Stjr } 469131377Stjr state->distcode = (code const FAR *)(state->next); 470131377Stjr state->distbits = 6; 471131377Stjr ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist, 472131377Stjr &(state->next), &(state->distbits), state->work); 473131377Stjr if (ret) { 474131377Stjr strm->msg = (char *)"invalid distances set"; 475131377Stjr state->mode = BAD; 476131377Stjr break; 477131377Stjr } 478131377Stjr Tracev((stderr, "inflate: codes ok\n")); 479131377Stjr state->mode = LEN; 480131377Stjr 481131377Stjr case LEN: 482131377Stjr /* use inflate_fast() if we have enough input and output */ 483131377Stjr if (have >= 6 && left >= 258) { 484131377Stjr RESTORE(); 485131377Stjr if (state->whave < state->wsize) 486131377Stjr state->whave = state->wsize - left; 487131377Stjr inflate_fast(strm, state->wsize); 488131377Stjr LOAD(); 489131377Stjr break; 490131377Stjr } 491131377Stjr 492131377Stjr /* get a literal, length, or end-of-block code */ 493131377Stjr for (;;) { 494205471Sdelphij here = state->lencode[BITS(state->lenbits)]; 495205471Sdelphij if ((unsigned)(here.bits) <= bits) break; 496131377Stjr PULLBYTE(); 497131377Stjr } 498205471Sdelphij if (here.op && (here.op & 0xf0) == 0) { 499205471Sdelphij last = here; 500131377Stjr for (;;) { 501205471Sdelphij here = state->lencode[last.val + 502131377Stjr (BITS(last.bits + last.op) >> last.bits)]; 503205471Sdelphij if ((unsigned)(last.bits + here.bits) <= bits) break; 504131377Stjr PULLBYTE(); 505131377Stjr } 506131377Stjr DROPBITS(last.bits); 507131377Stjr } 508205471Sdelphij DROPBITS(here.bits); 509205471Sdelphij state->length = (unsigned)here.val; 510131377Stjr 511131377Stjr /* process literal */ 512205471Sdelphij if (here.op == 0) { 513205471Sdelphij Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ? 514131377Stjr "inflate: literal '%c'\n" : 515205471Sdelphij "inflate: literal 0x%02x\n", here.val)); 516131377Stjr ROOM(); 517131377Stjr *put++ = (unsigned char)(state->length); 518131377Stjr left--; 519131377Stjr state->mode = LEN; 520131377Stjr break; 521131377Stjr } 522131377Stjr 523131377Stjr /* process end of block */ 524205471Sdelphij if (here.op & 32) { 525131377Stjr Tracevv((stderr, "inflate: end of block\n")); 526131377Stjr state->mode = TYPE; 527131377Stjr break; 528131377Stjr } 529131377Stjr 530131377Stjr /* invalid code */ 531205471Sdelphij if (here.op & 64) { 532131377Stjr strm->msg = (char *)"invalid literal/length code"; 533131377Stjr state->mode = BAD; 534131377Stjr break; 535131377Stjr } 536131377Stjr 537131377Stjr /* length code -- get extra bits, if any */ 538205471Sdelphij state->extra = (unsigned)(here.op) & 15; 539131377Stjr if (state->extra != 0) { 540131377Stjr NEEDBITS(state->extra); 541131377Stjr state->length += BITS(state->extra); 542131377Stjr DROPBITS(state->extra); 543131377Stjr } 544131377Stjr Tracevv((stderr, "inflate: length %u\n", state->length)); 545131377Stjr 546131377Stjr /* get distance code */ 547131377Stjr for (;;) { 548205471Sdelphij here = state->distcode[BITS(state->distbits)]; 549205471Sdelphij if ((unsigned)(here.bits) <= bits) break; 550131377Stjr PULLBYTE(); 551131377Stjr } 552205471Sdelphij if ((here.op & 0xf0) == 0) { 553205471Sdelphij last = here; 554131377Stjr for (;;) { 555205471Sdelphij here = state->distcode[last.val + 556131377Stjr (BITS(last.bits + last.op) >> last.bits)]; 557205471Sdelphij if ((unsigned)(last.bits + here.bits) <= bits) break; 558131377Stjr PULLBYTE(); 559131377Stjr } 560131377Stjr DROPBITS(last.bits); 561131377Stjr } 562205471Sdelphij DROPBITS(here.bits); 563205471Sdelphij if (here.op & 64) { 564131377Stjr strm->msg = (char *)"invalid distance code"; 565131377Stjr state->mode = BAD; 566131377Stjr break; 567131377Stjr } 568205471Sdelphij state->offset = (unsigned)here.val; 569131377Stjr 570131377Stjr /* get distance extra bits, if any */ 571205471Sdelphij state->extra = (unsigned)(here.op) & 15; 572131377Stjr if (state->extra != 0) { 573131377Stjr NEEDBITS(state->extra); 574131377Stjr state->offset += BITS(state->extra); 575131377Stjr DROPBITS(state->extra); 576131377Stjr } 577131377Stjr if (state->offset > state->wsize - (state->whave < state->wsize ? 578131377Stjr left : 0)) { 579131377Stjr strm->msg = (char *)"invalid distance too far back"; 580131377Stjr state->mode = BAD; 581131377Stjr break; 582131377Stjr } 583131377Stjr Tracevv((stderr, "inflate: distance %u\n", state->offset)); 584131377Stjr 585131377Stjr /* copy match from window to output */ 586131377Stjr do { 587131377Stjr ROOM(); 588131377Stjr copy = state->wsize - state->offset; 589131377Stjr if (copy < left) { 590131377Stjr from = put + copy; 591131377Stjr copy = left - copy; 592131377Stjr } 593131377Stjr else { 594131377Stjr from = put - state->offset; 595131377Stjr copy = left; 596131377Stjr } 597131377Stjr if (copy > state->length) copy = state->length; 598131377Stjr state->length -= copy; 599131377Stjr left -= copy; 600131377Stjr do { 601131377Stjr *put++ = *from++; 602131377Stjr } while (--copy); 603131377Stjr } while (state->length != 0); 604131377Stjr break; 605131377Stjr 606131377Stjr case DONE: 607131377Stjr /* inflate stream terminated properly -- write leftover output */ 608131377Stjr ret = Z_STREAM_END; 609131377Stjr if (left < state->wsize) { 610131377Stjr if (out(out_desc, state->window, state->wsize - left)) 611131377Stjr ret = Z_BUF_ERROR; 612131377Stjr } 613131377Stjr goto inf_leave; 614131377Stjr 615131377Stjr case BAD: 616131377Stjr ret = Z_DATA_ERROR; 617131377Stjr goto inf_leave; 618131377Stjr 619131377Stjr default: /* can't happen, but makes compilers happy */ 620131377Stjr ret = Z_STREAM_ERROR; 621131377Stjr goto inf_leave; 622131377Stjr } 623131377Stjr 624131377Stjr /* Return unused input */ 625131377Stjr inf_leave: 626131377Stjr strm->next_in = next; 627131377Stjr strm->avail_in = have; 628131377Stjr return ret; 629131377Stjr} 630131377Stjr 631131377Stjrint ZEXPORT inflateBackEnd(strm) 632157046Sdesz_streamp strm; 633131377Stjr{ 634131377Stjr if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0) 635131377Stjr return Z_STREAM_ERROR; 636131377Stjr ZFREE(strm, strm->state); 637131377Stjr strm->state = Z_NULL; 638131377Stjr Tracev((stderr, "inflate: end\n")); 639131377Stjr return Z_OK; 640131377Stjr} 641