/* gzread.c -- zlib functions for reading gzip files
* Copyright (C) 2004, 2005, 2010, 2011, 2012, 2013 Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h
*/
#include "gzguts.h"
/* Local functions */
/* 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. */
unsigned char *buf;
unsigned len;
unsigned *have;
{
int ret;
*have = 0;
do {
if (ret <= 0)
break;
if (ret < 0) {
return -1;
}
if (ret == 0)
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.
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. */
{
unsigned got;
return -1;
do {
*p++ = *q++;
} while (--n);
}
return -1;
}
return 0;
}
/* Look for gzip header, set up for inflate or copy. state->x.have must be 0.
If this is the first time in, allocate required memory. state->how will be
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
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. */
{
/* allocate read buffers and inflate memory */
/* allocate buffers */
return -1;
}
/* allocate inflate memory */
return -1;
}
}
/* get at least the magic bytes in the input buffer */
return -1;
return 0;
}
/* 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) */
return 0;
}
/* no gzip header -- if we were decoding gzip before, then this is trailing
garbage. Ignore the trailing garbage and finish. */
return 0;
}
/* 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() */
}
return 0;
}
/* Decompress from input to the provided next_out and avail_out in the state.
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. */
{
unsigned had;
/* fill output buffer up to end of deflate stream */
do {
/* get more input for inflate() */
return -1;
break;
}
/* decompress and handle errors */
"internal error: inflate stream corrupt");
return -1;
}
if (ret == Z_MEM_ERROR) {
return -1;
}
return -1;
}
/* update available output */
/* if the gzip stream completed successfully, look for another */
if (ret == Z_STREAM_END)
/* good decompression */
return 0;
}
/* Fetch data and put it in the output buffer. Assumes state->x.have is 0.
Data is either copied from the input file or decompressed from the input
file depending on state->how. If state->how is LOOK, then a gzip header is
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. */
{
do {
case LOOK: /* -> LOOK, COPY (only if never GZIP), or GZIP */
return -1;
return 0;
break;
case COPY: /* -> COPY */
== -1)
return -1;
return 0;
case GZIP: /* -> GZIP or LOOK (if end of gzip stream) */
return -1;
}
return 0;
}
/* Skip len uncompressed bytes of output. Return -1 on error, 0 on success. */
{
unsigned n;
/* skip over len bytes or reach end-of-file, whichever comes first */
while (len)
/* skip over whatever is in output buffer */
len -= n;
}
/* output buffer empty -- return if we're at the end of the input */
break;
/* need more data to skip -- load up output buffer */
else {
/* get more output, looking for header if required */
return -1;
}
return 0;
}
/* -- see zlib.h -- */
unsigned len;
{
unsigned got, n;
/* get internal structure */
return -1;
/* check that we're reading and that there's no (serious) error */
return -1;
/* 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) {
return -1;
}
/* if len is zero, avoid unnecessary operations */
if (len == 0)
return 0;
/* process a skip request */
return -1;
}
/* get len bytes to buf, or less than len if at the end */
got = 0;
do {
/* first just try copying data from the output buffer */
}
/* output buffer empty -- return if we're at the end of the input */
break;
}
/* need output data -- for small len or new stream load up our output
buffer */
/* get more output, looking for header if required */
return -1;
continue; /* no progress yet -- go back to copy above */
/* the copy above assures that we will leave with space in the
output buffer, allowing at least one gzungetc() to succeed */
}
/* large len -- read directly into user buffer */
return -1;
}
/* large len -- decompress directly into user buffer */
else { /* state->how == GZIP */
return -1;
}
/* update progress */
len -= n;
got += n;
} while (len);
/* return number of bytes read into user buffer (will fit in int) */
return (int)got;
}
/* -- see zlib.h -- */
#ifdef Z_PREFIX_SET
#else
#endif
{
int ret;
/* get internal structure */
return -1;
/* check that we're reading and that there's no (serious) error */
return -1;
/* try output buffer (no need to check for skip request) */
}
/* nothing there -- try gzread() */
}
{
}
/* -- see zlib.h -- */
int c;
{
/* get internal structure */
return -1;
/* check that we're reading and that there's no (serious) error */
return -1;
/* process a skip request */
return -1;
}
/* can't push EOF */
if (c < 0)
return -1;
/* if output buffer empty, put byte at end (allows more pushing) */
return c;
}
/* if no room, give up (must have already done a gzungetc()) */
return -1;
}
/* slide output data if needed and insert byte before existing data */
}
return c;
}
/* -- see zlib.h -- */
char *buf;
int len;
{
unsigned left, n;
char *str;
unsigned char *eol;
/* check parameters and get internal structure */
return NULL;
/* check that we're reading and that there's no (serious) error */
return NULL;
/* process a skip request */
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) */
if (left) do {
/* assure that something is in the output buffer */
return NULL; /* error */
break; /* return what we have */
}
/* look for end-of-line in current output buffer */
/* copy through end-of-line, or remainder if not found */
left -= n;
buf += n;
/* return terminated string, or if nothing, end of file */
return NULL;
buf[0] = 0;
return str;
}
/* -- see zlib.h -- */
{
/* get internal structure */
return 0;
/* if the state is not known, but we can find out, then do so (this is
mainly for right after a gzopen() or gzdopen()) */
/* return 1 if transparent, 0 if processing a gzip stream */
}
/* -- see zlib.h -- */
{
/* get internal structure */
return Z_STREAM_ERROR;
/* check that we're reading */
return Z_STREAM_ERROR;
/* free memory and close file */
}
}