199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 1998 Michael Smith.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * All rights reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
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 * are met:
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 *
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 Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/cdefs.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome__FBSDID("$FreeBSD$");
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "stand.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/stat.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <string.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <zlib.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define Z_BUFSIZE 2048 /* XXX larger? */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct z_file
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int zf_rawfd;
199767f8919635c4928607450d9e0abb932109ceToomas Soome off_t zf_dataoffset;
199767f8919635c4928607450d9e0abb932109ceToomas Soome z_stream zf_zstream;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char zf_buf[Z_BUFSIZE];
199767f8919635c4928607450d9e0abb932109ceToomas Soome int zf_endseen;
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int zf_fill(struct z_file *z);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int zf_open(const char *path, struct open_file *f);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int zf_close(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
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct fs_ops gzipfs_fsops = {
199767f8919635c4928607450d9e0abb932109ceToomas Soome "zip",
199767f8919635c4928607450d9e0abb932109ceToomas Soome zf_open,
199767f8919635c4928607450d9e0abb932109ceToomas Soome zf_close,
199767f8919635c4928607450d9e0abb932109ceToomas Soome zf_read,
199767f8919635c4928607450d9e0abb932109ceToomas Soome null_write,
199767f8919635c4928607450d9e0abb932109ceToomas Soome zf_seek,
199767f8919635c4928607450d9e0abb932109ceToomas Soome zf_stat,
199767f8919635c4928607450d9e0abb932109ceToomas Soome null_readdir
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomezf_fill(struct z_file *zf)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int result;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int req;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome req = Z_BUFSIZE - zf->zf_zstream.avail_in;
199767f8919635c4928607450d9e0abb932109ceToomas Soome result = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* If we need more */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (req > 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* move old data to bottom of buffer */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (req < Z_BUFSIZE)
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(zf->zf_buf + req, zf->zf_buf, Z_BUFSIZE - req);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
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 zf->zf_zstream.next_in = zf->zf_buf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (result >= 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome zf->zf_zstream.avail_in += result;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(result);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Adapted from get_byte/check_header in libz
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Returns 0 if the header is OK, nonzero if not.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomeget_byte(struct z_file *zf, off_t *curoffp)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((zf->zf_zstream.avail_in == 0) && (zf_fill(zf) == -1))
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome zf->zf_zstream.avail_in--;
199767f8919635c4928607450d9e0abb932109ceToomas Soome ++*curoffp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(*(zf->zf_zstream.next_in)++);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
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#define RESERVED 0xE0 /* bits 5..7: reserved */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomecheck_header(struct z_file *zf)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int method; /* method byte */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int flags; /* flags byte */
199767f8919635c4928607450d9e0abb932109ceToomas Soome uInt len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int c;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome zf->zf_dataoffset = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Check the gzip magic header */
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (len = 0; len < 2; len++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome c = get_byte(zf, &zf->zf_dataoffset);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (c != gz_magic[len]) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome method = get_byte(zf, &zf->zf_dataoffset);
199767f8919635c4928607450d9e0abb932109ceToomas Soome flags = get_byte(zf, &zf->zf_dataoffset);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
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
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((flags & EXTRA_FIELD) != 0) { /* skip the extra field */
199767f8919635c4928607450d9e0abb932109ceToomas Soome len = (uInt)get_byte(zf, &zf->zf_dataoffset);
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 }
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 }
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 }
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 }
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* if there's data left, we're in business */
199767f8919635c4928607450d9e0abb932109ceToomas Soome return((c == -1) ? 1 : 0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomezf_open(const char *fname, struct open_file *f)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *zfname;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int rawfd;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct z_file *zf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *cp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int error;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct stat sb;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Have to be in "just read it" mode */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (f->f_flags != F_READ)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(EPERM);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
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 return(ENOENT);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Try to open the compressed datafile */
199767f8919635c4928607450d9e0abb932109ceToomas Soome rawfd = open(fname, O_RDONLY | F_GZIP);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rawfd == -1) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* add .gz sufix and try again */
199767f8919635c4928607450d9e0abb932109ceToomas Soome zfname = malloc(strlen(fname) + 4);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (zfname == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(ENOMEM);
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(zfname, "%s.gz", fname);
199767f8919635c4928607450d9e0abb932109ceToomas Soome rawfd = open(zfname, O_RDONLY);
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(zfname);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rawfd == -1)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(ENOENT);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fstat(rawfd, &sb) < 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("zf_open: stat failed\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome close(rawfd);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(ENOENT);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!S_ISREG(sb.st_mode)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome close(rawfd);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(EISDIR); /* best guess */
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Allocate a z_file structure, populate it */
199767f8919635c4928607450d9e0abb932109ceToomas Soome zf = malloc(sizeof(struct z_file));
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (zf == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(ENOMEM);
199767f8919635c4928607450d9e0abb932109ceToomas Soome bzero(zf, sizeof(struct z_file));
199767f8919635c4928607450d9e0abb932109ceToomas Soome zf->zf_rawfd = rawfd;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Verify that the file is gzipped */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (check_header(zf)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome close(zf->zf_rawfd);
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(zf);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(EFTYPE);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
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 close(zf->zf_rawfd);
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(zf);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Looks OK, we'll take it */
199767f8919635c4928607450d9e0abb932109ceToomas Soome f->f_fsdata = zf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomezf_close(struct open_file *f)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct z_file *zf = (struct z_file *)f->f_fsdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome inflateEnd(&(zf->zf_zstream));
199767f8919635c4928607450d9e0abb932109ceToomas Soome close(zf->zf_rawfd);
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(zf);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomezf_read(struct open_file *f, void *buf, size_t size, size_t *resid)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct z_file *zf = (struct z_file *)f->f_fsdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int error;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome zf->zf_zstream.next_out = buf; /* where and how much */
199767f8919635c4928607450d9e0abb932109ceToomas Soome zf->zf_zstream.avail_out = size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
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 printf("zf_read: fill error\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (zf->zf_zstream.avail_in == 0) { /* oops, unexpected EOF */
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("zf_read: unexpected EOF\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (zf->zf_zstream.avail_out == size)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome error = inflate(&zf->zf_zstream, Z_SYNC_FLUSH); /* decompression pass */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (error == Z_STREAM_END) { /* EOF, all done */
199767f8919635c4928607450d9e0abb932109ceToomas Soome zf->zf_endseen = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (error != Z_OK) { /* argh, decompression error */
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("inflate: %s\n", zf->zf_zstream.msg);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (resid != NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome *resid = zf->zf_zstream.avail_out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomezf_rewind(struct open_file *f)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct z_file *zf = (struct z_file *)f->f_fsdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (lseek(zf->zf_rawfd, zf->zf_dataoffset, SEEK_SET) == -1)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome zf->zf_zstream.avail_in = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome zf->zf_zstream.next_in = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome zf->zf_endseen = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome (void)inflateReset(&zf->zf_zstream);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic off_t
199767f8919635c4928607450d9e0abb932109ceToomas Soomezf_seek(struct open_file *f, off_t offset, int where)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct z_file *zf = (struct z_file *)f->f_fsdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome off_t target;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char discard[16];
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome switch (where) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome case SEEK_SET:
199767f8919635c4928607450d9e0abb932109ceToomas Soome target = offset;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case SEEK_CUR:
199767f8919635c4928607450d9e0abb932109ceToomas Soome target = offset + zf->zf_zstream.total_out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case SEEK_END:
199767f8919635c4928607450d9e0abb932109ceToomas Soome target = -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome default:
199767f8919635c4928607450d9e0abb932109ceToomas Soome errno = EINVAL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* rewind if required */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (target < zf->zf_zstream.total_out && zf_rewind(f) != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* skip forwards if required */
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (target > zf->zf_zstream.total_out) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome errno = zf_read(f, discard, min(sizeof(discard),
199767f8919635c4928607450d9e0abb932109ceToomas Soome target - zf->zf_zstream.total_out), NULL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (errno)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* This is where we are (be honest if we overshot) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(zf->zf_zstream.total_out);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomezf_stat(struct open_file *f, struct stat *sb)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct z_file *zf = (struct z_file *)f->f_fsdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int result, res;
199767f8919635c4928607450d9e0abb932109ceToomas Soome off_t pos1, pos2;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* stat as normal, but indicate that size is unknown */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((result = fstat(zf->zf_rawfd, sb)) == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (sb->st_size == -1)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (result);
199767f8919635c4928607450d9e0abb932109ceToomas Soome pos1 = lseek(zf->zf_rawfd, 0, SEEK_CUR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome pos2 = lseek(zf->zf_rawfd, sb->st_size - 4, SEEK_SET);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (pos2 != -1) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (read(zf->zf_rawfd, &size, 4) == 4)
199767f8919635c4928607450d9e0abb932109ceToomas Soome sb->st_size = (off_t) size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome sb->st_size = -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else
199767f8919635c4928607450d9e0abb932109ceToomas Soome sb->st_size = -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome pos1 = lseek(zf->zf_rawfd, pos1, SEEK_SET);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(result);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}