199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 1998 Michael Smith.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * All rights reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Redistribution and use in source and binary forms, with or without
199767f8919635c4928607450d9e0abb932109ceToomas Soome * modification, are permitted provided that the following conditions
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 1. Redistributions of source code must retain the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer in the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * documentation and/or other materials provided with the distribution.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
199767f8919635c4928607450d9e0abb932109ceToomas Soome * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
199767f8919635c4928607450d9e0abb932109ceToomas Soome * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
199767f8919635c4928607450d9e0abb932109ceToomas Soome * SUCH DAMAGE.
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int zf_open(const char *path, struct open_file *f);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int zf_read(struct open_file *f, void *buf, size_t size, size_t *resid);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic off_t zf_seek(struct open_file *f, off_t offset, int where);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int zf_stat(struct open_file *f, struct stat *sb);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* If we need more */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* move old data to bottom of buffer */
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(zf->zf_buf + req, zf->zf_buf, Z_BUFSIZE - req);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* read to fill buffer and update availibility data */
199767f8919635c4928607450d9e0abb932109ceToomas Soome result = read(zf->zf_rawfd, zf->zf_buf + zf->zf_zstream.avail_in, req);
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Adapted from get_byte/check_header in libz
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Returns 0 if the header is OK, nonzero if not.
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((zf->zf_zstream.avail_in == 0) && (zf_fill(zf) == -1))
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* gzip flag byte */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define HEAD_CRC 0x02 /* bit 1 set: header CRC present */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define ORIG_NAME 0x08 /* bit 3 set: original file name present */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define COMMENT 0x10 /* bit 4 set: file comment present */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Check the gzip magic header */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Discard time, xflags and OS code: */
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (len = 0; len < 6; len++) (void)get_byte(zf, &zf->zf_dataoffset);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((flags & EXTRA_FIELD) != 0) { /* skip the extra field */
199767f8919635c4928607450d9e0abb932109ceToomas Soome len += ((uInt)get_byte(zf, &zf->zf_dataoffset))<<8;
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* len is garbage if EOF but the loop below will quit anyway */
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (len-- != 0 && get_byte(zf, &zf->zf_dataoffset) != -1) ;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((flags & ORIG_NAME) != 0) { /* skip the original file name */
199767f8919635c4928607450d9e0abb932109ceToomas Soome while ((c = get_byte(zf, &zf->zf_dataoffset)) != 0 && c != -1) ;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((flags & COMMENT) != 0) { /* skip the .gz file comment */
199767f8919635c4928607450d9e0abb932109ceToomas Soome while ((c = get_byte(zf, &zf->zf_dataoffset)) != 0 && c != -1) ;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((flags & HEAD_CRC) != 0) { /* skip the header crc */
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (len = 0; len < 2; len++) c = get_byte(zf, &zf->zf_dataoffset);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* if there's data left, we're in business */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Have to be in "just read it" mode */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* If the name already ends in .gz or .bz2, ignore it */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((cp = strrchr(fname, '.')) && (!strcmp(cp, ".gz")
199767f8919635c4928607450d9e0abb932109ceToomas Soome || !strcmp(cp, ".bz2") || !strcmp(cp, ".split")))
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Try to open the compressed datafile */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* add .gz sufix and try again */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Allocate a z_file structure, populate it */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Verify that the file is gzipped */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Initialise the inflation engine */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((error = inflateInit2(&(zf->zf_zstream), -15)) != Z_OK) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("zf_open: inflateInit returned %d : %s\n", error, zf->zf_zstream.msg);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Looks OK, we'll take it */
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct z_file *zf = (struct z_file *)f->f_fsdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soomezf_read(struct open_file *f, void *buf, size_t size, size_t *resid)
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct z_file *zf = (struct z_file *)f->f_fsdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome zf->zf_zstream.next_out = buf; /* where and how much */
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (zf->zf_zstream.avail_out && zf->zf_endseen == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((zf->zf_zstream.avail_in == 0) && (zf_fill(zf) == -1)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (zf->zf_zstream.avail_in == 0) { /* oops, unexpected EOF */
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = inflate(&zf->zf_zstream, Z_SYNC_FLUSH); /* decompression pass */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (error == Z_STREAM_END) { /* EOF, all done */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (error != Z_OK) { /* argh, decompression error */
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct z_file *zf = (struct z_file *)f->f_fsdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (lseek(zf->zf_rawfd, zf->zf_dataoffset, SEEK_SET) == -1)
199767f8919635c4928607450d9e0abb932109ceToomas Soomezf_seek(struct open_file *f, off_t offset, int where)
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct z_file *zf = (struct z_file *)f->f_fsdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* rewind if required */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (target < zf->zf_zstream.total_out && zf_rewind(f) != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* skip forwards if required */
199767f8919635c4928607450d9e0abb932109ceToomas Soome errno = zf_read(f, discard, min(sizeof(discard),
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* This is where we are (be honest if we overshot) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct z_file *zf = (struct z_file *)f->f_fsdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* stat as normal, but indicate that size is unknown */