annotate src/zlib-1.2.7/contrib/infback9/infback9.c @ 143:e95e00bdc3eb

Further win32 build updates
author Chris Cannam <cannam@all-day-breakfast.com>
date Mon, 09 Jan 2017 13:51:38 +0000
parents 8a15ff55d9af
children
rev   line source
cannam@89 1 /* infback9.c -- inflate deflate64 data using a call-back interface
cannam@89 2 * Copyright (C) 1995-2008 Mark Adler
cannam@89 3 * For conditions of distribution and use, see copyright notice in zlib.h
cannam@89 4 */
cannam@89 5
cannam@89 6 #include "zutil.h"
cannam@89 7 #include "infback9.h"
cannam@89 8 #include "inftree9.h"
cannam@89 9 #include "inflate9.h"
cannam@89 10
cannam@89 11 #define WSIZE 65536UL
cannam@89 12
cannam@89 13 /*
cannam@89 14 strm provides memory allocation functions in zalloc and zfree, or
cannam@89 15 Z_NULL to use the library memory allocation functions.
cannam@89 16
cannam@89 17 window is a user-supplied window and output buffer that is 64K bytes.
cannam@89 18 */
cannam@89 19 int ZEXPORT inflateBack9Init_(strm, window, version, stream_size)
cannam@89 20 z_stream FAR *strm;
cannam@89 21 unsigned char FAR *window;
cannam@89 22 const char *version;
cannam@89 23 int stream_size;
cannam@89 24 {
cannam@89 25 struct inflate_state FAR *state;
cannam@89 26
cannam@89 27 if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
cannam@89 28 stream_size != (int)(sizeof(z_stream)))
cannam@89 29 return Z_VERSION_ERROR;
cannam@89 30 if (strm == Z_NULL || window == Z_NULL)
cannam@89 31 return Z_STREAM_ERROR;
cannam@89 32 strm->msg = Z_NULL; /* in case we return an error */
cannam@89 33 if (strm->zalloc == (alloc_func)0) {
cannam@89 34 strm->zalloc = zcalloc;
cannam@89 35 strm->opaque = (voidpf)0;
cannam@89 36 }
cannam@89 37 if (strm->zfree == (free_func)0) strm->zfree = zcfree;
cannam@89 38 state = (struct inflate_state FAR *)ZALLOC(strm, 1,
cannam@89 39 sizeof(struct inflate_state));
cannam@89 40 if (state == Z_NULL) return Z_MEM_ERROR;
cannam@89 41 Tracev((stderr, "inflate: allocated\n"));
cannam@89 42 strm->state = (voidpf)state;
cannam@89 43 state->window = window;
cannam@89 44 return Z_OK;
cannam@89 45 }
cannam@89 46
cannam@89 47 /*
cannam@89 48 Build and output length and distance decoding tables for fixed code
cannam@89 49 decoding.
cannam@89 50 */
cannam@89 51 #ifdef MAKEFIXED
cannam@89 52 #include <stdio.h>
cannam@89 53
cannam@89 54 void makefixed9(void)
cannam@89 55 {
cannam@89 56 unsigned sym, bits, low, size;
cannam@89 57 code *next, *lenfix, *distfix;
cannam@89 58 struct inflate_state state;
cannam@89 59 code fixed[544];
cannam@89 60
cannam@89 61 /* literal/length table */
cannam@89 62 sym = 0;
cannam@89 63 while (sym < 144) state.lens[sym++] = 8;
cannam@89 64 while (sym < 256) state.lens[sym++] = 9;
cannam@89 65 while (sym < 280) state.lens[sym++] = 7;
cannam@89 66 while (sym < 288) state.lens[sym++] = 8;
cannam@89 67 next = fixed;
cannam@89 68 lenfix = next;
cannam@89 69 bits = 9;
cannam@89 70 inflate_table9(LENS, state.lens, 288, &(next), &(bits), state.work);
cannam@89 71
cannam@89 72 /* distance table */
cannam@89 73 sym = 0;
cannam@89 74 while (sym < 32) state.lens[sym++] = 5;
cannam@89 75 distfix = next;
cannam@89 76 bits = 5;
cannam@89 77 inflate_table9(DISTS, state.lens, 32, &(next), &(bits), state.work);
cannam@89 78
cannam@89 79 /* write tables */
cannam@89 80 puts(" /* inffix9.h -- table for decoding deflate64 fixed codes");
cannam@89 81 puts(" * Generated automatically by makefixed9().");
cannam@89 82 puts(" */");
cannam@89 83 puts("");
cannam@89 84 puts(" /* WARNING: this file should *not* be used by applications.");
cannam@89 85 puts(" It is part of the implementation of this library and is");
cannam@89 86 puts(" subject to change. Applications should only use zlib.h.");
cannam@89 87 puts(" */");
cannam@89 88 puts("");
cannam@89 89 size = 1U << 9;
cannam@89 90 printf(" static const code lenfix[%u] = {", size);
cannam@89 91 low = 0;
cannam@89 92 for (;;) {
cannam@89 93 if ((low % 6) == 0) printf("\n ");
cannam@89 94 printf("{%u,%u,%d}", lenfix[low].op, lenfix[low].bits,
cannam@89 95 lenfix[low].val);
cannam@89 96 if (++low == size) break;
cannam@89 97 putchar(',');
cannam@89 98 }
cannam@89 99 puts("\n };");
cannam@89 100 size = 1U << 5;
cannam@89 101 printf("\n static const code distfix[%u] = {", size);
cannam@89 102 low = 0;
cannam@89 103 for (;;) {
cannam@89 104 if ((low % 5) == 0) printf("\n ");
cannam@89 105 printf("{%u,%u,%d}", distfix[low].op, distfix[low].bits,
cannam@89 106 distfix[low].val);
cannam@89 107 if (++low == size) break;
cannam@89 108 putchar(',');
cannam@89 109 }
cannam@89 110 puts("\n };");
cannam@89 111 }
cannam@89 112 #endif /* MAKEFIXED */
cannam@89 113
cannam@89 114 /* Macros for inflateBack(): */
cannam@89 115
cannam@89 116 /* Clear the input bit accumulator */
cannam@89 117 #define INITBITS() \
cannam@89 118 do { \
cannam@89 119 hold = 0; \
cannam@89 120 bits = 0; \
cannam@89 121 } while (0)
cannam@89 122
cannam@89 123 /* Assure that some input is available. If input is requested, but denied,
cannam@89 124 then return a Z_BUF_ERROR from inflateBack(). */
cannam@89 125 #define PULL() \
cannam@89 126 do { \
cannam@89 127 if (have == 0) { \
cannam@89 128 have = in(in_desc, &next); \
cannam@89 129 if (have == 0) { \
cannam@89 130 next = Z_NULL; \
cannam@89 131 ret = Z_BUF_ERROR; \
cannam@89 132 goto inf_leave; \
cannam@89 133 } \
cannam@89 134 } \
cannam@89 135 } while (0)
cannam@89 136
cannam@89 137 /* Get a byte of input into the bit accumulator, or return from inflateBack()
cannam@89 138 with an error if there is no input available. */
cannam@89 139 #define PULLBYTE() \
cannam@89 140 do { \
cannam@89 141 PULL(); \
cannam@89 142 have--; \
cannam@89 143 hold += (unsigned long)(*next++) << bits; \
cannam@89 144 bits += 8; \
cannam@89 145 } while (0)
cannam@89 146
cannam@89 147 /* Assure that there are at least n bits in the bit accumulator. If there is
cannam@89 148 not enough available input to do that, then return from inflateBack() with
cannam@89 149 an error. */
cannam@89 150 #define NEEDBITS(n) \
cannam@89 151 do { \
cannam@89 152 while (bits < (unsigned)(n)) \
cannam@89 153 PULLBYTE(); \
cannam@89 154 } while (0)
cannam@89 155
cannam@89 156 /* Return the low n bits of the bit accumulator (n <= 16) */
cannam@89 157 #define BITS(n) \
cannam@89 158 ((unsigned)hold & ((1U << (n)) - 1))
cannam@89 159
cannam@89 160 /* Remove n bits from the bit accumulator */
cannam@89 161 #define DROPBITS(n) \
cannam@89 162 do { \
cannam@89 163 hold >>= (n); \
cannam@89 164 bits -= (unsigned)(n); \
cannam@89 165 } while (0)
cannam@89 166
cannam@89 167 /* Remove zero to seven bits as needed to go to a byte boundary */
cannam@89 168 #define BYTEBITS() \
cannam@89 169 do { \
cannam@89 170 hold >>= bits & 7; \
cannam@89 171 bits -= bits & 7; \
cannam@89 172 } while (0)
cannam@89 173
cannam@89 174 /* Assure that some output space is available, by writing out the window
cannam@89 175 if it's full. If the write fails, return from inflateBack() with a
cannam@89 176 Z_BUF_ERROR. */
cannam@89 177 #define ROOM() \
cannam@89 178 do { \
cannam@89 179 if (left == 0) { \
cannam@89 180 put = window; \
cannam@89 181 left = WSIZE; \
cannam@89 182 wrap = 1; \
cannam@89 183 if (out(out_desc, put, (unsigned)left)) { \
cannam@89 184 ret = Z_BUF_ERROR; \
cannam@89 185 goto inf_leave; \
cannam@89 186 } \
cannam@89 187 } \
cannam@89 188 } while (0)
cannam@89 189
cannam@89 190 /*
cannam@89 191 strm provides the memory allocation functions and window buffer on input,
cannam@89 192 and provides information on the unused input on return. For Z_DATA_ERROR
cannam@89 193 returns, strm will also provide an error message.
cannam@89 194
cannam@89 195 in() and out() are the call-back input and output functions. When
cannam@89 196 inflateBack() needs more input, it calls in(). When inflateBack() has
cannam@89 197 filled the window with output, or when it completes with data in the
cannam@89 198 window, it calls out() to write out the data. The application must not
cannam@89 199 change the provided input until in() is called again or inflateBack()
cannam@89 200 returns. The application must not change the window/output buffer until
cannam@89 201 inflateBack() returns.
cannam@89 202
cannam@89 203 in() and out() are called with a descriptor parameter provided in the
cannam@89 204 inflateBack() call. This parameter can be a structure that provides the
cannam@89 205 information required to do the read or write, as well as accumulated
cannam@89 206 information on the input and output such as totals and check values.
cannam@89 207
cannam@89 208 in() should return zero on failure. out() should return non-zero on
cannam@89 209 failure. If either in() or out() fails, than inflateBack() returns a
cannam@89 210 Z_BUF_ERROR. strm->next_in can be checked for Z_NULL to see whether it
cannam@89 211 was in() or out() that caused in the error. Otherwise, inflateBack()
cannam@89 212 returns Z_STREAM_END on success, Z_DATA_ERROR for an deflate format
cannam@89 213 error, or Z_MEM_ERROR if it could not allocate memory for the state.
cannam@89 214 inflateBack() can also return Z_STREAM_ERROR if the input parameters
cannam@89 215 are not correct, i.e. strm is Z_NULL or the state was not initialized.
cannam@89 216 */
cannam@89 217 int ZEXPORT inflateBack9(strm, in, in_desc, out, out_desc)
cannam@89 218 z_stream FAR *strm;
cannam@89 219 in_func in;
cannam@89 220 void FAR *in_desc;
cannam@89 221 out_func out;
cannam@89 222 void FAR *out_desc;
cannam@89 223 {
cannam@89 224 struct inflate_state FAR *state;
cannam@89 225 unsigned char FAR *next; /* next input */
cannam@89 226 unsigned char FAR *put; /* next output */
cannam@89 227 unsigned have; /* available input */
cannam@89 228 unsigned long left; /* available output */
cannam@89 229 inflate_mode mode; /* current inflate mode */
cannam@89 230 int lastblock; /* true if processing last block */
cannam@89 231 int wrap; /* true if the window has wrapped */
cannam@89 232 unsigned long write; /* window write index */
cannam@89 233 unsigned char FAR *window; /* allocated sliding window, if needed */
cannam@89 234 unsigned long hold; /* bit buffer */
cannam@89 235 unsigned bits; /* bits in bit buffer */
cannam@89 236 unsigned extra; /* extra bits needed */
cannam@89 237 unsigned long length; /* literal or length of data to copy */
cannam@89 238 unsigned long offset; /* distance back to copy string from */
cannam@89 239 unsigned long copy; /* number of stored or match bytes to copy */
cannam@89 240 unsigned char FAR *from; /* where to copy match bytes from */
cannam@89 241 code const FAR *lencode; /* starting table for length/literal codes */
cannam@89 242 code const FAR *distcode; /* starting table for distance codes */
cannam@89 243 unsigned lenbits; /* index bits for lencode */
cannam@89 244 unsigned distbits; /* index bits for distcode */
cannam@89 245 code here; /* current decoding table entry */
cannam@89 246 code last; /* parent table entry */
cannam@89 247 unsigned len; /* length to copy for repeats, bits to drop */
cannam@89 248 int ret; /* return code */
cannam@89 249 static const unsigned short order[19] = /* permutation of code lengths */
cannam@89 250 {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
cannam@89 251 #include "inffix9.h"
cannam@89 252
cannam@89 253 /* Check that the strm exists and that the state was initialized */
cannam@89 254 if (strm == Z_NULL || strm->state == Z_NULL)
cannam@89 255 return Z_STREAM_ERROR;
cannam@89 256 state = (struct inflate_state FAR *)strm->state;
cannam@89 257
cannam@89 258 /* Reset the state */
cannam@89 259 strm->msg = Z_NULL;
cannam@89 260 mode = TYPE;
cannam@89 261 lastblock = 0;
cannam@89 262 write = 0;
cannam@89 263 wrap = 0;
cannam@89 264 window = state->window;
cannam@89 265 next = strm->next_in;
cannam@89 266 have = next != Z_NULL ? strm->avail_in : 0;
cannam@89 267 hold = 0;
cannam@89 268 bits = 0;
cannam@89 269 put = window;
cannam@89 270 left = WSIZE;
cannam@89 271 lencode = Z_NULL;
cannam@89 272 distcode = Z_NULL;
cannam@89 273
cannam@89 274 /* Inflate until end of block marked as last */
cannam@89 275 for (;;)
cannam@89 276 switch (mode) {
cannam@89 277 case TYPE:
cannam@89 278 /* determine and dispatch block type */
cannam@89 279 if (lastblock) {
cannam@89 280 BYTEBITS();
cannam@89 281 mode = DONE;
cannam@89 282 break;
cannam@89 283 }
cannam@89 284 NEEDBITS(3);
cannam@89 285 lastblock = BITS(1);
cannam@89 286 DROPBITS(1);
cannam@89 287 switch (BITS(2)) {
cannam@89 288 case 0: /* stored block */
cannam@89 289 Tracev((stderr, "inflate: stored block%s\n",
cannam@89 290 lastblock ? " (last)" : ""));
cannam@89 291 mode = STORED;
cannam@89 292 break;
cannam@89 293 case 1: /* fixed block */
cannam@89 294 lencode = lenfix;
cannam@89 295 lenbits = 9;
cannam@89 296 distcode = distfix;
cannam@89 297 distbits = 5;
cannam@89 298 Tracev((stderr, "inflate: fixed codes block%s\n",
cannam@89 299 lastblock ? " (last)" : ""));
cannam@89 300 mode = LEN; /* decode codes */
cannam@89 301 break;
cannam@89 302 case 2: /* dynamic block */
cannam@89 303 Tracev((stderr, "inflate: dynamic codes block%s\n",
cannam@89 304 lastblock ? " (last)" : ""));
cannam@89 305 mode = TABLE;
cannam@89 306 break;
cannam@89 307 case 3:
cannam@89 308 strm->msg = (char *)"invalid block type";
cannam@89 309 mode = BAD;
cannam@89 310 }
cannam@89 311 DROPBITS(2);
cannam@89 312 break;
cannam@89 313
cannam@89 314 case STORED:
cannam@89 315 /* get and verify stored block length */
cannam@89 316 BYTEBITS(); /* go to byte boundary */
cannam@89 317 NEEDBITS(32);
cannam@89 318 if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
cannam@89 319 strm->msg = (char *)"invalid stored block lengths";
cannam@89 320 mode = BAD;
cannam@89 321 break;
cannam@89 322 }
cannam@89 323 length = (unsigned)hold & 0xffff;
cannam@89 324 Tracev((stderr, "inflate: stored length %lu\n",
cannam@89 325 length));
cannam@89 326 INITBITS();
cannam@89 327
cannam@89 328 /* copy stored block from input to output */
cannam@89 329 while (length != 0) {
cannam@89 330 copy = length;
cannam@89 331 PULL();
cannam@89 332 ROOM();
cannam@89 333 if (copy > have) copy = have;
cannam@89 334 if (copy > left) copy = left;
cannam@89 335 zmemcpy(put, next, copy);
cannam@89 336 have -= copy;
cannam@89 337 next += copy;
cannam@89 338 left -= copy;
cannam@89 339 put += copy;
cannam@89 340 length -= copy;
cannam@89 341 }
cannam@89 342 Tracev((stderr, "inflate: stored end\n"));
cannam@89 343 mode = TYPE;
cannam@89 344 break;
cannam@89 345
cannam@89 346 case TABLE:
cannam@89 347 /* get dynamic table entries descriptor */
cannam@89 348 NEEDBITS(14);
cannam@89 349 state->nlen = BITS(5) + 257;
cannam@89 350 DROPBITS(5);
cannam@89 351 state->ndist = BITS(5) + 1;
cannam@89 352 DROPBITS(5);
cannam@89 353 state->ncode = BITS(4) + 4;
cannam@89 354 DROPBITS(4);
cannam@89 355 if (state->nlen > 286) {
cannam@89 356 strm->msg = (char *)"too many length symbols";
cannam@89 357 mode = BAD;
cannam@89 358 break;
cannam@89 359 }
cannam@89 360 Tracev((stderr, "inflate: table sizes ok\n"));
cannam@89 361
cannam@89 362 /* get code length code lengths (not a typo) */
cannam@89 363 state->have = 0;
cannam@89 364 while (state->have < state->ncode) {
cannam@89 365 NEEDBITS(3);
cannam@89 366 state->lens[order[state->have++]] = (unsigned short)BITS(3);
cannam@89 367 DROPBITS(3);
cannam@89 368 }
cannam@89 369 while (state->have < 19)
cannam@89 370 state->lens[order[state->have++]] = 0;
cannam@89 371 state->next = state->codes;
cannam@89 372 lencode = (code const FAR *)(state->next);
cannam@89 373 lenbits = 7;
cannam@89 374 ret = inflate_table9(CODES, state->lens, 19, &(state->next),
cannam@89 375 &(lenbits), state->work);
cannam@89 376 if (ret) {
cannam@89 377 strm->msg = (char *)"invalid code lengths set";
cannam@89 378 mode = BAD;
cannam@89 379 break;
cannam@89 380 }
cannam@89 381 Tracev((stderr, "inflate: code lengths ok\n"));
cannam@89 382
cannam@89 383 /* get length and distance code code lengths */
cannam@89 384 state->have = 0;
cannam@89 385 while (state->have < state->nlen + state->ndist) {
cannam@89 386 for (;;) {
cannam@89 387 here = lencode[BITS(lenbits)];
cannam@89 388 if ((unsigned)(here.bits) <= bits) break;
cannam@89 389 PULLBYTE();
cannam@89 390 }
cannam@89 391 if (here.val < 16) {
cannam@89 392 NEEDBITS(here.bits);
cannam@89 393 DROPBITS(here.bits);
cannam@89 394 state->lens[state->have++] = here.val;
cannam@89 395 }
cannam@89 396 else {
cannam@89 397 if (here.val == 16) {
cannam@89 398 NEEDBITS(here.bits + 2);
cannam@89 399 DROPBITS(here.bits);
cannam@89 400 if (state->have == 0) {
cannam@89 401 strm->msg = (char *)"invalid bit length repeat";
cannam@89 402 mode = BAD;
cannam@89 403 break;
cannam@89 404 }
cannam@89 405 len = (unsigned)(state->lens[state->have - 1]);
cannam@89 406 copy = 3 + BITS(2);
cannam@89 407 DROPBITS(2);
cannam@89 408 }
cannam@89 409 else if (here.val == 17) {
cannam@89 410 NEEDBITS(here.bits + 3);
cannam@89 411 DROPBITS(here.bits);
cannam@89 412 len = 0;
cannam@89 413 copy = 3 + BITS(3);
cannam@89 414 DROPBITS(3);
cannam@89 415 }
cannam@89 416 else {
cannam@89 417 NEEDBITS(here.bits + 7);
cannam@89 418 DROPBITS(here.bits);
cannam@89 419 len = 0;
cannam@89 420 copy = 11 + BITS(7);
cannam@89 421 DROPBITS(7);
cannam@89 422 }
cannam@89 423 if (state->have + copy > state->nlen + state->ndist) {
cannam@89 424 strm->msg = (char *)"invalid bit length repeat";
cannam@89 425 mode = BAD;
cannam@89 426 break;
cannam@89 427 }
cannam@89 428 while (copy--)
cannam@89 429 state->lens[state->have++] = (unsigned short)len;
cannam@89 430 }
cannam@89 431 }
cannam@89 432
cannam@89 433 /* handle error breaks in while */
cannam@89 434 if (mode == BAD) break;
cannam@89 435
cannam@89 436 /* check for end-of-block code (better have one) */
cannam@89 437 if (state->lens[256] == 0) {
cannam@89 438 strm->msg = (char *)"invalid code -- missing end-of-block";
cannam@89 439 mode = BAD;
cannam@89 440 break;
cannam@89 441 }
cannam@89 442
cannam@89 443 /* build code tables -- note: do not change the lenbits or distbits
cannam@89 444 values here (9 and 6) without reading the comments in inftree9.h
cannam@89 445 concerning the ENOUGH constants, which depend on those values */
cannam@89 446 state->next = state->codes;
cannam@89 447 lencode = (code const FAR *)(state->next);
cannam@89 448 lenbits = 9;
cannam@89 449 ret = inflate_table9(LENS, state->lens, state->nlen,
cannam@89 450 &(state->next), &(lenbits), state->work);
cannam@89 451 if (ret) {
cannam@89 452 strm->msg = (char *)"invalid literal/lengths set";
cannam@89 453 mode = BAD;
cannam@89 454 break;
cannam@89 455 }
cannam@89 456 distcode = (code const FAR *)(state->next);
cannam@89 457 distbits = 6;
cannam@89 458 ret = inflate_table9(DISTS, state->lens + state->nlen,
cannam@89 459 state->ndist, &(state->next), &(distbits),
cannam@89 460 state->work);
cannam@89 461 if (ret) {
cannam@89 462 strm->msg = (char *)"invalid distances set";
cannam@89 463 mode = BAD;
cannam@89 464 break;
cannam@89 465 }
cannam@89 466 Tracev((stderr, "inflate: codes ok\n"));
cannam@89 467 mode = LEN;
cannam@89 468
cannam@89 469 case LEN:
cannam@89 470 /* get a literal, length, or end-of-block code */
cannam@89 471 for (;;) {
cannam@89 472 here = lencode[BITS(lenbits)];
cannam@89 473 if ((unsigned)(here.bits) <= bits) break;
cannam@89 474 PULLBYTE();
cannam@89 475 }
cannam@89 476 if (here.op && (here.op & 0xf0) == 0) {
cannam@89 477 last = here;
cannam@89 478 for (;;) {
cannam@89 479 here = lencode[last.val +
cannam@89 480 (BITS(last.bits + last.op) >> last.bits)];
cannam@89 481 if ((unsigned)(last.bits + here.bits) <= bits) break;
cannam@89 482 PULLBYTE();
cannam@89 483 }
cannam@89 484 DROPBITS(last.bits);
cannam@89 485 }
cannam@89 486 DROPBITS(here.bits);
cannam@89 487 length = (unsigned)here.val;
cannam@89 488
cannam@89 489 /* process literal */
cannam@89 490 if (here.op == 0) {
cannam@89 491 Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
cannam@89 492 "inflate: literal '%c'\n" :
cannam@89 493 "inflate: literal 0x%02x\n", here.val));
cannam@89 494 ROOM();
cannam@89 495 *put++ = (unsigned char)(length);
cannam@89 496 left--;
cannam@89 497 mode = LEN;
cannam@89 498 break;
cannam@89 499 }
cannam@89 500
cannam@89 501 /* process end of block */
cannam@89 502 if (here.op & 32) {
cannam@89 503 Tracevv((stderr, "inflate: end of block\n"));
cannam@89 504 mode = TYPE;
cannam@89 505 break;
cannam@89 506 }
cannam@89 507
cannam@89 508 /* invalid code */
cannam@89 509 if (here.op & 64) {
cannam@89 510 strm->msg = (char *)"invalid literal/length code";
cannam@89 511 mode = BAD;
cannam@89 512 break;
cannam@89 513 }
cannam@89 514
cannam@89 515 /* length code -- get extra bits, if any */
cannam@89 516 extra = (unsigned)(here.op) & 31;
cannam@89 517 if (extra != 0) {
cannam@89 518 NEEDBITS(extra);
cannam@89 519 length += BITS(extra);
cannam@89 520 DROPBITS(extra);
cannam@89 521 }
cannam@89 522 Tracevv((stderr, "inflate: length %lu\n", length));
cannam@89 523
cannam@89 524 /* get distance code */
cannam@89 525 for (;;) {
cannam@89 526 here = distcode[BITS(distbits)];
cannam@89 527 if ((unsigned)(here.bits) <= bits) break;
cannam@89 528 PULLBYTE();
cannam@89 529 }
cannam@89 530 if ((here.op & 0xf0) == 0) {
cannam@89 531 last = here;
cannam@89 532 for (;;) {
cannam@89 533 here = distcode[last.val +
cannam@89 534 (BITS(last.bits + last.op) >> last.bits)];
cannam@89 535 if ((unsigned)(last.bits + here.bits) <= bits) break;
cannam@89 536 PULLBYTE();
cannam@89 537 }
cannam@89 538 DROPBITS(last.bits);
cannam@89 539 }
cannam@89 540 DROPBITS(here.bits);
cannam@89 541 if (here.op & 64) {
cannam@89 542 strm->msg = (char *)"invalid distance code";
cannam@89 543 mode = BAD;
cannam@89 544 break;
cannam@89 545 }
cannam@89 546 offset = (unsigned)here.val;
cannam@89 547
cannam@89 548 /* get distance extra bits, if any */
cannam@89 549 extra = (unsigned)(here.op) & 15;
cannam@89 550 if (extra != 0) {
cannam@89 551 NEEDBITS(extra);
cannam@89 552 offset += BITS(extra);
cannam@89 553 DROPBITS(extra);
cannam@89 554 }
cannam@89 555 if (offset > WSIZE - (wrap ? 0: left)) {
cannam@89 556 strm->msg = (char *)"invalid distance too far back";
cannam@89 557 mode = BAD;
cannam@89 558 break;
cannam@89 559 }
cannam@89 560 Tracevv((stderr, "inflate: distance %lu\n", offset));
cannam@89 561
cannam@89 562 /* copy match from window to output */
cannam@89 563 do {
cannam@89 564 ROOM();
cannam@89 565 copy = WSIZE - offset;
cannam@89 566 if (copy < left) {
cannam@89 567 from = put + copy;
cannam@89 568 copy = left - copy;
cannam@89 569 }
cannam@89 570 else {
cannam@89 571 from = put - offset;
cannam@89 572 copy = left;
cannam@89 573 }
cannam@89 574 if (copy > length) copy = length;
cannam@89 575 length -= copy;
cannam@89 576 left -= copy;
cannam@89 577 do {
cannam@89 578 *put++ = *from++;
cannam@89 579 } while (--copy);
cannam@89 580 } while (length != 0);
cannam@89 581 break;
cannam@89 582
cannam@89 583 case DONE:
cannam@89 584 /* inflate stream terminated properly -- write leftover output */
cannam@89 585 ret = Z_STREAM_END;
cannam@89 586 if (left < WSIZE) {
cannam@89 587 if (out(out_desc, window, (unsigned)(WSIZE - left)))
cannam@89 588 ret = Z_BUF_ERROR;
cannam@89 589 }
cannam@89 590 goto inf_leave;
cannam@89 591
cannam@89 592 case BAD:
cannam@89 593 ret = Z_DATA_ERROR;
cannam@89 594 goto inf_leave;
cannam@89 595
cannam@89 596 default: /* can't happen, but makes compilers happy */
cannam@89 597 ret = Z_STREAM_ERROR;
cannam@89 598 goto inf_leave;
cannam@89 599 }
cannam@89 600
cannam@89 601 /* Return unused input */
cannam@89 602 inf_leave:
cannam@89 603 strm->next_in = next;
cannam@89 604 strm->avail_in = have;
cannam@89 605 return ret;
cannam@89 606 }
cannam@89 607
cannam@89 608 int ZEXPORT inflateBack9End(strm)
cannam@89 609 z_stream FAR *strm;
cannam@89 610 {
cannam@89 611 if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
cannam@89 612 return Z_STREAM_ERROR;
cannam@89 613 ZFREE(strm, strm->state);
cannam@89 614 strm->state = Z_NULL;
cannam@89 615 Tracev((stderr, "inflate: end\n"));
cannam@89 616 return Z_OK;
cannam@89 617 }