Fossil SCM

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

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button