Update the window with the last wsize (normally 32K) bytes written before returning. If window does not exist yet, create it. This is only called when a window is already in use, or when output has been written during this inflate call, but the end of the deflate stream has not been reached yet. It is also called to create a window for dictionary data when a dictionary is loaded.
| 362 | The advantage may be dependent on the size of the processor's data caches. |
| 363 | */ |
| 364 | local int updatewindow(z_streamp strm, const Bytef *end, unsigned copy) |
| 365 | { |
| 366 | struct inflate_state FAR *state; |
| 367 | unsigned dist; |
| 368 | |
| 369 | state = (struct inflate_state FAR *)strm->state; |
| 370 | |
| 371 | /* if it hasn't been done already, allocate space for the window */ |
| 372 | if (state->window == Z_NULL) { |
| 373 | state->window = (unsigned char FAR *) |
| 374 | ZALLOC(strm, 1U << state->wbits, |
| 375 | sizeof(unsigned char)); |
| 376 | if (state->window == Z_NULL) return 1; |
| 377 | } |
| 378 | |
| 379 | /* if window not in use yet, initialize */ |
| 380 | if (state->wsize == 0) { |
| 381 | state->wsize = 1U << state->wbits; |
| 382 | state->wnext = 0; |
| 383 | state->whave = 0; |
| 384 | } |
| 385 | |
| 386 | /* copy state->wsize or less output bytes into the circular window */ |
| 387 | if (copy >= state->wsize) { |
| 388 | zmemcpy(state->window, end - state->wsize, state->wsize); |
| 389 | state->wnext = 0; |
| 390 | state->whave = state->wsize; |
| 391 | } |
| 392 | else { |
| 393 | dist = state->wsize - state->wnext; |
| 394 | if (dist > copy) dist = copy; |
| 395 | zmemcpy(state->window + state->wnext, end - copy, dist); |
| 396 | copy -= dist; |
| 397 | if (copy) { |
| 398 | zmemcpy(state->window, end - copy, copy); |
| 399 | state->wnext = copy; |
| 400 | state->whave = state->wsize; |
| 401 | } |
| 402 | else { |
| 403 | state->wnext += dist; |
| 404 | if (state->wnext == state->wsize) state->wnext = 0; |
| 405 | if (state->whave < state->wsize) state->whave += dist; |
| 406 | } |
| 407 | } |
| 408 | return 0; |
| 409 | } |
| 410 | |
| 411 | /* Macros for inflate(): */ |
| 412 |