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