2011-09-10 02:26:49 -04:00
|
|
|
/* gzread.c -- zlib functions for reading gzip files
|
2012-02-12 17:11:48 -05:00
|
|
|
* Copyright (C) 2004, 2005, 2010, 2011, 2012 Mark Adler
|
2011-09-10 02:26:49 -04:00
|
|
|
* For conditions of distribution and use, see copyright notice in zlib.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gzguts.h"
|
|
|
|
|
|
|
|
/* Local functions */
|
|
|
|
local int gz_load OF((gz_statep, unsigned char *, unsigned, unsigned *));
|
|
|
|
local int gz_avail OF((gz_statep));
|
2011-09-26 03:57:26 -04:00
|
|
|
local int gz_look OF((gz_statep));
|
2011-09-10 02:26:49 -04:00
|
|
|
local int gz_decomp OF((gz_statep));
|
2011-09-26 03:57:26 -04:00
|
|
|
local int gz_fetch OF((gz_statep));
|
2011-09-10 02:27:17 -04:00
|
|
|
local int gz_skip OF((gz_statep, z_off64_t));
|
2011-09-10 02:26:49 -04:00
|
|
|
|
|
|
|
/* Use read() to load a buffer -- return -1 on error, otherwise 0. Read from
|
|
|
|
state->fd, and update state->eof, state->err, and state->msg as appropriate.
|
|
|
|
This function needs to loop on read(), since read() is not guaranteed to
|
|
|
|
read the number of bytes requested, depending on the type of descriptor. */
|
|
|
|
local int gz_load(state, buf, len, have)
|
|
|
|
gz_statep state;
|
|
|
|
unsigned char *buf;
|
|
|
|
unsigned len;
|
|
|
|
unsigned *have;
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
*have = 0;
|
|
|
|
do {
|
|
|
|
ret = read(state->fd, buf + *have, len - *have);
|
|
|
|
if (ret <= 0)
|
|
|
|
break;
|
|
|
|
*have += ret;
|
|
|
|
} while (*have < len);
|
|
|
|
if (ret < 0) {
|
|
|
|
gz_error(state, Z_ERRNO, zstrerror());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (ret == 0)
|
|
|
|
state->eof = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Load up input buffer and set eof flag if last data loaded -- return -1 on
|
|
|
|
error, 0 otherwise. Note that the eof flag is set when the end of the input
|
|
|
|
file is reached, even though there may be unused data in the buffer. Once
|
|
|
|
that data has been used, no more attempts will be made to read the file.
|
2011-09-26 03:57:26 -04:00
|
|
|
If strm->avail_in != 0, then the current data is moved to the beginning of
|
|
|
|
the input buffer, and then the remainder of the buffer is loaded with the
|
|
|
|
available data from the input file. */
|
2011-09-10 02:26:49 -04:00
|
|
|
local int gz_avail(state)
|
|
|
|
gz_statep state;
|
|
|
|
{
|
2011-09-26 03:57:26 -04:00
|
|
|
unsigned got;
|
2011-09-10 02:26:49 -04:00
|
|
|
z_streamp strm = &(state->strm);
|
|
|
|
|
2011-09-26 03:57:26 -04:00
|
|
|
if (state->err != Z_OK && state->err != Z_BUF_ERROR)
|
2011-09-10 02:26:49 -04:00
|
|
|
return -1;
|
|
|
|
if (state->eof == 0) {
|
2012-02-18 11:19:59 -05:00
|
|
|
if (strm->avail_in) { /* copy what's there to the start */
|
2012-08-12 21:08:52 -04:00
|
|
|
unsigned char *p = state->in;
|
|
|
|
unsigned const char *q = strm->next_in;
|
2012-02-18 11:19:59 -05:00
|
|
|
unsigned n = strm->avail_in;
|
|
|
|
do {
|
|
|
|
*p++ = *q++;
|
|
|
|
} while (--n);
|
|
|
|
}
|
2011-09-26 03:57:26 -04:00
|
|
|
if (gz_load(state, state->in + strm->avail_in,
|
|
|
|
state->size - strm->avail_in, &got) == -1)
|
2011-09-10 02:26:49 -04:00
|
|
|
return -1;
|
2011-09-26 03:57:26 -04:00
|
|
|
strm->avail_in += got;
|
2011-09-10 02:26:49 -04:00
|
|
|
strm->next_in = state->in;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-09-27 01:50:28 -04:00
|
|
|
/* Look for gzip header, set up for inflate or copy. state->x.have must be 0.
|
2011-09-10 02:26:49 -04:00
|
|
|
If this is the first time in, allocate required memory. state->how will be
|
2011-09-10 02:27:08 -04:00
|
|
|
left unchanged if there is no more input data available, will be set to COPY
|
|
|
|
if there is no gzip header and direct copying will be performed, or it will
|
2011-09-26 03:57:26 -04:00
|
|
|
be set to GZIP for decompression. If direct copying, then leftover input
|
|
|
|
data from the input buffer will be copied to the output buffer. In that
|
|
|
|
case, all further file reads will be directly to either the output buffer or
|
|
|
|
a user buffer. If decompressing, the inflate state will be initialized.
|
|
|
|
gz_look() will return 0 on success or -1 on failure. */
|
|
|
|
local int gz_look(state)
|
2011-09-10 02:26:49 -04:00
|
|
|
gz_statep state;
|
|
|
|
{
|
|
|
|
z_streamp strm = &(state->strm);
|
|
|
|
|
|
|
|
/* allocate read buffers and inflate memory */
|
|
|
|
if (state->size == 0) {
|
|
|
|
/* allocate buffers */
|
2013-03-24 18:18:02 -04:00
|
|
|
state->in = (unsigned char *)malloc(state->want);
|
|
|
|
state->out = (unsigned char *)malloc(state->want << 1);
|
2011-09-10 02:26:49 -04:00
|
|
|
if (state->in == NULL || state->out == NULL) {
|
|
|
|
if (state->out != NULL)
|
|
|
|
free(state->out);
|
|
|
|
if (state->in != NULL)
|
|
|
|
free(state->in);
|
|
|
|
gz_error(state, Z_MEM_ERROR, "out of memory");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
state->size = state->want;
|
|
|
|
|
|
|
|
/* allocate inflate memory */
|
|
|
|
state->strm.zalloc = Z_NULL;
|
|
|
|
state->strm.zfree = Z_NULL;
|
|
|
|
state->strm.opaque = Z_NULL;
|
|
|
|
state->strm.avail_in = 0;
|
|
|
|
state->strm.next_in = Z_NULL;
|
2011-09-26 03:57:26 -04:00
|
|
|
if (inflateInit2(&(state->strm), 15 + 16) != Z_OK) { /* gunzip */
|
2011-09-10 02:26:49 -04:00
|
|
|
free(state->out);
|
|
|
|
free(state->in);
|
|
|
|
state->size = 0;
|
|
|
|
gz_error(state, Z_MEM_ERROR, "out of memory");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-26 03:57:26 -04:00
|
|
|
/* get at least the magic bytes in the input buffer */
|
|
|
|
if (strm->avail_in < 2) {
|
2011-09-10 02:26:49 -04:00
|
|
|
if (gz_avail(state) == -1)
|
|
|
|
return -1;
|
|
|
|
if (strm->avail_in == 0)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-09-26 03:57:26 -04:00
|
|
|
/* look for gzip magic bytes -- if there, do gzip decoding (note: there is
|
|
|
|
a logical dilemma here when considering the case of a partially written
|
|
|
|
gzip file, to wit, if a single 31 byte is written, then we cannot tell
|
|
|
|
whether this is a single-byte file, or just a partially written gzip
|
|
|
|
file -- for here we assume that if a gzip file is being written, then
|
|
|
|
the header will be written in a single operation, so that reading a
|
|
|
|
single byte is sufficient indication that it is not a gzip file) */
|
|
|
|
if (strm->avail_in > 1 &&
|
|
|
|
strm->next_in[0] == 31 && strm->next_in[1] == 139) {
|
|
|
|
inflateReset(strm);
|
|
|
|
state->how = GZIP;
|
|
|
|
state->direct = 0;
|
|
|
|
return 0;
|
2011-09-10 02:26:49 -04:00
|
|
|
}
|
|
|
|
|
2011-09-24 13:26:07 -04:00
|
|
|
/* no gzip header -- if we were decoding gzip before, then this is trailing
|
|
|
|
garbage. Ignore the trailing garbage and finish. */
|
|
|
|
if (state->direct == 0) {
|
|
|
|
strm->avail_in = 0;
|
|
|
|
state->eof = 1;
|
2011-09-27 01:50:28 -04:00
|
|
|
state->x.have = 0;
|
2011-09-24 13:26:07 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-09-26 21:34:07 -04:00
|
|
|
/* 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
|
|
|
|
space for gzungetc() */
|
2011-09-27 01:50:28 -04:00
|
|
|
state->x.next = state->out;
|
2011-09-10 02:26:49 -04:00
|
|
|
if (strm->avail_in) {
|
2011-09-27 01:50:28 -04:00
|
|
|
memcpy(state->x.next, strm->next_in, strm->avail_in);
|
|
|
|
state->x.have = strm->avail_in;
|
2011-09-10 02:26:49 -04:00
|
|
|
strm->avail_in = 0;
|
|
|
|
}
|
2011-09-10 02:27:08 -04:00
|
|
|
state->how = COPY;
|
|
|
|
state->direct = 1;
|
2011-09-10 02:26:49 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Decompress from input to the provided next_out and avail_out in the state.
|
2011-09-27 01:50:28 -04:00
|
|
|
On return, state->x.have and state->x.next point to the just decompressed
|
|
|
|
data. If the gzip stream completes, state->how is reset to LOOK to look for
|
|
|
|
the next gzip stream or raw data, once state->x.have is depleted. Returns 0
|
|
|
|
on success, -1 on failure. */
|
2011-09-10 02:26:49 -04:00
|
|
|
local int gz_decomp(state)
|
|
|
|
gz_statep state;
|
|
|
|
{
|
2011-09-26 03:57:26 -04:00
|
|
|
int ret = Z_OK;
|
2011-09-10 02:26:49 -04:00
|
|
|
unsigned had;
|
|
|
|
z_streamp strm = &(state->strm);
|
|
|
|
|
|
|
|
/* fill output buffer up to end of deflate stream */
|
|
|
|
had = strm->avail_out;
|
|
|
|
do {
|
|
|
|
/* get more input for inflate() */
|
|
|
|
if (strm->avail_in == 0 && gz_avail(state) == -1)
|
|
|
|
return -1;
|
|
|
|
if (strm->avail_in == 0) {
|
2011-09-26 03:57:26 -04:00
|
|
|
gz_error(state, Z_BUF_ERROR, "unexpected end of file");
|
|
|
|
break;
|
2011-09-10 02:26:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* decompress and handle errors */
|
|
|
|
ret = inflate(strm, Z_NO_FLUSH);
|
|
|
|
if (ret == Z_STREAM_ERROR || ret == Z_NEED_DICT) {
|
|
|
|
gz_error(state, Z_STREAM_ERROR,
|
2011-09-26 03:57:26 -04:00
|
|
|
"internal error: inflate stream corrupt");
|
2011-09-10 02:26:49 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (ret == Z_MEM_ERROR) {
|
|
|
|
gz_error(state, Z_MEM_ERROR, "out of memory");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (ret == Z_DATA_ERROR) { /* deflate stream invalid */
|
|
|
|
gz_error(state, Z_DATA_ERROR,
|
2011-09-26 03:57:26 -04:00
|
|
|
strm->msg == NULL ? "compressed data error" : strm->msg);
|
2011-09-10 02:26:49 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} while (strm->avail_out && ret != Z_STREAM_END);
|
|
|
|
|
2011-09-26 03:57:26 -04:00
|
|
|
/* update available output */
|
2011-09-27 01:50:28 -04:00
|
|
|
state->x.have = had - strm->avail_out;
|
|
|
|
state->x.next = strm->next_out - state->x.have;
|
2011-09-10 02:26:49 -04:00
|
|
|
|
2011-09-26 03:57:26 -04:00
|
|
|
/* if the gzip stream completed successfully, look for another */
|
|
|
|
if (ret == Z_STREAM_END)
|
|
|
|
state->how = LOOK;
|
2011-09-10 02:26:49 -04:00
|
|
|
|
|
|
|
/* good decompression */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-09-27 01:50:28 -04:00
|
|
|
/* Fetch data and put it in the output buffer. Assumes state->x.have is 0.
|
2011-09-10 02:26:49 -04:00
|
|
|
Data is either copied from the input file or decompressed from the input
|
2011-09-10 02:27:08 -04:00
|
|
|
file depending on state->how. If state->how is LOOK, then a gzip header is
|
2011-09-26 03:57:26 -04:00
|
|
|
looked for to determine whether to copy or decompress. Returns -1 on error,
|
|
|
|
otherwise 0. gz_fetch() will leave state->how as COPY or GZIP unless the
|
|
|
|
end of the input file has been reached and all data has been processed. */
|
|
|
|
local int gz_fetch(state)
|
2011-09-10 02:26:49 -04:00
|
|
|
gz_statep state;
|
|
|
|
{
|
|
|
|
z_streamp strm = &(state->strm);
|
|
|
|
|
2011-09-26 03:57:26 -04:00
|
|
|
do {
|
|
|
|
switch(state->how) {
|
|
|
|
case LOOK: /* -> LOOK, COPY (only if never GZIP), or GZIP */
|
|
|
|
if (gz_look(state) == -1)
|
|
|
|
return -1;
|
|
|
|
if (state->how == LOOK)
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
case COPY: /* -> COPY */
|
2011-09-27 01:50:28 -04:00
|
|
|
if (gz_load(state, state->out, state->size << 1, &(state->x.have))
|
2011-09-26 03:57:26 -04:00
|
|
|
== -1)
|
|
|
|
return -1;
|
2011-09-27 01:50:28 -04:00
|
|
|
state->x.next = state->out;
|
2011-09-10 02:26:49 -04:00
|
|
|
return 0;
|
2011-09-26 03:57:26 -04:00
|
|
|
case GZIP: /* -> GZIP or LOOK (if end of gzip stream) */
|
|
|
|
strm->avail_out = state->size << 1;
|
|
|
|
strm->next_out = state->out;
|
|
|
|
if (gz_decomp(state) == -1)
|
|
|
|
return -1;
|
|
|
|
}
|
2011-12-12 02:30:56 -05:00
|
|
|
} while (state->x.have == 0 && (!state->eof || strm->avail_in));
|
2011-09-10 02:26:49 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Skip len uncompressed bytes of output. Return -1 on error, 0 on success. */
|
|
|
|
local int gz_skip(state, len)
|
|
|
|
gz_statep state;
|
2011-09-10 02:27:17 -04:00
|
|
|
z_off64_t len;
|
2011-09-10 02:26:49 -04:00
|
|
|
{
|
|
|
|
unsigned n;
|
|
|
|
|
|
|
|
/* skip over len bytes or reach end-of-file, whichever comes first */
|
|
|
|
while (len)
|
|
|
|
/* skip over whatever is in output buffer */
|
2011-09-27 01:50:28 -04:00
|
|
|
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;
|
2011-09-10 02:26:49 -04:00
|
|
|
len -= n;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* output buffer empty -- return if we're at the end of the input */
|
|
|
|
else if (state->eof && state->strm.avail_in == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* need more data to skip -- load up output buffer */
|
|
|
|
else {
|
|
|
|
/* get more output, looking for header if required */
|
2011-09-26 03:57:26 -04:00
|
|
|
if (gz_fetch(state) == -1)
|
2011-09-10 02:26:49 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -- see zlib.h -- */
|
|
|
|
int ZEXPORT gzread(file, buf, len)
|
|
|
|
gzFile file;
|
|
|
|
voidp buf;
|
|
|
|
unsigned len;
|
|
|
|
{
|
|
|
|
unsigned got, n;
|
|
|
|
gz_statep state;
|
|
|
|
z_streamp strm;
|
|
|
|
|
|
|
|
/* get internal structure */
|
|
|
|
if (file == NULL)
|
|
|
|
return -1;
|
|
|
|
state = (gz_statep)file;
|
|
|
|
strm = &(state->strm);
|
|
|
|
|
2011-09-26 03:57:26 -04:00
|
|
|
/* check that we're reading and that there's no (serious) error */
|
|
|
|
if (state->mode != GZ_READ ||
|
|
|
|
(state->err != Z_OK && state->err != Z_BUF_ERROR))
|
2011-09-10 02:26:49 -04:00
|
|
|
return -1;
|
|
|
|
|
2011-09-10 02:26:58 -04:00
|
|
|
/* since an int is returned, make sure len fits in one, otherwise return
|
|
|
|
with an error (this avoids the flaw in the interface) */
|
|
|
|
if ((int)len < 0) {
|
2011-12-14 01:25:59 -05:00
|
|
|
gz_error(state, Z_DATA_ERROR, "requested length does not fit in int");
|
2011-09-10 02:26:58 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if len is zero, avoid unnecessary operations */
|
|
|
|
if (len == 0)
|
|
|
|
return 0;
|
|
|
|
|
2011-09-10 02:26:49 -04:00
|
|
|
/* process a skip request */
|
|
|
|
if (state->seek) {
|
|
|
|
state->seek = 0;
|
|
|
|
if (gz_skip(state, state->skip) == -1)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get len bytes to buf, or less than len if at the end */
|
|
|
|
got = 0;
|
2011-09-10 02:26:58 -04:00
|
|
|
do {
|
2011-09-10 02:26:49 -04:00
|
|
|
/* first just try copying data from the output buffer */
|
2011-09-27 01:50:28 -04:00
|
|
|
if (state->x.have) {
|
|
|
|
n = state->x.have > len ? len : state->x.have;
|
|
|
|
memcpy(buf, state->x.next, n);
|
|
|
|
state->x.next += n;
|
|
|
|
state->x.have -= n;
|
2011-09-10 02:26:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* output buffer empty -- return if we're at the end of the input */
|
2011-10-13 02:24:31 -04:00
|
|
|
else if (state->eof && strm->avail_in == 0) {
|
|
|
|
state->past = 1; /* tried to read past end */
|
2011-09-10 02:26:49 -04:00
|
|
|
break;
|
2011-10-13 02:24:31 -04:00
|
|
|
}
|
2011-09-10 02:26:49 -04:00
|
|
|
|
|
|
|
/* need output data -- for small len or new stream load up our output
|
|
|
|
buffer */
|
2011-09-10 02:27:08 -04:00
|
|
|
else if (state->how == LOOK || len < (state->size << 1)) {
|
2011-09-10 02:26:49 -04:00
|
|
|
/* get more output, looking for header if required */
|
2011-09-26 03:57:26 -04:00
|
|
|
if (gz_fetch(state) == -1)
|
2011-09-10 02:26:49 -04:00
|
|
|
return -1;
|
2012-02-18 11:19:59 -05:00
|
|
|
continue; /* no progress yet -- go back to copy above */
|
2011-09-10 02:26:58 -04:00
|
|
|
/* the copy above assures that we will leave with space in the
|
|
|
|
output buffer, allowing at least one gzungetc() to succeed */
|
2011-09-10 02:26:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* large len -- read directly into user buffer */
|
2011-09-10 02:27:08 -04:00
|
|
|
else if (state->how == COPY) { /* read directly */
|
2013-03-24 18:18:02 -04:00
|
|
|
if (gz_load(state, (unsigned char *)buf, len, &n) == -1)
|
2011-09-10 02:26:49 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* large len -- decompress directly into user buffer */
|
2011-09-10 02:27:08 -04:00
|
|
|
else { /* state->how == GZIP */
|
2011-09-10 02:26:49 -04:00
|
|
|
strm->avail_out = len;
|
2013-03-24 18:18:02 -04:00
|
|
|
strm->next_out = (unsigned char *)buf;
|
2011-09-10 02:26:49 -04:00
|
|
|
if (gz_decomp(state) == -1)
|
|
|
|
return -1;
|
2011-09-27 01:50:28 -04:00
|
|
|
n = state->x.have;
|
|
|
|
state->x.have = 0;
|
2011-09-10 02:26:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* update progress */
|
|
|
|
len -= n;
|
2011-09-10 02:26:58 -04:00
|
|
|
buf = (char *)buf + n;
|
2011-09-10 02:26:49 -04:00
|
|
|
got += n;
|
2011-09-27 01:50:28 -04:00
|
|
|
state->x.pos += n;
|
2011-09-10 02:26:58 -04:00
|
|
|
} while (len);
|
2011-09-10 02:26:49 -04:00
|
|
|
|
2011-09-10 02:26:58 -04:00
|
|
|
/* return number of bytes read into user buffer (will fit in int) */
|
|
|
|
return (int)got;
|
2011-09-10 02:26:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* -- see zlib.h -- */
|
2012-05-18 00:13:23 -04:00
|
|
|
#ifdef Z_PREFIX_SET
|
|
|
|
# undef z_gzgetc
|
|
|
|
#else
|
|
|
|
# undef gzgetc
|
|
|
|
#endif
|
2012-02-11 02:55:12 -05:00
|
|
|
int ZEXPORT gzgetc(file)
|
2011-09-10 02:26:49 -04:00
|
|
|
gzFile file;
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
unsigned char buf[1];
|
|
|
|
gz_statep state;
|
|
|
|
|
|
|
|
/* get internal structure */
|
|
|
|
if (file == NULL)
|
|
|
|
return -1;
|
|
|
|
state = (gz_statep)file;
|
|
|
|
|
2011-09-26 03:57:26 -04:00
|
|
|
/* check that we're reading and that there's no (serious) error */
|
|
|
|
if (state->mode != GZ_READ ||
|
|
|
|
(state->err != Z_OK && state->err != Z_BUF_ERROR))
|
2011-09-10 02:26:49 -04:00
|
|
|
return -1;
|
|
|
|
|
2011-12-18 17:47:08 -05:00
|
|
|
/* try output buffer (no need to check for skip request) */
|
2011-09-27 01:50:28 -04:00
|
|
|
if (state->x.have) {
|
|
|
|
state->x.have--;
|
|
|
|
state->x.pos++;
|
|
|
|
return *(state->x.next)++;
|
2011-09-10 02:26:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* nothing there -- try gzread() */
|
|
|
|
ret = gzread(file, buf, 1);
|
|
|
|
return ret < 1 ? -1 : buf[0];
|
|
|
|
}
|
|
|
|
|
2012-02-18 17:10:20 -05:00
|
|
|
int ZEXPORT gzgetc_(file)
|
|
|
|
gzFile file;
|
|
|
|
{
|
|
|
|
return gzgetc(file);
|
|
|
|
}
|
|
|
|
|
2011-09-10 02:26:49 -04:00
|
|
|
/* -- see zlib.h -- */
|
|
|
|
int ZEXPORT gzungetc(c, file)
|
|
|
|
int c;
|
|
|
|
gzFile file;
|
|
|
|
{
|
|
|
|
gz_statep state;
|
|
|
|
|
|
|
|
/* get internal structure */
|
|
|
|
if (file == NULL)
|
|
|
|
return -1;
|
|
|
|
state = (gz_statep)file;
|
|
|
|
|
2011-09-26 03:57:26 -04:00
|
|
|
/* check that we're reading and that there's no (serious) error */
|
|
|
|
if (state->mode != GZ_READ ||
|
|
|
|
(state->err != Z_OK && state->err != Z_BUF_ERROR))
|
2011-09-10 02:26:49 -04:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* process a skip request */
|
|
|
|
if (state->seek) {
|
|
|
|
state->seek = 0;
|
|
|
|
if (gz_skip(state, state->skip) == -1)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* can't push EOF */
|
|
|
|
if (c < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* if output buffer empty, put byte at end (allows more pushing) */
|
2011-09-27 01:50:28 -04:00
|
|
|
if (state->x.have == 0) {
|
|
|
|
state->x.have = 1;
|
|
|
|
state->x.next = state->out + (state->size << 1) - 1;
|
|
|
|
state->x.next[0] = c;
|
|
|
|
state->x.pos--;
|
2011-10-13 02:24:31 -04:00
|
|
|
state->past = 0;
|
2011-09-10 02:26:49 -04:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2011-09-10 02:26:58 -04:00
|
|
|
/* if no room, give up (must have already done a gzungetc()) */
|
2011-09-27 01:50:28 -04:00
|
|
|
if (state->x.have == (state->size << 1)) {
|
2011-12-14 01:25:59 -05:00
|
|
|
gz_error(state, Z_DATA_ERROR, "out of room to push characters");
|
2011-09-10 02:26:49 -04:00
|
|
|
return -1;
|
2011-09-10 02:26:58 -04:00
|
|
|
}
|
2011-09-10 02:26:49 -04:00
|
|
|
|
|
|
|
/* slide output data if needed and insert byte before existing data */
|
2011-09-27 01:50:28 -04:00
|
|
|
if (state->x.next == state->out) {
|
|
|
|
unsigned char *src = state->out + state->x.have;
|
2011-09-10 02:26:49 -04:00
|
|
|
unsigned char *dest = state->out + (state->size << 1);
|
|
|
|
while (src > state->out)
|
|
|
|
*--dest = *--src;
|
2011-09-27 01:50:28 -04:00
|
|
|
state->x.next = dest;
|
2011-09-10 02:26:49 -04:00
|
|
|
}
|
2011-09-27 01:50:28 -04:00
|
|
|
state->x.have++;
|
|
|
|
state->x.next--;
|
|
|
|
state->x.next[0] = c;
|
|
|
|
state->x.pos--;
|
2011-10-13 02:24:31 -04:00
|
|
|
state->past = 0;
|
2011-09-10 02:26:49 -04:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -- see zlib.h -- */
|
|
|
|
char * ZEXPORT gzgets(file, buf, len)
|
|
|
|
gzFile file;
|
|
|
|
char *buf;
|
|
|
|
int len;
|
|
|
|
{
|
|
|
|
unsigned left, n;
|
|
|
|
char *str;
|
|
|
|
unsigned char *eol;
|
|
|
|
gz_statep state;
|
|
|
|
|
2011-09-10 02:27:26 -04:00
|
|
|
/* check parameters and get internal structure */
|
|
|
|
if (file == NULL || buf == NULL || len < 1)
|
2011-09-10 02:26:49 -04:00
|
|
|
return NULL;
|
|
|
|
state = (gz_statep)file;
|
|
|
|
|
2011-09-26 03:57:26 -04:00
|
|
|
/* check that we're reading and that there's no (serious) error */
|
|
|
|
if (state->mode != GZ_READ ||
|
|
|
|
(state->err != Z_OK && state->err != Z_BUF_ERROR))
|
2011-09-10 02:26:49 -04:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* process a skip request */
|
|
|
|
if (state->seek) {
|
|
|
|
state->seek = 0;
|
|
|
|
if (gz_skip(state, state->skip) == -1)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* copy output bytes up to new line or len - 1, whichever comes first --
|
|
|
|
append a terminating zero to the string (we don't check for a zero in
|
|
|
|
the contents, let the user worry about that) */
|
|
|
|
str = buf;
|
|
|
|
left = (unsigned)len - 1;
|
2011-09-10 02:27:26 -04:00
|
|
|
if (left) do {
|
2011-09-10 02:26:49 -04:00
|
|
|
/* assure that something is in the output buffer */
|
2011-09-27 01:50:28 -04:00
|
|
|
if (state->x.have == 0 && gz_fetch(state) == -1)
|
2011-09-26 03:57:26 -04:00
|
|
|
return NULL; /* error */
|
2011-09-27 01:50:28 -04:00
|
|
|
if (state->x.have == 0) { /* end of file */
|
2011-10-13 02:24:31 -04:00
|
|
|
state->past = 1; /* read past end */
|
|
|
|
break; /* return what we have */
|
2011-09-10 02:26:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* look for end-of-line in current output buffer */
|
2011-09-27 01:50:28 -04:00
|
|
|
n = state->x.have > left ? left : state->x.have;
|
2013-03-24 18:18:02 -04:00
|
|
|
eol = (unsigned char *)memchr(state->x.next, '\n', n);
|
2011-09-10 02:26:49 -04:00
|
|
|
if (eol != NULL)
|
2011-09-27 01:50:28 -04:00
|
|
|
n = (unsigned)(eol - state->x.next) + 1;
|
2011-09-10 02:26:49 -04:00
|
|
|
|
|
|
|
/* copy through end-of-line, or remainder if not found */
|
2011-09-27 01:50:28 -04:00
|
|
|
memcpy(buf, state->x.next, n);
|
|
|
|
state->x.have -= n;
|
|
|
|
state->x.next += n;
|
|
|
|
state->x.pos += n;
|
2011-09-10 02:26:49 -04:00
|
|
|
left -= n;
|
|
|
|
buf += n;
|
|
|
|
} while (left && eol == NULL);
|
|
|
|
|
2011-10-13 02:24:31 -04:00
|
|
|
/* return terminated string, or if nothing, end of file */
|
|
|
|
if (buf == str)
|
|
|
|
return NULL;
|
2011-09-10 02:26:49 -04:00
|
|
|
buf[0] = 0;
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -- see zlib.h -- */
|
|
|
|
int ZEXPORT gzdirect(file)
|
|
|
|
gzFile file;
|
|
|
|
{
|
|
|
|
gz_statep state;
|
|
|
|
|
|
|
|
/* get internal structure */
|
|
|
|
if (file == NULL)
|
|
|
|
return 0;
|
|
|
|
state = (gz_statep)file;
|
|
|
|
|
2011-09-10 02:27:08 -04:00
|
|
|
/* if the state is not known, but we can find out, then do so (this is
|
|
|
|
mainly for right after a gzopen() or gzdopen()) */
|
2011-10-02 16:24:43 -04:00
|
|
|
if (state->mode == GZ_READ && state->how == LOOK && state->x.have == 0)
|
2011-09-26 03:57:26 -04:00
|
|
|
(void)gz_look(state);
|
2011-09-10 02:27:08 -04:00
|
|
|
|
2011-10-02 16:24:43 -04:00
|
|
|
/* return 1 if transparent, 0 if processing a gzip stream */
|
2011-09-10 02:27:08 -04:00
|
|
|
return state->direct;
|
2011-09-10 02:26:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* -- see zlib.h -- */
|
|
|
|
int ZEXPORT gzclose_r(file)
|
|
|
|
gzFile file;
|
|
|
|
{
|
2011-12-14 07:21:00 -05:00
|
|
|
int ret, err;
|
2011-09-10 02:26:49 -04:00
|
|
|
gz_statep state;
|
|
|
|
|
|
|
|
/* get internal structure */
|
|
|
|
if (file == NULL)
|
|
|
|
return Z_STREAM_ERROR;
|
|
|
|
state = (gz_statep)file;
|
|
|
|
|
|
|
|
/* check that we're reading */
|
|
|
|
if (state->mode != GZ_READ)
|
|
|
|
return Z_STREAM_ERROR;
|
|
|
|
|
|
|
|
/* free memory and close file */
|
|
|
|
if (state->size) {
|
|
|
|
inflateEnd(&(state->strm));
|
|
|
|
free(state->out);
|
|
|
|
free(state->in);
|
|
|
|
}
|
2011-12-14 07:21:00 -05:00
|
|
|
err = state->err == Z_BUF_ERROR ? Z_BUF_ERROR : Z_OK;
|
2011-09-10 02:26:49 -04:00
|
|
|
gz_error(state, Z_OK, NULL);
|
2011-09-10 02:32:36 -04:00
|
|
|
free(state->path);
|
2011-09-10 02:27:08 -04:00
|
|
|
ret = close(state->fd);
|
2011-09-10 02:26:49 -04:00
|
|
|
free(state);
|
2011-12-14 07:21:00 -05:00
|
|
|
return ret ? Z_ERRNO : err;
|
2011-09-10 02:26:49 -04:00
|
|
|
}
|