9 local int gz_load OF((gz_statep,
unsigned char *,
unsigned,
unsigned *));
20 local int gz_load(gz_statep state,
unsigned char *buf,
unsigned len,
unsigned *have)
26 ret = read(state->fd, buf + *have, len - *have);
30 }
while (*have < len);
32 gz_error(state, Z_ERRNO, zstrerror());
50 z_streamp strm = &(state->strm);
52 if (state->err != Z_OK && state->err != Z_BUF_ERROR)
54 if (state->eof == 0) {
56 unsigned char *p = state->in;
57 unsigned const char *q = strm->next_in;
58 unsigned n = strm->avail_in;
63 if (
gz_load(state, state->in + strm->avail_in,
64 state->size - strm->avail_in, &got) == -1)
66 strm->avail_in += got;
67 strm->next_in = state->in;
83 z_streamp strm = &(state->strm);
86 if (state->size == 0) {
88 state->in = (
unsigned char *)malloc(state->want);
89 state->out = (
unsigned char *)malloc(state->want << 1);
90 if (state->in == NULL || state->out == NULL) {
91 if (state->out != NULL)
93 if (state->in != NULL)
95 gz_error(state, Z_MEM_ERROR,
"out of memory");
98 state->size = state->want;
101 state->strm.zalloc = Z_NULL;
102 state->strm.zfree = Z_NULL;
103 state->strm.opaque = Z_NULL;
104 state->strm.avail_in = 0;
105 state->strm.next_in = Z_NULL;
106 if (inflateInit2(&(state->strm), 15 + 16) != Z_OK) {
110 gz_error(state, Z_MEM_ERROR,
"out of memory");
116 if (strm->avail_in < 2) {
119 if (strm->avail_in == 0)
130 if (strm->avail_in > 1 &&
131 strm->next_in[0] == 31 && strm->next_in[1] == 139) {
140 if (state->direct == 0) {
150 state->x.next = state->out;
151 if (strm->avail_in) {
152 memcpy(state->x.next, strm->next_in, strm->avail_in);
153 state->x.have = strm->avail_in;
170 z_streamp strm = &(state->strm);
173 had = strm->avail_out;
176 if (strm->avail_in == 0 &&
gz_avail(state) == -1)
178 if (strm->avail_in == 0) {
179 gz_error(state, Z_BUF_ERROR,
"unexpected end of file");
184 ret =
inflate(strm, Z_NO_FLUSH);
185 if (ret == Z_STREAM_ERROR || ret == Z_NEED_DICT) {
187 "internal error: inflate stream corrupt");
190 if (ret == Z_MEM_ERROR) {
191 gz_error(state, Z_MEM_ERROR,
"out of memory");
194 if (ret == Z_DATA_ERROR) {
196 strm->msg == NULL ?
"compressed data error" : strm->msg);
199 }
while (strm->avail_out && ret != Z_STREAM_END);
202 state->x.have = had - strm->avail_out;
203 state->x.next = strm->next_out - state->x.have;
206 if (ret == Z_STREAM_END)
221 z_streamp strm = &(state->strm);
228 if (state->how == LOOK)
232 if (
gz_load(state, state->out, state->size << 1, &(state->x.have))
235 state->x.next = state->out;
238 strm->avail_out = state->size << 1;
239 strm->next_out = state->out;
243 }
while (state->x.have == 0 && (!state->eof || strm->avail_in));
256 n = GT_OFF(state->x.have) || (z_off64_t)state->x.have > len ?
257 (
unsigned)len : state->x.have;
265 else if (state->eof && state->strm.avail_in == 0)
278 int ZEXPORT
gzread(gzFile file, voidp buf,
unsigned len)
287 state = (gz_statep)file;
288 strm = &(state->strm);
291 if (state->mode != GZ_READ ||
292 (state->err != Z_OK && state->err != Z_BUF_ERROR))
298 gz_error(state, Z_DATA_ERROR,
"requested length does not fit in int");
309 if (
gz_skip(state, state->skip) == -1)
318 n = state->x.have > len ? len : state->x.have;
319 memcpy(buf, state->x.next, n);
325 else if (state->eof && strm->avail_in == 0) {
332 else if (state->how == LOOK || len < (state->size << 1)) {
342 else if (state->how == COPY) {
343 if (
gz_load(state, (
unsigned char *)buf, len, &n) == -1)
349 strm->avail_out = len;
350 strm->next_out = (
unsigned char *)buf;
359 buf = (
char *)buf + n;
377 unsigned char buf[1];
383 state = (gz_statep)file;
386 if (state->mode != GZ_READ ||
387 (state->err != Z_OK && state->err != Z_BUF_ERROR))
394 return *(state->x.next)++;
398 ret =
gzread(file, buf, 1);
399 return ret < 1 ? -1 : buf[0];
415 state = (gz_statep)file;
418 if (state->mode != GZ_READ ||
419 (state->err != Z_OK && state->err != Z_BUF_ERROR))
425 if (
gz_skip(state, state->skip) == -1)
434 if (state->x.have == 0) {
436 state->x.next = state->out + (state->size << 1) - 1;
437 state->x.next[0] = c;
444 if (state->x.have == (state->size << 1)) {
445 gz_error(state, Z_DATA_ERROR,
"out of room to push characters");
450 if (state->x.next == state->out) {
451 unsigned char *src = state->out + state->x.have;
452 unsigned char *dest = state->out + (state->size << 1);
453 while (src > state->out)
455 state->x.next = dest;
459 state->x.next[0] = c;
466 char * ZEXPORT
gzgets(gzFile file,
char *buf,
int len)
474 if (file == NULL || buf == NULL || len < 1)
476 state = (gz_statep)file;
479 if (state->mode != GZ_READ ||
480 (state->err != Z_OK && state->err != Z_BUF_ERROR))
486 if (
gz_skip(state, state->skip) == -1)
494 left = (unsigned)len - 1;
497 if (state->x.have == 0 &&
gz_fetch(state) == -1)
499 if (state->x.have == 0) {
505 n = state->x.have > left ? left : state->x.have;
506 eol = (
unsigned char *)memchr(state->x.next,
'\n', n);
508 n = (unsigned)(eol - state->x.next) + 1;
511 memcpy(buf, state->x.next, n);
517 }
while (left && eol == NULL);
534 state = (gz_statep)file;
538 if (state->mode == GZ_READ && state->how == LOOK && state->x.have == 0)
542 return state->direct;
553 return Z_STREAM_ERROR;
554 state = (gz_statep)file;
557 if (state->mode != GZ_READ)
558 return Z_STREAM_ERROR;
566 err = state->err == Z_BUF_ERROR ? Z_BUF_ERROR : Z_OK;
569 ret = close(state->fd);
571 return ret ? Z_ERRNO : err;
int ZEXPORT gzgetc(gzFile file)
void ZLIB_INTERNAL gz_error(gz_statep state, int err, const char *msg)
int ZEXPORT gzungetc(int c, gzFile file)
local int gz_decomp(gz_statep state)
local int gz_fetch(gz_statep state)
local int gz_load(gz_statep state, unsigned char *buf, unsigned len, unsigned *have)
local int gz_look(gz_statep state)
char *ZEXPORT gzgets(gzFile file, char *buf, int len)
int ZEXPORT gzdirect(gzFile file)
int ZEXPORT gzread(gzFile file, voidp buf, unsigned len)
int ZEXPORT gzgetc_(gzFile file)
local int gz_skip(gz_statep state, z_off64_t len)
int ZEXPORT inflateEnd(z_streamp strm)
int ZEXPORT gzclose_r(gzFile file)
int ZEXPORT inflate(z_streamp strm, int flush)
local int gz_load OF((gz_statep, unsigned char *, unsigned, unsigned *))
local int gz_avail(gz_statep state)
int ZEXPORT inflateReset(z_streamp strm)