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