-- see zlib.h -- */
| 436 | |
| 437 | /* -- see zlib.h -- */ |
| 438 | int ZEXPORT gzungetc(int c, gzFile file) { |
| 439 | gz_statep state; |
| 440 | |
| 441 | /* get internal structure */ |
| 442 | if (file == NULL) |
| 443 | return -1; |
| 444 | state = (gz_statep)file; |
| 445 | |
| 446 | /* in case this was just opened, set up the input buffer */ |
| 447 | if (state->mode == GZ_READ && state->how == LOOK && state->x.have == 0) |
| 448 | (void)gz_look(state); |
| 449 | |
| 450 | /* check that we're reading and that there's no (serious) error */ |
| 451 | if (state->mode != GZ_READ || |
| 452 | (state->err != Z_OK && state->err != Z_BUF_ERROR)) |
| 453 | return -1; |
| 454 | |
| 455 | /* process a skip request */ |
| 456 | if (state->seek) { |
| 457 | state->seek = 0; |
| 458 | if (gz_skip(state, state->skip) == -1) |
| 459 | return -1; |
| 460 | } |
| 461 | |
| 462 | /* can't push EOF */ |
| 463 | if (c < 0) |
| 464 | return -1; |
| 465 | |
| 466 | /* if output buffer empty, put byte at end (allows more pushing) */ |
| 467 | if (state->x.have == 0) { |
| 468 | state->x.have = 1; |
| 469 | state->x.next = state->out + (state->size << 1) - 1; |
| 470 | state->x.next[0] = (unsigned char)c; |
| 471 | state->x.pos--; |
| 472 | state->past = 0; |
| 473 | return c; |
| 474 | } |
| 475 | |
| 476 | /* if no room, give up (must have already done a gzungetc()) */ |
| 477 | if (state->x.have == (state->size << 1)) { |
| 478 | gz_error(state, Z_DATA_ERROR, "out of room to push characters"); |
| 479 | return -1; |
| 480 | } |
| 481 | |
| 482 | /* slide output data if needed and insert byte before existing data */ |
| 483 | if (state->x.next == state->out) { |
| 484 | unsigned char *src = state->out + state->x.have; |
| 485 | unsigned char *dest = state->out + (state->size << 1); |
| 486 | while (src > state->out) |
| 487 | *--dest = *--src; |
| 488 | state->x.next = dest; |
| 489 | } |
| 490 | state->x.have++; |
| 491 | state->x.next--; |
| 492 | state->x.next[0] = (unsigned char)c; |
| 493 | state->x.pos--; |
| 494 | state->past = 0; |
| 495 | return c; |