lemon-project-template-glpk

annotate deps/glpk/src/zlib/infback.c @ 11:4fc6ad2fb8a6

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