2 * Copyright (c) 1998 Michael Smith.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * $FreeBSD: src/lib/libstand/gzipfs.c,v 1.11 2002/12/19 19:34:58 jake Exp $
35 #define Z_BUFSIZE 2048 /* XXX larger? */
41 char zf_buf[Z_BUFSIZE];
44 static int zf_fill(struct z_file *z);
45 static int zf_open(const char *path, struct open_file *f);
46 static int zf_close(struct open_file *f);
47 static int zf_read(struct open_file *f, void *buf, size_t size, size_t *resid);
48 static off_t zf_seek(struct open_file *f, off_t offset, int where);
49 static int zf_stat(struct open_file *f, struct stat *sb);
51 struct fs_ops gzipfs_fsops = {
63 zf_fill(struct z_file *zf)
68 req = Z_BUFSIZE - zf->zf_zstream.avail_in;
73 /* move old data to bottom of buffer */
75 bcopy(zf->zf_buf + req, zf->zf_buf, Z_BUFSIZE - req);
77 /* read to fill buffer and update availibility data */
78 result = read(zf->zf_rawfd, zf->zf_buf + zf->zf_zstream.avail_in, req);
79 zf->zf_zstream.next_in = zf->zf_buf;
81 zf->zf_zstream.avail_in += result;
87 * Adapted from get_byte/check_header in libz
89 * Returns 0 if the header is OK, nonzero if not.
92 get_byte(struct z_file *zf)
94 if ((zf->zf_zstream.avail_in == 0) && (zf_fill(zf) == -1))
96 zf->zf_zstream.avail_in--;
97 return(*(zf->zf_zstream.next_in)++);
100 static int gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */
103 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
104 #define HEAD_CRC 0x02 /* bit 1 set: header CRC present */
105 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
106 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
107 #define COMMENT 0x10 /* bit 4 set: file comment present */
108 #define RESERVED 0xE0 /* bits 5..7: reserved */
111 check_header(struct z_file *zf)
113 int method; /* method byte */
114 int flags; /* flags byte */
118 /* Check the gzip magic header */
119 for (len = 0; len < 2; len++) {
121 if (c != gz_magic[len]) {
125 method = get_byte(zf);
126 flags = get_byte(zf);
127 if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
131 /* Discard time, xflags and OS code: */
132 for (len = 0; len < 6; len++) (void)get_byte(zf);
134 if ((flags & EXTRA_FIELD) != 0) { /* skip the extra field */
135 len = (uInt)get_byte(zf);
136 len += ((uInt)get_byte(zf))<<8;
137 /* len is garbage if EOF but the loop below will quit anyway */
138 while (len-- != 0 && get_byte(zf) != -1) ;
140 if ((flags & ORIG_NAME) != 0) { /* skip the original file name */
141 while ((c = get_byte(zf)) != 0 && c != -1) ;
143 if ((flags & COMMENT) != 0) { /* skip the .gz file comment */
144 while ((c = get_byte(zf)) != 0 && c != -1) ;
146 if ((flags & HEAD_CRC) != 0) { /* skip the header crc */
147 for (len = 0; len < 2; len++) c = get_byte(zf);
149 /* if there's data left, we're in business */
150 return((c == -1) ? 1 : 0);
154 zf_open(const char *fname, struct open_file *f)
163 /* Have to be in "just read it" mode */
164 if ((f->f_flags & (F_READ | F_WRITE)) != F_READ)
167 /* If the name already ends in .gz or .bz2, ignore it */
168 if ((cp = strrchr(fname, '.')) && (!strcmp(cp, ".gz")
169 || !strcmp(cp, ".bz2") || !strcmp(cp, ".split")))
172 /* Construct new name */
173 zfname = malloc(strlen(fname) + 4);
176 sprintf(zfname, "%s.gz", fname);
178 /* Try to open the compressed datafile */
179 rawfd = open(zfname, O_RDONLY);
184 if (fstat(rawfd, &sb) < 0) {
185 printf("zf_open: stat failed\n");
189 if (!S_ISREG(sb.st_mode)) {
190 printf("zf_open: not a file\n");
192 return(EISDIR); /* best guess */
195 /* Allocate a z_file structure, populate it */
196 zf = malloc(sizeof(struct z_file));
199 bzero(zf, sizeof(struct z_file));
200 zf->zf_rawfd = rawfd;
202 /* Verify that the file is gzipped (XXX why do this afterwards?) */
203 if (check_header(zf)) {
205 inflateEnd(&(zf->zf_zstream));
210 /* Initialise the inflation engine */
211 if ((error = inflateInit2(&(zf->zf_zstream), -15)) != Z_OK) {
212 printf("zf_open: inflateInit returned %d : %s\n", error, zf->zf_zstream.msg);
218 /* Looks OK, we'll take it */
224 zf_close(struct open_file *f)
226 struct z_file *zf = (struct z_file *)f->f_fsdata;
230 inflateEnd(&(zf->zf_zstream));
238 zf_read(struct open_file *f, void *buf, size_t size, size_t *resid)
240 struct z_file *zf = (struct z_file *)f->f_fsdata;
243 zf->zf_zstream.next_out = buf; /* where and how much */
244 zf->zf_zstream.avail_out = size;
246 while (zf->zf_zstream.avail_out) {
247 if ((zf->zf_zstream.avail_in == 0) && (zf_fill(zf) == -1)) {
248 printf("zf_read: fill error\n");
251 if (zf->zf_zstream.avail_in == 0) { /* oops, unexpected EOF */
252 printf("zf_read: unexpected EOF\n");
256 error = inflate(&zf->zf_zstream, Z_SYNC_FLUSH); /* decompression pass */
257 if (error == Z_STREAM_END) { /* EOF, all done */
260 if (error != Z_OK) { /* argh, decompression error */
261 printf("inflate: %s\n", zf->zf_zstream.msg);
267 *resid = zf->zf_zstream.avail_out;
272 zf_seek(struct open_file *f, off_t offset, int where)
274 struct z_file *zf = (struct z_file *)f->f_fsdata;
283 target = offset + zf->zf_zstream.total_out;
289 /* Can we get there from here? */
290 if (target < zf->zf_zstream.total_out) {
295 /* skip forwards if required */
296 while (target > zf->zf_zstream.total_out) {
297 if (zf_read(f, discard, min(sizeof(discard), target - zf->zf_zstream.total_out), NULL) == -1)
300 /* This is where we are (be honest if we overshot) */
301 return (zf->zf_zstream.total_out);
305 zf_stat(struct open_file *f, struct stat *sb)
307 struct z_file *zf = (struct z_file *)f->f_fsdata;
310 /* stat as normal, but indicate that size is unknown */
311 if ((result = fstat(zf->zf_rawfd, sb)) == 0)