#include "zbuild.h"
#include "zutil_p.h"
#include "gzguts.h"
#include "gzread_mangle.h"
static int gz_read_init(gz_state *state);
static int gz_load(gz_state *, unsigned char *, unsigned, unsigned *);
static int gz_avail(gz_state *);
static int gz_look(gz_state *);
static int gz_decomp(gz_state *);
static int gz_fetch(gz_state *);
static int gz_skip(gz_state *, z_off64_t);
static size_t gz_read(gz_state *, void *, size_t);
static int gz_read_init(gz_state *state) {
if (gz_buffer_alloc(state) != 0) {
PREFIX(gz_error)(state, Z_MEM_ERROR, "out of memory");
return -1;
}
int ret = PREFIX(inflateInit2)(&(state->strm), MAX_WBITS + 16);
if (ret != Z_OK) {
gz_buffer_free(state);
if (ret == Z_MEM_ERROR) {
PREFIX(gz_error)(state, Z_MEM_ERROR, "out of memory");
} else {
PREFIX(gz_error)(state, Z_STREAM_ERROR, "invalid compression parameters");
}
return -1;
}
return 0;
}
static int gz_load(gz_state *state, unsigned char *buf, unsigned len, unsigned *have) {
ssize_t ret;
*have = 0;
do {
ret = read(state->fd, buf + *have, len - *have);
if (ret <= 0)
break;
*have += (unsigned)ret;
} while (*have < len);
if (ret < 0) {
PREFIX(gz_error)(state, Z_ERRNO, zstrerror());
return -1;
}
if (ret == 0)
state->eof = 1;
return 0;
}
static int gz_avail(gz_state *state) {
unsigned got;
PREFIX3(stream) *strm = &(state->strm);
if (state->err != Z_OK && state->err != Z_BUF_ERROR)
return -1;
if (state->eof == 0) {
if (strm->avail_in) {
unsigned char *p = state->in;
unsigned const char *q = strm->next_in;
unsigned n = strm->avail_in;
do {
*p++ = *q++;
} while (--n);
}
if (gz_load(state, state->in + strm->avail_in, state->size - strm->avail_in, &got) == -1)
return -1;
strm->avail_in += got;
strm->next_in = state->in;
}
return 0;
}
static int gz_look(gz_state *state) {
PREFIX3(stream) *strm = &(state->strm);
if (state->size == 0 && gz_read_init(state) == -1)
return -1;
if (strm->avail_in < 2) {
if (gz_avail(state) == -1)
return -1;
if (strm->avail_in == 0)
return 0;
}
if (strm->avail_in > 1 &&
strm->next_in[0] == 31 && strm->next_in[1] == 139) {
PREFIX(inflateReset)(strm);
state->how = GZIP;
state->direct = 0;
return 0;
}
if (state->direct == 0) {
strm->avail_in = 0;
state->eof = 1;
state->x.have = 0;
return 0;
}
state->x.next = state->out;
memcpy(state->x.next, strm->next_in, strm->avail_in);
state->x.have = strm->avail_in;
strm->avail_in = 0;
state->how = COPY;
state->direct = 1;
return 0;
}
static int gz_decomp(gz_state *state) {
int ret = Z_OK;
unsigned had;
PREFIX3(stream) *strm = &(state->strm);
had = strm->avail_out;
do {
if (strm->avail_in == 0 && gz_avail(state) == -1)
return -1;
if (strm->avail_in == 0) {
PREFIX(gz_error)(state, Z_BUF_ERROR, "unexpected end of file");
break;
}
ret = PREFIX(inflate)(strm, Z_NO_FLUSH);
if (ret == Z_STREAM_ERROR || ret == Z_NEED_DICT) {
PREFIX(gz_error)(state, Z_STREAM_ERROR, "internal error: inflate stream corrupt");
return -1;
}
if (ret == Z_MEM_ERROR) {
PREFIX(gz_error)(state, Z_MEM_ERROR, "out of memory");
return -1;
}
if (ret == Z_DATA_ERROR) {
PREFIX(gz_error)(state, Z_DATA_ERROR, strm->msg == NULL ? "compressed data error" : strm->msg);
return -1;
}
} while (strm->avail_out && ret != Z_STREAM_END);
state->x.have = had - strm->avail_out;
state->x.next = strm->next_out - state->x.have;
if (ret == Z_STREAM_END)
state->how = LOOK;
return 0;
}
static int gz_fetch(gz_state *state) {
PREFIX3(stream) *strm = &(state->strm);
Assert(state->x.have == 0, "Invalid state->x.have");
do {
switch (state->how) {
case LOOK:
if (gz_look(state) == -1)
return -1;
if (state->how == LOOK)
return 0;
break;
case COPY:
if (gz_load(state, state->out, state->size << 1, &(state->x.have))
== -1)
return -1;
state->x.next = state->out;
return 0;
case GZIP:
strm->avail_out = state->size << 1;
strm->next_out = state->out;
if (gz_decomp(state) == -1)
return -1;
continue;
default: return -1;
}
} while (state->x.have == 0 && (!state->eof || strm->avail_in));
return 0;
}
static int gz_skip(gz_state *state, z_off64_t len) {
unsigned n;
while (len)
if (state->x.have) {
n = GT_OFF(state->x.have) || (z_off64_t)state->x.have > len ?
(unsigned)len : state->x.have;
state->x.have -= n;
state->x.next += n;
state->x.pos += n;
len -= n;
} else if (state->eof && state->strm.avail_in == 0) {
break;
} else {
if (gz_fetch(state) == -1)
return -1;
}
return 0;
}
static size_t gz_read(gz_state *state, void *buf, size_t len) {
size_t got;
unsigned n;
if (len == 0)
return 0;
if (state->seek) {
state->seek = 0;
if (gz_skip(state, state->skip) == -1)
return 0;
}
got = 0;
do {
n = (unsigned)-1;
if (n > len)
n = (unsigned)len;
if (state->x.have) {
if (state->x.have < n)
n = state->x.have;
memcpy(buf, state->x.next, n);
state->x.next += n;
state->x.have -= n;
}
else if (state->eof && state->strm.avail_in == 0) {
state->past = 1;
break;
}
else if (state->how == LOOK || n < (state->size << 1)) {
if (gz_fetch(state) == -1)
return 0;
continue;
}
else if (state->how == COPY) {
if (gz_load(state, (unsigned char *)buf, n, &n) == -1)
return 0;
}
else {
state->strm.avail_out = n;
state->strm.next_out = (unsigned char *)buf;
if (gz_decomp(state) == -1)
return 0;
n = state->x.have;
state->x.have = 0;
}
len -= n;
buf = (char *)buf + n;
got += n;
state->x.pos += n;
} while (len);
return got;
}
z_int32_t Z_EXPORT PREFIX(gzread)(gzFile file, void *buf, z_uint32_t len) {
gz_state *state;
if (file == NULL)
return -1;
state = (gz_state *)file;
if (state->mode != GZ_READ ||
(state->err != Z_OK && state->err != Z_BUF_ERROR))
return -1;
if ((z_int32_t)len < 0) {
PREFIX(gz_error)(state, Z_STREAM_ERROR, "request does not fit in an int");
return -1;
}
len = (unsigned)gz_read(state, buf, len);
if (len == 0 && state->err != Z_OK && state->err != Z_BUF_ERROR)
return -1;
return (z_int32_t)len;
}
size_t Z_EXPORT PREFIX(gzfread)(void *buf, size_t size, size_t nitems, gzFile file) {
size_t len;
gz_state *state;
if (size == 0)
return 0;
if (file == NULL)
return 0;
state = (gz_state *)file;
if (state->mode != GZ_READ ||
(state->err != Z_OK && state->err != Z_BUF_ERROR))
return 0;
if (size && SIZE_MAX / size < nitems) {
PREFIX(gz_error)(state, Z_STREAM_ERROR, "request does not fit in a size_t");
return 0;
}
len = nitems * size;
return len ? gz_read(state, buf, len) / size : 0;
}
z_int32_t Z_EXPORT PREFIX(gzgetc)(gzFile file) {
unsigned char buf[1];
gz_state *state;
if (file == NULL)
return -1;
state = (gz_state *)file;
if (state->mode != GZ_READ || (state->err != Z_OK && state->err != Z_BUF_ERROR))
return -1;
if (state->x.have) {
state->x.have--;
state->x.pos++;
return *(state->x.next)++;
}
return gz_read(state, buf, 1) < 1 ? -1 : buf[0];
}
#ifdef ZLIB_COMPAT
int Z_EXPORT PREFIX(gzgetc_)(gzFile file) {
return PREFIX(gzgetc)(file);
}
#endif
z_int32_t Z_EXPORT PREFIX(gzungetc)(z_int32_t c, gzFile file) {
gz_state *state;
if (file == NULL)
return -1;
state = (gz_state *)file;
if (state->mode == GZ_READ && state->how == LOOK && state->x.have == 0)
(void)gz_look(state);
if (state->mode != GZ_READ || (state->err != Z_OK && state->err != Z_BUF_ERROR))
return -1;
if (state->seek) {
state->seek = 0;
if (gz_skip(state, state->skip) == -1)
return -1;
}
if (c < 0)
return -1;
if (state->x.have == 0) {
state->x.have = 1;
state->x.next = state->out + (state->size << 1) - 1;
state->x.next[0] = (unsigned char)c;
state->x.pos--;
state->past = 0;
return c;
}
if (state->x.have == (state->size << 1)) {
PREFIX(gz_error)(state, Z_DATA_ERROR, "out of room to push characters");
return -1;
}
if (state->x.next == state->out) {
unsigned char *src = state->out + state->x.have;
unsigned char *dest = state->out + (state->size << 1);
while (src > state->out)
*--dest = *--src;
state->x.next = dest;
}
state->x.have++;
state->x.next--;
state->x.next[0] = (unsigned char)c;
state->x.pos--;
state->past = 0;
return c;
}
char * Z_EXPORT PREFIX(gzgets)(gzFile file, char *buf, z_int32_t len) {
unsigned left, n;
char *str;
unsigned char *eol;
gz_state *state;
if (file == NULL || buf == NULL || len < 1)
return NULL;
state = (gz_state *)file;
if (state->mode != GZ_READ || (state->err != Z_OK && state->err != Z_BUF_ERROR))
return NULL;
if (state->seek) {
state->seek = 0;
if (gz_skip(state, state->skip) == -1)
return NULL;
}
str = buf;
left = (unsigned)len - 1;
if (left) {
do {
if (state->x.have == 0 && gz_fetch(state) == -1)
return NULL;
if (state->x.have == 0) {
state->past = 1;
break;
}
n = state->x.have > left ? left : state->x.have;
eol = (unsigned char *)memchr(state->x.next, '\n', n);
if (eol != NULL)
n = (unsigned)(eol - state->x.next) + 1;
memcpy(buf, state->x.next, n);
state->x.have -= n;
state->x.next += n;
state->x.pos += n;
left -= n;
buf += n;
} while (left && eol == NULL);
}
if (buf == str)
return NULL;
buf[0] = 0;
return str;
}
z_int32_t Z_EXPORT PREFIX(gzdirect)(gzFile file) {
gz_state *state;
if (file == NULL)
return 0;
state = (gz_state *)file;
if (state->mode == GZ_READ && state->how == LOOK && state->x.have == 0)
(void)gz_look(state);
return state->direct;
}
z_int32_t Z_EXPORT PREFIX(gzclose_r)(gzFile file) {
int ret, err;
gz_state *state;
if (file == NULL)
return Z_STREAM_ERROR;
state = (gz_state *)file;
if (state->mode != GZ_READ)
return Z_STREAM_ERROR;
if (state->size) {
PREFIX(inflateEnd)(&(state->strm));
gz_buffer_free(state);
}
err = state->err == Z_BUF_ERROR ? Z_BUF_ERROR : Z_OK;
PREFIX(gz_error)(state, Z_OK, NULL);
free(state->path);
ret = close(state->fd);
zng_free(state);
return ret ? Z_ERRNO : err;
}