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