zlib update 1.2.7
[platform/upstream/libwebsockets.git] / win32port / zlib / gzread.c
index 0f8649b..3493d34 100755 (executable)
-/* gzread.c -- zlib functions for reading gzip files\r
- * Copyright (C) 2004, 2005, 2010 Mark Adler\r
- * For conditions of distribution and use, see copyright notice in zlib.h\r
- */\r
-\r
-#include "gzguts.h"\r
-\r
-/* Local functions */\r
-local int gz_load OF((gz_statep, unsigned char *, unsigned, unsigned *));\r
-local int gz_avail OF((gz_statep));\r
-local int gz_next4 OF((gz_statep, unsigned long *));\r
-local int gz_head OF((gz_statep));\r
-local int gz_decomp OF((gz_statep));\r
-local int gz_make OF((gz_statep));\r
-local int gz_skip OF((gz_statep, z_off64_t));\r
-\r
-/* Use read() to load a buffer -- return -1 on error, otherwise 0.  Read from\r
-   state->fd, and update state->eof, state->err, and state->msg as appropriate.\r
-   This function needs to loop on read(), since read() is not guaranteed to\r
-   read the number of bytes requested, depending on the type of descriptor. */\r
-local int gz_load(state, buf, len, have)\r
-    gz_statep state;\r
-    unsigned char *buf;\r
-    unsigned len;\r
-    unsigned *have;\r
-{\r
-    int ret;\r
-\r
-    *have = 0;\r
-    do {\r
-        ret = read(state->fd, buf + *have, len - *have);\r
-        if (ret <= 0)\r
-            break;\r
-        *have += ret;\r
-    } while (*have < len);\r
-    if (ret < 0) {\r
-        gz_error(state, Z_ERRNO, zstrerror());\r
-        return -1;\r
-    }\r
-    if (ret == 0)\r
-        state->eof = 1;\r
-    return 0;\r
-}\r
-\r
-/* Load up input buffer and set eof flag if last data loaded -- return -1 on\r
-   error, 0 otherwise.  Note that the eof flag is set when the end of the input\r
-   file is reached, even though there may be unused data in the buffer.  Once\r
-   that data has been used, no more attempts will be made to read the file.\r
-   gz_avail() assumes that strm->avail_in == 0. */\r
-local int gz_avail(state)\r
-    gz_statep state;\r
-{\r
-    z_streamp strm = &(state->strm);\r
-\r
-    if (state->err != Z_OK)\r
-        return -1;\r
-    if (state->eof == 0) {\r
-        if (gz_load(state, state->in, state->size,\r
-                (unsigned *)&(strm->avail_in)) == -1)\r
-            return -1;\r
-        strm->next_in = state->in;\r
-    }\r
-    return 0;\r
-}\r
-\r
-/* Get next byte from input, or -1 if end or error. */\r
-#define NEXT() ((strm->avail_in == 0 && gz_avail(state) == -1) ? -1 : \\r
-                (strm->avail_in == 0 ? -1 : \\r
-                 (strm->avail_in--, *(strm->next_in)++)))\r
-\r
-/* Get a four-byte little-endian integer and return 0 on success and the value\r
-   in *ret.  Otherwise -1 is returned and *ret is not modified. */\r
-local int gz_next4(state, ret)\r
-    gz_statep state;\r
-    unsigned long *ret;\r
-{\r
-    int ch;\r
-    unsigned long val;\r
-    z_streamp strm = &(state->strm);\r
-\r
-    val = NEXT();\r
-    val += (unsigned)NEXT() << 8;\r
-    val += (unsigned long)NEXT() << 16;\r
-    ch = NEXT();\r
-    if (ch == -1)\r
-        return -1;\r
-    val += (unsigned long)ch << 24;\r
-    *ret = val;\r
-    return 0;\r
-}\r
-\r
-/* Look for gzip header, set up for inflate or copy.  state->have must be zero.\r
-   If this is the first time in, allocate required memory.  state->how will be\r
-   left unchanged if there is no more input data available, will be set to COPY\r
-   if there is no gzip header and direct copying will be performed, or it will\r
-   be set to GZIP for decompression, and the gzip header will be skipped so\r
-   that the next available input data is the raw deflate stream.  If direct\r
-   copying, then leftover input data from the input buffer will be copied to\r
-   the output buffer.  In that case, all further file reads will be directly to\r
-   either the output buffer or a user buffer.  If decompressing, the inflate\r
-   state and the check value will be initialized.  gz_head() will return 0 on\r
-   success or -1 on failure.  Failures may include read errors or gzip header\r
-   errors.  */\r
-local int gz_head(state)\r
-    gz_statep state;\r
-{\r
-    z_streamp strm = &(state->strm);\r
-    int flags;\r
-    unsigned len;\r
-\r
-    /* allocate read buffers and inflate memory */\r
-    if (state->size == 0) {\r
-        /* allocate buffers */\r
-        state->in = malloc(state->want);\r
-        state->out = malloc(state->want << 1);\r
-        if (state->in == NULL || state->out == NULL) {\r
-            if (state->out != NULL)\r
-                free(state->out);\r
-            if (state->in != NULL)\r
-                free(state->in);\r
-            gz_error(state, Z_MEM_ERROR, "out of memory");\r
-            return -1;\r
-        }\r
-        state->size = state->want;\r
-\r
-        /* allocate inflate memory */\r
-        state->strm.zalloc = Z_NULL;\r
-        state->strm.zfree = Z_NULL;\r
-        state->strm.opaque = Z_NULL;\r
-        state->strm.avail_in = 0;\r
-        state->strm.next_in = Z_NULL;\r
-        if (inflateInit2(&(state->strm), -15) != Z_OK) {    /* raw inflate */\r
-            free(state->out);\r
-            free(state->in);\r
-            state->size = 0;\r
-            gz_error(state, Z_MEM_ERROR, "out of memory");\r
-            return -1;\r
-        }\r
-    }\r
-\r
-    /* get some data in the input buffer */\r
-    if (strm->avail_in == 0) {\r
-        if (gz_avail(state) == -1)\r
-            return -1;\r
-        if (strm->avail_in == 0)\r
-            return 0;\r
-    }\r
-\r
-    /* look for the gzip magic header bytes 31 and 139 */\r
-    if (strm->next_in[0] == 31) {\r
-        strm->avail_in--;\r
-        strm->next_in++;\r
-        if (strm->avail_in == 0 && gz_avail(state) == -1)\r
-            return -1;\r
-        if (strm->avail_in && strm->next_in[0] == 139) {\r
-            /* we have a gzip header, woo hoo! */\r
-            strm->avail_in--;\r
-            strm->next_in++;\r
-\r
-            /* skip rest of header */\r
-            if (NEXT() != 8) {      /* compression method */\r
-                gz_error(state, Z_DATA_ERROR, "unknown compression method");\r
-                return -1;\r
-            }\r
-            flags = NEXT();\r
-            if (flags & 0xe0) {     /* reserved flag bits */\r
-                gz_error(state, Z_DATA_ERROR, "unknown header flags set");\r
-                return -1;\r
-            }\r
-            NEXT();                 /* modification time */\r
-            NEXT();\r
-            NEXT();\r
-            NEXT();\r
-            NEXT();                 /* extra flags */\r
-            NEXT();                 /* operating system */\r
-            if (flags & 4) {        /* extra field */\r
-                len = (unsigned)NEXT();\r
-                len += (unsigned)NEXT() << 8;\r
-                while (len--)\r
-                    if (NEXT() < 0)\r
-                        break;\r
-            }\r
-            if (flags & 8)          /* file name */\r
-                while (NEXT() > 0)\r
-                    ;\r
-            if (flags & 16)         /* comment */\r
-                while (NEXT() > 0)\r
-                    ;\r
-            if (flags & 2) {        /* header crc */\r
-                NEXT();\r
-                NEXT();\r
-            }\r
-            /* an unexpected end of file is not checked for here -- it will be\r
-               noticed on the first request for uncompressed data */\r
-\r
-            /* set up for decompression */\r
-            inflateReset(strm);\r
-            strm->adler = crc32(0L, Z_NULL, 0);\r
-            state->how = GZIP;\r
-            state->direct = 0;\r
-            return 0;\r
-        }\r
-        else {\r
-            /* not a gzip file -- save first byte (31) and fall to raw i/o */\r
-            state->out[0] = 31;\r
-            state->have = 1;\r
-        }\r
-    }\r
-\r
-    /* doing raw i/o, save start of raw data for seeking, copy any leftover\r
-       input to output -- this assumes that the output buffer is larger than\r
-       the input buffer, which also assures space for gzungetc() */\r
-    state->raw = state->pos;\r
-    state->next = state->out;\r
-    if (strm->avail_in) {\r
-        memcpy(state->next + state->have, strm->next_in, strm->avail_in);\r
-        state->have += strm->avail_in;\r
-        strm->avail_in = 0;\r
-    }\r
-    state->how = COPY;\r
-    state->direct = 1;\r
-    return 0;\r
-}\r
-\r
-/* Decompress from input to the provided next_out and avail_out in the state.\r
-   If the end of the compressed data is reached, then verify the gzip trailer\r
-   check value and length (modulo 2^32).  state->have and state->next are set\r
-   to point to the just decompressed data, and the crc is updated.  If the\r
-   trailer is verified, state->how is reset to LOOK to look for the next gzip\r
-   stream or raw data, once state->have is depleted.  Returns 0 on success, -1\r
-   on failure.  Failures may include invalid compressed data or a failed gzip\r
-   trailer verification. */\r
-local int gz_decomp(state)\r
-    gz_statep state;\r
-{\r
-    int ret;\r
-    unsigned had;\r
-    unsigned long crc, len;\r
-    z_streamp strm = &(state->strm);\r
-\r
-    /* fill output buffer up to end of deflate stream */\r
-    had = strm->avail_out;\r
-    do {\r
-        /* get more input for inflate() */\r
-        if (strm->avail_in == 0 && gz_avail(state) == -1)\r
-            return -1;\r
-        if (strm->avail_in == 0) {\r
-            gz_error(state, Z_DATA_ERROR, "unexpected end of file");\r
-            return -1;\r
-        }\r
-\r
-        /* decompress and handle errors */\r
-        ret = inflate(strm, Z_NO_FLUSH);\r
-        if (ret == Z_STREAM_ERROR || ret == Z_NEED_DICT) {\r
-            gz_error(state, Z_STREAM_ERROR,\r
-                      "internal error: inflate stream corrupt");\r
-            return -1;\r
-        }\r
-        if (ret == Z_MEM_ERROR) {\r
-            gz_error(state, Z_MEM_ERROR, "out of memory");\r
-            return -1;\r
-        }\r
-        if (ret == Z_DATA_ERROR) {              /* deflate stream invalid */\r
-            gz_error(state, Z_DATA_ERROR,\r
-                      strm->msg == NULL ? "compressed data error" : strm->msg);\r
-            return -1;\r
-        }\r
-    } while (strm->avail_out && ret != Z_STREAM_END);\r
-\r
-    /* update available output and crc check value */\r
-    state->have = had - strm->avail_out;\r
-    state->next = strm->next_out - state->have;\r
-    strm->adler = crc32(strm->adler, state->next, state->have);\r
-\r
-    /* check gzip trailer if at end of deflate stream */\r
-    if (ret == Z_STREAM_END) {\r
-        if (gz_next4(state, &crc) == -1 || gz_next4(state, &len) == -1) {\r
-            gz_error(state, Z_DATA_ERROR, "unexpected end of file");\r
-            return -1;\r
-        }\r
-        if (crc != strm->adler) {\r
-            gz_error(state, Z_DATA_ERROR, "incorrect data check");\r
-            return -1;\r
-        }\r
-        if (len != (strm->total_out & 0xffffffffL)) {\r
-            gz_error(state, Z_DATA_ERROR, "incorrect length check");\r
-            return -1;\r
-        }\r
-        state->how = LOOK;      /* ready for next stream, once have is 0 (leave\r
-                                   state->direct unchanged to remember how) */\r
-    }\r
-\r
-    /* good decompression */\r
-    return 0;\r
-}\r
-\r
-/* Make data and put in the output buffer.  Assumes that state->have == 0.\r
-   Data is either copied from the input file or decompressed from the input\r
-   file depending on state->how.  If state->how is LOOK, then a gzip header is\r
-   looked for (and skipped if found) to determine wither to copy or decompress.\r
-   Returns -1 on error, otherwise 0.  gz_make() will leave state->have as COPY\r
-   or GZIP unless the end of the input file has been reached and all data has\r
-   been processed.  */\r
-local int gz_make(state)\r
-    gz_statep state;\r
-{\r
-    z_streamp strm = &(state->strm);\r
-\r
-    if (state->how == LOOK) {           /* look for gzip header */\r
-        if (gz_head(state) == -1)\r
-            return -1;\r
-        if (state->have)                /* got some data from gz_head() */\r
-            return 0;\r
-    }\r
-    if (state->how == COPY) {           /* straight copy */\r
-        if (gz_load(state, state->out, state->size << 1, &(state->have)) == -1)\r
-            return -1;\r
-        state->next = state->out;\r
-    }\r
-    else if (state->how == GZIP) {      /* decompress */\r
-        strm->avail_out = state->size << 1;\r
-        strm->next_out = state->out;\r
-        if (gz_decomp(state) == -1)\r
-            return -1;\r
-    }\r
-    return 0;\r
-}\r
-\r
-/* Skip len uncompressed bytes of output.  Return -1 on error, 0 on success. */\r
-local int gz_skip(state, len)\r
-    gz_statep state;\r
-    z_off64_t len;\r
-{\r
-    unsigned n;\r
-\r
-    /* skip over len bytes or reach end-of-file, whichever comes first */\r
-    while (len)\r
-        /* skip over whatever is in output buffer */\r
-        if (state->have) {\r
-            n = GT_OFF(state->have) || (z_off64_t)state->have > len ?\r
-                (unsigned)len : state->have;\r
-            state->have -= n;\r
-            state->next += n;\r
-            state->pos += n;\r
-            len -= n;\r
-        }\r
-\r
-        /* output buffer empty -- return if we're at the end of the input */\r
-        else if (state->eof && state->strm.avail_in == 0)\r
-            break;\r
-\r
-        /* need more data to skip -- load up output buffer */\r
-        else {\r
-            /* get more output, looking for header if required */\r
-            if (gz_make(state) == -1)\r
-                return -1;\r
-        }\r
-    return 0;\r
-}\r
-\r
-/* -- see zlib.h -- */\r
-int ZEXPORT gzread(file, buf, len)\r
-    gzFile file;\r
-    voidp buf;\r
-    unsigned len;\r
-{\r
-    unsigned got, n;\r
-    gz_statep state;\r
-    z_streamp strm;\r
-\r
-    /* get internal structure */\r
-    if (file == NULL)\r
-        return -1;\r
-    state = (gz_statep)file;\r
-    strm = &(state->strm);\r
-\r
-    /* check that we're reading and that there's no error */\r
-    if (state->mode != GZ_READ || state->err != Z_OK)\r
-        return -1;\r
-\r
-    /* since an int is returned, make sure len fits in one, otherwise return\r
-       with an error (this avoids the flaw in the interface) */\r
-    if ((int)len < 0) {\r
-        gz_error(state, Z_BUF_ERROR, "requested length does not fit in int");\r
-        return -1;\r
-    }\r
-\r
-    /* if len is zero, avoid unnecessary operations */\r
-    if (len == 0)\r
-        return 0;\r
-\r
-    /* process a skip request */\r
-    if (state->seek) {\r
-        state->seek = 0;\r
-        if (gz_skip(state, state->skip) == -1)\r
-            return -1;\r
-    }\r
-\r
-    /* get len bytes to buf, or less than len if at the end */\r
-    got = 0;\r
-    do {\r
-        /* first just try copying data from the output buffer */\r
-        if (state->have) {\r
-            n = state->have > len ? len : state->have;\r
-            memcpy(buf, state->next, n);\r
-            state->next += n;\r
-            state->have -= n;\r
-        }\r
-\r
-        /* output buffer empty -- return if we're at the end of the input */\r
-        else if (state->eof && strm->avail_in == 0)\r
-            break;\r
-\r
-        /* need output data -- for small len or new stream load up our output\r
-           buffer */\r
-        else if (state->how == LOOK || len < (state->size << 1)) {\r
-            /* get more output, looking for header if required */\r
-            if (gz_make(state) == -1)\r
-                return -1;\r
-            continue;       /* no progress yet -- go back to memcpy() above */\r
-            /* the copy above assures that we will leave with space in the\r
-               output buffer, allowing at least one gzungetc() to succeed */\r
-        }\r
-\r
-        /* large len -- read directly into user buffer */\r
-        else if (state->how == COPY) {      /* read directly */\r
-            if (gz_load(state, buf, len, &n) == -1)\r
-                return -1;\r
-        }\r
-\r
-        /* large len -- decompress directly into user buffer */\r
-        else {  /* state->how == GZIP */\r
-            strm->avail_out = len;\r
-            strm->next_out = buf;\r
-            if (gz_decomp(state) == -1)\r
-                return -1;\r
-            n = state->have;\r
-            state->have = 0;\r
-        }\r
-\r
-        /* update progress */\r
-        len -= n;\r
-        buf = (char *)buf + n;\r
-        got += n;\r
-        state->pos += n;\r
-    } while (len);\r
-\r
-    /* return number of bytes read into user buffer (will fit in int) */\r
-    return (int)got;\r
-}\r
-\r
-/* -- see zlib.h -- */\r
-int ZEXPORT gzgetc(file)\r
-    gzFile file;\r
-{\r
-    int ret;\r
-    unsigned char buf[1];\r
-    gz_statep state;\r
-\r
-    /* get internal structure */\r
-    if (file == NULL)\r
-        return -1;\r
-    state = (gz_statep)file;\r
-\r
-    /* check that we're reading and that there's no error */\r
-    if (state->mode != GZ_READ || state->err != Z_OK)\r
-        return -1;\r
-\r
-    /* try output buffer (no need to check for skip request) */\r
-    if (state->have) {\r
-        state->have--;\r
-        state->pos++;\r
-        return *(state->next)++;\r
-    }\r
-\r
-    /* nothing there -- try gzread() */\r
-    ret = gzread(file, buf, 1);\r
-    return ret < 1 ? -1 : buf[0];\r
-}\r
-\r
-/* -- see zlib.h -- */\r
-int ZEXPORT gzungetc(c, file)\r
-    int c;\r
-    gzFile file;\r
-{\r
-    gz_statep state;\r
-\r
-    /* get internal structure */\r
-    if (file == NULL)\r
-        return -1;\r
-    state = (gz_statep)file;\r
-\r
-    /* check that we're reading and that there's no error */\r
-    if (state->mode != GZ_READ || state->err != Z_OK)\r
-        return -1;\r
-\r
-    /* process a skip request */\r
-    if (state->seek) {\r
-        state->seek = 0;\r
-        if (gz_skip(state, state->skip) == -1)\r
-            return -1;\r
-    }\r
-\r
-    /* can't push EOF */\r
-    if (c < 0)\r
-        return -1;\r
-\r
-    /* if output buffer empty, put byte at end (allows more pushing) */\r
-    if (state->have == 0) {\r
-        state->have = 1;\r
-        state->next = state->out + (state->size << 1) - 1;\r
-        state->next[0] = c;\r
-        state->pos--;\r
-        return c;\r
-    }\r
-\r
-    /* if no room, give up (must have already done a gzungetc()) */\r
-    if (state->have == (state->size << 1)) {\r
-        gz_error(state, Z_BUF_ERROR, "out of room to push characters");\r
-        return -1;\r
-    }\r
-\r
-    /* slide output data if needed and insert byte before existing data */\r
-    if (state->next == state->out) {\r
-        unsigned char *src = state->out + state->have;\r
-        unsigned char *dest = state->out + (state->size << 1);\r
-        while (src > state->out)\r
-            *--dest = *--src;\r
-        state->next = dest;\r
-    }\r
-    state->have++;\r
-    state->next--;\r
-    state->next[0] = c;\r
-    state->pos--;\r
-    return c;\r
-}\r
-\r
-/* -- see zlib.h -- */\r
-char * ZEXPORT gzgets(file, buf, len)\r
-    gzFile file;\r
-    char *buf;\r
-    int len;\r
-{\r
-    unsigned left, n;\r
-    char *str;\r
-    unsigned char *eol;\r
-    gz_statep state;\r
-\r
-    /* check parameters and get internal structure */\r
-    if (file == NULL || buf == NULL || len < 1)\r
-        return NULL;\r
-    state = (gz_statep)file;\r
-\r
-    /* check that we're reading and that there's no error */\r
-    if (state->mode != GZ_READ || state->err != Z_OK)\r
-        return NULL;\r
-\r
-    /* process a skip request */\r
-    if (state->seek) {\r
-        state->seek = 0;\r
-        if (gz_skip(state, state->skip) == -1)\r
-            return NULL;\r
-    }\r
-\r
-    /* copy output bytes up to new line or len - 1, whichever comes first --\r
-       append a terminating zero to the string (we don't check for a zero in\r
-       the contents, let the user worry about that) */\r
-    str = buf;\r
-    left = (unsigned)len - 1;\r
-    if (left) do {\r
-        /* assure that something is in the output buffer */\r
-        if (state->have == 0) {\r
-            if (gz_make(state) == -1)\r
-                return NULL;            /* error */\r
-            if (state->have == 0) {     /* end of file */\r
-                if (buf == str)         /* got bupkus */\r
-                    return NULL;\r
-                break;                  /* got something -- return it */\r
-            }\r
-        }\r
-\r
-        /* look for end-of-line in current output buffer */\r
-        n = state->have > left ? left : state->have;\r
-        eol = memchr(state->next, '\n', n);\r
-        if (eol != NULL)\r
-            n = (unsigned)(eol - state->next) + 1;\r
-\r
-        /* copy through end-of-line, or remainder if not found */\r
-        memcpy(buf, state->next, n);\r
-        state->have -= n;\r
-        state->next += n;\r
-        state->pos += n;\r
-        left -= n;\r
-        buf += n;\r
-    } while (left && eol == NULL);\r
-\r
-    /* found end-of-line or out of space -- terminate string and return it */\r
-    buf[0] = 0;\r
-    return str;\r
-}\r
-\r
-/* -- see zlib.h -- */\r
-int ZEXPORT gzdirect(file)\r
-    gzFile file;\r
-{\r
-    gz_statep state;\r
-\r
-    /* get internal structure */\r
-    if (file == NULL)\r
-        return 0;\r
-    state = (gz_statep)file;\r
-\r
-    /* check that we're reading */\r
-    if (state->mode != GZ_READ)\r
-        return 0;\r
-\r
-    /* if the state is not known, but we can find out, then do so (this is\r
-       mainly for right after a gzopen() or gzdopen()) */\r
-    if (state->how == LOOK && state->have == 0)\r
-        (void)gz_head(state);\r
-\r
-    /* return 1 if reading direct, 0 if decompressing a gzip stream */\r
-    return state->direct;\r
-}\r
-\r
-/* -- see zlib.h -- */\r
-int ZEXPORT gzclose_r(file)\r
-    gzFile file;\r
-{\r
-    int ret;\r
-    gz_statep state;\r
-\r
-    /* get internal structure */\r
-    if (file == NULL)\r
-        return Z_STREAM_ERROR;\r
-    state = (gz_statep)file;\r
-\r
-    /* check that we're reading */\r
-    if (state->mode != GZ_READ)\r
-        return Z_STREAM_ERROR;\r
-\r
-    /* free memory and close file */\r
-    if (state->size) {\r
-        inflateEnd(&(state->strm));\r
-        free(state->out);\r
-        free(state->in);\r
-    }\r
-    gz_error(state, Z_OK, NULL);\r
-    free(state->path);\r
-    ret = close(state->fd);\r
-    free(state);\r
-    return ret ? Z_ERRNO : Z_OK;\r
-}\r
+/* gzread.c -- zlib functions for reading gzip files
+ * Copyright (C) 2004, 2005, 2010, 2011, 2012 Mark Adler
+ * 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));
+local int gz_look OF((gz_statep));
+local int gz_decomp OF((gz_statep));
+local int gz_fetch OF((gz_statep));
+local int gz_skip OF((gz_statep, z_off64_t));
+
+/* 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.
+   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. */
+local int gz_avail(state)
+    gz_statep state;
+{
+    unsigned got;
+    z_streamp strm = &(state->strm);
+
+    if (state->err != Z_OK && state->err != Z_BUF_ERROR)
+        return -1;
+    if (state->eof == 0) {
+        if (strm->avail_in) {       /* copy what's there to the start */
+            unsigned char *p = state->in, *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;
+}
+
+/* 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. */
+local int gz_look(state)
+    gz_statep state;
+{
+    z_streamp strm = &(state->strm);
+
+    /* allocate read buffers and inflate memory */
+    if (state->size == 0) {
+        /* allocate buffers */
+        state->in = malloc(state->want);
+        state->out = malloc(state->want << 1);
+        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;
+        if (inflateInit2(&(state->strm), 15 + 16) != Z_OK) {    /* gunzip */
+            free(state->out);
+            free(state->in);
+            state->size = 0;
+            gz_error(state, Z_MEM_ERROR, "out of memory");
+            return -1;
+        }
+    }
+
+    /* get at least the magic bytes in the input buffer */
+    if (strm->avail_in < 2) {
+        if (gz_avail(state) == -1)
+            return -1;
+        if (strm->avail_in == 0)
+            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) */
+    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;
+    }
+
+    /* 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;
+        state->x.have = 0;
+        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() */
+    state->x.next = state->out;
+    if (strm->avail_in) {
+        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;
+}
+
+/* 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. */
+local int gz_decomp(state)
+    gz_statep state;
+{
+    int ret = Z_OK;
+    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) {
+            gz_error(state, Z_BUF_ERROR, "unexpected end of file");
+            break;
+        }
+
+        /* 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,
+                     "internal error: inflate stream corrupt");
+            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,
+                     strm->msg == NULL ? "compressed data error" : strm->msg);
+            return -1;
+        }
+    } while (strm->avail_out && ret != Z_STREAM_END);
+
+    /* update available output */
+    state->x.have = had - strm->avail_out;
+    state->x.next = strm->next_out - state->x.have;
+
+    /* if the gzip stream completed successfully, look for another */
+    if (ret == Z_STREAM_END)
+        state->how = LOOK;
+
+    /* 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.  */
+local int gz_fetch(state)
+    gz_statep state;
+{
+    z_streamp strm = &(state->strm);
+
+    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 */
+            if (gz_load(state, state->out, state->size << 1, &(state->x.have))
+                    == -1)
+                return -1;
+            state->x.next = state->out;
+            return 0;
+        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;
+        }
+    } while (state->x.have == 0 && (!state->eof || strm->avail_in));
+    return 0;
+}
+
+/* Skip len uncompressed bytes of output.  Return -1 on error, 0 on success. */
+local int gz_skip(state, len)
+    gz_statep state;
+    z_off64_t len;
+{
+    unsigned n;
+
+    /* skip over len bytes or reach end-of-file, whichever comes first */
+    while (len)
+        /* skip over whatever is in output buffer */
+        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;
+        }
+
+        /* 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 */
+            if (gz_fetch(state) == -1)
+                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);
+
+    /* 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))
+        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) {
+        gz_error(state, Z_DATA_ERROR, "requested length does not fit in int");
+        return -1;
+    }
+
+    /* if len is zero, avoid unnecessary operations */
+    if (len == 0)
+        return 0;
+
+    /* 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;
+    do {
+        /* first just try copying data from the output buffer */
+        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;
+        }
+
+        /* output buffer empty -- return if we're at the end of the input */
+        else if (state->eof && strm->avail_in == 0) {
+            state->past = 1;        /* tried to read past end */
+            break;
+        }
+
+        /* need output data -- for small len or new stream load up our output
+           buffer */
+        else if (state->how == LOOK || len < (state->size << 1)) {
+            /* get more output, looking for header if required */
+            if (gz_fetch(state) == -1)
+                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 */
+        else if (state->how == COPY) {      /* read directly */
+            if (gz_load(state, buf, len, &n) == -1)
+                return -1;
+        }
+
+        /* large len -- decompress directly into user buffer */
+        else {  /* state->how == GZIP */
+            strm->avail_out = len;
+            strm->next_out = buf;
+            if (gz_decomp(state) == -1)
+                return -1;
+            n = state->x.have;
+            state->x.have = 0;
+        }
+
+        /* update progress */
+        len -= n;
+        buf = (char *)buf + n;
+        got += n;
+        state->x.pos += n;
+    } while (len);
+
+    /* return number of bytes read into user buffer (will fit in int) */
+    return (int)got;
+}
+
+/* -- see zlib.h -- */
+#undef gzgetc
+int ZEXPORT gzgetc(file)
+    gzFile file;
+{
+    int ret;
+    unsigned char buf[1];
+    gz_statep state;
+
+    /* get internal structure */
+    if (file == NULL)
+        return -1;
+    state = (gz_statep)file;
+
+    /* 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))
+        return -1;
+
+    /* try output buffer (no need to check for skip request) */
+    if (state->x.have) {
+        state->x.have--;
+        state->x.pos++;
+        return *(state->x.next)++;
+    }
+
+    /* nothing there -- try gzread() */
+    ret = gzread(file, buf, 1);
+    return ret < 1 ? -1 : buf[0];
+}
+
+int ZEXPORT gzgetc_(file)
+gzFile file;
+{
+    return gzgetc(file);
+}
+
+/* -- 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;
+
+    /* 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))
+        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) */
+    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--;
+        state->past = 0;
+        return c;
+    }
+
+    /* if no room, give up (must have already done a gzungetc()) */
+    if (state->x.have == (state->size << 1)) {
+        gz_error(state, Z_DATA_ERROR, "out of room to push characters");
+        return -1;
+    }
+
+    /* slide output data if needed and insert byte before existing data */
+    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] = c;
+    state->x.pos--;
+    state->past = 0;
+    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;
+
+    /* check parameters and get internal structure */
+    if (file == NULL || buf == NULL || len < 1)
+        return NULL;
+    state = (gz_statep)file;
+
+    /* 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))
+        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;
+    if (left) do {
+        /* assure that something is in the output buffer */
+        if (state->x.have == 0 && gz_fetch(state) == -1)
+            return NULL;                /* error */
+        if (state->x.have == 0) {       /* end of file */
+            state->past = 1;            /* read past end */
+            break;                      /* return what we have */
+        }
+
+        /* look for end-of-line in current output buffer */
+        n = state->x.have > left ? left : state->x.have;
+        eol = memchr(state->x.next, '\n', n);
+        if (eol != NULL)
+            n = (unsigned)(eol - state->x.next) + 1;
+
+        /* copy through end-of-line, or remainder if not found */
+        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);
+
+    /* return terminated string, or if nothing, end of file */
+    if (buf == str)
+        return NULL;
+    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;
+
+    /* if the state is not known, but we can find out, then do so (this is
+       mainly for right after a gzopen() or gzdopen()) */
+    if (state->mode == GZ_READ && state->how == LOOK && state->x.have == 0)
+        (void)gz_look(state);
+
+    /* return 1 if transparent, 0 if processing a gzip stream */
+    return state->direct;
+}
+
+/* -- see zlib.h -- */
+int ZEXPORT gzclose_r(file)
+    gzFile file;
+{
+    int ret, err;
+    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);
+    }
+    err = state->err == Z_BUF_ERROR ? Z_BUF_ERROR : Z_OK;
+    gz_error(state, Z_OK, NULL);
+    free(state->path);
+    ret = close(state->fd);
+    free(state);
+    return ret ? Z_ERRNO : err;
+}