2 * Copyright (c) 1998 Michael Smith.
3 * Copyright (c) 2000 Maxim Sobolev
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
35 #include <sys/errno.h>
36 #include <sys/fcntl.h>
37 #include <sys/types.h>
38 #include <sys/unistd.h>
41 int f_flags; /* see F_* below */
42 void *f_fsdata; /* file system specific data */
44 #define F_READ 0x0001 /* file opened for reading */
45 #define EOFFSET (ELAST+8) /* relative seek not supported */
46 static inline u_int min(u_int a, u_int b) { return(a < b ? a : b); }
47 #define panic(x, y) abort()
54 #define BZ_BUFSIZE 2048 /* XXX larger? */
59 bz_stream bzf_bzstream;
60 char bzf_buf[BZ_BUFSIZE];
64 static int bzf_fill(struct bz_file *z);
65 static int bzf_open(const char *path, struct open_file *f);
66 static int bzf_close(struct open_file *f);
67 static int bzf_read(struct open_file *f, void *buf, size_t size, size_t *resid);
68 static off_t bzf_seek(struct open_file *f, off_t offset, int where);
69 static int bzf_stat(struct open_file *f, struct stat *sb);
72 struct fs_ops bzipfs_fsops = {
86 calloc(int items, size_t size)
88 return(malloc(items * size));
93 bzf_fill(struct bz_file *bzf)
98 req = BZ_BUFSIZE - bzf->bzf_bzstream.avail_in;
101 /* If we need more */
103 /* move old data to bottom of buffer */
104 if (req < BZ_BUFSIZE)
105 bcopy(bzf->bzf_buf + req, bzf->bzf_buf, BZ_BUFSIZE - req);
107 /* read to fill buffer and update availibility data */
108 result = read(bzf->bzf_rawfd, bzf->bzf_buf + bzf->bzf_bzstream.avail_in, req);
109 bzf->bzf_bzstream.next_in = bzf->bzf_buf;
111 bzf->bzf_bzstream.avail_in += result;
117 * Adapted from get_byte/check_header in libz
119 * Returns 0 if the header is OK, nonzero if not.
122 get_byte(struct bz_file *bzf)
124 if ((bzf->bzf_bzstream.avail_in == 0) && (bzf_fill(bzf) == -1))
126 bzf->bzf_bzstream.avail_in--;
127 return(*(bzf->bzf_bzstream.next_in)++);
130 static int bz_magic[3] = {'B', 'Z', 'h'}; /* bzip2 magic header */
133 check_header(struct bz_file *bzf)
138 /* Check the bzip2 magic header */
139 for (len = 0; len < 3; len++) {
141 if (c != bz_magic[len]) {
145 /* Check that the block size is valid */
147 if (c < '1' || c > '9')
150 /* Put back bytes that we've took from the input stream */
151 bzf->bzf_bzstream.next_in -= 4;
152 bzf->bzf_bzstream.avail_in += 4;
158 bzf_open(const char *fname, struct open_file *f)
160 static char *bzfname;
167 /* Have to be in "just read it" mode */
168 if (f->f_flags != F_READ)
171 /* If the name already ends in .gz or .bz2, ignore it */
172 if ((cp = strrchr(fname, '.')) && (!strcmp(cp, ".gz")
173 || !strcmp(cp, ".bz2") || !strcmp(cp, ".split")))
176 /* Construct new name */
177 bzfname = malloc(strlen(fname) + 5);
180 sprintf(bzfname, "%s.bz2", fname);
182 /* Try to open the compressed datafile */
183 rawfd = open(bzfname, O_RDONLY);
188 if (fstat(rawfd, &sb) < 0) {
189 printf("bzf_open: stat failed\n");
193 if (!S_ISREG(sb.st_mode)) {
194 printf("bzf_open: not a file\n");
196 return(EISDIR); /* best guess */
199 /* Allocate a bz_file structure, populate it */
200 bzf = malloc(sizeof(struct bz_file));
203 bzero(bzf, sizeof(struct bz_file));
204 bzf->bzf_rawfd = rawfd;
206 /* Verify that the file is bzipped */
207 if (check_header(bzf)) {
208 close(bzf->bzf_rawfd);
213 /* Initialise the inflation engine */
214 if ((error = BZ2_bzDecompressInit(&(bzf->bzf_bzstream), 0, 1)) != BZ_OK) {
215 printf("bzf_open: BZ2_bzDecompressInit returned %d\n", error);
216 close(bzf->bzf_rawfd);
221 /* Looks OK, we'll take it */
227 bzf_close(struct open_file *f)
229 struct bz_file *bzf = (struct bz_file *)f->f_fsdata;
231 BZ2_bzDecompressEnd(&(bzf->bzf_bzstream));
232 close(bzf->bzf_rawfd);
238 bzf_read(struct open_file *f, void *buf, size_t size, size_t *resid)
240 struct bz_file *bzf = (struct bz_file *)f->f_fsdata;
243 bzf->bzf_bzstream.next_out = buf; /* where and how much */
244 bzf->bzf_bzstream.avail_out = size;
246 while (bzf->bzf_bzstream.avail_out && bzf->bzf_endseen == 0) {
247 if ((bzf->bzf_bzstream.avail_in == 0) && (bzf_fill(bzf) == -1)) {
248 printf("bzf_read: fill error\n");
251 if (bzf->bzf_bzstream.avail_in == 0) { /* oops, unexpected EOF */
252 printf("bzf_read: unexpected EOF\n");
253 if (bzf->bzf_bzstream.avail_out == size)
258 error = BZ2_bzDecompress(&bzf->bzf_bzstream); /* decompression pass */
259 if (error == BZ_STREAM_END) { /* EOF, all done */
260 bzf->bzf_endseen = 1;
263 if (error != BZ_OK) { /* argh, decompression error */
264 printf("bzf_read: BZ2_bzDecompress returned %d\n", error);
269 *resid = bzf->bzf_bzstream.avail_out;
274 bzf_rewind(struct open_file *f)
276 struct bz_file *bzf = (struct bz_file *)f->f_fsdata;
277 struct bz_file *bzf_tmp;
280 * Since bzip2 does not have an equivalent inflateReset function a crude
281 * one needs to be provided. The functions all called in such a way that
282 * at any time an error occurs a roll back can be done (effectively making
283 * this rewind 'atomic', either the reset occurs successfully or not at all,
284 * with no 'undefined' state happening).
287 /* Allocate a bz_file structure, populate it */
288 bzf_tmp = malloc(sizeof(struct bz_file));
291 bzero(bzf_tmp, sizeof(struct bz_file));
292 bzf_tmp->bzf_rawfd = bzf->bzf_rawfd;
294 /* Initialise the inflation engine */
295 if (BZ2_bzDecompressInit(&(bzf_tmp->bzf_bzstream), 0, 1) != BZ_OK) {
300 /* Seek back to the beginning of the file */
301 if (lseek(bzf->bzf_rawfd, 0, SEEK_SET) == -1) {
302 BZ2_bzDecompressEnd(&(bzf_tmp->bzf_bzstream));
307 /* Free old bz_file data */
308 BZ2_bzDecompressEnd(&(bzf->bzf_bzstream));
311 /* Use the new bz_file data */
312 f->f_fsdata = bzf_tmp;
318 bzf_seek(struct open_file *f, off_t offset, int where)
320 struct bz_file *bzf = (struct bz_file *)f->f_fsdata;
329 target = offset + bzf->bzf_bzstream.total_out_lo32;
338 /* Can we get there from here? */
339 if (target < bzf->bzf_bzstream.total_out_lo32 && bzf_rewind(f) != 0) {
344 /* if bzf_rewind was called then bzf has changed */
345 bzf = (struct bz_file *)f->f_fsdata;
347 /* skip forwards if required */
348 while (target > bzf->bzf_bzstream.total_out_lo32) {
349 errno = bzf_read(f, discard, min(sizeof(discard),
350 target - bzf->bzf_bzstream.total_out_lo32), NULL);
354 /* This is where we are (be honest if we overshot) */
355 return(bzf->bzf_bzstream.total_out_lo32);
359 bzf_stat(struct open_file *f, struct stat *sb)
361 struct bz_file *bzf = (struct bz_file *)f->f_fsdata;
364 /* stat as normal, but indicate that size is unknown */
365 if ((result = fstat(bzf->bzf_rawfd, sb)) == 0)
371 bz_internal_error(int errorcode)
373 panic("bzipfs: critical error %d in bzip2 library occured\n", errorcode);
377 /* Small test case, open and decompress test.bz2 */
385 memset(&f, '\0', sizeof(f));
387 err = bzf_open("test", &f);
391 err = bzf_read(&f, buf, sizeof(buf), &resid);
392 } while (err == 0 && resid != sizeof(buf));