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