Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 1 | /* inflate.c -- zlib decompression |
| 2 | * Copyright (C) 1995-2005 Mark Adler |
| 3 | * For conditions of distribution and use, see copyright notice in zlib.h |
| 4 | */ |
| 5 | local void fixedtables OF((struct inflate_state FAR *state)); |
| 6 | local int updatewindow OF((z_streamp strm, unsigned out)); |
| 7 | |
Kim Phillips | 8eb6994 | 2012-10-29 13:34:35 +0000 | [diff] [blame] | 8 | int ZEXPORT inflateReset(z_streamp strm) |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 9 | { |
| 10 | struct inflate_state FAR *state; |
| 11 | |
| 12 | if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; |
| 13 | state = (struct inflate_state FAR *)strm->state; |
| 14 | strm->total_in = strm->total_out = state->total = 0; |
| 15 | strm->msg = Z_NULL; |
| 16 | strm->adler = 1; /* to support ill-conceived Java test suite */ |
| 17 | state->mode = HEAD; |
| 18 | state->last = 0; |
| 19 | state->havedict = 0; |
| 20 | state->dmax = 32768U; |
| 21 | state->head = Z_NULL; |
| 22 | state->wsize = 0; |
| 23 | state->whave = 0; |
Michal Simek | e9a1956 | 2024-03-27 15:14:51 +0100 | [diff] [blame] | 24 | state->wnext = 0; |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 25 | state->hold = 0; |
| 26 | state->bits = 0; |
| 27 | state->lencode = state->distcode = state->next = state->codes; |
Stefan Roese | 80877fa | 2022-09-02 14:10:46 +0200 | [diff] [blame] | 28 | schedule(); |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 29 | Tracev((stderr, "inflate: reset\n")); |
| 30 | return Z_OK; |
| 31 | } |
| 32 | |
Kim Phillips | 8eb6994 | 2012-10-29 13:34:35 +0000 | [diff] [blame] | 33 | int ZEXPORT inflateInit2_(z_streamp strm, int windowBits, const char *version, |
| 34 | int stream_size) |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 35 | { |
| 36 | struct inflate_state FAR *state; |
| 37 | |
| 38 | if (version == Z_NULL || version[0] != ZLIB_VERSION[0] || |
| 39 | stream_size != (int)(sizeof(z_stream))) |
| 40 | return Z_VERSION_ERROR; |
| 41 | if (strm == Z_NULL) return Z_STREAM_ERROR; |
| 42 | strm->msg = Z_NULL; /* in case we return an error */ |
| 43 | if (strm->zalloc == (alloc_func)0) { |
| 44 | strm->zalloc = zcalloc; |
| 45 | strm->opaque = (voidpf)0; |
| 46 | } |
| 47 | if (strm->zfree == (free_func)0) strm->zfree = zcfree; |
| 48 | state = (struct inflate_state FAR *) |
| 49 | ZALLOC(strm, 1, sizeof(struct inflate_state)); |
| 50 | if (state == Z_NULL) return Z_MEM_ERROR; |
| 51 | Tracev((stderr, "inflate: allocated\n")); |
| 52 | strm->state = (struct internal_state FAR *)state; |
| 53 | if (windowBits < 0) { |
| 54 | state->wrap = 0; |
| 55 | windowBits = -windowBits; |
| 56 | } |
| 57 | else { |
| 58 | state->wrap = (windowBits >> 4) + 1; |
| 59 | #ifdef GUNZIP |
| 60 | if (windowBits < 48) windowBits &= 15; |
| 61 | #endif |
| 62 | } |
| 63 | if (windowBits < 8 || windowBits > 15) { |
| 64 | ZFREE(strm, state); |
| 65 | strm->state = Z_NULL; |
| 66 | return Z_STREAM_ERROR; |
| 67 | } |
| 68 | state->wbits = (unsigned)windowBits; |
| 69 | state->window = Z_NULL; |
| 70 | return inflateReset(strm); |
| 71 | } |
| 72 | |
Kim Phillips | 8eb6994 | 2012-10-29 13:34:35 +0000 | [diff] [blame] | 73 | int ZEXPORT inflateInit_(z_streamp strm, const char *version, int stream_size) |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 74 | { |
| 75 | return inflateInit2_(strm, DEF_WBITS, version, stream_size); |
| 76 | } |
| 77 | |
Kim Phillips | 8eb6994 | 2012-10-29 13:34:35 +0000 | [diff] [blame] | 78 | local void fixedtables(struct inflate_state FAR *state) |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 79 | { |
| 80 | state->lencode = lenfix; |
| 81 | state->lenbits = 9; |
| 82 | state->distcode = distfix; |
| 83 | state->distbits = 5; |
| 84 | } |
| 85 | |
| 86 | /* |
| 87 | Update the window with the last wsize (normally 32K) bytes written before |
| 88 | returning. If window does not exist yet, create it. This is only called |
| 89 | when a window is already in use, or when output has been written during this |
| 90 | inflate call, but the end of the deflate stream has not been reached yet. |
| 91 | It is also called to create a window for dictionary data when a dictionary |
| 92 | is loaded. |
| 93 | |
| 94 | Providing output buffers larger than 32K to inflate() should provide a speed |
| 95 | advantage, since only the last 32K of output is copied to the sliding window |
| 96 | upon return from inflate(), and since all distances after the first 32K of |
| 97 | output will fall in the output data, making match copies simpler and faster. |
| 98 | The advantage may be dependent on the size of the processor's data caches. |
| 99 | */ |
Kim Phillips | 8eb6994 | 2012-10-29 13:34:35 +0000 | [diff] [blame] | 100 | local int updatewindow(z_streamp strm, unsigned out) |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 101 | { |
| 102 | struct inflate_state FAR *state; |
| 103 | unsigned copy, dist; |
| 104 | |
| 105 | state = (struct inflate_state FAR *)strm->state; |
| 106 | |
| 107 | /* if it hasn't been done already, allocate space for the window */ |
| 108 | if (state->window == Z_NULL) { |
| 109 | state->window = (unsigned char FAR *) |
| 110 | ZALLOC(strm, 1U << state->wbits, |
| 111 | sizeof(unsigned char)); |
| 112 | if (state->window == Z_NULL) return 1; |
| 113 | } |
| 114 | |
| 115 | /* if window not in use yet, initialize */ |
| 116 | if (state->wsize == 0) { |
| 117 | state->wsize = 1U << state->wbits; |
Michal Simek | e9a1956 | 2024-03-27 15:14:51 +0100 | [diff] [blame] | 118 | state->wnext = 0; |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 119 | state->whave = 0; |
| 120 | } |
| 121 | |
| 122 | /* copy state->wsize or less output bytes into the circular window */ |
| 123 | copy = out - strm->avail_out; |
| 124 | if (copy >= state->wsize) { |
| 125 | zmemcpy(state->window, strm->next_out - state->wsize, state->wsize); |
Michal Simek | e9a1956 | 2024-03-27 15:14:51 +0100 | [diff] [blame] | 126 | state->wnext = 0; |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 127 | state->whave = state->wsize; |
| 128 | } |
| 129 | else { |
Michal Simek | e9a1956 | 2024-03-27 15:14:51 +0100 | [diff] [blame] | 130 | dist = state->wsize - state->wnext; |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 131 | if (dist > copy) dist = copy; |
Michal Simek | e9a1956 | 2024-03-27 15:14:51 +0100 | [diff] [blame] | 132 | zmemcpy(state->window + state->wnext, strm->next_out - copy, dist); |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 133 | copy -= dist; |
| 134 | if (copy) { |
| 135 | zmemcpy(state->window, strm->next_out - copy, copy); |
Michal Simek | e9a1956 | 2024-03-27 15:14:51 +0100 | [diff] [blame] | 136 | state->wnext = copy; |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 137 | state->whave = state->wsize; |
| 138 | } |
| 139 | else { |
Michal Simek | e9a1956 | 2024-03-27 15:14:51 +0100 | [diff] [blame] | 140 | state->wnext += dist; |
| 141 | if (state->wnext == state->wsize) state->wnext = 0; |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 142 | if (state->whave < state->wsize) state->whave += dist; |
| 143 | } |
| 144 | } |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | /* Macros for inflate(): */ |
| 149 | |
| 150 | /* check function to use adler32() for zlib or crc32() for gzip */ |
| 151 | #ifdef GUNZIP |
| 152 | # define UPDATE(check, buf, len) \ |
| 153 | (state->flags ? crc32(check, buf, len) : adler32(check, buf, len)) |
| 154 | #else |
| 155 | # define UPDATE(check, buf, len) adler32(check, buf, len) |
| 156 | #endif |
| 157 | |
| 158 | /* check macros for header crc */ |
| 159 | #ifdef GUNZIP |
| 160 | # define CRC2(check, word) \ |
| 161 | do { \ |
| 162 | hbuf[0] = (unsigned char)(word); \ |
| 163 | hbuf[1] = (unsigned char)((word) >> 8); \ |
| 164 | check = crc32(check, hbuf, 2); \ |
| 165 | } while (0) |
| 166 | |
| 167 | # define CRC4(check, word) \ |
| 168 | do { \ |
| 169 | hbuf[0] = (unsigned char)(word); \ |
| 170 | hbuf[1] = (unsigned char)((word) >> 8); \ |
| 171 | hbuf[2] = (unsigned char)((word) >> 16); \ |
| 172 | hbuf[3] = (unsigned char)((word) >> 24); \ |
| 173 | check = crc32(check, hbuf, 4); \ |
| 174 | } while (0) |
| 175 | #endif |
| 176 | |
| 177 | /* Load registers with state in inflate() for speed */ |
| 178 | #define LOAD() \ |
| 179 | do { \ |
| 180 | put = strm->next_out; \ |
| 181 | left = strm->avail_out; \ |
| 182 | next = strm->next_in; \ |
| 183 | have = strm->avail_in; \ |
| 184 | hold = state->hold; \ |
| 185 | bits = state->bits; \ |
| 186 | } while (0) |
| 187 | |
| 188 | /* Restore state from registers in inflate() */ |
| 189 | #define RESTORE() \ |
| 190 | do { \ |
| 191 | strm->next_out = put; \ |
| 192 | strm->avail_out = left; \ |
| 193 | strm->next_in = next; \ |
| 194 | strm->avail_in = have; \ |
| 195 | state->hold = hold; \ |
| 196 | state->bits = bits; \ |
| 197 | } while (0) |
| 198 | |
| 199 | /* Clear the input bit accumulator */ |
| 200 | #define INITBITS() \ |
| 201 | do { \ |
| 202 | hold = 0; \ |
| 203 | bits = 0; \ |
| 204 | } while (0) |
| 205 | |
| 206 | /* Get a byte of input into the bit accumulator, or return from inflate() |
| 207 | if there is no input available. */ |
| 208 | #define PULLBYTE() \ |
| 209 | do { \ |
| 210 | if (have == 0) goto inf_leave; \ |
| 211 | have--; \ |
| 212 | hold += (unsigned long)(*next++) << bits; \ |
| 213 | bits += 8; \ |
| 214 | } while (0) |
| 215 | |
| 216 | /* Assure that there are at least n bits in the bit accumulator. If there is |
| 217 | not enough available input to do that, then return from inflate(). */ |
| 218 | #define NEEDBITS(n) \ |
| 219 | do { \ |
| 220 | while (bits < (unsigned)(n)) \ |
| 221 | PULLBYTE(); \ |
| 222 | } while (0) |
| 223 | |
| 224 | /* Return the low n bits of the bit accumulator (n < 16) */ |
| 225 | #define BITS(n) \ |
| 226 | ((unsigned)hold & ((1U << (n)) - 1)) |
| 227 | |
| 228 | /* Remove n bits from the bit accumulator */ |
| 229 | #define DROPBITS(n) \ |
| 230 | do { \ |
| 231 | hold >>= (n); \ |
| 232 | bits -= (unsigned)(n); \ |
| 233 | } while (0) |
| 234 | |
| 235 | /* Remove zero to seven bits as needed to go to a byte boundary */ |
| 236 | #define BYTEBITS() \ |
| 237 | do { \ |
| 238 | hold >>= bits & 7; \ |
| 239 | bits -= bits & 7; \ |
| 240 | } while (0) |
| 241 | |
| 242 | /* Reverse the bytes in a 32-bit value */ |
| 243 | #define REVERSE(q) \ |
| 244 | ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \ |
| 245 | (((q) & 0xff00) << 8) + (((q) & 0xff) << 24)) |
| 246 | |
| 247 | /* |
| 248 | inflate() uses a state machine to process as much input data and generate as |
| 249 | much output data as possible before returning. The state machine is |
| 250 | structured roughly as follows: |
| 251 | |
| 252 | for (;;) switch (state) { |
| 253 | ... |
| 254 | case STATEn: |
| 255 | if (not enough input data or output space to make progress) |
| 256 | return; |
| 257 | ... make progress ... |
| 258 | state = STATEm; |
| 259 | break; |
| 260 | ... |
| 261 | } |
| 262 | |
| 263 | so when inflate() is called again, the same case is attempted again, and |
| 264 | if the appropriate resources are provided, the machine proceeds to the |
| 265 | next state. The NEEDBITS() macro is usually the way the state evaluates |
| 266 | whether it can proceed or should return. NEEDBITS() does the return if |
| 267 | the requested bits are not available. The typical use of the BITS macros |
| 268 | is: |
| 269 | |
| 270 | NEEDBITS(n); |
| 271 | ... do something with BITS(n) ... |
| 272 | DROPBITS(n); |
| 273 | |
| 274 | where NEEDBITS(n) either returns from inflate() if there isn't enough |
| 275 | input left to load n bits into the accumulator, or it continues. BITS(n) |
| 276 | gives the low n bits in the accumulator. When done, DROPBITS(n) drops |
| 277 | the low n bits off the accumulator. INITBITS() clears the accumulator |
| 278 | and sets the number of available bits to zero. BYTEBITS() discards just |
| 279 | enough bits to put the accumulator on a byte boundary. After BYTEBITS() |
| 280 | and a NEEDBITS(8), then BITS(8) would return the next byte in the stream. |
| 281 | |
| 282 | NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return |
| 283 | if there is no input available. The decoding of variable length codes uses |
| 284 | PULLBYTE() directly in order to pull just enough bytes to decode the next |
| 285 | code, and no more. |
| 286 | |
| 287 | Some states loop until they get enough input, making sure that enough |
| 288 | state information is maintained to continue the loop where it left off |
| 289 | if NEEDBITS() returns in the loop. For example, want, need, and keep |
| 290 | would all have to actually be part of the saved state in case NEEDBITS() |
| 291 | returns: |
| 292 | |
| 293 | case STATEw: |
| 294 | while (want < need) { |
| 295 | NEEDBITS(n); |
| 296 | keep[want++] = BITS(n); |
| 297 | DROPBITS(n); |
| 298 | } |
| 299 | state = STATEx; |
| 300 | case STATEx: |
| 301 | |
| 302 | As shown above, if the next state is also the next case, then the break |
| 303 | is omitted. |
| 304 | |
| 305 | A state may also return if there is not enough output space available to |
| 306 | complete that state. Those states are copying stored data, writing a |
| 307 | literal byte, and copying a matching string. |
| 308 | |
| 309 | When returning, a "goto inf_leave" is used to update the total counters, |
| 310 | update the check value, and determine whether any progress has been made |
| 311 | during that inflate() call in order to return the proper return code. |
| 312 | Progress is defined as a change in either strm->avail_in or strm->avail_out. |
| 313 | When there is a window, goto inf_leave will update the window with the last |
| 314 | output written. If a goto inf_leave occurs in the middle of decompression |
| 315 | and there is no window currently, goto inf_leave will create one and copy |
| 316 | output to the window for the next call of inflate(). |
| 317 | |
| 318 | In this implementation, the flush parameter of inflate() only affects the |
| 319 | return code (per zlib.h). inflate() always writes as much as possible to |
| 320 | strm->next_out, given the space available and the provided input--the effect |
| 321 | documented in zlib.h of Z_SYNC_FLUSH. Furthermore, inflate() always defers |
| 322 | the allocation of and copying into a sliding window until necessary, which |
| 323 | provides the effect documented in zlib.h for Z_FINISH when the entire input |
| 324 | stream available. So the only thing the flush parameter actually does is: |
| 325 | when flush is set to Z_FINISH, inflate() cannot return Z_OK. Instead it |
| 326 | will return Z_BUF_ERROR if it has not reached the end of the stream. |
| 327 | */ |
Kim Phillips | 8eb6994 | 2012-10-29 13:34:35 +0000 | [diff] [blame] | 328 | int ZEXPORT inflate(z_streamp strm, int flush) |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 329 | { |
| 330 | struct inflate_state FAR *state; |
| 331 | unsigned char FAR *next; /* next input */ |
| 332 | unsigned char FAR *put; /* next output */ |
| 333 | unsigned have, left; /* available input and output */ |
| 334 | unsigned long hold; /* bit buffer */ |
| 335 | unsigned bits; /* bits in bit buffer */ |
| 336 | unsigned in, out; /* save starting available input and output */ |
| 337 | unsigned copy; /* number of stored or match bytes to copy */ |
| 338 | unsigned char FAR *from; /* where to copy match bytes from */ |
| 339 | code this; /* current decoding table entry */ |
| 340 | code last; /* parent table entry */ |
| 341 | unsigned len; /* length to copy for repeats, bits to drop */ |
| 342 | int ret; /* return code */ |
| 343 | #ifdef GUNZIP |
| 344 | unsigned char hbuf[4]; /* buffer for gzip header crc calculation */ |
| 345 | #endif |
| 346 | static const unsigned short order[19] = /* permutation of code lengths */ |
| 347 | {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; |
| 348 | |
| 349 | if (strm == Z_NULL || strm->state == Z_NULL || |
| 350 | (strm->next_in == Z_NULL && strm->avail_in != 0)) |
| 351 | return Z_STREAM_ERROR; |
| 352 | |
| 353 | state = (struct inflate_state FAR *)strm->state; |
| 354 | if (state->mode == TYPE) state->mode = TYPEDO; /* skip check */ |
| 355 | LOAD(); |
| 356 | in = have; |
| 357 | out = left; |
| 358 | ret = Z_OK; |
| 359 | for (;;) |
| 360 | switch (state->mode) { |
| 361 | case HEAD: |
| 362 | if (state->wrap == 0) { |
| 363 | state->mode = TYPEDO; |
| 364 | break; |
| 365 | } |
| 366 | NEEDBITS(16); |
| 367 | #ifdef GUNZIP |
| 368 | if ((state->wrap & 2) && hold == 0x8b1f) { /* gzip header */ |
| 369 | state->check = crc32(0L, Z_NULL, 0); |
| 370 | CRC2(state->check, hold); |
| 371 | INITBITS(); |
| 372 | state->mode = FLAGS; |
| 373 | break; |
| 374 | } |
| 375 | state->flags = 0; /* expect zlib header */ |
| 376 | if (state->head != Z_NULL) |
| 377 | state->head->done = -1; |
| 378 | if (!(state->wrap & 1) || /* check if zlib header allowed */ |
| 379 | #else |
| 380 | if ( |
| 381 | #endif |
| 382 | ((BITS(8) << 8) + (hold >> 8)) % 31) { |
| 383 | strm->msg = (char *)"incorrect header check"; |
| 384 | state->mode = BAD; |
| 385 | break; |
| 386 | } |
| 387 | if (BITS(4) != Z_DEFLATED) { |
| 388 | strm->msg = (char *)"unknown compression method"; |
| 389 | state->mode = BAD; |
| 390 | break; |
| 391 | } |
| 392 | DROPBITS(4); |
| 393 | len = BITS(4) + 8; |
| 394 | if (len > state->wbits) { |
| 395 | strm->msg = (char *)"invalid window size"; |
| 396 | state->mode = BAD; |
| 397 | break; |
| 398 | } |
| 399 | state->dmax = 1U << len; |
| 400 | Tracev((stderr, "inflate: zlib header ok\n")); |
| 401 | strm->adler = state->check = adler32(0L, Z_NULL, 0); |
| 402 | state->mode = hold & 0x200 ? DICTID : TYPE; |
| 403 | INITBITS(); |
| 404 | break; |
| 405 | #ifdef GUNZIP |
| 406 | case FLAGS: |
| 407 | NEEDBITS(16); |
| 408 | state->flags = (int)(hold); |
| 409 | if ((state->flags & 0xff) != Z_DEFLATED) { |
| 410 | strm->msg = (char *)"unknown compression method"; |
| 411 | state->mode = BAD; |
| 412 | break; |
| 413 | } |
| 414 | if (state->flags & 0xe000) { |
| 415 | strm->msg = (char *)"unknown header flags set"; |
| 416 | state->mode = BAD; |
| 417 | break; |
| 418 | } |
| 419 | if (state->head != Z_NULL) |
| 420 | state->head->text = (int)((hold >> 8) & 1); |
| 421 | if (state->flags & 0x0200) CRC2(state->check, hold); |
| 422 | INITBITS(); |
| 423 | state->mode = TIME; |
| 424 | case TIME: |
| 425 | NEEDBITS(32); |
| 426 | if (state->head != Z_NULL) |
| 427 | state->head->time = hold; |
| 428 | if (state->flags & 0x0200) CRC4(state->check, hold); |
| 429 | INITBITS(); |
| 430 | state->mode = OS; |
| 431 | case OS: |
| 432 | NEEDBITS(16); |
| 433 | if (state->head != Z_NULL) { |
| 434 | state->head->xflags = (int)(hold & 0xff); |
| 435 | state->head->os = (int)(hold >> 8); |
| 436 | } |
| 437 | if (state->flags & 0x0200) CRC2(state->check, hold); |
| 438 | INITBITS(); |
| 439 | state->mode = EXLEN; |
| 440 | case EXLEN: |
| 441 | if (state->flags & 0x0400) { |
| 442 | NEEDBITS(16); |
| 443 | state->length = (unsigned)(hold); |
| 444 | if (state->head != Z_NULL) |
| 445 | state->head->extra_len = (unsigned)hold; |
| 446 | if (state->flags & 0x0200) CRC2(state->check, hold); |
| 447 | INITBITS(); |
| 448 | } |
| 449 | else if (state->head != Z_NULL) |
| 450 | state->head->extra = Z_NULL; |
| 451 | state->mode = EXTRA; |
| 452 | case EXTRA: |
| 453 | if (state->flags & 0x0400) { |
| 454 | copy = state->length; |
| 455 | if (copy > have) copy = have; |
| 456 | if (copy) { |
| 457 | if (state->head != Z_NULL && |
Oleksandr Suvorov | 1b4014b | 2023-06-15 17:54:34 +0300 | [diff] [blame] | 458 | state->head->extra != Z_NULL && |
| 459 | (len = state->head->extra_len - state->length) < |
| 460 | state->head->extra_max) { |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 461 | zmemcpy(state->head->extra + len, next, |
| 462 | len + copy > state->head->extra_max ? |
| 463 | state->head->extra_max - len : copy); |
| 464 | } |
| 465 | if (state->flags & 0x0200) |
| 466 | state->check = crc32(state->check, next, copy); |
| 467 | have -= copy; |
| 468 | next += copy; |
| 469 | state->length -= copy; |
| 470 | } |
| 471 | if (state->length) goto inf_leave; |
| 472 | } |
| 473 | state->length = 0; |
| 474 | state->mode = NAME; |
| 475 | case NAME: |
| 476 | if (state->flags & 0x0800) { |
| 477 | if (have == 0) goto inf_leave; |
| 478 | copy = 0; |
| 479 | do { |
| 480 | len = (unsigned)(next[copy++]); |
| 481 | if (state->head != Z_NULL && |
| 482 | state->head->name != Z_NULL && |
| 483 | state->length < state->head->name_max) |
| 484 | state->head->name[state->length++] = len; |
| 485 | } while (len && copy < have); |
| 486 | if (state->flags & 0x0200) |
| 487 | state->check = crc32(state->check, next, copy); |
| 488 | have -= copy; |
| 489 | next += copy; |
| 490 | if (len) goto inf_leave; |
| 491 | } |
| 492 | else if (state->head != Z_NULL) |
| 493 | state->head->name = Z_NULL; |
| 494 | state->length = 0; |
| 495 | state->mode = COMMENT; |
| 496 | case COMMENT: |
| 497 | if (state->flags & 0x1000) { |
| 498 | if (have == 0) goto inf_leave; |
| 499 | copy = 0; |
| 500 | do { |
| 501 | len = (unsigned)(next[copy++]); |
| 502 | if (state->head != Z_NULL && |
| 503 | state->head->comment != Z_NULL && |
| 504 | state->length < state->head->comm_max) |
| 505 | state->head->comment[state->length++] = len; |
| 506 | } while (len && copy < have); |
| 507 | if (state->flags & 0x0200) |
| 508 | state->check = crc32(state->check, next, copy); |
| 509 | have -= copy; |
| 510 | next += copy; |
| 511 | if (len) goto inf_leave; |
| 512 | } |
| 513 | else if (state->head != Z_NULL) |
| 514 | state->head->comment = Z_NULL; |
| 515 | state->mode = HCRC; |
| 516 | case HCRC: |
| 517 | if (state->flags & 0x0200) { |
| 518 | NEEDBITS(16); |
| 519 | if (hold != (state->check & 0xffff)) { |
| 520 | strm->msg = (char *)"header crc mismatch"; |
| 521 | state->mode = BAD; |
| 522 | break; |
| 523 | } |
| 524 | INITBITS(); |
| 525 | } |
| 526 | if (state->head != Z_NULL) { |
| 527 | state->head->hcrc = (int)((state->flags >> 9) & 1); |
| 528 | state->head->done = 1; |
| 529 | } |
| 530 | strm->adler = state->check = crc32(0L, Z_NULL, 0); |
| 531 | state->mode = TYPE; |
| 532 | break; |
| 533 | #endif |
| 534 | case DICTID: |
| 535 | NEEDBITS(32); |
| 536 | strm->adler = state->check = REVERSE(hold); |
| 537 | INITBITS(); |
| 538 | state->mode = DICT; |
| 539 | case DICT: |
| 540 | if (state->havedict == 0) { |
| 541 | RESTORE(); |
| 542 | return Z_NEED_DICT; |
| 543 | } |
| 544 | strm->adler = state->check = adler32(0L, Z_NULL, 0); |
| 545 | state->mode = TYPE; |
| 546 | case TYPE: |
Stefan Roese | 80877fa | 2022-09-02 14:10:46 +0200 | [diff] [blame] | 547 | schedule(); |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 548 | if (flush == Z_BLOCK) goto inf_leave; |
| 549 | case TYPEDO: |
| 550 | if (state->last) { |
| 551 | BYTEBITS(); |
| 552 | state->mode = CHECK; |
| 553 | break; |
| 554 | } |
| 555 | NEEDBITS(3); |
| 556 | state->last = BITS(1); |
| 557 | DROPBITS(1); |
| 558 | switch (BITS(2)) { |
| 559 | case 0: /* stored block */ |
| 560 | Tracev((stderr, "inflate: stored block%s\n", |
| 561 | state->last ? " (last)" : "")); |
| 562 | state->mode = STORED; |
| 563 | break; |
| 564 | case 1: /* fixed block */ |
| 565 | fixedtables(state); |
| 566 | Tracev((stderr, "inflate: fixed codes block%s\n", |
| 567 | state->last ? " (last)" : "")); |
| 568 | state->mode = LEN; /* decode codes */ |
| 569 | break; |
| 570 | case 2: /* dynamic block */ |
| 571 | Tracev((stderr, "inflate: dynamic codes block%s\n", |
| 572 | state->last ? " (last)" : "")); |
| 573 | state->mode = TABLE; |
| 574 | break; |
| 575 | case 3: |
| 576 | strm->msg = (char *)"invalid block type"; |
| 577 | state->mode = BAD; |
| 578 | } |
| 579 | DROPBITS(2); |
| 580 | break; |
| 581 | case STORED: |
| 582 | BYTEBITS(); /* go to byte boundary */ |
| 583 | NEEDBITS(32); |
| 584 | if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) { |
| 585 | strm->msg = (char *)"invalid stored block lengths"; |
| 586 | state->mode = BAD; |
| 587 | break; |
| 588 | } |
| 589 | state->length = (unsigned)hold & 0xffff; |
| 590 | Tracev((stderr, "inflate: stored length %u\n", |
| 591 | state->length)); |
| 592 | INITBITS(); |
| 593 | state->mode = COPY; |
| 594 | case COPY: |
| 595 | copy = state->length; |
| 596 | if (copy) { |
| 597 | if (copy > have) copy = have; |
| 598 | if (copy > left) copy = left; |
| 599 | if (copy == 0) goto inf_leave; |
| 600 | zmemcpy(put, next, copy); |
| 601 | have -= copy; |
| 602 | next += copy; |
| 603 | left -= copy; |
| 604 | put += copy; |
| 605 | state->length -= copy; |
| 606 | break; |
| 607 | } |
| 608 | Tracev((stderr, "inflate: stored end\n")); |
| 609 | state->mode = TYPE; |
| 610 | break; |
| 611 | case TABLE: |
| 612 | NEEDBITS(14); |
| 613 | state->nlen = BITS(5) + 257; |
| 614 | DROPBITS(5); |
| 615 | state->ndist = BITS(5) + 1; |
| 616 | DROPBITS(5); |
| 617 | state->ncode = BITS(4) + 4; |
| 618 | DROPBITS(4); |
| 619 | #ifndef PKZIP_BUG_WORKAROUND |
| 620 | if (state->nlen > 286 || state->ndist > 30) { |
| 621 | strm->msg = (char *)"too many length or distance symbols"; |
| 622 | state->mode = BAD; |
| 623 | break; |
| 624 | } |
| 625 | #endif |
| 626 | Tracev((stderr, "inflate: table sizes ok\n")); |
| 627 | state->have = 0; |
| 628 | state->mode = LENLENS; |
| 629 | case LENLENS: |
| 630 | while (state->have < state->ncode) { |
| 631 | NEEDBITS(3); |
| 632 | state->lens[order[state->have++]] = (unsigned short)BITS(3); |
| 633 | DROPBITS(3); |
| 634 | } |
| 635 | while (state->have < 19) |
| 636 | state->lens[order[state->have++]] = 0; |
| 637 | state->next = state->codes; |
| 638 | state->lencode = (code const FAR *)(state->next); |
| 639 | state->lenbits = 7; |
| 640 | ret = inflate_table(CODES, state->lens, 19, &(state->next), |
| 641 | &(state->lenbits), state->work); |
| 642 | if (ret) { |
| 643 | strm->msg = (char *)"invalid code lengths set"; |
| 644 | state->mode = BAD; |
| 645 | break; |
| 646 | } |
| 647 | Tracev((stderr, "inflate: code lengths ok\n")); |
| 648 | state->have = 0; |
| 649 | state->mode = CODELENS; |
| 650 | case CODELENS: |
| 651 | while (state->have < state->nlen + state->ndist) { |
| 652 | for (;;) { |
| 653 | this = state->lencode[BITS(state->lenbits)]; |
| 654 | if ((unsigned)(this.bits) <= bits) break; |
| 655 | PULLBYTE(); |
| 656 | } |
| 657 | if (this.val < 16) { |
| 658 | NEEDBITS(this.bits); |
| 659 | DROPBITS(this.bits); |
| 660 | state->lens[state->have++] = this.val; |
| 661 | } |
| 662 | else { |
| 663 | if (this.val == 16) { |
| 664 | NEEDBITS(this.bits + 2); |
| 665 | DROPBITS(this.bits); |
| 666 | if (state->have == 0) { |
| 667 | strm->msg = (char *)"invalid bit length repeat"; |
| 668 | state->mode = BAD; |
| 669 | break; |
| 670 | } |
| 671 | len = state->lens[state->have - 1]; |
| 672 | copy = 3 + BITS(2); |
| 673 | DROPBITS(2); |
| 674 | } |
| 675 | else if (this.val == 17) { |
| 676 | NEEDBITS(this.bits + 3); |
| 677 | DROPBITS(this.bits); |
| 678 | len = 0; |
| 679 | copy = 3 + BITS(3); |
| 680 | DROPBITS(3); |
| 681 | } |
| 682 | else { |
| 683 | NEEDBITS(this.bits + 7); |
| 684 | DROPBITS(this.bits); |
| 685 | len = 0; |
| 686 | copy = 11 + BITS(7); |
| 687 | DROPBITS(7); |
| 688 | } |
| 689 | if (state->have + copy > state->nlen + state->ndist) { |
| 690 | strm->msg = (char *)"invalid bit length repeat"; |
| 691 | state->mode = BAD; |
| 692 | break; |
| 693 | } |
| 694 | while (copy--) |
| 695 | state->lens[state->have++] = (unsigned short)len; |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | /* handle error breaks in while */ |
| 700 | if (state->mode == BAD) break; |
| 701 | |
| 702 | /* build code tables */ |
| 703 | state->next = state->codes; |
| 704 | state->lencode = (code const FAR *)(state->next); |
| 705 | state->lenbits = 9; |
| 706 | ret = inflate_table(LENS, state->lens, state->nlen, &(state->next), |
| 707 | &(state->lenbits), state->work); |
| 708 | if (ret) { |
| 709 | strm->msg = (char *)"invalid literal/lengths set"; |
| 710 | state->mode = BAD; |
| 711 | break; |
| 712 | } |
| 713 | state->distcode = (code const FAR *)(state->next); |
| 714 | state->distbits = 6; |
| 715 | ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist, |
| 716 | &(state->next), &(state->distbits), state->work); |
| 717 | if (ret) { |
| 718 | strm->msg = (char *)"invalid distances set"; |
| 719 | state->mode = BAD; |
| 720 | break; |
| 721 | } |
| 722 | Tracev((stderr, "inflate: codes ok\n")); |
| 723 | state->mode = LEN; |
| 724 | case LEN: |
Stefan Roese | 80877fa | 2022-09-02 14:10:46 +0200 | [diff] [blame] | 725 | schedule(); |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 726 | if (have >= 6 && left >= 258) { |
| 727 | RESTORE(); |
| 728 | inflate_fast(strm, out); |
| 729 | LOAD(); |
| 730 | break; |
| 731 | } |
| 732 | for (;;) { |
| 733 | this = state->lencode[BITS(state->lenbits)]; |
| 734 | if ((unsigned)(this.bits) <= bits) break; |
| 735 | PULLBYTE(); |
| 736 | } |
| 737 | if (this.op && (this.op & 0xf0) == 0) { |
| 738 | last = this; |
| 739 | for (;;) { |
| 740 | this = state->lencode[last.val + |
| 741 | (BITS(last.bits + last.op) >> last.bits)]; |
| 742 | if ((unsigned)(last.bits + this.bits) <= bits) break; |
| 743 | PULLBYTE(); |
| 744 | } |
| 745 | DROPBITS(last.bits); |
| 746 | } |
| 747 | DROPBITS(this.bits); |
| 748 | state->length = (unsigned)this.val; |
| 749 | if ((int)(this.op) == 0) { |
| 750 | Tracevv((stderr, this.val >= 0x20 && this.val < 0x7f ? |
| 751 | "inflate: literal '%c'\n" : |
| 752 | "inflate: literal 0x%02x\n", this.val)); |
| 753 | state->mode = LIT; |
| 754 | break; |
| 755 | } |
| 756 | if (this.op & 32) { |
| 757 | Tracevv((stderr, "inflate: end of block\n")); |
| 758 | state->mode = TYPE; |
| 759 | break; |
| 760 | } |
| 761 | if (this.op & 64) { |
| 762 | strm->msg = (char *)"invalid literal/length code"; |
| 763 | state->mode = BAD; |
| 764 | break; |
| 765 | } |
| 766 | state->extra = (unsigned)(this.op) & 15; |
| 767 | state->mode = LENEXT; |
| 768 | case LENEXT: |
| 769 | if (state->extra) { |
| 770 | NEEDBITS(state->extra); |
| 771 | state->length += BITS(state->extra); |
| 772 | DROPBITS(state->extra); |
| 773 | } |
| 774 | Tracevv((stderr, "inflate: length %u\n", state->length)); |
| 775 | state->mode = DIST; |
| 776 | case DIST: |
| 777 | for (;;) { |
| 778 | this = state->distcode[BITS(state->distbits)]; |
| 779 | if ((unsigned)(this.bits) <= bits) break; |
| 780 | PULLBYTE(); |
| 781 | } |
| 782 | if ((this.op & 0xf0) == 0) { |
| 783 | last = this; |
| 784 | for (;;) { |
| 785 | this = state->distcode[last.val + |
| 786 | (BITS(last.bits + last.op) >> last.bits)]; |
| 787 | if ((unsigned)(last.bits + this.bits) <= bits) break; |
| 788 | PULLBYTE(); |
| 789 | } |
| 790 | DROPBITS(last.bits); |
| 791 | } |
| 792 | DROPBITS(this.bits); |
| 793 | if (this.op & 64) { |
| 794 | strm->msg = (char *)"invalid distance code"; |
| 795 | state->mode = BAD; |
| 796 | break; |
| 797 | } |
| 798 | state->offset = (unsigned)this.val; |
| 799 | state->extra = (unsigned)(this.op) & 15; |
| 800 | state->mode = DISTEXT; |
| 801 | case DISTEXT: |
| 802 | if (state->extra) { |
| 803 | NEEDBITS(state->extra); |
| 804 | state->offset += BITS(state->extra); |
| 805 | DROPBITS(state->extra); |
| 806 | } |
| 807 | #ifdef INFLATE_STRICT |
| 808 | if (state->offset > state->dmax) { |
| 809 | strm->msg = (char *)"invalid distance too far back"; |
| 810 | state->mode = BAD; |
| 811 | break; |
| 812 | } |
| 813 | #endif |
| 814 | if (state->offset > state->whave + out - left) { |
| 815 | strm->msg = (char *)"invalid distance too far back"; |
| 816 | state->mode = BAD; |
| 817 | break; |
| 818 | } |
| 819 | Tracevv((stderr, "inflate: distance %u\n", state->offset)); |
| 820 | state->mode = MATCH; |
| 821 | case MATCH: |
| 822 | if (left == 0) goto inf_leave; |
| 823 | copy = out - left; |
| 824 | if (state->offset > copy) { /* copy from window */ |
| 825 | copy = state->offset - copy; |
Michal Simek | e9a1956 | 2024-03-27 15:14:51 +0100 | [diff] [blame] | 826 | if (copy > state->wnext) { |
| 827 | copy -= state->wnext; |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 828 | from = state->window + (state->wsize - copy); |
| 829 | } |
| 830 | else |
Michal Simek | e9a1956 | 2024-03-27 15:14:51 +0100 | [diff] [blame] | 831 | from = state->window + (state->wnext - copy); |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 832 | if (copy > state->length) copy = state->length; |
| 833 | } |
| 834 | else { /* copy from output */ |
| 835 | from = put - state->offset; |
| 836 | copy = state->length; |
| 837 | } |
| 838 | if (copy > left) copy = left; |
| 839 | left -= copy; |
| 840 | state->length -= copy; |
| 841 | do { |
| 842 | *put++ = *from++; |
| 843 | } while (--copy); |
| 844 | if (state->length == 0) state->mode = LEN; |
| 845 | break; |
| 846 | case LIT: |
| 847 | if (left == 0) goto inf_leave; |
| 848 | *put++ = (unsigned char)(state->length); |
| 849 | left--; |
| 850 | state->mode = LEN; |
| 851 | break; |
| 852 | case CHECK: |
| 853 | if (state->wrap) { |
| 854 | NEEDBITS(32); |
| 855 | out -= left; |
| 856 | strm->total_out += out; |
| 857 | state->total += out; |
| 858 | if (out) |
| 859 | strm->adler = state->check = |
| 860 | UPDATE(state->check, put - out, out); |
| 861 | out = left; |
| 862 | if (( |
| 863 | #ifdef GUNZIP |
| 864 | state->flags ? hold : |
| 865 | #endif |
| 866 | REVERSE(hold)) != state->check) { |
| 867 | strm->msg = (char *)"incorrect data check"; |
| 868 | state->mode = BAD; |
| 869 | break; |
| 870 | } |
| 871 | INITBITS(); |
| 872 | Tracev((stderr, "inflate: check matches trailer\n")); |
| 873 | } |
| 874 | #ifdef GUNZIP |
| 875 | state->mode = LENGTH; |
| 876 | case LENGTH: |
| 877 | if (state->wrap && state->flags) { |
| 878 | NEEDBITS(32); |
| 879 | if (hold != (state->total & 0xffffffffUL)) { |
| 880 | strm->msg = (char *)"incorrect length check"; |
| 881 | state->mode = BAD; |
| 882 | break; |
| 883 | } |
| 884 | INITBITS(); |
| 885 | Tracev((stderr, "inflate: length matches trailer\n")); |
| 886 | } |
| 887 | #endif |
| 888 | state->mode = DONE; |
| 889 | case DONE: |
| 890 | ret = Z_STREAM_END; |
| 891 | goto inf_leave; |
| 892 | case BAD: |
| 893 | ret = Z_DATA_ERROR; |
| 894 | goto inf_leave; |
| 895 | case MEM: |
| 896 | return Z_MEM_ERROR; |
| 897 | case SYNC: |
| 898 | default: |
| 899 | return Z_STREAM_ERROR; |
| 900 | } |
| 901 | |
| 902 | /* |
| 903 | Return from inflate(), updating the total counts and the check value. |
| 904 | If there was no progress during the inflate() call, return a buffer |
| 905 | error. Call updatewindow() to create and/or update the window state. |
| 906 | Note: a memory error from inflate() is non-recoverable. |
| 907 | */ |
| 908 | inf_leave: |
| 909 | RESTORE(); |
| 910 | if (state->wsize || (state->mode < CHECK && out != strm->avail_out)) |
| 911 | if (updatewindow(strm, out)) { |
| 912 | state->mode = MEM; |
| 913 | return Z_MEM_ERROR; |
| 914 | } |
| 915 | in -= strm->avail_in; |
| 916 | out -= strm->avail_out; |
| 917 | strm->total_in += in; |
| 918 | strm->total_out += out; |
| 919 | state->total += out; |
| 920 | if (state->wrap && out) |
| 921 | strm->adler = state->check = |
| 922 | UPDATE(state->check, strm->next_out - out, out); |
| 923 | strm->data_type = state->bits + (state->last ? 64 : 0) + |
| 924 | (state->mode == TYPE ? 128 : 0); |
| 925 | if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK) |
| 926 | ret = Z_BUF_ERROR; |
| 927 | return ret; |
| 928 | } |
| 929 | |
Kim Phillips | 8eb6994 | 2012-10-29 13:34:35 +0000 | [diff] [blame] | 930 | int ZEXPORT inflateEnd(z_streamp strm) |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 931 | { |
| 932 | struct inflate_state FAR *state; |
| 933 | if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0) |
| 934 | return Z_STREAM_ERROR; |
| 935 | state = (struct inflate_state FAR *)strm->state; |
| 936 | if (state->window != Z_NULL) { |
Stefan Roese | 80877fa | 2022-09-02 14:10:46 +0200 | [diff] [blame] | 937 | schedule(); |
Mike Frysinger | 99596bd | 2011-04-08 12:23:30 +0000 | [diff] [blame] | 938 | ZFREE(strm, state->window); |
| 939 | } |
| 940 | ZFREE(strm, strm->state); |
| 941 | strm->state = Z_NULL; |
| 942 | Tracev((stderr, "inflate: end\n")); |
| 943 | return Z_OK; |
| 944 | } |