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 | /* inflate.c -- zlib decompression |
michael@0 | 2 | * Copyright (C) 1995-2012 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 | * Change history: |
michael@0 | 8 | * |
michael@0 | 9 | * 1.2.beta0 24 Nov 2002 |
michael@0 | 10 | * - First version -- complete rewrite of inflate to simplify code, avoid |
michael@0 | 11 | * creation of window when not needed, minimize use of window when it is |
michael@0 | 12 | * needed, make inffast.c even faster, implement gzip decoding, and to |
michael@0 | 13 | * improve code readability and style over the previous zlib inflate code |
michael@0 | 14 | * |
michael@0 | 15 | * 1.2.beta1 25 Nov 2002 |
michael@0 | 16 | * - Use pointers for available input and output checking in inffast.c |
michael@0 | 17 | * - Remove input and output counters in inffast.c |
michael@0 | 18 | * - Change inffast.c entry and loop from avail_in >= 7 to >= 6 |
michael@0 | 19 | * - Remove unnecessary second byte pull from length extra in inffast.c |
michael@0 | 20 | * - Unroll direct copy to three copies per loop in inffast.c |
michael@0 | 21 | * |
michael@0 | 22 | * 1.2.beta2 4 Dec 2002 |
michael@0 | 23 | * - Change external routine names to reduce potential conflicts |
michael@0 | 24 | * - Correct filename to inffixed.h for fixed tables in inflate.c |
michael@0 | 25 | * - Make hbuf[] unsigned char to match parameter type in inflate.c |
michael@0 | 26 | * - Change strm->next_out[-state->offset] to *(strm->next_out - state->offset) |
michael@0 | 27 | * to avoid negation problem on Alphas (64 bit) in inflate.c |
michael@0 | 28 | * |
michael@0 | 29 | * 1.2.beta3 22 Dec 2002 |
michael@0 | 30 | * - Add comments on state->bits assertion in inffast.c |
michael@0 | 31 | * - Add comments on op field in inftrees.h |
michael@0 | 32 | * - Fix bug in reuse of allocated window after inflateReset() |
michael@0 | 33 | * - Remove bit fields--back to byte structure for speed |
michael@0 | 34 | * - Remove distance extra == 0 check in inflate_fast()--only helps for lengths |
michael@0 | 35 | * - Change post-increments to pre-increments in inflate_fast(), PPC biased? |
michael@0 | 36 | * - Add compile time option, POSTINC, to use post-increments instead (Intel?) |
michael@0 | 37 | * - Make MATCH copy in inflate() much faster for when inflate_fast() not used |
michael@0 | 38 | * - Use local copies of stream next and avail values, as well as local bit |
michael@0 | 39 | * buffer and bit count in inflate()--for speed when inflate_fast() not used |
michael@0 | 40 | * |
michael@0 | 41 | * 1.2.beta4 1 Jan 2003 |
michael@0 | 42 | * - Split ptr - 257 statements in inflate_table() to avoid compiler warnings |
michael@0 | 43 | * - Move a comment on output buffer sizes from inffast.c to inflate.c |
michael@0 | 44 | * - Add comments in inffast.c to introduce the inflate_fast() routine |
michael@0 | 45 | * - Rearrange window copies in inflate_fast() for speed and simplification |
michael@0 | 46 | * - Unroll last copy for window match in inflate_fast() |
michael@0 | 47 | * - Use local copies of window variables in inflate_fast() for speed |
michael@0 | 48 | * - Pull out common wnext == 0 case for speed in inflate_fast() |
michael@0 | 49 | * - Make op and len in inflate_fast() unsigned for consistency |
michael@0 | 50 | * - Add FAR to lcode and dcode declarations in inflate_fast() |
michael@0 | 51 | * - Simplified bad distance check in inflate_fast() |
michael@0 | 52 | * - Added inflateBackInit(), inflateBack(), and inflateBackEnd() in new |
michael@0 | 53 | * source file infback.c to provide a call-back interface to inflate for |
michael@0 | 54 | * programs like gzip and unzip -- uses window as output buffer to avoid |
michael@0 | 55 | * window copying |
michael@0 | 56 | * |
michael@0 | 57 | * 1.2.beta5 1 Jan 2003 |
michael@0 | 58 | * - Improved inflateBack() interface to allow the caller to provide initial |
michael@0 | 59 | * input in strm. |
michael@0 | 60 | * - Fixed stored blocks bug in inflateBack() |
michael@0 | 61 | * |
michael@0 | 62 | * 1.2.beta6 4 Jan 2003 |
michael@0 | 63 | * - Added comments in inffast.c on effectiveness of POSTINC |
michael@0 | 64 | * - Typecasting all around to reduce compiler warnings |
michael@0 | 65 | * - Changed loops from while (1) or do {} while (1) to for (;;), again to |
michael@0 | 66 | * make compilers happy |
michael@0 | 67 | * - Changed type of window in inflateBackInit() to unsigned char * |
michael@0 | 68 | * |
michael@0 | 69 | * 1.2.beta7 27 Jan 2003 |
michael@0 | 70 | * - Changed many types to unsigned or unsigned short to avoid warnings |
michael@0 | 71 | * - Added inflateCopy() function |
michael@0 | 72 | * |
michael@0 | 73 | * 1.2.0 9 Mar 2003 |
michael@0 | 74 | * - Changed inflateBack() interface to provide separate opaque descriptors |
michael@0 | 75 | * for the in() and out() functions |
michael@0 | 76 | * - Changed inflateBack() argument and in_func typedef to swap the length |
michael@0 | 77 | * and buffer address return values for the input function |
michael@0 | 78 | * - Check next_in and next_out for Z_NULL on entry to inflate() |
michael@0 | 79 | * |
michael@0 | 80 | * The history for versions after 1.2.0 are in ChangeLog in zlib distribution. |
michael@0 | 81 | */ |
michael@0 | 82 | |
michael@0 | 83 | #include "zutil.h" |
michael@0 | 84 | #include "inftrees.h" |
michael@0 | 85 | #include "inflate.h" |
michael@0 | 86 | #include "inffast.h" |
michael@0 | 87 | |
michael@0 | 88 | #ifdef MAKEFIXED |
michael@0 | 89 | # ifndef BUILDFIXED |
michael@0 | 90 | # define BUILDFIXED |
michael@0 | 91 | # endif |
michael@0 | 92 | #endif |
michael@0 | 93 | |
michael@0 | 94 | /* function prototypes */ |
michael@0 | 95 | local void fixedtables OF((struct inflate_state FAR *state)); |
michael@0 | 96 | local int updatewindow OF((z_streamp strm, const unsigned char FAR *end, |
michael@0 | 97 | unsigned copy)); |
michael@0 | 98 | #ifdef BUILDFIXED |
michael@0 | 99 | void makefixed OF((void)); |
michael@0 | 100 | #endif |
michael@0 | 101 | local unsigned syncsearch OF((unsigned FAR *have, const unsigned char FAR *buf, |
michael@0 | 102 | unsigned len)); |
michael@0 | 103 | |
michael@0 | 104 | int ZEXPORT inflateResetKeep(strm) |
michael@0 | 105 | z_streamp strm; |
michael@0 | 106 | { |
michael@0 | 107 | struct inflate_state FAR *state; |
michael@0 | 108 | |
michael@0 | 109 | if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; |
michael@0 | 110 | state = (struct inflate_state FAR *)strm->state; |
michael@0 | 111 | strm->total_in = strm->total_out = state->total = 0; |
michael@0 | 112 | strm->msg = Z_NULL; |
michael@0 | 113 | if (state->wrap) /* to support ill-conceived Java test suite */ |
michael@0 | 114 | strm->adler = state->wrap & 1; |
michael@0 | 115 | state->mode = HEAD; |
michael@0 | 116 | state->last = 0; |
michael@0 | 117 | state->havedict = 0; |
michael@0 | 118 | state->dmax = 32768U; |
michael@0 | 119 | state->head = Z_NULL; |
michael@0 | 120 | state->hold = 0; |
michael@0 | 121 | state->bits = 0; |
michael@0 | 122 | state->lencode = state->distcode = state->next = state->codes; |
michael@0 | 123 | state->sane = 1; |
michael@0 | 124 | state->back = -1; |
michael@0 | 125 | Tracev((stderr, "inflate: reset\n")); |
michael@0 | 126 | return Z_OK; |
michael@0 | 127 | } |
michael@0 | 128 | |
michael@0 | 129 | int ZEXPORT inflateReset(strm) |
michael@0 | 130 | z_streamp strm; |
michael@0 | 131 | { |
michael@0 | 132 | struct inflate_state FAR *state; |
michael@0 | 133 | |
michael@0 | 134 | if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; |
michael@0 | 135 | state = (struct inflate_state FAR *)strm->state; |
michael@0 | 136 | state->wsize = 0; |
michael@0 | 137 | state->whave = 0; |
michael@0 | 138 | state->wnext = 0; |
michael@0 | 139 | return inflateResetKeep(strm); |
michael@0 | 140 | } |
michael@0 | 141 | |
michael@0 | 142 | int ZEXPORT inflateReset2(strm, windowBits) |
michael@0 | 143 | z_streamp strm; |
michael@0 | 144 | int windowBits; |
michael@0 | 145 | { |
michael@0 | 146 | int wrap; |
michael@0 | 147 | struct inflate_state FAR *state; |
michael@0 | 148 | |
michael@0 | 149 | /* get the state */ |
michael@0 | 150 | if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; |
michael@0 | 151 | state = (struct inflate_state FAR *)strm->state; |
michael@0 | 152 | |
michael@0 | 153 | /* extract wrap request from windowBits parameter */ |
michael@0 | 154 | if (windowBits < 0) { |
michael@0 | 155 | wrap = 0; |
michael@0 | 156 | windowBits = -windowBits; |
michael@0 | 157 | } |
michael@0 | 158 | else { |
michael@0 | 159 | wrap = (windowBits >> 4) + 1; |
michael@0 | 160 | #ifdef GUNZIP |
michael@0 | 161 | if (windowBits < 48) |
michael@0 | 162 | windowBits &= 15; |
michael@0 | 163 | #endif |
michael@0 | 164 | } |
michael@0 | 165 | |
michael@0 | 166 | /* set number of window bits, free window if different */ |
michael@0 | 167 | if (windowBits && (windowBits < 8 || windowBits > 15)) |
michael@0 | 168 | return Z_STREAM_ERROR; |
michael@0 | 169 | if (state->window != Z_NULL && state->wbits != (unsigned)windowBits) { |
michael@0 | 170 | ZFREE(strm, state->window); |
michael@0 | 171 | state->window = Z_NULL; |
michael@0 | 172 | } |
michael@0 | 173 | |
michael@0 | 174 | /* update state and reset the rest of it */ |
michael@0 | 175 | state->wrap = wrap; |
michael@0 | 176 | state->wbits = (unsigned)windowBits; |
michael@0 | 177 | return inflateReset(strm); |
michael@0 | 178 | } |
michael@0 | 179 | |
michael@0 | 180 | int ZEXPORT inflateInit2_(strm, windowBits, version, stream_size) |
michael@0 | 181 | z_streamp strm; |
michael@0 | 182 | int windowBits; |
michael@0 | 183 | const char *version; |
michael@0 | 184 | int stream_size; |
michael@0 | 185 | { |
michael@0 | 186 | int ret; |
michael@0 | 187 | struct inflate_state FAR *state; |
michael@0 | 188 | |
michael@0 | 189 | if (version == Z_NULL || version[0] != ZLIB_VERSION[0] || |
michael@0 | 190 | stream_size != (int)(sizeof(z_stream))) |
michael@0 | 191 | return Z_VERSION_ERROR; |
michael@0 | 192 | if (strm == Z_NULL) return Z_STREAM_ERROR; |
michael@0 | 193 | strm->msg = Z_NULL; /* in case we return an error */ |
michael@0 | 194 | if (strm->zalloc == (alloc_func)0) { |
michael@0 | 195 | #ifdef Z_SOLO |
michael@0 | 196 | return Z_STREAM_ERROR; |
michael@0 | 197 | #else |
michael@0 | 198 | strm->zalloc = zcalloc; |
michael@0 | 199 | strm->opaque = (voidpf)0; |
michael@0 | 200 | #endif |
michael@0 | 201 | } |
michael@0 | 202 | if (strm->zfree == (free_func)0) |
michael@0 | 203 | #ifdef Z_SOLO |
michael@0 | 204 | return Z_STREAM_ERROR; |
michael@0 | 205 | #else |
michael@0 | 206 | strm->zfree = zcfree; |
michael@0 | 207 | #endif |
michael@0 | 208 | state = (struct inflate_state FAR *) |
michael@0 | 209 | ZALLOC(strm, 1, sizeof(struct inflate_state)); |
michael@0 | 210 | if (state == Z_NULL) return Z_MEM_ERROR; |
michael@0 | 211 | Tracev((stderr, "inflate: allocated\n")); |
michael@0 | 212 | strm->state = (struct internal_state FAR *)state; |
michael@0 | 213 | state->window = Z_NULL; |
michael@0 | 214 | ret = inflateReset2(strm, windowBits); |
michael@0 | 215 | if (ret != Z_OK) { |
michael@0 | 216 | ZFREE(strm, state); |
michael@0 | 217 | strm->state = Z_NULL; |
michael@0 | 218 | } |
michael@0 | 219 | return ret; |
michael@0 | 220 | } |
michael@0 | 221 | |
michael@0 | 222 | int ZEXPORT inflateInit_(strm, version, stream_size) |
michael@0 | 223 | z_streamp strm; |
michael@0 | 224 | const char *version; |
michael@0 | 225 | int stream_size; |
michael@0 | 226 | { |
michael@0 | 227 | return inflateInit2_(strm, DEF_WBITS, version, stream_size); |
michael@0 | 228 | } |
michael@0 | 229 | |
michael@0 | 230 | int ZEXPORT inflatePrime(strm, bits, value) |
michael@0 | 231 | z_streamp strm; |
michael@0 | 232 | int bits; |
michael@0 | 233 | int value; |
michael@0 | 234 | { |
michael@0 | 235 | struct inflate_state FAR *state; |
michael@0 | 236 | |
michael@0 | 237 | if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; |
michael@0 | 238 | state = (struct inflate_state FAR *)strm->state; |
michael@0 | 239 | if (bits < 0) { |
michael@0 | 240 | state->hold = 0; |
michael@0 | 241 | state->bits = 0; |
michael@0 | 242 | return Z_OK; |
michael@0 | 243 | } |
michael@0 | 244 | if (bits > 16 || state->bits + bits > 32) return Z_STREAM_ERROR; |
michael@0 | 245 | value &= (1L << bits) - 1; |
michael@0 | 246 | state->hold += value << state->bits; |
michael@0 | 247 | state->bits += bits; |
michael@0 | 248 | return Z_OK; |
michael@0 | 249 | } |
michael@0 | 250 | |
michael@0 | 251 | /* |
michael@0 | 252 | Return state with length and distance decoding tables and index sizes set to |
michael@0 | 253 | fixed code decoding. Normally this returns fixed tables from inffixed.h. |
michael@0 | 254 | If BUILDFIXED is defined, then instead this routine builds the tables the |
michael@0 | 255 | first time it's called, and returns those tables the first time and |
michael@0 | 256 | thereafter. This reduces the size of the code by about 2K bytes, in |
michael@0 | 257 | exchange for a little execution time. However, BUILDFIXED should not be |
michael@0 | 258 | used for threaded applications, since the rewriting of the tables and virgin |
michael@0 | 259 | may not be thread-safe. |
michael@0 | 260 | */ |
michael@0 | 261 | local void fixedtables(state) |
michael@0 | 262 | struct inflate_state FAR *state; |
michael@0 | 263 | { |
michael@0 | 264 | #ifdef BUILDFIXED |
michael@0 | 265 | static int virgin = 1; |
michael@0 | 266 | static code *lenfix, *distfix; |
michael@0 | 267 | static code fixed[544]; |
michael@0 | 268 | |
michael@0 | 269 | /* build fixed huffman tables if first call (may not be thread safe) */ |
michael@0 | 270 | if (virgin) { |
michael@0 | 271 | unsigned sym, bits; |
michael@0 | 272 | static code *next; |
michael@0 | 273 | |
michael@0 | 274 | /* literal/length table */ |
michael@0 | 275 | sym = 0; |
michael@0 | 276 | while (sym < 144) state->lens[sym++] = 8; |
michael@0 | 277 | while (sym < 256) state->lens[sym++] = 9; |
michael@0 | 278 | while (sym < 280) state->lens[sym++] = 7; |
michael@0 | 279 | while (sym < 288) state->lens[sym++] = 8; |
michael@0 | 280 | next = fixed; |
michael@0 | 281 | lenfix = next; |
michael@0 | 282 | bits = 9; |
michael@0 | 283 | inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work); |
michael@0 | 284 | |
michael@0 | 285 | /* distance table */ |
michael@0 | 286 | sym = 0; |
michael@0 | 287 | while (sym < 32) state->lens[sym++] = 5; |
michael@0 | 288 | distfix = next; |
michael@0 | 289 | bits = 5; |
michael@0 | 290 | inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work); |
michael@0 | 291 | |
michael@0 | 292 | /* do this just once */ |
michael@0 | 293 | virgin = 0; |
michael@0 | 294 | } |
michael@0 | 295 | #else /* !BUILDFIXED */ |
michael@0 | 296 | # include "inffixed.h" |
michael@0 | 297 | #endif /* BUILDFIXED */ |
michael@0 | 298 | state->lencode = lenfix; |
michael@0 | 299 | state->lenbits = 9; |
michael@0 | 300 | state->distcode = distfix; |
michael@0 | 301 | state->distbits = 5; |
michael@0 | 302 | } |
michael@0 | 303 | |
michael@0 | 304 | #ifdef MAKEFIXED |
michael@0 | 305 | #include <stdio.h> |
michael@0 | 306 | |
michael@0 | 307 | /* |
michael@0 | 308 | Write out the inffixed.h that is #include'd above. Defining MAKEFIXED also |
michael@0 | 309 | defines BUILDFIXED, so the tables are built on the fly. makefixed() writes |
michael@0 | 310 | those tables to stdout, which would be piped to inffixed.h. A small program |
michael@0 | 311 | can simply call makefixed to do this: |
michael@0 | 312 | |
michael@0 | 313 | void makefixed(void); |
michael@0 | 314 | |
michael@0 | 315 | int main(void) |
michael@0 | 316 | { |
michael@0 | 317 | makefixed(); |
michael@0 | 318 | return 0; |
michael@0 | 319 | } |
michael@0 | 320 | |
michael@0 | 321 | Then that can be linked with zlib built with MAKEFIXED defined and run: |
michael@0 | 322 | |
michael@0 | 323 | a.out > inffixed.h |
michael@0 | 324 | */ |
michael@0 | 325 | void makefixed() |
michael@0 | 326 | { |
michael@0 | 327 | unsigned low, size; |
michael@0 | 328 | struct inflate_state state; |
michael@0 | 329 | |
michael@0 | 330 | fixedtables(&state); |
michael@0 | 331 | puts(" /* inffixed.h -- table for decoding fixed codes"); |
michael@0 | 332 | puts(" * Generated automatically by makefixed()."); |
michael@0 | 333 | puts(" */"); |
michael@0 | 334 | puts(""); |
michael@0 | 335 | puts(" /* WARNING: this file should *not* be used by applications."); |
michael@0 | 336 | puts(" It is part of the implementation of this library and is"); |
michael@0 | 337 | puts(" subject to change. Applications should only use zlib.h."); |
michael@0 | 338 | puts(" */"); |
michael@0 | 339 | puts(""); |
michael@0 | 340 | size = 1U << 9; |
michael@0 | 341 | printf(" static const code lenfix[%u] = {", size); |
michael@0 | 342 | low = 0; |
michael@0 | 343 | for (;;) { |
michael@0 | 344 | if ((low % 7) == 0) printf("\n "); |
michael@0 | 345 | printf("{%u,%u,%d}", (low & 127) == 99 ? 64 : state.lencode[low].op, |
michael@0 | 346 | state.lencode[low].bits, state.lencode[low].val); |
michael@0 | 347 | if (++low == size) break; |
michael@0 | 348 | putchar(','); |
michael@0 | 349 | } |
michael@0 | 350 | puts("\n };"); |
michael@0 | 351 | size = 1U << 5; |
michael@0 | 352 | printf("\n static const code distfix[%u] = {", size); |
michael@0 | 353 | low = 0; |
michael@0 | 354 | for (;;) { |
michael@0 | 355 | if ((low % 6) == 0) printf("\n "); |
michael@0 | 356 | printf("{%u,%u,%d}", state.distcode[low].op, state.distcode[low].bits, |
michael@0 | 357 | state.distcode[low].val); |
michael@0 | 358 | if (++low == size) break; |
michael@0 | 359 | putchar(','); |
michael@0 | 360 | } |
michael@0 | 361 | puts("\n };"); |
michael@0 | 362 | } |
michael@0 | 363 | #endif /* MAKEFIXED */ |
michael@0 | 364 | |
michael@0 | 365 | /* |
michael@0 | 366 | Update the window with the last wsize (normally 32K) bytes written before |
michael@0 | 367 | returning. If window does not exist yet, create it. This is only called |
michael@0 | 368 | when a window is already in use, or when output has been written during this |
michael@0 | 369 | inflate call, but the end of the deflate stream has not been reached yet. |
michael@0 | 370 | It is also called to create a window for dictionary data when a dictionary |
michael@0 | 371 | is loaded. |
michael@0 | 372 | |
michael@0 | 373 | Providing output buffers larger than 32K to inflate() should provide a speed |
michael@0 | 374 | advantage, since only the last 32K of output is copied to the sliding window |
michael@0 | 375 | upon return from inflate(), and since all distances after the first 32K of |
michael@0 | 376 | output will fall in the output data, making match copies simpler and faster. |
michael@0 | 377 | The advantage may be dependent on the size of the processor's data caches. |
michael@0 | 378 | */ |
michael@0 | 379 | local int updatewindow(strm, end, copy) |
michael@0 | 380 | z_streamp strm; |
michael@0 | 381 | const Bytef *end; |
michael@0 | 382 | unsigned copy; |
michael@0 | 383 | { |
michael@0 | 384 | struct inflate_state FAR *state; |
michael@0 | 385 | unsigned dist; |
michael@0 | 386 | |
michael@0 | 387 | state = (struct inflate_state FAR *)strm->state; |
michael@0 | 388 | |
michael@0 | 389 | /* if it hasn't been done already, allocate space for the window */ |
michael@0 | 390 | if (state->window == Z_NULL) { |
michael@0 | 391 | state->window = (unsigned char FAR *) |
michael@0 | 392 | ZALLOC(strm, 1U << state->wbits, |
michael@0 | 393 | sizeof(unsigned char)); |
michael@0 | 394 | if (state->window == Z_NULL) return 1; |
michael@0 | 395 | } |
michael@0 | 396 | |
michael@0 | 397 | /* if window not in use yet, initialize */ |
michael@0 | 398 | if (state->wsize == 0) { |
michael@0 | 399 | state->wsize = 1U << state->wbits; |
michael@0 | 400 | state->wnext = 0; |
michael@0 | 401 | state->whave = 0; |
michael@0 | 402 | } |
michael@0 | 403 | |
michael@0 | 404 | /* copy state->wsize or less output bytes into the circular window */ |
michael@0 | 405 | if (copy >= state->wsize) { |
michael@0 | 406 | zmemcpy(state->window, end - state->wsize, state->wsize); |
michael@0 | 407 | state->wnext = 0; |
michael@0 | 408 | state->whave = state->wsize; |
michael@0 | 409 | } |
michael@0 | 410 | else { |
michael@0 | 411 | dist = state->wsize - state->wnext; |
michael@0 | 412 | if (dist > copy) dist = copy; |
michael@0 | 413 | zmemcpy(state->window + state->wnext, end - copy, dist); |
michael@0 | 414 | copy -= dist; |
michael@0 | 415 | if (copy) { |
michael@0 | 416 | zmemcpy(state->window, end - copy, copy); |
michael@0 | 417 | state->wnext = copy; |
michael@0 | 418 | state->whave = state->wsize; |
michael@0 | 419 | } |
michael@0 | 420 | else { |
michael@0 | 421 | state->wnext += dist; |
michael@0 | 422 | if (state->wnext == state->wsize) state->wnext = 0; |
michael@0 | 423 | if (state->whave < state->wsize) state->whave += dist; |
michael@0 | 424 | } |
michael@0 | 425 | } |
michael@0 | 426 | return 0; |
michael@0 | 427 | } |
michael@0 | 428 | |
michael@0 | 429 | /* Macros for inflate(): */ |
michael@0 | 430 | |
michael@0 | 431 | /* check function to use adler32() for zlib or crc32() for gzip */ |
michael@0 | 432 | #ifdef GUNZIP |
michael@0 | 433 | # define UPDATE(check, buf, len) \ |
michael@0 | 434 | (state->flags ? crc32(check, buf, len) : adler32(check, buf, len)) |
michael@0 | 435 | #else |
michael@0 | 436 | # define UPDATE(check, buf, len) adler32(check, buf, len) |
michael@0 | 437 | #endif |
michael@0 | 438 | |
michael@0 | 439 | /* check macros for header crc */ |
michael@0 | 440 | #ifdef GUNZIP |
michael@0 | 441 | # define CRC2(check, word) \ |
michael@0 | 442 | do { \ |
michael@0 | 443 | hbuf[0] = (unsigned char)(word); \ |
michael@0 | 444 | hbuf[1] = (unsigned char)((word) >> 8); \ |
michael@0 | 445 | check = crc32(check, hbuf, 2); \ |
michael@0 | 446 | } while (0) |
michael@0 | 447 | |
michael@0 | 448 | # define CRC4(check, word) \ |
michael@0 | 449 | do { \ |
michael@0 | 450 | hbuf[0] = (unsigned char)(word); \ |
michael@0 | 451 | hbuf[1] = (unsigned char)((word) >> 8); \ |
michael@0 | 452 | hbuf[2] = (unsigned char)((word) >> 16); \ |
michael@0 | 453 | hbuf[3] = (unsigned char)((word) >> 24); \ |
michael@0 | 454 | check = crc32(check, hbuf, 4); \ |
michael@0 | 455 | } while (0) |
michael@0 | 456 | #endif |
michael@0 | 457 | |
michael@0 | 458 | /* Load registers with state in inflate() for speed */ |
michael@0 | 459 | #define LOAD() \ |
michael@0 | 460 | do { \ |
michael@0 | 461 | put = strm->next_out; \ |
michael@0 | 462 | left = strm->avail_out; \ |
michael@0 | 463 | next = strm->next_in; \ |
michael@0 | 464 | have = strm->avail_in; \ |
michael@0 | 465 | hold = state->hold; \ |
michael@0 | 466 | bits = state->bits; \ |
michael@0 | 467 | } while (0) |
michael@0 | 468 | |
michael@0 | 469 | /* Restore state from registers in inflate() */ |
michael@0 | 470 | #define RESTORE() \ |
michael@0 | 471 | do { \ |
michael@0 | 472 | strm->next_out = put; \ |
michael@0 | 473 | strm->avail_out = left; \ |
michael@0 | 474 | strm->next_in = next; \ |
michael@0 | 475 | strm->avail_in = have; \ |
michael@0 | 476 | state->hold = hold; \ |
michael@0 | 477 | state->bits = bits; \ |
michael@0 | 478 | } while (0) |
michael@0 | 479 | |
michael@0 | 480 | /* Clear the input bit accumulator */ |
michael@0 | 481 | #define INITBITS() \ |
michael@0 | 482 | do { \ |
michael@0 | 483 | hold = 0; \ |
michael@0 | 484 | bits = 0; \ |
michael@0 | 485 | } while (0) |
michael@0 | 486 | |
michael@0 | 487 | /* Get a byte of input into the bit accumulator, or return from inflate() |
michael@0 | 488 | if there is no input available. */ |
michael@0 | 489 | #define PULLBYTE() \ |
michael@0 | 490 | do { \ |
michael@0 | 491 | if (have == 0) goto inf_leave; \ |
michael@0 | 492 | have--; \ |
michael@0 | 493 | hold += (unsigned long)(*next++) << bits; \ |
michael@0 | 494 | bits += 8; \ |
michael@0 | 495 | } while (0) |
michael@0 | 496 | |
michael@0 | 497 | /* Assure that there are at least n bits in the bit accumulator. If there is |
michael@0 | 498 | not enough available input to do that, then return from inflate(). */ |
michael@0 | 499 | #define NEEDBITS(n) \ |
michael@0 | 500 | do { \ |
michael@0 | 501 | while (bits < (unsigned)(n)) \ |
michael@0 | 502 | PULLBYTE(); \ |
michael@0 | 503 | } while (0) |
michael@0 | 504 | |
michael@0 | 505 | /* Return the low n bits of the bit accumulator (n < 16) */ |
michael@0 | 506 | #define BITS(n) \ |
michael@0 | 507 | ((unsigned)hold & ((1U << (n)) - 1)) |
michael@0 | 508 | |
michael@0 | 509 | /* Remove n bits from the bit accumulator */ |
michael@0 | 510 | #define DROPBITS(n) \ |
michael@0 | 511 | do { \ |
michael@0 | 512 | hold >>= (n); \ |
michael@0 | 513 | bits -= (unsigned)(n); \ |
michael@0 | 514 | } while (0) |
michael@0 | 515 | |
michael@0 | 516 | /* Remove zero to seven bits as needed to go to a byte boundary */ |
michael@0 | 517 | #define BYTEBITS() \ |
michael@0 | 518 | do { \ |
michael@0 | 519 | hold >>= bits & 7; \ |
michael@0 | 520 | bits -= bits & 7; \ |
michael@0 | 521 | } while (0) |
michael@0 | 522 | |
michael@0 | 523 | /* |
michael@0 | 524 | inflate() uses a state machine to process as much input data and generate as |
michael@0 | 525 | much output data as possible before returning. The state machine is |
michael@0 | 526 | structured roughly as follows: |
michael@0 | 527 | |
michael@0 | 528 | for (;;) switch (state) { |
michael@0 | 529 | ... |
michael@0 | 530 | case STATEn: |
michael@0 | 531 | if (not enough input data or output space to make progress) |
michael@0 | 532 | return; |
michael@0 | 533 | ... make progress ... |
michael@0 | 534 | state = STATEm; |
michael@0 | 535 | break; |
michael@0 | 536 | ... |
michael@0 | 537 | } |
michael@0 | 538 | |
michael@0 | 539 | so when inflate() is called again, the same case is attempted again, and |
michael@0 | 540 | if the appropriate resources are provided, the machine proceeds to the |
michael@0 | 541 | next state. The NEEDBITS() macro is usually the way the state evaluates |
michael@0 | 542 | whether it can proceed or should return. NEEDBITS() does the return if |
michael@0 | 543 | the requested bits are not available. The typical use of the BITS macros |
michael@0 | 544 | is: |
michael@0 | 545 | |
michael@0 | 546 | NEEDBITS(n); |
michael@0 | 547 | ... do something with BITS(n) ... |
michael@0 | 548 | DROPBITS(n); |
michael@0 | 549 | |
michael@0 | 550 | where NEEDBITS(n) either returns from inflate() if there isn't enough |
michael@0 | 551 | input left to load n bits into the accumulator, or it continues. BITS(n) |
michael@0 | 552 | gives the low n bits in the accumulator. When done, DROPBITS(n) drops |
michael@0 | 553 | the low n bits off the accumulator. INITBITS() clears the accumulator |
michael@0 | 554 | and sets the number of available bits to zero. BYTEBITS() discards just |
michael@0 | 555 | enough bits to put the accumulator on a byte boundary. After BYTEBITS() |
michael@0 | 556 | and a NEEDBITS(8), then BITS(8) would return the next byte in the stream. |
michael@0 | 557 | |
michael@0 | 558 | NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return |
michael@0 | 559 | if there is no input available. The decoding of variable length codes uses |
michael@0 | 560 | PULLBYTE() directly in order to pull just enough bytes to decode the next |
michael@0 | 561 | code, and no more. |
michael@0 | 562 | |
michael@0 | 563 | Some states loop until they get enough input, making sure that enough |
michael@0 | 564 | state information is maintained to continue the loop where it left off |
michael@0 | 565 | if NEEDBITS() returns in the loop. For example, want, need, and keep |
michael@0 | 566 | would all have to actually be part of the saved state in case NEEDBITS() |
michael@0 | 567 | returns: |
michael@0 | 568 | |
michael@0 | 569 | case STATEw: |
michael@0 | 570 | while (want < need) { |
michael@0 | 571 | NEEDBITS(n); |
michael@0 | 572 | keep[want++] = BITS(n); |
michael@0 | 573 | DROPBITS(n); |
michael@0 | 574 | } |
michael@0 | 575 | state = STATEx; |
michael@0 | 576 | case STATEx: |
michael@0 | 577 | |
michael@0 | 578 | As shown above, if the next state is also the next case, then the break |
michael@0 | 579 | is omitted. |
michael@0 | 580 | |
michael@0 | 581 | A state may also return if there is not enough output space available to |
michael@0 | 582 | complete that state. Those states are copying stored data, writing a |
michael@0 | 583 | literal byte, and copying a matching string. |
michael@0 | 584 | |
michael@0 | 585 | When returning, a "goto inf_leave" is used to update the total counters, |
michael@0 | 586 | update the check value, and determine whether any progress has been made |
michael@0 | 587 | during that inflate() call in order to return the proper return code. |
michael@0 | 588 | Progress is defined as a change in either strm->avail_in or strm->avail_out. |
michael@0 | 589 | When there is a window, goto inf_leave will update the window with the last |
michael@0 | 590 | output written. If a goto inf_leave occurs in the middle of decompression |
michael@0 | 591 | and there is no window currently, goto inf_leave will create one and copy |
michael@0 | 592 | output to the window for the next call of inflate(). |
michael@0 | 593 | |
michael@0 | 594 | In this implementation, the flush parameter of inflate() only affects the |
michael@0 | 595 | return code (per zlib.h). inflate() always writes as much as possible to |
michael@0 | 596 | strm->next_out, given the space available and the provided input--the effect |
michael@0 | 597 | documented in zlib.h of Z_SYNC_FLUSH. Furthermore, inflate() always defers |
michael@0 | 598 | the allocation of and copying into a sliding window until necessary, which |
michael@0 | 599 | provides the effect documented in zlib.h for Z_FINISH when the entire input |
michael@0 | 600 | stream available. So the only thing the flush parameter actually does is: |
michael@0 | 601 | when flush is set to Z_FINISH, inflate() cannot return Z_OK. Instead it |
michael@0 | 602 | will return Z_BUF_ERROR if it has not reached the end of the stream. |
michael@0 | 603 | */ |
michael@0 | 604 | |
michael@0 | 605 | int ZEXPORT inflate(strm, flush) |
michael@0 | 606 | z_streamp strm; |
michael@0 | 607 | int flush; |
michael@0 | 608 | { |
michael@0 | 609 | struct inflate_state FAR *state; |
michael@0 | 610 | z_const unsigned char FAR *next; /* next input */ |
michael@0 | 611 | unsigned char FAR *put; /* next output */ |
michael@0 | 612 | unsigned have, left; /* available input and output */ |
michael@0 | 613 | unsigned long hold; /* bit buffer */ |
michael@0 | 614 | unsigned bits; /* bits in bit buffer */ |
michael@0 | 615 | unsigned in, out; /* save starting available input and output */ |
michael@0 | 616 | unsigned copy; /* number of stored or match bytes to copy */ |
michael@0 | 617 | unsigned char FAR *from; /* where to copy match bytes from */ |
michael@0 | 618 | code here; /* current decoding table entry */ |
michael@0 | 619 | code last; /* parent table entry */ |
michael@0 | 620 | unsigned len; /* length to copy for repeats, bits to drop */ |
michael@0 | 621 | int ret; /* return code */ |
michael@0 | 622 | #ifdef GUNZIP |
michael@0 | 623 | unsigned char hbuf[4]; /* buffer for gzip header crc calculation */ |
michael@0 | 624 | #endif |
michael@0 | 625 | static const unsigned short order[19] = /* permutation of code lengths */ |
michael@0 | 626 | {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; |
michael@0 | 627 | |
michael@0 | 628 | if (strm == Z_NULL || strm->state == Z_NULL || strm->next_out == Z_NULL || |
michael@0 | 629 | (strm->next_in == Z_NULL && strm->avail_in != 0)) |
michael@0 | 630 | return Z_STREAM_ERROR; |
michael@0 | 631 | |
michael@0 | 632 | state = (struct inflate_state FAR *)strm->state; |
michael@0 | 633 | if (state->mode == TYPE) state->mode = TYPEDO; /* skip check */ |
michael@0 | 634 | LOAD(); |
michael@0 | 635 | in = have; |
michael@0 | 636 | out = left; |
michael@0 | 637 | ret = Z_OK; |
michael@0 | 638 | for (;;) |
michael@0 | 639 | switch (state->mode) { |
michael@0 | 640 | case HEAD: |
michael@0 | 641 | if (state->wrap == 0) { |
michael@0 | 642 | state->mode = TYPEDO; |
michael@0 | 643 | break; |
michael@0 | 644 | } |
michael@0 | 645 | NEEDBITS(16); |
michael@0 | 646 | #ifdef GUNZIP |
michael@0 | 647 | if ((state->wrap & 2) && hold == 0x8b1f) { /* gzip header */ |
michael@0 | 648 | state->check = crc32(0L, Z_NULL, 0); |
michael@0 | 649 | CRC2(state->check, hold); |
michael@0 | 650 | INITBITS(); |
michael@0 | 651 | state->mode = FLAGS; |
michael@0 | 652 | break; |
michael@0 | 653 | } |
michael@0 | 654 | state->flags = 0; /* expect zlib header */ |
michael@0 | 655 | if (state->head != Z_NULL) |
michael@0 | 656 | state->head->done = -1; |
michael@0 | 657 | if (!(state->wrap & 1) || /* check if zlib header allowed */ |
michael@0 | 658 | #else |
michael@0 | 659 | if ( |
michael@0 | 660 | #endif |
michael@0 | 661 | ((BITS(8) << 8) + (hold >> 8)) % 31) { |
michael@0 | 662 | strm->msg = (char *)"incorrect header check"; |
michael@0 | 663 | state->mode = BAD; |
michael@0 | 664 | break; |
michael@0 | 665 | } |
michael@0 | 666 | if (BITS(4) != Z_DEFLATED) { |
michael@0 | 667 | strm->msg = (char *)"unknown compression method"; |
michael@0 | 668 | state->mode = BAD; |
michael@0 | 669 | break; |
michael@0 | 670 | } |
michael@0 | 671 | DROPBITS(4); |
michael@0 | 672 | len = BITS(4) + 8; |
michael@0 | 673 | if (state->wbits == 0) |
michael@0 | 674 | state->wbits = len; |
michael@0 | 675 | else if (len > state->wbits) { |
michael@0 | 676 | strm->msg = (char *)"invalid window size"; |
michael@0 | 677 | state->mode = BAD; |
michael@0 | 678 | break; |
michael@0 | 679 | } |
michael@0 | 680 | state->dmax = 1U << len; |
michael@0 | 681 | Tracev((stderr, "inflate: zlib header ok\n")); |
michael@0 | 682 | strm->adler = state->check = adler32(0L, Z_NULL, 0); |
michael@0 | 683 | state->mode = hold & 0x200 ? DICTID : TYPE; |
michael@0 | 684 | INITBITS(); |
michael@0 | 685 | break; |
michael@0 | 686 | #ifdef GUNZIP |
michael@0 | 687 | case FLAGS: |
michael@0 | 688 | NEEDBITS(16); |
michael@0 | 689 | state->flags = (int)(hold); |
michael@0 | 690 | if ((state->flags & 0xff) != Z_DEFLATED) { |
michael@0 | 691 | strm->msg = (char *)"unknown compression method"; |
michael@0 | 692 | state->mode = BAD; |
michael@0 | 693 | break; |
michael@0 | 694 | } |
michael@0 | 695 | if (state->flags & 0xe000) { |
michael@0 | 696 | strm->msg = (char *)"unknown header flags set"; |
michael@0 | 697 | state->mode = BAD; |
michael@0 | 698 | break; |
michael@0 | 699 | } |
michael@0 | 700 | if (state->head != Z_NULL) |
michael@0 | 701 | state->head->text = (int)((hold >> 8) & 1); |
michael@0 | 702 | if (state->flags & 0x0200) CRC2(state->check, hold); |
michael@0 | 703 | INITBITS(); |
michael@0 | 704 | state->mode = TIME; |
michael@0 | 705 | case TIME: |
michael@0 | 706 | NEEDBITS(32); |
michael@0 | 707 | if (state->head != Z_NULL) |
michael@0 | 708 | state->head->time = hold; |
michael@0 | 709 | if (state->flags & 0x0200) CRC4(state->check, hold); |
michael@0 | 710 | INITBITS(); |
michael@0 | 711 | state->mode = OS; |
michael@0 | 712 | case OS: |
michael@0 | 713 | NEEDBITS(16); |
michael@0 | 714 | if (state->head != Z_NULL) { |
michael@0 | 715 | state->head->xflags = (int)(hold & 0xff); |
michael@0 | 716 | state->head->os = (int)(hold >> 8); |
michael@0 | 717 | } |
michael@0 | 718 | if (state->flags & 0x0200) CRC2(state->check, hold); |
michael@0 | 719 | INITBITS(); |
michael@0 | 720 | state->mode = EXLEN; |
michael@0 | 721 | case EXLEN: |
michael@0 | 722 | if (state->flags & 0x0400) { |
michael@0 | 723 | NEEDBITS(16); |
michael@0 | 724 | state->length = (unsigned)(hold); |
michael@0 | 725 | if (state->head != Z_NULL) |
michael@0 | 726 | state->head->extra_len = (unsigned)hold; |
michael@0 | 727 | if (state->flags & 0x0200) CRC2(state->check, hold); |
michael@0 | 728 | INITBITS(); |
michael@0 | 729 | } |
michael@0 | 730 | else if (state->head != Z_NULL) |
michael@0 | 731 | state->head->extra = Z_NULL; |
michael@0 | 732 | state->mode = EXTRA; |
michael@0 | 733 | case EXTRA: |
michael@0 | 734 | if (state->flags & 0x0400) { |
michael@0 | 735 | copy = state->length; |
michael@0 | 736 | if (copy > have) copy = have; |
michael@0 | 737 | if (copy) { |
michael@0 | 738 | if (state->head != Z_NULL && |
michael@0 | 739 | state->head->extra != Z_NULL) { |
michael@0 | 740 | len = state->head->extra_len - state->length; |
michael@0 | 741 | zmemcpy(state->head->extra + len, next, |
michael@0 | 742 | len + copy > state->head->extra_max ? |
michael@0 | 743 | state->head->extra_max - len : copy); |
michael@0 | 744 | } |
michael@0 | 745 | if (state->flags & 0x0200) |
michael@0 | 746 | state->check = crc32(state->check, next, copy); |
michael@0 | 747 | have -= copy; |
michael@0 | 748 | next += copy; |
michael@0 | 749 | state->length -= copy; |
michael@0 | 750 | } |
michael@0 | 751 | if (state->length) goto inf_leave; |
michael@0 | 752 | } |
michael@0 | 753 | state->length = 0; |
michael@0 | 754 | state->mode = NAME; |
michael@0 | 755 | case NAME: |
michael@0 | 756 | if (state->flags & 0x0800) { |
michael@0 | 757 | if (have == 0) goto inf_leave; |
michael@0 | 758 | copy = 0; |
michael@0 | 759 | do { |
michael@0 | 760 | len = (unsigned)(next[copy++]); |
michael@0 | 761 | if (state->head != Z_NULL && |
michael@0 | 762 | state->head->name != Z_NULL && |
michael@0 | 763 | state->length < state->head->name_max) |
michael@0 | 764 | state->head->name[state->length++] = len; |
michael@0 | 765 | } while (len && copy < have); |
michael@0 | 766 | if (state->flags & 0x0200) |
michael@0 | 767 | state->check = crc32(state->check, next, copy); |
michael@0 | 768 | have -= copy; |
michael@0 | 769 | next += copy; |
michael@0 | 770 | if (len) goto inf_leave; |
michael@0 | 771 | } |
michael@0 | 772 | else if (state->head != Z_NULL) |
michael@0 | 773 | state->head->name = Z_NULL; |
michael@0 | 774 | state->length = 0; |
michael@0 | 775 | state->mode = COMMENT; |
michael@0 | 776 | case COMMENT: |
michael@0 | 777 | if (state->flags & 0x1000) { |
michael@0 | 778 | if (have == 0) goto inf_leave; |
michael@0 | 779 | copy = 0; |
michael@0 | 780 | do { |
michael@0 | 781 | len = (unsigned)(next[copy++]); |
michael@0 | 782 | if (state->head != Z_NULL && |
michael@0 | 783 | state->head->comment != Z_NULL && |
michael@0 | 784 | state->length < state->head->comm_max) |
michael@0 | 785 | state->head->comment[state->length++] = len; |
michael@0 | 786 | } while (len && copy < have); |
michael@0 | 787 | if (state->flags & 0x0200) |
michael@0 | 788 | state->check = crc32(state->check, next, copy); |
michael@0 | 789 | have -= copy; |
michael@0 | 790 | next += copy; |
michael@0 | 791 | if (len) goto inf_leave; |
michael@0 | 792 | } |
michael@0 | 793 | else if (state->head != Z_NULL) |
michael@0 | 794 | state->head->comment = Z_NULL; |
michael@0 | 795 | state->mode = HCRC; |
michael@0 | 796 | case HCRC: |
michael@0 | 797 | if (state->flags & 0x0200) { |
michael@0 | 798 | NEEDBITS(16); |
michael@0 | 799 | if (hold != (state->check & 0xffff)) { |
michael@0 | 800 | strm->msg = (char *)"header crc mismatch"; |
michael@0 | 801 | state->mode = BAD; |
michael@0 | 802 | break; |
michael@0 | 803 | } |
michael@0 | 804 | INITBITS(); |
michael@0 | 805 | } |
michael@0 | 806 | if (state->head != Z_NULL) { |
michael@0 | 807 | state->head->hcrc = (int)((state->flags >> 9) & 1); |
michael@0 | 808 | state->head->done = 1; |
michael@0 | 809 | } |
michael@0 | 810 | strm->adler = state->check = crc32(0L, Z_NULL, 0); |
michael@0 | 811 | state->mode = TYPE; |
michael@0 | 812 | break; |
michael@0 | 813 | #endif |
michael@0 | 814 | case DICTID: |
michael@0 | 815 | NEEDBITS(32); |
michael@0 | 816 | strm->adler = state->check = ZSWAP32(hold); |
michael@0 | 817 | INITBITS(); |
michael@0 | 818 | state->mode = DICT; |
michael@0 | 819 | case DICT: |
michael@0 | 820 | if (state->havedict == 0) { |
michael@0 | 821 | RESTORE(); |
michael@0 | 822 | return Z_NEED_DICT; |
michael@0 | 823 | } |
michael@0 | 824 | strm->adler = state->check = adler32(0L, Z_NULL, 0); |
michael@0 | 825 | state->mode = TYPE; |
michael@0 | 826 | case TYPE: |
michael@0 | 827 | if (flush == Z_BLOCK || flush == Z_TREES) goto inf_leave; |
michael@0 | 828 | case TYPEDO: |
michael@0 | 829 | if (state->last) { |
michael@0 | 830 | BYTEBITS(); |
michael@0 | 831 | state->mode = CHECK; |
michael@0 | 832 | break; |
michael@0 | 833 | } |
michael@0 | 834 | NEEDBITS(3); |
michael@0 | 835 | state->last = BITS(1); |
michael@0 | 836 | DROPBITS(1); |
michael@0 | 837 | switch (BITS(2)) { |
michael@0 | 838 | case 0: /* stored block */ |
michael@0 | 839 | Tracev((stderr, "inflate: stored block%s\n", |
michael@0 | 840 | state->last ? " (last)" : "")); |
michael@0 | 841 | state->mode = STORED; |
michael@0 | 842 | break; |
michael@0 | 843 | case 1: /* fixed block */ |
michael@0 | 844 | fixedtables(state); |
michael@0 | 845 | Tracev((stderr, "inflate: fixed codes block%s\n", |
michael@0 | 846 | state->last ? " (last)" : "")); |
michael@0 | 847 | state->mode = LEN_; /* decode codes */ |
michael@0 | 848 | if (flush == Z_TREES) { |
michael@0 | 849 | DROPBITS(2); |
michael@0 | 850 | goto inf_leave; |
michael@0 | 851 | } |
michael@0 | 852 | break; |
michael@0 | 853 | case 2: /* dynamic block */ |
michael@0 | 854 | Tracev((stderr, "inflate: dynamic codes block%s\n", |
michael@0 | 855 | state->last ? " (last)" : "")); |
michael@0 | 856 | state->mode = TABLE; |
michael@0 | 857 | break; |
michael@0 | 858 | case 3: |
michael@0 | 859 | strm->msg = (char *)"invalid block type"; |
michael@0 | 860 | state->mode = BAD; |
michael@0 | 861 | } |
michael@0 | 862 | DROPBITS(2); |
michael@0 | 863 | break; |
michael@0 | 864 | case STORED: |
michael@0 | 865 | BYTEBITS(); /* go to byte boundary */ |
michael@0 | 866 | NEEDBITS(32); |
michael@0 | 867 | if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) { |
michael@0 | 868 | strm->msg = (char *)"invalid stored block lengths"; |
michael@0 | 869 | state->mode = BAD; |
michael@0 | 870 | break; |
michael@0 | 871 | } |
michael@0 | 872 | state->length = (unsigned)hold & 0xffff; |
michael@0 | 873 | Tracev((stderr, "inflate: stored length %u\n", |
michael@0 | 874 | state->length)); |
michael@0 | 875 | INITBITS(); |
michael@0 | 876 | state->mode = COPY_; |
michael@0 | 877 | if (flush == Z_TREES) goto inf_leave; |
michael@0 | 878 | case COPY_: |
michael@0 | 879 | state->mode = COPY; |
michael@0 | 880 | case COPY: |
michael@0 | 881 | copy = state->length; |
michael@0 | 882 | if (copy) { |
michael@0 | 883 | if (copy > have) copy = have; |
michael@0 | 884 | if (copy > left) copy = left; |
michael@0 | 885 | if (copy == 0) goto inf_leave; |
michael@0 | 886 | zmemcpy(put, next, copy); |
michael@0 | 887 | have -= copy; |
michael@0 | 888 | next += copy; |
michael@0 | 889 | left -= copy; |
michael@0 | 890 | put += copy; |
michael@0 | 891 | state->length -= copy; |
michael@0 | 892 | break; |
michael@0 | 893 | } |
michael@0 | 894 | Tracev((stderr, "inflate: stored end\n")); |
michael@0 | 895 | state->mode = TYPE; |
michael@0 | 896 | break; |
michael@0 | 897 | case TABLE: |
michael@0 | 898 | NEEDBITS(14); |
michael@0 | 899 | state->nlen = BITS(5) + 257; |
michael@0 | 900 | DROPBITS(5); |
michael@0 | 901 | state->ndist = BITS(5) + 1; |
michael@0 | 902 | DROPBITS(5); |
michael@0 | 903 | state->ncode = BITS(4) + 4; |
michael@0 | 904 | DROPBITS(4); |
michael@0 | 905 | #ifndef PKZIP_BUG_WORKAROUND |
michael@0 | 906 | if (state->nlen > 286 || state->ndist > 30) { |
michael@0 | 907 | strm->msg = (char *)"too many length or distance symbols"; |
michael@0 | 908 | state->mode = BAD; |
michael@0 | 909 | break; |
michael@0 | 910 | } |
michael@0 | 911 | #endif |
michael@0 | 912 | Tracev((stderr, "inflate: table sizes ok\n")); |
michael@0 | 913 | state->have = 0; |
michael@0 | 914 | state->mode = LENLENS; |
michael@0 | 915 | case LENLENS: |
michael@0 | 916 | while (state->have < state->ncode) { |
michael@0 | 917 | NEEDBITS(3); |
michael@0 | 918 | state->lens[order[state->have++]] = (unsigned short)BITS(3); |
michael@0 | 919 | DROPBITS(3); |
michael@0 | 920 | } |
michael@0 | 921 | while (state->have < 19) |
michael@0 | 922 | state->lens[order[state->have++]] = 0; |
michael@0 | 923 | state->next = state->codes; |
michael@0 | 924 | state->lencode = (const code FAR *)(state->next); |
michael@0 | 925 | state->lenbits = 7; |
michael@0 | 926 | ret = inflate_table(CODES, state->lens, 19, &(state->next), |
michael@0 | 927 | &(state->lenbits), state->work); |
michael@0 | 928 | if (ret) { |
michael@0 | 929 | strm->msg = (char *)"invalid code lengths set"; |
michael@0 | 930 | state->mode = BAD; |
michael@0 | 931 | break; |
michael@0 | 932 | } |
michael@0 | 933 | Tracev((stderr, "inflate: code lengths ok\n")); |
michael@0 | 934 | state->have = 0; |
michael@0 | 935 | state->mode = CODELENS; |
michael@0 | 936 | case CODELENS: |
michael@0 | 937 | while (state->have < state->nlen + state->ndist) { |
michael@0 | 938 | for (;;) { |
michael@0 | 939 | here = state->lencode[BITS(state->lenbits)]; |
michael@0 | 940 | if ((unsigned)(here.bits) <= bits) break; |
michael@0 | 941 | PULLBYTE(); |
michael@0 | 942 | } |
michael@0 | 943 | if (here.val < 16) { |
michael@0 | 944 | DROPBITS(here.bits); |
michael@0 | 945 | state->lens[state->have++] = here.val; |
michael@0 | 946 | } |
michael@0 | 947 | else { |
michael@0 | 948 | if (here.val == 16) { |
michael@0 | 949 | NEEDBITS(here.bits + 2); |
michael@0 | 950 | DROPBITS(here.bits); |
michael@0 | 951 | if (state->have == 0) { |
michael@0 | 952 | strm->msg = (char *)"invalid bit length repeat"; |
michael@0 | 953 | state->mode = BAD; |
michael@0 | 954 | break; |
michael@0 | 955 | } |
michael@0 | 956 | len = state->lens[state->have - 1]; |
michael@0 | 957 | copy = 3 + BITS(2); |
michael@0 | 958 | DROPBITS(2); |
michael@0 | 959 | } |
michael@0 | 960 | else if (here.val == 17) { |
michael@0 | 961 | NEEDBITS(here.bits + 3); |
michael@0 | 962 | DROPBITS(here.bits); |
michael@0 | 963 | len = 0; |
michael@0 | 964 | copy = 3 + BITS(3); |
michael@0 | 965 | DROPBITS(3); |
michael@0 | 966 | } |
michael@0 | 967 | else { |
michael@0 | 968 | NEEDBITS(here.bits + 7); |
michael@0 | 969 | DROPBITS(here.bits); |
michael@0 | 970 | len = 0; |
michael@0 | 971 | copy = 11 + BITS(7); |
michael@0 | 972 | DROPBITS(7); |
michael@0 | 973 | } |
michael@0 | 974 | if (state->have + copy > state->nlen + state->ndist) { |
michael@0 | 975 | strm->msg = (char *)"invalid bit length repeat"; |
michael@0 | 976 | state->mode = BAD; |
michael@0 | 977 | break; |
michael@0 | 978 | } |
michael@0 | 979 | while (copy--) |
michael@0 | 980 | state->lens[state->have++] = (unsigned short)len; |
michael@0 | 981 | } |
michael@0 | 982 | } |
michael@0 | 983 | |
michael@0 | 984 | /* handle error breaks in while */ |
michael@0 | 985 | if (state->mode == BAD) break; |
michael@0 | 986 | |
michael@0 | 987 | /* check for end-of-block code (better have one) */ |
michael@0 | 988 | if (state->lens[256] == 0) { |
michael@0 | 989 | strm->msg = (char *)"invalid code -- missing end-of-block"; |
michael@0 | 990 | state->mode = BAD; |
michael@0 | 991 | break; |
michael@0 | 992 | } |
michael@0 | 993 | |
michael@0 | 994 | /* build code tables -- note: do not change the lenbits or distbits |
michael@0 | 995 | values here (9 and 6) without reading the comments in inftrees.h |
michael@0 | 996 | concerning the ENOUGH constants, which depend on those values */ |
michael@0 | 997 | state->next = state->codes; |
michael@0 | 998 | state->lencode = (const code FAR *)(state->next); |
michael@0 | 999 | state->lenbits = 9; |
michael@0 | 1000 | ret = inflate_table(LENS, state->lens, state->nlen, &(state->next), |
michael@0 | 1001 | &(state->lenbits), state->work); |
michael@0 | 1002 | if (ret) { |
michael@0 | 1003 | strm->msg = (char *)"invalid literal/lengths set"; |
michael@0 | 1004 | state->mode = BAD; |
michael@0 | 1005 | break; |
michael@0 | 1006 | } |
michael@0 | 1007 | state->distcode = (const code FAR *)(state->next); |
michael@0 | 1008 | state->distbits = 6; |
michael@0 | 1009 | ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist, |
michael@0 | 1010 | &(state->next), &(state->distbits), state->work); |
michael@0 | 1011 | if (ret) { |
michael@0 | 1012 | strm->msg = (char *)"invalid distances set"; |
michael@0 | 1013 | state->mode = BAD; |
michael@0 | 1014 | break; |
michael@0 | 1015 | } |
michael@0 | 1016 | Tracev((stderr, "inflate: codes ok\n")); |
michael@0 | 1017 | state->mode = LEN_; |
michael@0 | 1018 | if (flush == Z_TREES) goto inf_leave; |
michael@0 | 1019 | case LEN_: |
michael@0 | 1020 | state->mode = LEN; |
michael@0 | 1021 | case LEN: |
michael@0 | 1022 | if (have >= 6 && left >= 258) { |
michael@0 | 1023 | RESTORE(); |
michael@0 | 1024 | inflate_fast(strm, out); |
michael@0 | 1025 | LOAD(); |
michael@0 | 1026 | if (state->mode == TYPE) |
michael@0 | 1027 | state->back = -1; |
michael@0 | 1028 | break; |
michael@0 | 1029 | } |
michael@0 | 1030 | state->back = 0; |
michael@0 | 1031 | for (;;) { |
michael@0 | 1032 | here = state->lencode[BITS(state->lenbits)]; |
michael@0 | 1033 | if ((unsigned)(here.bits) <= bits) break; |
michael@0 | 1034 | PULLBYTE(); |
michael@0 | 1035 | } |
michael@0 | 1036 | if (here.op && (here.op & 0xf0) == 0) { |
michael@0 | 1037 | last = here; |
michael@0 | 1038 | for (;;) { |
michael@0 | 1039 | here = state->lencode[last.val + |
michael@0 | 1040 | (BITS(last.bits + last.op) >> last.bits)]; |
michael@0 | 1041 | if ((unsigned)(last.bits + here.bits) <= bits) break; |
michael@0 | 1042 | PULLBYTE(); |
michael@0 | 1043 | } |
michael@0 | 1044 | DROPBITS(last.bits); |
michael@0 | 1045 | state->back += last.bits; |
michael@0 | 1046 | } |
michael@0 | 1047 | DROPBITS(here.bits); |
michael@0 | 1048 | state->back += here.bits; |
michael@0 | 1049 | state->length = (unsigned)here.val; |
michael@0 | 1050 | if ((int)(here.op) == 0) { |
michael@0 | 1051 | Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ? |
michael@0 | 1052 | "inflate: literal '%c'\n" : |
michael@0 | 1053 | "inflate: literal 0x%02x\n", here.val)); |
michael@0 | 1054 | state->mode = LIT; |
michael@0 | 1055 | break; |
michael@0 | 1056 | } |
michael@0 | 1057 | if (here.op & 32) { |
michael@0 | 1058 | Tracevv((stderr, "inflate: end of block\n")); |
michael@0 | 1059 | state->back = -1; |
michael@0 | 1060 | state->mode = TYPE; |
michael@0 | 1061 | break; |
michael@0 | 1062 | } |
michael@0 | 1063 | if (here.op & 64) { |
michael@0 | 1064 | strm->msg = (char *)"invalid literal/length code"; |
michael@0 | 1065 | state->mode = BAD; |
michael@0 | 1066 | break; |
michael@0 | 1067 | } |
michael@0 | 1068 | state->extra = (unsigned)(here.op) & 15; |
michael@0 | 1069 | state->mode = LENEXT; |
michael@0 | 1070 | case LENEXT: |
michael@0 | 1071 | if (state->extra) { |
michael@0 | 1072 | NEEDBITS(state->extra); |
michael@0 | 1073 | state->length += BITS(state->extra); |
michael@0 | 1074 | DROPBITS(state->extra); |
michael@0 | 1075 | state->back += state->extra; |
michael@0 | 1076 | } |
michael@0 | 1077 | Tracevv((stderr, "inflate: length %u\n", state->length)); |
michael@0 | 1078 | state->was = state->length; |
michael@0 | 1079 | state->mode = DIST; |
michael@0 | 1080 | case DIST: |
michael@0 | 1081 | for (;;) { |
michael@0 | 1082 | here = state->distcode[BITS(state->distbits)]; |
michael@0 | 1083 | if ((unsigned)(here.bits) <= bits) break; |
michael@0 | 1084 | PULLBYTE(); |
michael@0 | 1085 | } |
michael@0 | 1086 | if ((here.op & 0xf0) == 0) { |
michael@0 | 1087 | last = here; |
michael@0 | 1088 | for (;;) { |
michael@0 | 1089 | here = state->distcode[last.val + |
michael@0 | 1090 | (BITS(last.bits + last.op) >> last.bits)]; |
michael@0 | 1091 | if ((unsigned)(last.bits + here.bits) <= bits) break; |
michael@0 | 1092 | PULLBYTE(); |
michael@0 | 1093 | } |
michael@0 | 1094 | DROPBITS(last.bits); |
michael@0 | 1095 | state->back += last.bits; |
michael@0 | 1096 | } |
michael@0 | 1097 | DROPBITS(here.bits); |
michael@0 | 1098 | state->back += here.bits; |
michael@0 | 1099 | if (here.op & 64) { |
michael@0 | 1100 | strm->msg = (char *)"invalid distance code"; |
michael@0 | 1101 | state->mode = BAD; |
michael@0 | 1102 | break; |
michael@0 | 1103 | } |
michael@0 | 1104 | state->offset = (unsigned)here.val; |
michael@0 | 1105 | state->extra = (unsigned)(here.op) & 15; |
michael@0 | 1106 | state->mode = DISTEXT; |
michael@0 | 1107 | case DISTEXT: |
michael@0 | 1108 | if (state->extra) { |
michael@0 | 1109 | NEEDBITS(state->extra); |
michael@0 | 1110 | state->offset += BITS(state->extra); |
michael@0 | 1111 | DROPBITS(state->extra); |
michael@0 | 1112 | state->back += state->extra; |
michael@0 | 1113 | } |
michael@0 | 1114 | #ifdef INFLATE_STRICT |
michael@0 | 1115 | if (state->offset > state->dmax) { |
michael@0 | 1116 | strm->msg = (char *)"invalid distance too far back"; |
michael@0 | 1117 | state->mode = BAD; |
michael@0 | 1118 | break; |
michael@0 | 1119 | } |
michael@0 | 1120 | #endif |
michael@0 | 1121 | Tracevv((stderr, "inflate: distance %u\n", state->offset)); |
michael@0 | 1122 | state->mode = MATCH; |
michael@0 | 1123 | case MATCH: |
michael@0 | 1124 | if (left == 0) goto inf_leave; |
michael@0 | 1125 | copy = out - left; |
michael@0 | 1126 | if (state->offset > copy) { /* copy from window */ |
michael@0 | 1127 | copy = state->offset - copy; |
michael@0 | 1128 | if (copy > state->whave) { |
michael@0 | 1129 | if (state->sane) { |
michael@0 | 1130 | strm->msg = (char *)"invalid distance too far back"; |
michael@0 | 1131 | state->mode = BAD; |
michael@0 | 1132 | break; |
michael@0 | 1133 | } |
michael@0 | 1134 | #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR |
michael@0 | 1135 | Trace((stderr, "inflate.c too far\n")); |
michael@0 | 1136 | copy -= state->whave; |
michael@0 | 1137 | if (copy > state->length) copy = state->length; |
michael@0 | 1138 | if (copy > left) copy = left; |
michael@0 | 1139 | left -= copy; |
michael@0 | 1140 | state->length -= copy; |
michael@0 | 1141 | do { |
michael@0 | 1142 | *put++ = 0; |
michael@0 | 1143 | } while (--copy); |
michael@0 | 1144 | if (state->length == 0) state->mode = LEN; |
michael@0 | 1145 | break; |
michael@0 | 1146 | #endif |
michael@0 | 1147 | } |
michael@0 | 1148 | if (copy > state->wnext) { |
michael@0 | 1149 | copy -= state->wnext; |
michael@0 | 1150 | from = state->window + (state->wsize - copy); |
michael@0 | 1151 | } |
michael@0 | 1152 | else |
michael@0 | 1153 | from = state->window + (state->wnext - copy); |
michael@0 | 1154 | if (copy > state->length) copy = state->length; |
michael@0 | 1155 | } |
michael@0 | 1156 | else { /* copy from output */ |
michael@0 | 1157 | from = put - state->offset; |
michael@0 | 1158 | copy = state->length; |
michael@0 | 1159 | } |
michael@0 | 1160 | if (copy > left) copy = left; |
michael@0 | 1161 | left -= copy; |
michael@0 | 1162 | state->length -= copy; |
michael@0 | 1163 | do { |
michael@0 | 1164 | *put++ = *from++; |
michael@0 | 1165 | } while (--copy); |
michael@0 | 1166 | if (state->length == 0) state->mode = LEN; |
michael@0 | 1167 | break; |
michael@0 | 1168 | case LIT: |
michael@0 | 1169 | if (left == 0) goto inf_leave; |
michael@0 | 1170 | *put++ = (unsigned char)(state->length); |
michael@0 | 1171 | left--; |
michael@0 | 1172 | state->mode = LEN; |
michael@0 | 1173 | break; |
michael@0 | 1174 | case CHECK: |
michael@0 | 1175 | if (state->wrap) { |
michael@0 | 1176 | NEEDBITS(32); |
michael@0 | 1177 | out -= left; |
michael@0 | 1178 | strm->total_out += out; |
michael@0 | 1179 | state->total += out; |
michael@0 | 1180 | if (out) |
michael@0 | 1181 | strm->adler = state->check = |
michael@0 | 1182 | UPDATE(state->check, put - out, out); |
michael@0 | 1183 | out = left; |
michael@0 | 1184 | if (( |
michael@0 | 1185 | #ifdef GUNZIP |
michael@0 | 1186 | state->flags ? hold : |
michael@0 | 1187 | #endif |
michael@0 | 1188 | ZSWAP32(hold)) != state->check) { |
michael@0 | 1189 | strm->msg = (char *)"incorrect data check"; |
michael@0 | 1190 | state->mode = BAD; |
michael@0 | 1191 | break; |
michael@0 | 1192 | } |
michael@0 | 1193 | INITBITS(); |
michael@0 | 1194 | Tracev((stderr, "inflate: check matches trailer\n")); |
michael@0 | 1195 | } |
michael@0 | 1196 | #ifdef GUNZIP |
michael@0 | 1197 | state->mode = LENGTH; |
michael@0 | 1198 | case LENGTH: |
michael@0 | 1199 | if (state->wrap && state->flags) { |
michael@0 | 1200 | NEEDBITS(32); |
michael@0 | 1201 | if (hold != (state->total & 0xffffffffUL)) { |
michael@0 | 1202 | strm->msg = (char *)"incorrect length check"; |
michael@0 | 1203 | state->mode = BAD; |
michael@0 | 1204 | break; |
michael@0 | 1205 | } |
michael@0 | 1206 | INITBITS(); |
michael@0 | 1207 | Tracev((stderr, "inflate: length matches trailer\n")); |
michael@0 | 1208 | } |
michael@0 | 1209 | #endif |
michael@0 | 1210 | state->mode = DONE; |
michael@0 | 1211 | case DONE: |
michael@0 | 1212 | ret = Z_STREAM_END; |
michael@0 | 1213 | goto inf_leave; |
michael@0 | 1214 | case BAD: |
michael@0 | 1215 | ret = Z_DATA_ERROR; |
michael@0 | 1216 | goto inf_leave; |
michael@0 | 1217 | case MEM: |
michael@0 | 1218 | return Z_MEM_ERROR; |
michael@0 | 1219 | case SYNC: |
michael@0 | 1220 | default: |
michael@0 | 1221 | return Z_STREAM_ERROR; |
michael@0 | 1222 | } |
michael@0 | 1223 | |
michael@0 | 1224 | /* |
michael@0 | 1225 | Return from inflate(), updating the total counts and the check value. |
michael@0 | 1226 | If there was no progress during the inflate() call, return a buffer |
michael@0 | 1227 | error. Call updatewindow() to create and/or update the window state. |
michael@0 | 1228 | Note: a memory error from inflate() is non-recoverable. |
michael@0 | 1229 | */ |
michael@0 | 1230 | inf_leave: |
michael@0 | 1231 | RESTORE(); |
michael@0 | 1232 | if (state->wsize || (out != strm->avail_out && state->mode < BAD && |
michael@0 | 1233 | (state->mode < CHECK || flush != Z_FINISH))) |
michael@0 | 1234 | if (updatewindow(strm, strm->next_out, out - strm->avail_out)) { |
michael@0 | 1235 | state->mode = MEM; |
michael@0 | 1236 | return Z_MEM_ERROR; |
michael@0 | 1237 | } |
michael@0 | 1238 | in -= strm->avail_in; |
michael@0 | 1239 | out -= strm->avail_out; |
michael@0 | 1240 | strm->total_in += in; |
michael@0 | 1241 | strm->total_out += out; |
michael@0 | 1242 | state->total += out; |
michael@0 | 1243 | if (state->wrap && out) |
michael@0 | 1244 | strm->adler = state->check = |
michael@0 | 1245 | UPDATE(state->check, strm->next_out - out, out); |
michael@0 | 1246 | strm->data_type = state->bits + (state->last ? 64 : 0) + |
michael@0 | 1247 | (state->mode == TYPE ? 128 : 0) + |
michael@0 | 1248 | (state->mode == LEN_ || state->mode == COPY_ ? 256 : 0); |
michael@0 | 1249 | if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK) |
michael@0 | 1250 | ret = Z_BUF_ERROR; |
michael@0 | 1251 | return ret; |
michael@0 | 1252 | } |
michael@0 | 1253 | |
michael@0 | 1254 | int ZEXPORT inflateEnd(strm) |
michael@0 | 1255 | z_streamp strm; |
michael@0 | 1256 | { |
michael@0 | 1257 | struct inflate_state FAR *state; |
michael@0 | 1258 | if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0) |
michael@0 | 1259 | return Z_STREAM_ERROR; |
michael@0 | 1260 | state = (struct inflate_state FAR *)strm->state; |
michael@0 | 1261 | if (state->window != Z_NULL) ZFREE(strm, state->window); |
michael@0 | 1262 | ZFREE(strm, strm->state); |
michael@0 | 1263 | strm->state = Z_NULL; |
michael@0 | 1264 | Tracev((stderr, "inflate: end\n")); |
michael@0 | 1265 | return Z_OK; |
michael@0 | 1266 | } |
michael@0 | 1267 | |
michael@0 | 1268 | int ZEXPORT inflateGetDictionary(strm, dictionary, dictLength) |
michael@0 | 1269 | z_streamp strm; |
michael@0 | 1270 | Bytef *dictionary; |
michael@0 | 1271 | uInt *dictLength; |
michael@0 | 1272 | { |
michael@0 | 1273 | struct inflate_state FAR *state; |
michael@0 | 1274 | |
michael@0 | 1275 | /* check state */ |
michael@0 | 1276 | if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; |
michael@0 | 1277 | state = (struct inflate_state FAR *)strm->state; |
michael@0 | 1278 | |
michael@0 | 1279 | /* copy dictionary */ |
michael@0 | 1280 | if (state->whave && dictionary != Z_NULL) { |
michael@0 | 1281 | zmemcpy(dictionary, state->window + state->wnext, |
michael@0 | 1282 | state->whave - state->wnext); |
michael@0 | 1283 | zmemcpy(dictionary + state->whave - state->wnext, |
michael@0 | 1284 | state->window, state->wnext); |
michael@0 | 1285 | } |
michael@0 | 1286 | if (dictLength != Z_NULL) |
michael@0 | 1287 | *dictLength = state->whave; |
michael@0 | 1288 | return Z_OK; |
michael@0 | 1289 | } |
michael@0 | 1290 | |
michael@0 | 1291 | int ZEXPORT inflateSetDictionary(strm, dictionary, dictLength) |
michael@0 | 1292 | z_streamp strm; |
michael@0 | 1293 | const Bytef *dictionary; |
michael@0 | 1294 | uInt dictLength; |
michael@0 | 1295 | { |
michael@0 | 1296 | struct inflate_state FAR *state; |
michael@0 | 1297 | unsigned long dictid; |
michael@0 | 1298 | int ret; |
michael@0 | 1299 | |
michael@0 | 1300 | /* check state */ |
michael@0 | 1301 | if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; |
michael@0 | 1302 | state = (struct inflate_state FAR *)strm->state; |
michael@0 | 1303 | if (state->wrap != 0 && state->mode != DICT) |
michael@0 | 1304 | return Z_STREAM_ERROR; |
michael@0 | 1305 | |
michael@0 | 1306 | /* check for correct dictionary identifier */ |
michael@0 | 1307 | if (state->mode == DICT) { |
michael@0 | 1308 | dictid = adler32(0L, Z_NULL, 0); |
michael@0 | 1309 | dictid = adler32(dictid, dictionary, dictLength); |
michael@0 | 1310 | if (dictid != state->check) |
michael@0 | 1311 | return Z_DATA_ERROR; |
michael@0 | 1312 | } |
michael@0 | 1313 | |
michael@0 | 1314 | /* copy dictionary to window using updatewindow(), which will amend the |
michael@0 | 1315 | existing dictionary if appropriate */ |
michael@0 | 1316 | ret = updatewindow(strm, dictionary + dictLength, dictLength); |
michael@0 | 1317 | if (ret) { |
michael@0 | 1318 | state->mode = MEM; |
michael@0 | 1319 | return Z_MEM_ERROR; |
michael@0 | 1320 | } |
michael@0 | 1321 | state->havedict = 1; |
michael@0 | 1322 | Tracev((stderr, "inflate: dictionary set\n")); |
michael@0 | 1323 | return Z_OK; |
michael@0 | 1324 | } |
michael@0 | 1325 | |
michael@0 | 1326 | int ZEXPORT inflateGetHeader(strm, head) |
michael@0 | 1327 | z_streamp strm; |
michael@0 | 1328 | gz_headerp head; |
michael@0 | 1329 | { |
michael@0 | 1330 | struct inflate_state FAR *state; |
michael@0 | 1331 | |
michael@0 | 1332 | /* check state */ |
michael@0 | 1333 | if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; |
michael@0 | 1334 | state = (struct inflate_state FAR *)strm->state; |
michael@0 | 1335 | if ((state->wrap & 2) == 0) return Z_STREAM_ERROR; |
michael@0 | 1336 | |
michael@0 | 1337 | /* save header structure */ |
michael@0 | 1338 | state->head = head; |
michael@0 | 1339 | head->done = 0; |
michael@0 | 1340 | return Z_OK; |
michael@0 | 1341 | } |
michael@0 | 1342 | |
michael@0 | 1343 | /* |
michael@0 | 1344 | Search buf[0..len-1] for the pattern: 0, 0, 0xff, 0xff. Return when found |
michael@0 | 1345 | or when out of input. When called, *have is the number of pattern bytes |
michael@0 | 1346 | found in order so far, in 0..3. On return *have is updated to the new |
michael@0 | 1347 | state. If on return *have equals four, then the pattern was found and the |
michael@0 | 1348 | return value is how many bytes were read including the last byte of the |
michael@0 | 1349 | pattern. If *have is less than four, then the pattern has not been found |
michael@0 | 1350 | yet and the return value is len. In the latter case, syncsearch() can be |
michael@0 | 1351 | called again with more data and the *have state. *have is initialized to |
michael@0 | 1352 | zero for the first call. |
michael@0 | 1353 | */ |
michael@0 | 1354 | local unsigned syncsearch(have, buf, len) |
michael@0 | 1355 | unsigned FAR *have; |
michael@0 | 1356 | const unsigned char FAR *buf; |
michael@0 | 1357 | unsigned len; |
michael@0 | 1358 | { |
michael@0 | 1359 | unsigned got; |
michael@0 | 1360 | unsigned next; |
michael@0 | 1361 | |
michael@0 | 1362 | got = *have; |
michael@0 | 1363 | next = 0; |
michael@0 | 1364 | while (next < len && got < 4) { |
michael@0 | 1365 | if ((int)(buf[next]) == (got < 2 ? 0 : 0xff)) |
michael@0 | 1366 | got++; |
michael@0 | 1367 | else if (buf[next]) |
michael@0 | 1368 | got = 0; |
michael@0 | 1369 | else |
michael@0 | 1370 | got = 4 - got; |
michael@0 | 1371 | next++; |
michael@0 | 1372 | } |
michael@0 | 1373 | *have = got; |
michael@0 | 1374 | return next; |
michael@0 | 1375 | } |
michael@0 | 1376 | |
michael@0 | 1377 | int ZEXPORT inflateSync(strm) |
michael@0 | 1378 | z_streamp strm; |
michael@0 | 1379 | { |
michael@0 | 1380 | unsigned len; /* number of bytes to look at or looked at */ |
michael@0 | 1381 | unsigned long in, out; /* temporary to save total_in and total_out */ |
michael@0 | 1382 | unsigned char buf[4]; /* to restore bit buffer to byte string */ |
michael@0 | 1383 | struct inflate_state FAR *state; |
michael@0 | 1384 | |
michael@0 | 1385 | /* check parameters */ |
michael@0 | 1386 | if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; |
michael@0 | 1387 | state = (struct inflate_state FAR *)strm->state; |
michael@0 | 1388 | if (strm->avail_in == 0 && state->bits < 8) return Z_BUF_ERROR; |
michael@0 | 1389 | |
michael@0 | 1390 | /* if first time, start search in bit buffer */ |
michael@0 | 1391 | if (state->mode != SYNC) { |
michael@0 | 1392 | state->mode = SYNC; |
michael@0 | 1393 | state->hold <<= state->bits & 7; |
michael@0 | 1394 | state->bits -= state->bits & 7; |
michael@0 | 1395 | len = 0; |
michael@0 | 1396 | while (state->bits >= 8) { |
michael@0 | 1397 | buf[len++] = (unsigned char)(state->hold); |
michael@0 | 1398 | state->hold >>= 8; |
michael@0 | 1399 | state->bits -= 8; |
michael@0 | 1400 | } |
michael@0 | 1401 | state->have = 0; |
michael@0 | 1402 | syncsearch(&(state->have), buf, len); |
michael@0 | 1403 | } |
michael@0 | 1404 | |
michael@0 | 1405 | /* search available input */ |
michael@0 | 1406 | len = syncsearch(&(state->have), strm->next_in, strm->avail_in); |
michael@0 | 1407 | strm->avail_in -= len; |
michael@0 | 1408 | strm->next_in += len; |
michael@0 | 1409 | strm->total_in += len; |
michael@0 | 1410 | |
michael@0 | 1411 | /* return no joy or set up to restart inflate() on a new block */ |
michael@0 | 1412 | if (state->have != 4) return Z_DATA_ERROR; |
michael@0 | 1413 | in = strm->total_in; out = strm->total_out; |
michael@0 | 1414 | inflateReset(strm); |
michael@0 | 1415 | strm->total_in = in; strm->total_out = out; |
michael@0 | 1416 | state->mode = TYPE; |
michael@0 | 1417 | return Z_OK; |
michael@0 | 1418 | } |
michael@0 | 1419 | |
michael@0 | 1420 | /* |
michael@0 | 1421 | Returns true if inflate is currently at the end of a block generated by |
michael@0 | 1422 | Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP |
michael@0 | 1423 | implementation to provide an additional safety check. PPP uses |
michael@0 | 1424 | Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored |
michael@0 | 1425 | block. When decompressing, PPP checks that at the end of input packet, |
michael@0 | 1426 | inflate is waiting for these length bytes. |
michael@0 | 1427 | */ |
michael@0 | 1428 | int ZEXPORT inflateSyncPoint(strm) |
michael@0 | 1429 | z_streamp strm; |
michael@0 | 1430 | { |
michael@0 | 1431 | struct inflate_state FAR *state; |
michael@0 | 1432 | |
michael@0 | 1433 | if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; |
michael@0 | 1434 | state = (struct inflate_state FAR *)strm->state; |
michael@0 | 1435 | return state->mode == STORED && state->bits == 0; |
michael@0 | 1436 | } |
michael@0 | 1437 | |
michael@0 | 1438 | int ZEXPORT inflateCopy(dest, source) |
michael@0 | 1439 | z_streamp dest; |
michael@0 | 1440 | z_streamp source; |
michael@0 | 1441 | { |
michael@0 | 1442 | struct inflate_state FAR *state; |
michael@0 | 1443 | struct inflate_state FAR *copy; |
michael@0 | 1444 | unsigned char FAR *window; |
michael@0 | 1445 | unsigned wsize; |
michael@0 | 1446 | |
michael@0 | 1447 | /* check input */ |
michael@0 | 1448 | if (dest == Z_NULL || source == Z_NULL || source->state == Z_NULL || |
michael@0 | 1449 | source->zalloc == (alloc_func)0 || source->zfree == (free_func)0) |
michael@0 | 1450 | return Z_STREAM_ERROR; |
michael@0 | 1451 | state = (struct inflate_state FAR *)source->state; |
michael@0 | 1452 | |
michael@0 | 1453 | /* allocate space */ |
michael@0 | 1454 | copy = (struct inflate_state FAR *) |
michael@0 | 1455 | ZALLOC(source, 1, sizeof(struct inflate_state)); |
michael@0 | 1456 | if (copy == Z_NULL) return Z_MEM_ERROR; |
michael@0 | 1457 | window = Z_NULL; |
michael@0 | 1458 | if (state->window != Z_NULL) { |
michael@0 | 1459 | window = (unsigned char FAR *) |
michael@0 | 1460 | ZALLOC(source, 1U << state->wbits, sizeof(unsigned char)); |
michael@0 | 1461 | if (window == Z_NULL) { |
michael@0 | 1462 | ZFREE(source, copy); |
michael@0 | 1463 | return Z_MEM_ERROR; |
michael@0 | 1464 | } |
michael@0 | 1465 | } |
michael@0 | 1466 | |
michael@0 | 1467 | /* copy state */ |
michael@0 | 1468 | zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream)); |
michael@0 | 1469 | zmemcpy((voidpf)copy, (voidpf)state, sizeof(struct inflate_state)); |
michael@0 | 1470 | if (state->lencode >= state->codes && |
michael@0 | 1471 | state->lencode <= state->codes + ENOUGH - 1) { |
michael@0 | 1472 | copy->lencode = copy->codes + (state->lencode - state->codes); |
michael@0 | 1473 | copy->distcode = copy->codes + (state->distcode - state->codes); |
michael@0 | 1474 | } |
michael@0 | 1475 | copy->next = copy->codes + (state->next - state->codes); |
michael@0 | 1476 | if (window != Z_NULL) { |
michael@0 | 1477 | wsize = 1U << state->wbits; |
michael@0 | 1478 | zmemcpy(window, state->window, wsize); |
michael@0 | 1479 | } |
michael@0 | 1480 | copy->window = window; |
michael@0 | 1481 | dest->state = (struct internal_state FAR *)copy; |
michael@0 | 1482 | return Z_OK; |
michael@0 | 1483 | } |
michael@0 | 1484 | |
michael@0 | 1485 | int ZEXPORT inflateUndermine(strm, subvert) |
michael@0 | 1486 | z_streamp strm; |
michael@0 | 1487 | int subvert; |
michael@0 | 1488 | { |
michael@0 | 1489 | struct inflate_state FAR *state; |
michael@0 | 1490 | |
michael@0 | 1491 | if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; |
michael@0 | 1492 | state = (struct inflate_state FAR *)strm->state; |
michael@0 | 1493 | state->sane = !subvert; |
michael@0 | 1494 | #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR |
michael@0 | 1495 | return Z_OK; |
michael@0 | 1496 | #else |
michael@0 | 1497 | state->sane = 1; |
michael@0 | 1498 | return Z_DATA_ERROR; |
michael@0 | 1499 | #endif |
michael@0 | 1500 | } |
michael@0 | 1501 | |
michael@0 | 1502 | long ZEXPORT inflateMark(strm) |
michael@0 | 1503 | z_streamp strm; |
michael@0 | 1504 | { |
michael@0 | 1505 | struct inflate_state FAR *state; |
michael@0 | 1506 | |
michael@0 | 1507 | if (strm == Z_NULL || strm->state == Z_NULL) return -1L << 16; |
michael@0 | 1508 | state = (struct inflate_state FAR *)strm->state; |
michael@0 | 1509 | return ((long)(state->back) << 16) + |
michael@0 | 1510 | (state->mode == COPY ? state->length : |
michael@0 | 1511 | (state->mode == MATCH ? state->was - state->length : 0)); |
michael@0 | 1512 | } |