xref: /titanic_52/usr/src/boot/lib/libstand/bzipfs.c (revision 5c420b564d29aecc3d2a4e4b8718a21a952cab9b)
1*4a5d661aSToomas Soome /*
2*4a5d661aSToomas Soome  * Copyright (c) 1998 Michael Smith.
3*4a5d661aSToomas Soome  * Copyright (c) 2000 Maxim Sobolev
4*4a5d661aSToomas Soome  * All rights reserved.
5*4a5d661aSToomas Soome  *
6*4a5d661aSToomas Soome  * Redistribution and use in source and binary forms, with or without
7*4a5d661aSToomas Soome  * modification, are permitted provided that the following conditions
8*4a5d661aSToomas Soome  * are met:
9*4a5d661aSToomas Soome  * 1. Redistributions of source code must retain the above copyright
10*4a5d661aSToomas Soome  *    notice, this list of conditions and the following disclaimer.
11*4a5d661aSToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
12*4a5d661aSToomas Soome  *    notice, this list of conditions and the following disclaimer in the
13*4a5d661aSToomas Soome  *    documentation and/or other materials provided with the distribution.
14*4a5d661aSToomas Soome  *
15*4a5d661aSToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16*4a5d661aSToomas Soome  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17*4a5d661aSToomas Soome  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18*4a5d661aSToomas Soome  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19*4a5d661aSToomas Soome  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20*4a5d661aSToomas Soome  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21*4a5d661aSToomas Soome  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22*4a5d661aSToomas Soome  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23*4a5d661aSToomas Soome  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24*4a5d661aSToomas Soome  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25*4a5d661aSToomas Soome  * SUCH DAMAGE.
26*4a5d661aSToomas Soome  */
27*4a5d661aSToomas Soome 
28*4a5d661aSToomas Soome #include <sys/cdefs.h>
29*4a5d661aSToomas Soome 
30*4a5d661aSToomas Soome #ifndef REGRESSION
31*4a5d661aSToomas Soome #include "stand.h"
32*4a5d661aSToomas Soome #else
33*4a5d661aSToomas Soome #include <stdlib.h>
34*4a5d661aSToomas Soome #include <sys/errno.h>
35*4a5d661aSToomas Soome #include <sys/fcntl.h>
36*4a5d661aSToomas Soome #include <sys/types.h>
37*4a5d661aSToomas Soome #include <sys/unistd.h>
38*4a5d661aSToomas Soome 
39*4a5d661aSToomas Soome struct open_file {
40*4a5d661aSToomas Soome     int                 f_flags;        /* see F_* below */
41*4a5d661aSToomas Soome     void                *f_fsdata;      /* file system specific data */
42*4a5d661aSToomas Soome };
43*4a5d661aSToomas Soome #define F_READ          0x0001  /* file opened for reading */
44*4a5d661aSToomas Soome #define EOFFSET (ELAST+8)       /* relative seek not supported */
45*4a5d661aSToomas Soome static inline u_int min(u_int a, u_int b) { return(a < b ? a : b); }
46*4a5d661aSToomas Soome #define panic(x, y) abort()
47*4a5d661aSToomas Soome #endif
48*4a5d661aSToomas Soome 
49*4a5d661aSToomas Soome #include <sys/stat.h>
50*4a5d661aSToomas Soome #include <string.h>
51*4a5d661aSToomas Soome #include <bzlib.h>
52*4a5d661aSToomas Soome 
53*4a5d661aSToomas Soome #define BZ_BUFSIZE 2048	/* XXX larger? */
54*4a5d661aSToomas Soome 
55*4a5d661aSToomas Soome struct bz_file
56*4a5d661aSToomas Soome {
57*4a5d661aSToomas Soome     int			bzf_rawfd;
58*4a5d661aSToomas Soome     bz_stream		bzf_bzstream;
59*4a5d661aSToomas Soome     char		bzf_buf[BZ_BUFSIZE];
60*4a5d661aSToomas Soome     int			bzf_endseen;
61*4a5d661aSToomas Soome };
62*4a5d661aSToomas Soome 
63*4a5d661aSToomas Soome static int	bzf_fill(struct bz_file *z);
64*4a5d661aSToomas Soome static int	bzf_open(const char *path, struct open_file *f);
65*4a5d661aSToomas Soome static int	bzf_close(struct open_file *f);
66*4a5d661aSToomas Soome static int	bzf_read(struct open_file *f, void *buf, size_t size, size_t *resid);
67*4a5d661aSToomas Soome static off_t	bzf_seek(struct open_file *f, off_t offset, int where);
68*4a5d661aSToomas Soome static int	bzf_stat(struct open_file *f, struct stat *sb);
69*4a5d661aSToomas Soome 
70*4a5d661aSToomas Soome #ifndef REGRESSION
71*4a5d661aSToomas Soome struct fs_ops bzipfs_fsops = {
72*4a5d661aSToomas Soome     "bzip",
73*4a5d661aSToomas Soome     bzf_open,
74*4a5d661aSToomas Soome     bzf_close,
75*4a5d661aSToomas Soome     bzf_read,
76*4a5d661aSToomas Soome     null_write,
77*4a5d661aSToomas Soome     bzf_seek,
78*4a5d661aSToomas Soome     bzf_stat,
79*4a5d661aSToomas Soome     null_readdir
80*4a5d661aSToomas Soome };
81*4a5d661aSToomas Soome #endif
82*4a5d661aSToomas Soome 
83*4a5d661aSToomas Soome static int
84*4a5d661aSToomas Soome bzf_fill(struct bz_file *bzf)
85*4a5d661aSToomas Soome {
86*4a5d661aSToomas Soome     int		result;
87*4a5d661aSToomas Soome     int		req;
88*4a5d661aSToomas Soome 
89*4a5d661aSToomas Soome     req = BZ_BUFSIZE - bzf->bzf_bzstream.avail_in;
90*4a5d661aSToomas Soome     result = 0;
91*4a5d661aSToomas Soome 
92*4a5d661aSToomas Soome     /* If we need more */
93*4a5d661aSToomas Soome     if (req > 0) {
94*4a5d661aSToomas Soome 	/* move old data to bottom of buffer */
95*4a5d661aSToomas Soome 	if (req < BZ_BUFSIZE)
96*4a5d661aSToomas Soome 	    bcopy(bzf->bzf_buf + req, bzf->bzf_buf, BZ_BUFSIZE - req);
97*4a5d661aSToomas Soome 
98*4a5d661aSToomas Soome 	/* read to fill buffer and update availibility data */
99*4a5d661aSToomas Soome 	result = read(bzf->bzf_rawfd, bzf->bzf_buf + bzf->bzf_bzstream.avail_in, req);
100*4a5d661aSToomas Soome 	bzf->bzf_bzstream.next_in = bzf->bzf_buf;
101*4a5d661aSToomas Soome 	if (result >= 0)
102*4a5d661aSToomas Soome 	    bzf->bzf_bzstream.avail_in += result;
103*4a5d661aSToomas Soome     }
104*4a5d661aSToomas Soome     return(result);
105*4a5d661aSToomas Soome }
106*4a5d661aSToomas Soome 
107*4a5d661aSToomas Soome /*
108*4a5d661aSToomas Soome  * Adapted from get_byte/check_header in libz
109*4a5d661aSToomas Soome  *
110*4a5d661aSToomas Soome  * Returns 0 if the header is OK, nonzero if not.
111*4a5d661aSToomas Soome  */
112*4a5d661aSToomas Soome static int
113*4a5d661aSToomas Soome get_byte(struct bz_file *bzf)
114*4a5d661aSToomas Soome {
115*4a5d661aSToomas Soome     if ((bzf->bzf_bzstream.avail_in == 0) && (bzf_fill(bzf) == -1))
116*4a5d661aSToomas Soome 	return(-1);
117*4a5d661aSToomas Soome     bzf->bzf_bzstream.avail_in--;
118*4a5d661aSToomas Soome     return(*(bzf->bzf_bzstream.next_in)++);
119*4a5d661aSToomas Soome }
120*4a5d661aSToomas Soome 
121*4a5d661aSToomas Soome static int bz_magic[3] = {'B', 'Z', 'h'}; /* bzip2 magic header */
122*4a5d661aSToomas Soome 
123*4a5d661aSToomas Soome static int
124*4a5d661aSToomas Soome check_header(struct bz_file *bzf)
125*4a5d661aSToomas Soome {
126*4a5d661aSToomas Soome     unsigned int len;
127*4a5d661aSToomas Soome     int		 c;
128*4a5d661aSToomas Soome 
129*4a5d661aSToomas Soome     /* Check the bzip2 magic header */
130*4a5d661aSToomas Soome     for (len = 0; len < 3; len++) {
131*4a5d661aSToomas Soome 	c = get_byte(bzf);
132*4a5d661aSToomas Soome 	if (c != bz_magic[len]) {
133*4a5d661aSToomas Soome 	    return(1);
134*4a5d661aSToomas Soome 	}
135*4a5d661aSToomas Soome     }
136*4a5d661aSToomas Soome     /* Check that the block size is valid */
137*4a5d661aSToomas Soome     c = get_byte(bzf);
138*4a5d661aSToomas Soome     if (c < '1' || c > '9')
139*4a5d661aSToomas Soome 	return(1);
140*4a5d661aSToomas Soome 
141*4a5d661aSToomas Soome     /* Put back bytes that we've took from the input stream */
142*4a5d661aSToomas Soome     bzf->bzf_bzstream.next_in -= 4;
143*4a5d661aSToomas Soome     bzf->bzf_bzstream.avail_in += 4;
144*4a5d661aSToomas Soome 
145*4a5d661aSToomas Soome     return(0);
146*4a5d661aSToomas Soome }
147*4a5d661aSToomas Soome 
148*4a5d661aSToomas Soome static int
149*4a5d661aSToomas Soome bzf_open(const char *fname, struct open_file *f)
150*4a5d661aSToomas Soome {
151*4a5d661aSToomas Soome     static char		*bzfname;
152*4a5d661aSToomas Soome     int			rawfd;
153*4a5d661aSToomas Soome     struct bz_file	*bzf;
154*4a5d661aSToomas Soome     char		*cp;
155*4a5d661aSToomas Soome     int			error;
156*4a5d661aSToomas Soome     struct stat		sb;
157*4a5d661aSToomas Soome 
158*4a5d661aSToomas Soome     /* Have to be in "just read it" mode */
159*4a5d661aSToomas Soome     if (f->f_flags != F_READ)
160*4a5d661aSToomas Soome 	return(EPERM);
161*4a5d661aSToomas Soome 
162*4a5d661aSToomas Soome     /* If the name already ends in .gz or .bz2, ignore it */
163*4a5d661aSToomas Soome     if ((cp = strrchr(fname, '.')) && (!strcmp(cp, ".gz")
164*4a5d661aSToomas Soome 	    || !strcmp(cp, ".bz2") || !strcmp(cp, ".split")))
165*4a5d661aSToomas Soome 	return(ENOENT);
166*4a5d661aSToomas Soome 
167*4a5d661aSToomas Soome     /* Construct new name */
168*4a5d661aSToomas Soome     bzfname = malloc(strlen(fname) + 5);
169*4a5d661aSToomas Soome     if (bzfname == NULL)
170*4a5d661aSToomas Soome 	return(ENOMEM);
171*4a5d661aSToomas Soome     sprintf(bzfname, "%s.bz2", fname);
172*4a5d661aSToomas Soome 
173*4a5d661aSToomas Soome     /* Try to open the compressed datafile */
174*4a5d661aSToomas Soome     rawfd = open(bzfname, O_RDONLY);
175*4a5d661aSToomas Soome     free(bzfname);
176*4a5d661aSToomas Soome     if (rawfd == -1)
177*4a5d661aSToomas Soome 	return(ENOENT);
178*4a5d661aSToomas Soome 
179*4a5d661aSToomas Soome     if (fstat(rawfd, &sb) < 0) {
180*4a5d661aSToomas Soome 	printf("bzf_open: stat failed\n");
181*4a5d661aSToomas Soome 	close(rawfd);
182*4a5d661aSToomas Soome 	return(ENOENT);
183*4a5d661aSToomas Soome     }
184*4a5d661aSToomas Soome     if (!S_ISREG(sb.st_mode)) {
185*4a5d661aSToomas Soome 	printf("bzf_open: not a file\n");
186*4a5d661aSToomas Soome 	close(rawfd);
187*4a5d661aSToomas Soome 	return(EISDIR);			/* best guess */
188*4a5d661aSToomas Soome     }
189*4a5d661aSToomas Soome 
190*4a5d661aSToomas Soome     /* Allocate a bz_file structure, populate it */
191*4a5d661aSToomas Soome     bzf = malloc(sizeof(struct bz_file));
192*4a5d661aSToomas Soome     if (bzf == NULL)
193*4a5d661aSToomas Soome 	return(ENOMEM);
194*4a5d661aSToomas Soome     bzero(bzf, sizeof(struct bz_file));
195*4a5d661aSToomas Soome     bzf->bzf_rawfd = rawfd;
196*4a5d661aSToomas Soome 
197*4a5d661aSToomas Soome     /* Verify that the file is bzipped */
198*4a5d661aSToomas Soome     if (check_header(bzf)) {
199*4a5d661aSToomas Soome 	close(bzf->bzf_rawfd);
200*4a5d661aSToomas Soome 	free(bzf);
201*4a5d661aSToomas Soome 	return(EFTYPE);
202*4a5d661aSToomas Soome     }
203*4a5d661aSToomas Soome 
204*4a5d661aSToomas Soome     /* Initialise the inflation engine */
205*4a5d661aSToomas Soome     if ((error = BZ2_bzDecompressInit(&(bzf->bzf_bzstream), 0, 1)) != BZ_OK) {
206*4a5d661aSToomas Soome 	printf("bzf_open: BZ2_bzDecompressInit returned %d\n", error);
207*4a5d661aSToomas Soome 	close(bzf->bzf_rawfd);
208*4a5d661aSToomas Soome 	free(bzf);
209*4a5d661aSToomas Soome 	return(EIO);
210*4a5d661aSToomas Soome     }
211*4a5d661aSToomas Soome 
212*4a5d661aSToomas Soome     /* Looks OK, we'll take it */
213*4a5d661aSToomas Soome     f->f_fsdata = bzf;
214*4a5d661aSToomas Soome     return(0);
215*4a5d661aSToomas Soome }
216*4a5d661aSToomas Soome 
217*4a5d661aSToomas Soome static int
218*4a5d661aSToomas Soome bzf_close(struct open_file *f)
219*4a5d661aSToomas Soome {
220*4a5d661aSToomas Soome     struct bz_file	*bzf = (struct bz_file *)f->f_fsdata;
221*4a5d661aSToomas Soome 
222*4a5d661aSToomas Soome     BZ2_bzDecompressEnd(&(bzf->bzf_bzstream));
223*4a5d661aSToomas Soome     close(bzf->bzf_rawfd);
224*4a5d661aSToomas Soome     free(bzf);
225*4a5d661aSToomas Soome     return(0);
226*4a5d661aSToomas Soome }
227*4a5d661aSToomas Soome 
228*4a5d661aSToomas Soome static int
229*4a5d661aSToomas Soome bzf_read(struct open_file *f, void *buf, size_t size, size_t *resid)
230*4a5d661aSToomas Soome {
231*4a5d661aSToomas Soome     struct bz_file	*bzf = (struct bz_file *)f->f_fsdata;
232*4a5d661aSToomas Soome     int			error;
233*4a5d661aSToomas Soome 
234*4a5d661aSToomas Soome     bzf->bzf_bzstream.next_out = buf;			/* where and how much */
235*4a5d661aSToomas Soome     bzf->bzf_bzstream.avail_out = size;
236*4a5d661aSToomas Soome 
237*4a5d661aSToomas Soome     while (bzf->bzf_bzstream.avail_out && bzf->bzf_endseen == 0) {
238*4a5d661aSToomas Soome 	if ((bzf->bzf_bzstream.avail_in == 0) && (bzf_fill(bzf) == -1)) {
239*4a5d661aSToomas Soome 	    printf("bzf_read: fill error\n");
240*4a5d661aSToomas Soome 	    return(EIO);
241*4a5d661aSToomas Soome 	}
242*4a5d661aSToomas Soome 	if (bzf->bzf_bzstream.avail_in == 0) {		/* oops, unexpected EOF */
243*4a5d661aSToomas Soome 	    printf("bzf_read: unexpected EOF\n");
244*4a5d661aSToomas Soome 	    if (bzf->bzf_bzstream.avail_out == size)
245*4a5d661aSToomas Soome 		return(EIO);
246*4a5d661aSToomas Soome 	    break;
247*4a5d661aSToomas Soome 	}
248*4a5d661aSToomas Soome 
249*4a5d661aSToomas Soome 	error = BZ2_bzDecompress(&bzf->bzf_bzstream);	/* decompression pass */
250*4a5d661aSToomas Soome 	if (error == BZ_STREAM_END) {			/* EOF, all done */
251*4a5d661aSToomas Soome 	    bzf->bzf_endseen = 1;
252*4a5d661aSToomas Soome 	    break;
253*4a5d661aSToomas Soome 	}
254*4a5d661aSToomas Soome 	if (error != BZ_OK) {				/* argh, decompression error */
255*4a5d661aSToomas Soome 	    printf("bzf_read: BZ2_bzDecompress returned %d\n", error);
256*4a5d661aSToomas Soome 	    return(EIO);
257*4a5d661aSToomas Soome 	}
258*4a5d661aSToomas Soome     }
259*4a5d661aSToomas Soome     if (resid != NULL)
260*4a5d661aSToomas Soome 	*resid = bzf->bzf_bzstream.avail_out;
261*4a5d661aSToomas Soome     return(0);
262*4a5d661aSToomas Soome }
263*4a5d661aSToomas Soome 
264*4a5d661aSToomas Soome static int
265*4a5d661aSToomas Soome bzf_rewind(struct open_file *f)
266*4a5d661aSToomas Soome {
267*4a5d661aSToomas Soome     struct bz_file	*bzf = (struct bz_file *)f->f_fsdata;
268*4a5d661aSToomas Soome     struct bz_file	*bzf_tmp;
269*4a5d661aSToomas Soome 
270*4a5d661aSToomas Soome     /*
271*4a5d661aSToomas Soome      * Since bzip2 does not have an equivalent inflateReset function a crude
272*4a5d661aSToomas Soome      * one needs to be provided.  The functions all called in such a way that
273*4a5d661aSToomas Soome      * at any time an error occurs a roll back can be done (effectively making
274*4a5d661aSToomas Soome      * this rewind 'atomic', either the reset occurs successfully or not at all,
275*4a5d661aSToomas Soome      * with no 'undefined' state happening).
276*4a5d661aSToomas Soome      */
277*4a5d661aSToomas Soome 
278*4a5d661aSToomas Soome     /* Allocate a bz_file structure, populate it */
279*4a5d661aSToomas Soome     bzf_tmp = malloc(sizeof(struct bz_file));
280*4a5d661aSToomas Soome     if (bzf_tmp == NULL)
281*4a5d661aSToomas Soome 	return(-1);
282*4a5d661aSToomas Soome     bzero(bzf_tmp, sizeof(struct bz_file));
283*4a5d661aSToomas Soome     bzf_tmp->bzf_rawfd = bzf->bzf_rawfd;
284*4a5d661aSToomas Soome 
285*4a5d661aSToomas Soome     /* Initialise the inflation engine */
286*4a5d661aSToomas Soome     if (BZ2_bzDecompressInit(&(bzf_tmp->bzf_bzstream), 0, 1) != BZ_OK) {
287*4a5d661aSToomas Soome 	free(bzf_tmp);
288*4a5d661aSToomas Soome 	return(-1);
289*4a5d661aSToomas Soome     }
290*4a5d661aSToomas Soome 
291*4a5d661aSToomas Soome     /* Seek back to the beginning of the file */
292*4a5d661aSToomas Soome     if (lseek(bzf->bzf_rawfd, 0, SEEK_SET) == -1) {
293*4a5d661aSToomas Soome 	BZ2_bzDecompressEnd(&(bzf_tmp->bzf_bzstream));
294*4a5d661aSToomas Soome 	free(bzf_tmp);
295*4a5d661aSToomas Soome 	return(-1);
296*4a5d661aSToomas Soome     }
297*4a5d661aSToomas Soome 
298*4a5d661aSToomas Soome     /* Free old bz_file data */
299*4a5d661aSToomas Soome     BZ2_bzDecompressEnd(&(bzf->bzf_bzstream));
300*4a5d661aSToomas Soome     free(bzf);
301*4a5d661aSToomas Soome 
302*4a5d661aSToomas Soome     /* Use the new bz_file data */
303*4a5d661aSToomas Soome     f->f_fsdata = bzf_tmp;
304*4a5d661aSToomas Soome 
305*4a5d661aSToomas Soome     return(0);
306*4a5d661aSToomas Soome }
307*4a5d661aSToomas Soome 
308*4a5d661aSToomas Soome static off_t
309*4a5d661aSToomas Soome bzf_seek(struct open_file *f, off_t offset, int where)
310*4a5d661aSToomas Soome {
311*4a5d661aSToomas Soome     struct bz_file	*bzf = (struct bz_file *)f->f_fsdata;
312*4a5d661aSToomas Soome     off_t		target;
313*4a5d661aSToomas Soome     char		discard[16];
314*4a5d661aSToomas Soome 
315*4a5d661aSToomas Soome     switch (where) {
316*4a5d661aSToomas Soome     case SEEK_SET:
317*4a5d661aSToomas Soome 	target = offset;
318*4a5d661aSToomas Soome 	break;
319*4a5d661aSToomas Soome     case SEEK_CUR:
320*4a5d661aSToomas Soome 	target = offset + bzf->bzf_bzstream.total_out_lo32;
321*4a5d661aSToomas Soome 	break;
322*4a5d661aSToomas Soome     default:
323*4a5d661aSToomas Soome 	errno = EINVAL;
324*4a5d661aSToomas Soome 	return(-1);
325*4a5d661aSToomas Soome     }
326*4a5d661aSToomas Soome 
327*4a5d661aSToomas Soome     /* Can we get there from here? */
328*4a5d661aSToomas Soome     if (target < bzf->bzf_bzstream.total_out_lo32 && bzf_rewind(f) != 0) {
329*4a5d661aSToomas Soome 	errno = EOFFSET;
330*4a5d661aSToomas Soome 	return -1;
331*4a5d661aSToomas Soome     }
332*4a5d661aSToomas Soome 
333*4a5d661aSToomas Soome     /* if bzf_rewind was called then bzf has changed */
334*4a5d661aSToomas Soome     bzf = (struct bz_file *)f->f_fsdata;
335*4a5d661aSToomas Soome 
336*4a5d661aSToomas Soome     /* skip forwards if required */
337*4a5d661aSToomas Soome     while (target > bzf->bzf_bzstream.total_out_lo32) {
338*4a5d661aSToomas Soome 	errno = bzf_read(f, discard, min(sizeof(discard),
339*4a5d661aSToomas Soome 	    target - bzf->bzf_bzstream.total_out_lo32), NULL);
340*4a5d661aSToomas Soome 	if (errno)
341*4a5d661aSToomas Soome 	    return(-1);
342*4a5d661aSToomas Soome     }
343*4a5d661aSToomas Soome     /* This is where we are (be honest if we overshot) */
344*4a5d661aSToomas Soome     return(bzf->bzf_bzstream.total_out_lo32);
345*4a5d661aSToomas Soome }
346*4a5d661aSToomas Soome 
347*4a5d661aSToomas Soome static int
348*4a5d661aSToomas Soome bzf_stat(struct open_file *f, struct stat *sb)
349*4a5d661aSToomas Soome {
350*4a5d661aSToomas Soome     struct bz_file	*bzf = (struct bz_file *)f->f_fsdata;
351*4a5d661aSToomas Soome     int			result;
352*4a5d661aSToomas Soome 
353*4a5d661aSToomas Soome     /* stat as normal, but indicate that size is unknown */
354*4a5d661aSToomas Soome     if ((result = fstat(bzf->bzf_rawfd, sb)) == 0)
355*4a5d661aSToomas Soome 	sb->st_size = -1;
356*4a5d661aSToomas Soome     return(result);
357*4a5d661aSToomas Soome }
358*4a5d661aSToomas Soome 
359*4a5d661aSToomas Soome void
360*4a5d661aSToomas Soome bz_internal_error(int errorcode)
361*4a5d661aSToomas Soome {
362*4a5d661aSToomas Soome     panic("bzipfs: critical error %d in bzip2 library occured\n", errorcode);
363*4a5d661aSToomas Soome }
364*4a5d661aSToomas Soome 
365*4a5d661aSToomas Soome #ifdef REGRESSION
366*4a5d661aSToomas Soome /* Small test case, open and decompress test.bz2 */
367*4a5d661aSToomas Soome int main()
368*4a5d661aSToomas Soome {
369*4a5d661aSToomas Soome     struct open_file f;
370*4a5d661aSToomas Soome     char buf[1024];
371*4a5d661aSToomas Soome     size_t resid;
372*4a5d661aSToomas Soome     int err;
373*4a5d661aSToomas Soome 
374*4a5d661aSToomas Soome     memset(&f, '\0', sizeof(f));
375*4a5d661aSToomas Soome     f.f_flags = F_READ;
376*4a5d661aSToomas Soome     err = bzf_open("test", &f);
377*4a5d661aSToomas Soome     if (err != 0)
378*4a5d661aSToomas Soome 	exit(1);
379*4a5d661aSToomas Soome     do {
380*4a5d661aSToomas Soome 	err = bzf_read(&f, buf, sizeof(buf), &resid);
381*4a5d661aSToomas Soome     } while (err == 0 && resid != sizeof(buf));
382*4a5d661aSToomas Soome 
383*4a5d661aSToomas Soome     if (err != 0)
384*4a5d661aSToomas Soome 	exit(2);
385*4a5d661aSToomas Soome     exit(0);
386*4a5d661aSToomas Soome }
387*4a5d661aSToomas Soome #endif
388