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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
36 #define Z_BUFSIZE 2048 /* XXX larger? */
43 char zf_buf[Z_BUFSIZE];
46 static int zf_fill(struct z_file *z);
47 static int zf_open(const char *path, struct open_file *f);
48 static int zf_close(struct open_file *f);
49 static int zf_read(struct open_file *f, void *buf, size_t size, size_t *resid);
50 static off_t zf_seek(struct open_file *f, off_t offset, int where);
51 static int zf_stat(struct open_file *f, struct stat *sb);
53 struct fs_ops gzipfs_fsops = {
66 calloc(int items, size_t size)
68 return(malloc(items * size));
73 zf_fill(struct z_file *zf)
78 req = Z_BUFSIZE - zf->zf_zstream.avail_in;
83 /* move old data to bottom of buffer */
85 bcopy(zf->zf_buf + req, zf->zf_buf, Z_BUFSIZE - req);
87 /* read to fill buffer and update availibility data */
88 result = read(zf->zf_rawfd, zf->zf_buf + zf->zf_zstream.avail_in, req);
89 zf->zf_zstream.next_in = zf->zf_buf;
91 zf->zf_zstream.avail_in += result;
97 * Adapted from get_byte/check_header in libz
99 * Returns 0 if the header is OK, nonzero if not.
102 get_byte(struct z_file *zf, off_t *curoffp)
104 if ((zf->zf_zstream.avail_in == 0) && (zf_fill(zf) == -1))
106 zf->zf_zstream.avail_in--;
108 return(*(zf->zf_zstream.next_in)++);
111 static int gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */
114 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
115 #define HEAD_CRC 0x02 /* bit 1 set: header CRC present */
116 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
117 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
118 #define COMMENT 0x10 /* bit 4 set: file comment present */
119 #define RESERVED 0xE0 /* bits 5..7: reserved */
122 check_header(struct z_file *zf)
124 int method; /* method byte */
125 int flags; /* flags byte */
129 zf->zf_dataoffset = 0;
130 /* Check the gzip magic header */
131 for (len = 0; len < 2; len++) {
132 c = get_byte(zf, &zf->zf_dataoffset);
133 if (c != gz_magic[len]) {
137 method = get_byte(zf, &zf->zf_dataoffset);
138 flags = get_byte(zf, &zf->zf_dataoffset);
139 if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
143 /* Discard time, xflags and OS code: */
144 for (len = 0; len < 6; len++) (void)get_byte(zf, &zf->zf_dataoffset);
146 if ((flags & EXTRA_FIELD) != 0) { /* skip the extra field */
147 len = (uInt)get_byte(zf, &zf->zf_dataoffset);
148 len += ((uInt)get_byte(zf, &zf->zf_dataoffset))<<8;
149 /* len is garbage if EOF but the loop below will quit anyway */
150 while (len-- != 0 && get_byte(zf, &zf->zf_dataoffset) != -1) ;
152 if ((flags & ORIG_NAME) != 0) { /* skip the original file name */
153 while ((c = get_byte(zf, &zf->zf_dataoffset)) != 0 && c != -1) ;
155 if ((flags & COMMENT) != 0) { /* skip the .gz file comment */
156 while ((c = get_byte(zf, &zf->zf_dataoffset)) != 0 && c != -1) ;
158 if ((flags & HEAD_CRC) != 0) { /* skip the header crc */
159 for (len = 0; len < 2; len++) c = get_byte(zf, &zf->zf_dataoffset);
161 /* if there's data left, we're in business */
162 return((c == -1) ? 1 : 0);
166 zf_open(const char *fname, struct open_file *f)
175 /* Have to be in "just read it" mode */
176 if (f->f_flags != F_READ)
179 /* If the name already ends in .gz or .bz2, ignore it */
180 if ((cp = strrchr(fname, '.')) && (!strcmp(cp, ".gz")
181 || !strcmp(cp, ".bz2") || !strcmp(cp, ".split")))
184 /* Construct new name */
185 zfname = malloc(strlen(fname) + 4);
188 sprintf(zfname, "%s.gz", fname);
190 /* Try to open the compressed datafile */
191 rawfd = open(zfname, O_RDONLY);
196 if (fstat(rawfd, &sb) < 0) {
197 printf("zf_open: stat failed\n");
201 if (!S_ISREG(sb.st_mode)) {
202 printf("zf_open: not a file\n");
204 return(EISDIR); /* best guess */
207 /* Allocate a z_file structure, populate it */
208 zf = malloc(sizeof(struct z_file));
211 bzero(zf, sizeof(struct z_file));
212 zf->zf_rawfd = rawfd;
214 /* Verify that the file is gzipped (XXX why do this afterwards?) */
215 if (check_header(zf)) {
217 inflateEnd(&(zf->zf_zstream));
222 /* Initialise the inflation engine */
223 if ((error = inflateInit2(&(zf->zf_zstream), -15)) != Z_OK) {
224 printf("zf_open: inflateInit returned %d : %s\n", error, zf->zf_zstream.msg);
230 /* Looks OK, we'll take it */
236 zf_close(struct open_file *f)
238 struct z_file *zf = (struct z_file *)f->f_fsdata;
240 inflateEnd(&(zf->zf_zstream));
247 zf_read(struct open_file *f, void *buf, size_t size, size_t *resid)
249 struct z_file *zf = (struct z_file *)f->f_fsdata;
252 zf->zf_zstream.next_out = buf; /* where and how much */
253 zf->zf_zstream.avail_out = size;
255 while (zf->zf_zstream.avail_out) {
256 if ((zf->zf_zstream.avail_in == 0) && (zf_fill(zf) == -1)) {
257 printf("zf_read: fill error\n");
260 if (zf->zf_zstream.avail_in == 0) { /* oops, unexpected EOF */
261 printf("zf_read: unexpected EOF\n");
262 if (zf->zf_zstream.avail_out == size)
267 error = inflate(&zf->zf_zstream, Z_SYNC_FLUSH); /* decompression pass */
268 if (error == Z_STREAM_END) { /* EOF, all done */
271 if (error != Z_OK) { /* argh, decompression error */
272 printf("inflate: %s\n", zf->zf_zstream.msg);
277 *resid = zf->zf_zstream.avail_out;
282 zf_rewind(struct open_file *f)
284 struct z_file *zf = (struct z_file *)f->f_fsdata;
286 if (lseek(zf->zf_rawfd, zf->zf_dataoffset, SEEK_SET) == -1)
288 zf->zf_zstream.avail_in = 0;
289 zf->zf_zstream.next_in = NULL;
290 (void)inflateReset(&zf->zf_zstream);
296 zf_seek(struct open_file *f, off_t offset, int where)
298 struct z_file *zf = (struct z_file *)f->f_fsdata;
307 target = offset + zf->zf_zstream.total_out;
316 /* rewind if required */
317 if (target < zf->zf_zstream.total_out && zf_rewind(f) != 0)
320 /* skip forwards if required */
321 while (target > zf->zf_zstream.total_out) {
322 errno = zf_read(f, discard, min(sizeof(discard),
323 target - zf->zf_zstream.total_out), NULL);
327 /* This is where we are (be honest if we overshot) */
328 return (zf->zf_zstream.total_out);
333 zf_stat(struct open_file *f, struct stat *sb)
335 struct z_file *zf = (struct z_file *)f->f_fsdata;
338 /* stat as normal, but indicate that size is unknown */
339 if ((result = fstat(zf->zf_rawfd, sb)) == 0)