annotate src/zlib-1.2.8/contrib/infback9/infback9.c @ 84:08ae793730bd

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