#include "stand.h"
#include <sys/stat.h>
#include <string.h>
#include <zlib.h>
#define Z_BUFSIZE 2048
struct z_file
{
int zf_rawfd;
off_t zf_dataoffset;
z_stream zf_zstream;
unsigned char zf_buf[Z_BUFSIZE];
int zf_endseen;
};
static int zf_fill(struct z_file *z);
static int zf_open(const char *path, struct open_file *f);
static int zf_close(struct open_file *f);
static int zf_read(struct open_file *f, void *buf, size_t size, size_t *resid);
static off_t zf_seek(struct open_file *f, off_t offset, int where);
static int zf_stat(struct open_file *f, struct stat *sb);
struct fs_ops gzipfs_fsops = {
.fs_name = "zip",
.fs_flags = 0,
.fo_open = zf_open,
.fo_close = zf_close,
.fo_read = zf_read,
.fo_write = null_write,
.fo_seek = zf_seek,
.fo_stat = zf_stat,
.fo_readdir = null_readdir,
};
static int
zf_fill(struct z_file *zf)
{
int result;
int req;
req = Z_BUFSIZE - zf->zf_zstream.avail_in;
result = 0;
if (req > 0) {
if (req < Z_BUFSIZE)
bcopy(zf->zf_buf + req, zf->zf_buf, Z_BUFSIZE - req);
result = read(zf->zf_rawfd, zf->zf_buf + zf->zf_zstream.avail_in, req);
zf->zf_zstream.next_in = zf->zf_buf;
if (result >= 0)
zf->zf_zstream.avail_in += result;
}
return(result);
}
static int
get_byte(struct z_file *zf, off_t *curoffp)
{
if ((zf->zf_zstream.avail_in == 0) && (zf_fill(zf) == -1))
return(-1);
zf->zf_zstream.avail_in--;
++*curoffp;
return(*(zf->zf_zstream.next_in)++);
}
static int gz_magic[2] = {0x1f, 0x8b};
#define ASCII_FLAG 0x01
#define HEAD_CRC 0x02
#define EXTRA_FIELD 0x04
#define ORIG_NAME 0x08
#define COMMENT 0x10
#define RESERVED 0xE0
static int
check_header(struct z_file *zf)
{
int method;
int flags;
uInt len;
int c;
zf->zf_dataoffset = 0;
for (len = 0; len < 2; len++) {
c = get_byte(zf, &zf->zf_dataoffset);
if (c != gz_magic[len]) {
return(1);
}
}
method = get_byte(zf, &zf->zf_dataoffset);
flags = get_byte(zf, &zf->zf_dataoffset);
if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
return(1);
}
for (len = 0; len < 6; len++) (void)get_byte(zf, &zf->zf_dataoffset);
if ((flags & EXTRA_FIELD) != 0) {
len = (uInt)get_byte(zf, &zf->zf_dataoffset);
len += ((uInt)get_byte(zf, &zf->zf_dataoffset))<<8;
while (len-- != 0 && get_byte(zf, &zf->zf_dataoffset) != -1) ;
}
if ((flags & ORIG_NAME) != 0) {
while ((c = get_byte(zf, &zf->zf_dataoffset)) != 0 && c != -1) ;
}
if ((flags & COMMENT) != 0) {
while ((c = get_byte(zf, &zf->zf_dataoffset)) != 0 && c != -1) ;
}
if ((flags & HEAD_CRC) != 0) {
for (len = 0; len < 2; len++) c = get_byte(zf, &zf->zf_dataoffset);
}
return((c == -1) ? 1 : 0);
}
static int
zf_open(const char *fname, struct open_file *f)
{
static char *zfname;
int rawfd;
struct z_file *zf;
char *cp;
int error;
struct stat sb;
if (f->f_flags != F_READ)
return(EPERM);
if ((cp = strrchr(fname, '.')) && (!strcmp(cp, ".gz")
|| !strcmp(cp, ".bz2") || !strcmp(cp, ".split")))
return(ENOENT);
zfname = malloc(strlen(fname) + 4);
if (zfname == NULL)
return(ENOMEM);
sprintf(zfname, "%s.gz", fname);
rawfd = open(zfname, O_RDONLY);
free(zfname);
if (rawfd == -1)
return(ENOENT);
if (fstat(rawfd, &sb) < 0) {
printf("zf_open: stat failed\n");
close(rawfd);
return(ENOENT);
}
if (!S_ISREG(sb.st_mode)) {
printf("zf_open: not a file\n");
close(rawfd);
return(EISDIR);
}
zf = malloc(sizeof(struct z_file));
if (zf == NULL)
return(ENOMEM);
bzero(zf, sizeof(struct z_file));
zf->zf_rawfd = rawfd;
if (check_header(zf)) {
close(zf->zf_rawfd);
free(zf);
return(EFTYPE);
}
if ((error = inflateInit2(&(zf->zf_zstream), -15)) != Z_OK) {
printf("zf_open: inflateInit returned %d : %s\n", error, zf->zf_zstream.msg);
close(zf->zf_rawfd);
free(zf);
return(EIO);
}
f->f_fsdata = zf;
return(0);
}
static int
zf_close(struct open_file *f)
{
struct z_file *zf = (struct z_file *)f->f_fsdata;
inflateEnd(&(zf->zf_zstream));
close(zf->zf_rawfd);
free(zf);
return(0);
}
static int
zf_read(struct open_file *f, void *buf, size_t size, size_t *resid)
{
struct z_file *zf = (struct z_file *)f->f_fsdata;
int error;
zf->zf_zstream.next_out = buf;
zf->zf_zstream.avail_out = size;
while (zf->zf_zstream.avail_out && zf->zf_endseen == 0) {
if ((zf->zf_zstream.avail_in == 0) && (zf_fill(zf) == -1)) {
printf("zf_read: fill error\n");
return(EIO);
}
if (zf->zf_zstream.avail_in == 0) {
printf("zf_read: unexpected EOF\n");
if (zf->zf_zstream.avail_out == size)
return(EIO);
break;
}
error = inflate(&zf->zf_zstream, Z_SYNC_FLUSH);
if (error == Z_STREAM_END) {
zf->zf_endseen = 1;
break;
}
if (error != Z_OK) {
printf("inflate: %s\n", zf->zf_zstream.msg);
return(EIO);
}
}
if (resid != NULL)
*resid = zf->zf_zstream.avail_out;
return(0);
}
static int
zf_rewind(struct open_file *f)
{
struct z_file *zf = (struct z_file *)f->f_fsdata;
if (lseek(zf->zf_rawfd, zf->zf_dataoffset, SEEK_SET) == -1)
return(-1);
zf->zf_zstream.avail_in = 0;
zf->zf_zstream.next_in = NULL;
zf->zf_endseen = 0;
(void)inflateReset(&zf->zf_zstream);
return(0);
}
static off_t
zf_seek(struct open_file *f, off_t offset, int where)
{
struct z_file *zf = (struct z_file *)f->f_fsdata;
off_t target;
char discard[16];
switch (where) {
case SEEK_SET:
target = offset;
break;
case SEEK_CUR:
target = offset + zf->zf_zstream.total_out;
break;
default:
errno = EINVAL;
return(-1);
}
if (target < zf->zf_zstream.total_out && zf_rewind(f) != 0)
return(-1);
while (target > zf->zf_zstream.total_out) {
errno = zf_read(f, discard, min(sizeof(discard),
target - zf->zf_zstream.total_out), NULL);
if (errno)
return(-1);
if (zf->zf_endseen)
break;
}
return(zf->zf_zstream.total_out);
}
static int
zf_stat(struct open_file *f, struct stat *sb)
{
struct z_file *zf = (struct z_file *)f->f_fsdata;
int result;
if ((result = fstat(zf->zf_rawfd, sb)) == 0)
sb->st_size = -1;
return(result);
}