gzread.c, gzwrite.c: gz_statep is union

This commit is contained in:
Przemyslaw Skibinski 2016-11-29 16:05:52 +01:00
parent a1f60632cb
commit 0feb24a2fa
2 changed files with 247 additions and 247 deletions

View File

@ -27,7 +27,7 @@ local int gz_load(state, buf, len, have)
*have = 0; *have = 0;
do { do {
ret = read(state->fd, buf + *have, len - *have); ret = read(state.state->fd, buf + *have, len - *have);
if (ret <= 0) if (ret <= 0)
break; break;
*have += ret; *have += ret;
@ -37,7 +37,7 @@ local int gz_load(state, buf, len, have)
return -1; return -1;
} }
if (ret == 0) if (ret == 0)
state->eof = 1; state.state->eof = 1;
return 0; return 0;
} }
@ -52,24 +52,24 @@ local int gz_avail(state)
gz_statep state; gz_statep state;
{ {
unsigned got; unsigned got;
z_streamp strm = &(state->strm); z_streamp strm = &(state.state->strm);
if (state->err != Z_OK && state->err != Z_BUF_ERROR) if (state.state->err != Z_OK && state.state->err != Z_BUF_ERROR)
return -1; return -1;
if (state->eof == 0) { if (state.state->eof == 0) {
if (strm->avail_in) { /* copy what's there to the start */ if (strm->avail_in) { /* copy what's there to the start */
unsigned char *p = state->in; unsigned char *p = state.state->in;
unsigned const char *q = strm->next_in; unsigned const char *q = strm->next_in;
unsigned n = strm->avail_in; unsigned n = strm->avail_in;
do { do {
*p++ = *q++; *p++ = *q++;
} while (--n); } while (--n);
} }
if (gz_load(state, state->in + strm->avail_in, if (gz_load(state, state.state->in + strm->avail_in,
state->size - strm->avail_in, &got) == -1) state.state->size - strm->avail_in, &got) == -1)
return -1; return -1;
strm->avail_in += got; strm->avail_in += got;
strm->next_in = state->in; strm->next_in = state.state->in;
} }
return 0; return 0;
} }
@ -86,33 +86,33 @@ local int gz_avail(state)
local int gz_look(state) local int gz_look(state)
gz_statep state; gz_statep state;
{ {
z_streamp strm = &(state->strm); z_streamp strm = &(state.state->strm);
/* allocate read buffers and inflate memory */ /* allocate read buffers and inflate memory */
if (state->size == 0) { if (state.state->size == 0) {
/* allocate buffers */ /* allocate buffers */
state->in = (unsigned char *)malloc(state->want); state.state->in = (unsigned char *)malloc(state.state->want);
state->out = (unsigned char *)malloc(state->want << 1); state.state->out = (unsigned char *)malloc(state.state->want << 1);
if (state->in == NULL || state->out == NULL) { if (state.state->in == NULL || state.state->out == NULL) {
if (state->out != NULL) if (state.state->out != NULL)
free(state->out); free(state.state->out);
if (state->in != NULL) if (state.state->in != NULL)
free(state->in); free(state.state->in);
gz_error(state, Z_MEM_ERROR, "out of memory"); gz_error(state, Z_MEM_ERROR, "out of memory");
return -1; return -1;
} }
state->size = state->want; state.state->size = state.state->want;
/* allocate inflate memory */ /* allocate inflate memory */
state->strm.zalloc = Z_NULL; state.state->strm.zalloc = Z_NULL;
state->strm.zfree = Z_NULL; state.state->strm.zfree = Z_NULL;
state->strm.opaque = Z_NULL; state.state->strm.opaque = Z_NULL;
state->strm.avail_in = 0; state.state->strm.avail_in = 0;
state->strm.next_in = Z_NULL; state.state->strm.next_in = Z_NULL;
if (inflateInit2(&(state->strm), 15 + 16) != Z_OK) { /* gunzip */ if (inflateInit2(&(state.state->strm), 15 + 16) != Z_OK) { /* gunzip */
free(state->out); free(state.state->out);
free(state->in); free(state.state->in);
state->size = 0; state.state->size = 0;
gz_error(state, Z_MEM_ERROR, "out of memory"); gz_error(state, Z_MEM_ERROR, "out of memory");
return -1; return -1;
} }
@ -138,31 +138,31 @@ local int gz_look(state)
((strm->next_in[0] == 31 && strm->next_in[1] == 139) ((strm->next_in[0] == 31 && strm->next_in[1] == 139)
|| (strm->next_in[0] == 40 && strm->next_in[1] == 181))) { // zstd || (strm->next_in[0] == 40 && strm->next_in[1] == 181))) { // zstd
inflateReset(strm); inflateReset(strm);
state->how = GZIP; state.state->how = GZIP;
state->direct = 0; state.state->direct = 0;
return 0; return 0;
} }
/* no gzip header -- if we were decoding gzip before, then this is trailing /* no gzip header -- if we were decoding gzip before, then this is trailing
garbage. Ignore the trailing garbage and finish. */ garbage. Ignore the trailing garbage and finish. */
if (state->direct == 0) { if (state.state->direct == 0) {
strm->avail_in = 0; strm->avail_in = 0;
state->eof = 1; state.state->eof = 1;
state->x.have = 0; state.state->x.have = 0;
return 0; return 0;
} }
/* doing raw i/o, copy any leftover input to output -- this assumes that /* doing raw i/o, copy any leftover input to output -- this assumes that
the output buffer is larger than the input buffer, which also assures the output buffer is larger than the input buffer, which also assures
space for gzungetc() */ space for gzungetc() */
state->x.next = state->out; state.state->x.next = state.state->out;
if (strm->avail_in) { if (strm->avail_in) {
memcpy(state->x.next, strm->next_in, strm->avail_in); memcpy(state.state->x.next, strm->next_in, strm->avail_in);
state->x.have = strm->avail_in; state.state->x.have = strm->avail_in;
strm->avail_in = 0; strm->avail_in = 0;
} }
state->how = COPY; state.state->how = COPY;
state->direct = 1; state.state->direct = 1;
return 0; return 0;
} }
@ -176,7 +176,7 @@ local int gz_decomp(state)
{ {
int ret = Z_OK; int ret = Z_OK;
unsigned had; unsigned had;
z_streamp strm = &(state->strm); z_streamp strm = &(state.state->strm);
/* fill output buffer up to end of deflate stream */ /* fill output buffer up to end of deflate stream */
had = strm->avail_out; had = strm->avail_out;
@ -208,12 +208,12 @@ local int gz_decomp(state)
} while (strm->avail_out && ret != Z_STREAM_END); } while (strm->avail_out && ret != Z_STREAM_END);
/* update available output */ /* update available output */
state->x.have = had - strm->avail_out; state.state->x.have = had - strm->avail_out;
state->x.next = strm->next_out - state->x.have; state.state->x.next = strm->next_out - state.state->x.have;
/* if the gzip stream completed successfully, look for another */ /* if the gzip stream completed successfully, look for another */
if (ret == Z_STREAM_END) if (ret == Z_STREAM_END)
state->how = LOOK; state.state->how = LOOK;
/* good decompression */ /* good decompression */
return 0; return 0;
@ -228,29 +228,29 @@ local int gz_decomp(state)
local int gz_fetch(state) local int gz_fetch(state)
gz_statep state; gz_statep state;
{ {
z_streamp strm = &(state->strm); z_streamp strm = &(state.state->strm);
do { do {
switch(state->how) { switch(state.state->how) {
case LOOK: /* -> LOOK, COPY (only if never GZIP), or GZIP */ case LOOK: /* -> LOOK, COPY (only if never GZIP), or GZIP */
if (gz_look(state) == -1) if (gz_look(state) == -1)
return -1; return -1;
if (state->how == LOOK) if (state.state->how == LOOK)
return 0; return 0;
break; break;
case COPY: /* -> COPY */ case COPY: /* -> COPY */
if (gz_load(state, state->out, state->size << 1, &(state->x.have)) if (gz_load(state, state.state->out, state.state->size << 1, &(state.state->x.have))
== -1) == -1)
return -1; return -1;
state->x.next = state->out; state.state->x.next = state.state->out;
return 0; return 0;
case GZIP: /* -> GZIP or LOOK (if end of gzip stream) */ case GZIP: /* -> GZIP or LOOK (if end of gzip stream) */
strm->avail_out = state->size << 1; strm->avail_out = state.state->size << 1;
strm->next_out = state->out; strm->next_out = state.state->out;
if (gz_decomp(state) == -1) if (gz_decomp(state) == -1)
return -1; return -1;
} }
} while (state->x.have == 0 && (!state->eof || strm->avail_in)); } while (state.state->x.have == 0 && (!state.state->eof || strm->avail_in));
return 0; return 0;
} }
@ -264,17 +264,17 @@ local int gz_skip(state, len)
/* skip over len bytes or reach end-of-file, whichever comes first */ /* skip over len bytes or reach end-of-file, whichever comes first */
while (len) while (len)
/* skip over whatever is in output buffer */ /* skip over whatever is in output buffer */
if (state->x.have) { if (state.state->x.have) {
n = GT_OFF(state->x.have) || (z_off64_t)state->x.have > len ? n = GT_OFF(state.state->x.have) || (z_off64_t)state.state->x.have > len ?
(unsigned)len : state->x.have; (unsigned)len : state.state->x.have;
state->x.have -= n; state.state->x.have -= n;
state->x.next += n; state.state->x.next += n;
state->x.pos += n; state.state->x.pos += n;
len -= n; len -= n;
} }
/* output buffer empty -- return if we're at the end of the input */ /* output buffer empty -- return if we're at the end of the input */
else if (state->eof && state->strm.avail_in == 0) else if (state.state->eof && state.state->strm.avail_in == 0)
break; break;
/* need more data to skip -- load up output buffer */ /* need more data to skip -- load up output buffer */
@ -300,11 +300,11 @@ int ZEXPORT gzread(file, buf, len)
if (file == NULL) if (file == NULL)
return -1; return -1;
state = (gz_statep)file; state = (gz_statep)file;
strm = &(state->strm); strm = &(state.state->strm);
/* check that we're reading and that there's no (serious) error */ /* check that we're reading and that there's no (serious) error */
if (state->mode != GZ_READ || if (state.state->mode != GZ_READ ||
(state->err != Z_OK && state->err != Z_BUF_ERROR)) (state.state->err != Z_OK && state.state->err != Z_BUF_ERROR))
return -1; return -1;
/* since an int is returned, make sure len fits in one, otherwise return /* since an int is returned, make sure len fits in one, otherwise return
@ -319,9 +319,9 @@ int ZEXPORT gzread(file, buf, len)
return 0; return 0;
/* process a skip request */ /* process a skip request */
if (state->seek) { if (state.state->seek) {
state->seek = 0; state.state->seek = 0;
if (gz_skip(state, state->skip) == -1) if (gz_skip(state, state.state->skip) == -1)
return -1; return -1;
} }
@ -329,22 +329,22 @@ int ZEXPORT gzread(file, buf, len)
got = 0; got = 0;
do { do {
/* first just try copying data from the output buffer */ /* first just try copying data from the output buffer */
if (state->x.have) { if (state.state->x.have) {
n = state->x.have > len ? len : state->x.have; n = state.state->x.have > len ? len : state.state->x.have;
memcpy(buf, state->x.next, n); memcpy(buf, state.state->x.next, n);
state->x.next += n; state.state->x.next += n;
state->x.have -= n; state.state->x.have -= n;
} }
/* output buffer empty -- return if we're at the end of the input */ /* output buffer empty -- return if we're at the end of the input */
else if (state->eof && strm->avail_in == 0) { else if (state.state->eof && strm->avail_in == 0) {
state->past = 1; /* tried to read past end */ state.state->past = 1; /* tried to read past end */
break; break;
} }
/* need output data -- for small len or new stream load up our output /* need output data -- for small len or new stream load up our output
buffer */ buffer */
else if (state->how == LOOK || len < (state->size << 1)) { else if (state.state->how == LOOK || len < (state.state->size << 1)) {
/* get more output, looking for header if required */ /* get more output, looking for header if required */
if (gz_fetch(state) == -1) if (gz_fetch(state) == -1)
return -1; return -1;
@ -354,7 +354,7 @@ int ZEXPORT gzread(file, buf, len)
} }
/* large len -- read directly into user buffer */ /* large len -- read directly into user buffer */
else if (state->how == COPY) { /* read directly */ else if (state.state->how == COPY) { /* read directly */
if (gz_load(state, (unsigned char *)buf, len, &n) == -1) if (gz_load(state, (unsigned char *)buf, len, &n) == -1)
return -1; return -1;
} }
@ -365,15 +365,15 @@ int ZEXPORT gzread(file, buf, len)
strm->next_out = (unsigned char *)buf; strm->next_out = (unsigned char *)buf;
if (gz_decomp(state) == -1) if (gz_decomp(state) == -1)
return -1; return -1;
n = state->x.have; n = state.state->x.have;
state->x.have = 0; state.state->x.have = 0;
} }
/* update progress */ /* update progress */
len -= n; len -= n;
buf = (char *)buf + n; buf = (char *)buf + n;
got += n; got += n;
state->x.pos += n; state.state->x.pos += n;
} while (len); } while (len);
/* return number of bytes read into user buffer (will fit in int) */ /* return number of bytes read into user buffer (will fit in int) */
@ -412,15 +412,15 @@ int ZEXPORT gzgetc(file)
state = (gz_statep)file; state = (gz_statep)file;
/* check that we're reading and that there's no (serious) error */ /* check that we're reading and that there's no (serious) error */
if (state->mode != GZ_READ || if (state.state->mode != GZ_READ ||
(state->err != Z_OK && state->err != Z_BUF_ERROR)) (state.state->err != Z_OK && state.state->err != Z_BUF_ERROR))
return -1; return -1;
/* try output buffer (no need to check for skip request) */ /* try output buffer (no need to check for skip request) */
if (state->x.have) { if (state.state->x.have) {
state->x.have--; state.state->x.have--;
state->x.pos++; state.state->x.pos++;
return *(state->x.next)++; return *(state.state->x.next)++;
} }
/* nothing there -- try gzread() */ /* nothing there -- try gzread() */
@ -447,14 +447,14 @@ int ZEXPORT gzungetc(c, file)
state = (gz_statep)file; state = (gz_statep)file;
/* check that we're reading and that there's no (serious) error */ /* check that we're reading and that there's no (serious) error */
if (state->mode != GZ_READ || if (state.state->mode != GZ_READ ||
(state->err != Z_OK && state->err != Z_BUF_ERROR)) (state.state->err != Z_OK && state.state->err != Z_BUF_ERROR))
return -1; return -1;
/* process a skip request */ /* process a skip request */
if (state->seek) { if (state.state->seek) {
state->seek = 0; state.state->seek = 0;
if (gz_skip(state, state->skip) == -1) if (gz_skip(state, state.state->skip) == -1)
return -1; return -1;
} }
@ -463,34 +463,34 @@ int ZEXPORT gzungetc(c, file)
return -1; return -1;
/* if output buffer empty, put byte at end (allows more pushing) */ /* if output buffer empty, put byte at end (allows more pushing) */
if (state->x.have == 0) { if (state.state->x.have == 0) {
state->x.have = 1; state.state->x.have = 1;
state->x.next = state->out + (state->size << 1) - 1; state.state->x.next = state.state->out + (state.state->size << 1) - 1;
state->x.next[0] = c; state.state->x.next[0] = c;
state->x.pos--; state.state->x.pos--;
state->past = 0; state.state->past = 0;
return c; return c;
} }
/* if no room, give up (must have already done a gzungetc()) */ /* if no room, give up (must have already done a gzungetc()) */
if (state->x.have == (state->size << 1)) { if (state.state->x.have == (state.state->size << 1)) {
gz_error(state, Z_DATA_ERROR, "out of room to push characters"); gz_error(state, Z_DATA_ERROR, "out of room to push characters");
return -1; return -1;
} }
/* slide output data if needed and insert byte before existing data */ /* slide output data if needed and insert byte before existing data */
if (state->x.next == state->out) { if (state.state->x.next == state.state->out) {
unsigned char *src = state->out + state->x.have; unsigned char *src = state.state->out + state.state->x.have;
unsigned char *dest = state->out + (state->size << 1); unsigned char *dest = state.state->out + (state.state->size << 1);
while (src > state->out) while (src > state.state->out)
*--dest = *--src; *--dest = *--src;
state->x.next = dest; state.state->x.next = dest;
} }
state->x.have++; state.state->x.have++;
state->x.next--; state.state->x.next--;
state->x.next[0] = c; state.state->x.next[0] = c;
state->x.pos--; state.state->x.pos--;
state->past = 0; state.state->past = 0;
return c; return c;
} }
@ -511,14 +511,14 @@ char * ZEXPORT gzgets(file, buf, len)
state = (gz_statep)file; state = (gz_statep)file;
/* check that we're reading and that there's no (serious) error */ /* check that we're reading and that there's no (serious) error */
if (state->mode != GZ_READ || if (state.state->mode != GZ_READ ||
(state->err != Z_OK && state->err != Z_BUF_ERROR)) (state.state->err != Z_OK && state.state->err != Z_BUF_ERROR))
return NULL; return NULL;
/* process a skip request */ /* process a skip request */
if (state->seek) { if (state.state->seek) {
state->seek = 0; state.state->seek = 0;
if (gz_skip(state, state->skip) == -1) if (gz_skip(state, state.state->skip) == -1)
return NULL; return NULL;
} }
@ -529,24 +529,24 @@ char * ZEXPORT gzgets(file, buf, len)
left = (unsigned)len - 1; left = (unsigned)len - 1;
if (left) do { if (left) do {
/* assure that something is in the output buffer */ /* assure that something is in the output buffer */
if (state->x.have == 0 && gz_fetch(state) == -1) if (state.state->x.have == 0 && gz_fetch(state) == -1)
return NULL; /* error */ return NULL; /* error */
if (state->x.have == 0) { /* end of file */ if (state.state->x.have == 0) { /* end of file */
state->past = 1; /* read past end */ state.state->past = 1; /* read past end */
break; /* return what we have */ break; /* return what we have */
} }
/* look for end-of-line in current output buffer */ /* look for end-of-line in current output buffer */
n = state->x.have > left ? left : state->x.have; n = state.state->x.have > left ? left : state.state->x.have;
eol = (unsigned char *)memchr(state->x.next, '\n', n); eol = (unsigned char *)memchr(state.state->x.next, '\n', n);
if (eol != NULL) if (eol != NULL)
n = (unsigned)(eol - state->x.next) + 1; n = (unsigned)(eol - state.state->x.next) + 1;
/* copy through end-of-line, or remainder if not found */ /* copy through end-of-line, or remainder if not found */
memcpy(buf, state->x.next, n); memcpy(buf, state.state->x.next, n);
state->x.have -= n; state.state->x.have -= n;
state->x.next += n; state.state->x.next += n;
state->x.pos += n; state.state->x.pos += n;
left -= n; left -= n;
buf += n; buf += n;
} while (left && eol == NULL); } while (left && eol == NULL);
@ -571,11 +571,11 @@ int ZEXPORT gzdirect(file)
/* if the state is not known, but we can find out, then do so (this is /* if the state is not known, but we can find out, then do so (this is
mainly for right after a gzopen() or gzdopen()) */ mainly for right after a gzopen() or gzdopen()) */
if (state->mode == GZ_READ && state->how == LOOK && state->x.have == 0) if (state.state->mode == GZ_READ && state.state->how == LOOK && state.state->x.have == 0)
(void)gz_look(state); (void)gz_look(state);
/* return 1 if transparent, 0 if processing a gzip stream */ /* return 1 if transparent, 0 if processing a gzip stream */
return state->direct; return state.state->direct;
} }
/* -- see zlib.h -- */ /* -- see zlib.h -- */
@ -591,19 +591,19 @@ int ZEXPORT gzclose_r(file)
state = (gz_statep)file; state = (gz_statep)file;
/* check that we're reading */ /* check that we're reading */
if (state->mode != GZ_READ) if (state.state->mode != GZ_READ)
return Z_STREAM_ERROR; return Z_STREAM_ERROR;
/* free memory and close file */ /* free memory and close file */
if (state->size) { if (state.state->size) {
inflateEnd(&(state->strm)); inflateEnd(&(state.state->strm));
free(state->out); free(state.state->out);
free(state->in); free(state.state->in);
} }
err = state->err == Z_BUF_ERROR ? Z_BUF_ERROR : Z_OK; err = state.state->err == Z_BUF_ERROR ? Z_BUF_ERROR : Z_OK;
gz_error(state, Z_OK, NULL); gz_error(state, Z_OK, NULL);
free(state->path); free(state.state->path);
ret = close(state->fd); ret = close(state.state->fd);
free(state); free(state.state);
return ret ? Z_ERRNO : err; return ret ? Z_ERRNO : err;
} }

View File

@ -16,21 +16,21 @@ local int gz_init(state)
gz_statep state; gz_statep state;
{ {
int ret; int ret;
z_streamp strm = &(state->strm); z_streamp strm = &(state.state->strm);
/* allocate input buffer */ /* allocate input buffer */
state->in = (unsigned char *)malloc(state->want); state.state->in = (unsigned char *)malloc(state.state->want);
if (state->in == NULL) { if (state.state->in == NULL) {
gz_error(state, Z_MEM_ERROR, "out of memory"); gz_error(state, Z_MEM_ERROR, "out of memory");
return -1; return -1;
} }
/* only need output buffer and deflate state if compressing */ /* only need output buffer and deflate state if compressing */
if (!state->direct) { if (!state.state->direct) {
/* allocate output buffer */ /* allocate output buffer */
state->out = (unsigned char *)malloc(state->want); state.state->out = (unsigned char *)malloc(state.state->want);
if (state->out == NULL) { if (state.state->out == NULL) {
free(state->in); free(state.state->in);
gz_error(state, Z_MEM_ERROR, "out of memory"); gz_error(state, Z_MEM_ERROR, "out of memory");
return -1; return -1;
} }
@ -39,24 +39,24 @@ local int gz_init(state)
strm->zalloc = Z_NULL; strm->zalloc = Z_NULL;
strm->zfree = Z_NULL; strm->zfree = Z_NULL;
strm->opaque = Z_NULL; strm->opaque = Z_NULL;
ret = deflateInit2(strm, state->level, Z_DEFLATED, ret = deflateInit2(strm, state.state->level, Z_DEFLATED,
MAX_WBITS + 16, DEF_MEM_LEVEL, state->strategy); MAX_WBITS + 16, DEF_MEM_LEVEL, state.state->strategy);
if (ret != Z_OK) { if (ret != Z_OK) {
free(state->out); free(state.state->out);
free(state->in); free(state.state->in);
gz_error(state, Z_MEM_ERROR, "out of memory"); gz_error(state, Z_MEM_ERROR, "out of memory");
return -1; return -1;
} }
} }
/* mark state as initialized */ /* mark state as initialized */
state->size = state->want; state.state->size = state.state->want;
/* initialize write buffer if compressing */ /* initialize write buffer if compressing */
if (!state->direct) { if (!state.state->direct) {
strm->avail_out = state->size; strm->avail_out = state.state->size;
strm->next_out = state->out; strm->next_out = state.state->out;
state->x.next = strm->next_out; state.state->x.next = strm->next_out;
} }
return 0; return 0;
} }
@ -73,15 +73,15 @@ local int gz_comp(state, flush)
{ {
int ret, got; int ret, got;
unsigned have; unsigned have;
z_streamp strm = &(state->strm); z_streamp strm = &(state.state->strm);
/* allocate memory if this is the first time through */ /* allocate memory if this is the first time through */
if (state->size == 0 && gz_init(state) == -1) if (state.state->size == 0 && gz_init(state) == -1)
return -1; return -1;
/* write directly if requested */ /* write directly if requested */
if (state->direct) { if (state.state->direct) {
got = write(state->fd, strm->next_in, strm->avail_in); got = write(state.state->fd, strm->next_in, strm->avail_in);
if (got < 0 || (unsigned)got != strm->avail_in) { if (got < 0 || (unsigned)got != strm->avail_in) {
gz_error(state, Z_ERRNO, zstrerror()); gz_error(state, Z_ERRNO, zstrerror());
return -1; return -1;
@ -97,17 +97,17 @@ local int gz_comp(state, flush)
doing Z_FINISH then don't write until we get to Z_STREAM_END */ doing Z_FINISH then don't write until we get to Z_STREAM_END */
if (strm->avail_out == 0 || (flush != Z_NO_FLUSH && if (strm->avail_out == 0 || (flush != Z_NO_FLUSH &&
(flush != Z_FINISH || ret == Z_STREAM_END))) { (flush != Z_FINISH || ret == Z_STREAM_END))) {
have = (unsigned)(strm->next_out - state->x.next); have = (unsigned)(strm->next_out - state.state->x.next);
if (have && ((got = write(state->fd, state->x.next, have)) < 0 || if (have && ((got = write(state.state->fd, state.state->x.next, have)) < 0 ||
(unsigned)got != have)) { (unsigned)got != have)) {
gz_error(state, Z_ERRNO, zstrerror()); gz_error(state, Z_ERRNO, zstrerror());
return -1; return -1;
} }
if (strm->avail_out == 0) { if (strm->avail_out == 0) {
strm->avail_out = state->size; strm->avail_out = state.state->size;
strm->next_out = state->out; strm->next_out = state.state->out;
} }
state->x.next = strm->next_out; state.state->x.next = strm->next_out;
} }
/* compress */ /* compress */
@ -136,7 +136,7 @@ local int gz_zero(state, len)
{ {
int first; int first;
unsigned n; unsigned n;
z_streamp strm = &(state->strm); z_streamp strm = &(state.state->strm);
/* consume whatever's left in the input buffer */ /* consume whatever's left in the input buffer */
if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1) if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
@ -145,15 +145,15 @@ local int gz_zero(state, len)
/* compress len zeros (len guaranteed > 0) */ /* compress len zeros (len guaranteed > 0) */
first = 1; first = 1;
while (len) { while (len) {
n = GT_OFF(state->size) || (z_off64_t)state->size > len ? n = GT_OFF(state.state->size) || (z_off64_t)state.state->size > len ?
(unsigned)len : state->size; (unsigned)len : state.state->size;
if (first) { if (first) {
memset(state->in, 0, n); memset(state.state->in, 0, n);
first = 0; first = 0;
} }
strm->avail_in = n; strm->avail_in = n;
strm->next_in = state->in; strm->next_in = state.state->in;
state->x.pos += n; state.state->x.pos += n;
if (gz_comp(state, Z_NO_FLUSH) == -1) if (gz_comp(state, Z_NO_FLUSH) == -1)
return -1; return -1;
len -= n; len -= n;
@ -175,10 +175,10 @@ int ZEXPORT gzwrite(file, buf, len)
if (file == NULL) if (file == NULL)
return 0; return 0;
state = (gz_statep)file; state = (gz_statep)file;
strm = &(state->strm); strm = &(state.state->strm);
/* check that we're writing and that there's no error */ /* check that we're writing and that there's no error */
if (state->mode != GZ_WRITE || state->err != Z_OK) if (state.state->mode != GZ_WRITE || state.state->err != Z_OK)
return 0; return 0;
/* since an int is returned, make sure len fits in one, otherwise return /* since an int is returned, make sure len fits in one, otherwise return
@ -193,31 +193,31 @@ int ZEXPORT gzwrite(file, buf, len)
return 0; return 0;
/* allocate memory if this is the first time through */ /* allocate memory if this is the first time through */
if (state->size == 0 && gz_init(state) == -1) if (state.state->size == 0 && gz_init(state) == -1)
return 0; return 0;
/* check for seek request */ /* check for seek request */
if (state->seek) { if (state.state->seek) {
state->seek = 0; state.state->seek = 0;
if (gz_zero(state, state->skip) == -1) if (gz_zero(state, state.state->skip) == -1)
return 0; return 0;
} }
/* for small len, copy to input buffer, otherwise compress directly */ /* for small len, copy to input buffer, otherwise compress directly */
if (len < state->size) { if (len < state.state->size) {
/* copy to input buffer, compress when full */ /* copy to input buffer, compress when full */
do { do {
unsigned have, copy; unsigned have, copy;
if (strm->avail_in == 0) if (strm->avail_in == 0)
strm->next_in = state->in; strm->next_in = state.state->in;
have = (unsigned)((strm->next_in + strm->avail_in) - state->in); have = (unsigned)((strm->next_in + strm->avail_in) - state.state->in);
copy = state->size - have; copy = state.state->size - have;
if (copy > len) if (copy > len)
copy = len; copy = len;
memcpy(state->in + have, buf, copy); memcpy(state.state->in + have, buf, copy);
strm->avail_in += copy; strm->avail_in += copy;
state->x.pos += copy; state.state->x.pos += copy;
buf = (const char *)buf + copy; buf = (const char *)buf + copy;
len -= copy; len -= copy;
if (len && gz_comp(state, Z_NO_FLUSH) == -1) if (len && gz_comp(state, Z_NO_FLUSH) == -1)
@ -232,7 +232,7 @@ int ZEXPORT gzwrite(file, buf, len)
/* directly compress user buffer to file */ /* directly compress user buffer to file */
strm->avail_in = len; strm->avail_in = len;
strm->next_in = (z_const Bytef *)buf; strm->next_in = (z_const Bytef *)buf;
state->x.pos += len; state.state->x.pos += len;
if (gz_comp(state, Z_NO_FLUSH) == -1) if (gz_comp(state, Z_NO_FLUSH) == -1)
return 0; return 0;
} }
@ -255,29 +255,29 @@ int ZEXPORT gzputc(file, c)
if (file == NULL) if (file == NULL)
return -1; return -1;
state = (gz_statep)file; state = (gz_statep)file;
strm = &(state->strm); strm = &(state.state->strm);
/* check that we're writing and that there's no error */ /* check that we're writing and that there's no error */
if (state->mode != GZ_WRITE || state->err != Z_OK) if (state.state->mode != GZ_WRITE || state.state->err != Z_OK)
return -1; return -1;
/* check for seek request */ /* check for seek request */
if (state->seek) { if (state.state->seek) {
state->seek = 0; state.state->seek = 0;
if (gz_zero(state, state->skip) == -1) if (gz_zero(state, state.state->skip) == -1)
return -1; return -1;
} }
/* try writing to input buffer for speed (state->size == 0 if buffer not /* try writing to input buffer for speed (state->size == 0 if buffer not
initialized) */ initialized) */
if (state->size) { if (state.state->size) {
if (strm->avail_in == 0) if (strm->avail_in == 0)
strm->next_in = state->in; strm->next_in = state.state->in;
have = (unsigned)((strm->next_in + strm->avail_in) - state->in); have = (unsigned)((strm->next_in + strm->avail_in) - state.state->in);
if (have < state->size) { if (have < state.state->size) {
state->in[have] = c; state.state->in[have] = c;
strm->avail_in++; strm->avail_in++;
state->x.pos++; state.state->x.pos++;
return c & 0xff; return c & 0xff;
} }
} }
@ -317,20 +317,20 @@ int ZEXPORTVA gzvprintf(gzFile file, const char *format, va_list va)
if (file == NULL) if (file == NULL)
return -1; return -1;
state = (gz_statep)file; state = (gz_statep)file;
strm = &(state->strm); strm = &(state.state->strm);
/* check that we're writing and that there's no error */ /* check that we're writing and that there's no error */
if (state->mode != GZ_WRITE || state->err != Z_OK) if (state.state->mode != GZ_WRITE || state.state->err != Z_OK)
return 0; return 0;
/* make sure we have some buffer space */ /* make sure we have some buffer space */
if (state->size == 0 && gz_init(state) == -1) if (state.state->size == 0 && gz_init(state) == -1)
return 0; return 0;
/* check for seek request */ /* check for seek request */
if (state->seek) { if (state.state->seek) {
state->seek = 0; state.state->seek = 0;
if (gz_zero(state, state->skip) == -1) if (gz_zero(state, state.state->skip) == -1)
return 0; return 0;
} }
@ -339,33 +339,33 @@ int ZEXPORTVA gzvprintf(gzFile file, const char *format, va_list va)
return 0; return 0;
/* do the printf() into the input buffer, put length in len */ /* do the printf() into the input buffer, put length in len */
size = (int)(state->size); size = (int)(state.state->size);
state->in[size - 1] = 0; state.state->in[size - 1] = 0;
#ifdef NO_vsnprintf #ifdef NO_vsnprintf
# ifdef HAS_vsprintf_void # ifdef HAS_vsprintf_void
(void)vsprintf((char *)(state->in), format, va); (void)vsprintf((char *)(state.state->in), format, va);
for (len = 0; len < size; len++) for (len = 0; len < size; len++)
if (state->in[len] == 0) break; if (state.state->in[len] == 0) break;
# else # else
len = vsprintf((char *)(state->in), format, va); len = vsprintf((char *)(state.state->in), format, va);
# endif # endif
#else #else
# ifdef HAS_vsnprintf_void # ifdef HAS_vsnprintf_void
(void)vsnprintf((char *)(state->in), size, format, va); (void)vsnprintf((char *)(state.state->in), size, format, va);
len = strlen((char *)(state->in)); len = strlen((char *)(state.state->in));
# else # else
len = vsnprintf((char *)(state->in), size, format, va); len = vsnprintf((char *)(state.state->in), size, format, va);
# endif # endif
#endif #endif
/* check that printf() results fit in buffer */ /* check that printf() results fit in buffer */
if (len <= 0 || len >= (int)size || state->in[size - 1] != 0) if (len <= 0 || len >= (int)size || state.state->in[size - 1] != 0)
return 0; return 0;
/* update buffer and position, defer compression until needed */ /* update buffer and position, defer compression until needed */
strm->avail_in = (unsigned)len; strm->avail_in = (unsigned)len;
strm->next_in = state->in; strm->next_in = state.state->in;
state->x.pos += len; state.state->x.pos += len;
return len; return len;
} }
@ -398,24 +398,24 @@ int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
if (file == NULL) if (file == NULL)
return -1; return -1;
state = (gz_statep)file; state = (gz_statep)file;
strm = &(state->strm); strm = &(state.state->strm);
/* check that can really pass pointer in ints */ /* check that can really pass pointer in ints */
if (sizeof(int) != sizeof(void *)) if (sizeof(int) != sizeof(void *))
return 0; return 0;
/* check that we're writing and that there's no error */ /* check that we're writing and that there's no error */
if (state->mode != GZ_WRITE || state->err != Z_OK) if (state.state->mode != GZ_WRITE || state.state->err != Z_OK)
return 0; return 0;
/* make sure we have some buffer space */ /* make sure we have some buffer space */
if (state->size == 0 && gz_init(state) == -1) if (state.state->size == 0 && gz_init(state) == -1)
return 0; return 0;
/* check for seek request */ /* check for seek request */
if (state->seek) { if (state.state->seek) {
state->seek = 0; state.state->seek = 0;
if (gz_zero(state, state->skip) == -1) if (gz_zero(state, state.state->skip) == -1)
return 0; return 0;
} }
@ -424,38 +424,38 @@ int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
return 0; return 0;
/* do the printf() into the input buffer, put length in len */ /* do the printf() into the input buffer, put length in len */
size = (int)(state->size); size = (int)(state.state->size);
state->in[size - 1] = 0; state.state->in[size - 1] = 0;
#ifdef NO_snprintf #ifdef NO_snprintf
# ifdef HAS_sprintf_void # ifdef HAS_sprintf_void
sprintf((char *)(state->in), format, a1, a2, a3, a4, a5, a6, a7, a8, sprintf((char *)(state.state->in), format, a1, a2, a3, a4, a5, a6, a7, a8,
a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20); a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
for (len = 0; len < size; len++) for (len = 0; len < size; len++)
if (state->in[len] == 0) break; if (state.state->in[len] == 0) break;
# else # else
len = sprintf((char *)(state->in), format, a1, a2, a3, a4, a5, a6, a7, a8, len = sprintf((char *)(state.state->in), format, a1, a2, a3, a4, a5, a6, a7, a8,
a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20); a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
# endif # endif
#else #else
# ifdef HAS_snprintf_void # ifdef HAS_snprintf_void
snprintf((char *)(state->in), size, format, a1, a2, a3, a4, a5, a6, a7, a8, snprintf((char *)(state.state->in), size, format, a1, a2, a3, a4, a5, a6, a7, a8,
a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20); a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
len = strlen((char *)(state->in)); len = strlen((char *)(state.state->in));
# else # else
len = snprintf((char *)(state->in), size, format, a1, a2, a3, a4, a5, a6, len = snprintf((char *)(state.state->in), size, format, a1, a2, a3, a4, a5, a6,
a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18,
a19, a20); a19, a20);
# endif # endif
#endif #endif
/* check that printf() results fit in buffer */ /* check that printf() results fit in buffer */
if (len <= 0 || len >= (int)size || state->in[size - 1] != 0) if (len <= 0 || len >= (int)size || state.state->in[size - 1] != 0)
return 0; return 0;
/* update buffer and position, defer compression until needed */ /* update buffer and position, defer compression until needed */
strm->avail_in = (unsigned)len; strm->avail_in = (unsigned)len;
strm->next_in = state->in; strm->next_in = state.state->in;
state->x.pos += len; state.state->x.pos += len;
return len; return len;
} }
@ -474,7 +474,7 @@ int ZEXPORT gzflush(file, flush)
state = (gz_statep)file; state = (gz_statep)file;
/* check that we're writing and that there's no error */ /* check that we're writing and that there's no error */
if (state->mode != GZ_WRITE || state->err != Z_OK) if (state.state->mode != GZ_WRITE || state.state->err != Z_OK)
return Z_STREAM_ERROR; return Z_STREAM_ERROR;
/* check flush parameter */ /* check flush parameter */
@ -482,15 +482,15 @@ int ZEXPORT gzflush(file, flush)
return Z_STREAM_ERROR; return Z_STREAM_ERROR;
/* check for seek request */ /* check for seek request */
if (state->seek) { if (state.state->seek) {
state->seek = 0; state.state->seek = 0;
if (gz_zero(state, state->skip) == -1) if (gz_zero(state, state.state->skip) == -1)
return -1; return -1;
} }
/* compress remaining data with requested flush */ /* compress remaining data with requested flush */
gz_comp(state, flush); gz_comp(state, flush);
return state->err; return state.state->err;
} }
/* -- see zlib.h -- */ /* -- see zlib.h -- */
@ -506,32 +506,32 @@ int ZEXPORT gzsetparams(file, level, strategy)
if (file == NULL) if (file == NULL)
return Z_STREAM_ERROR; return Z_STREAM_ERROR;
state = (gz_statep)file; state = (gz_statep)file;
strm = &(state->strm); strm = &(state.state->strm);
/* check that we're writing and that there's no error */ /* check that we're writing and that there's no error */
if (state->mode != GZ_WRITE || state->err != Z_OK) if (state.state->mode != GZ_WRITE || state.state->err != Z_OK)
return Z_STREAM_ERROR; return Z_STREAM_ERROR;
/* if no change is requested, then do nothing */ /* if no change is requested, then do nothing */
if (level == state->level && strategy == state->strategy) if (level == state.state->level && strategy == state.state->strategy)
return Z_OK; return Z_OK;
/* check for seek request */ /* check for seek request */
if (state->seek) { if (state.state->seek) {
state->seek = 0; state.state->seek = 0;
if (gz_zero(state, state->skip) == -1) if (gz_zero(state, state.state->skip) == -1)
return -1; return -1;
} }
/* change compression parameters for subsequent input */ /* change compression parameters for subsequent input */
if (state->size) { if (state.state->size) {
/* flush previous input with previous parameters before changing */ /* flush previous input with previous parameters before changing */
if (strm->avail_in && gz_comp(state, Z_PARTIAL_FLUSH) == -1) if (strm->avail_in && gz_comp(state, Z_PARTIAL_FLUSH) == -1)
return state->err; return state.state->err;
deflateParams(strm, level, strategy); deflateParams(strm, level, strategy);
} }
state->level = level; state.state->level = level;
state->strategy = strategy; state.state->strategy = strategy;
return Z_OK; return Z_OK;
} }
@ -548,30 +548,30 @@ int ZEXPORT gzclose_w(file)
state = (gz_statep)file; state = (gz_statep)file;
/* check that we're writing */ /* check that we're writing */
if (state->mode != GZ_WRITE) if (state.state->mode != GZ_WRITE)
return Z_STREAM_ERROR; return Z_STREAM_ERROR;
/* check for seek request */ /* check for seek request */
if (state->seek) { if (state.state->seek) {
state->seek = 0; state.state->seek = 0;
if (gz_zero(state, state->skip) == -1) if (gz_zero(state, state.state->skip) == -1)
ret = state->err; ret = state.state->err;
} }
/* flush, free memory, and close file */ /* flush, free memory, and close file */
if (gz_comp(state, Z_FINISH) == -1) if (gz_comp(state, Z_FINISH) == -1)
ret = state->err; ret = state.state->err;
if (state->size) { if (state.state->size) {
if (!state->direct) { if (!state.state->direct) {
(void)deflateEnd(&(state->strm)); (void)deflateEnd(&(state.state->strm));
free(state->out); free(state.state->out);
} }
free(state->in); free(state.state->in);
} }
gz_error(state, Z_OK, NULL); gz_error(state, Z_OK, NULL);
free(state->path); free(state.state->path);
if (close(state->fd) == -1) if (close(state.state->fd) == -1)
ret = Z_ERRNO; ret = Z_ERRNO;
free(state); free(state.state);
return ret; return ret;
} }