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