unbzip2.c revision 1.2
1/*	$NetBSD: unbzip2.c,v 1.2 2004/03/30 09:15:07 mrg Exp $	*/
2
3/* This file is #included by gzip.c */
4
5static off_t
6unbzip2(int in, int out)
7{
8	FILE		*f_in;
9	BZFILE		*b_in;
10	int		bzerror, n;
11	off_t		bytes_out = 0;
12	char            buffer[64 * 1024];
13
14	if ((in = dup(in)) < 0)
15		maybe_err(1, "dup");
16
17	if ((f_in = fdopen(in, "r")) == NULL)
18		maybe_err(1, "fdopen");
19
20	if ((b_in = BZ2_bzReadOpen(&bzerror, f_in, 0, 0, NULL, 0)) == NULL)
21		maybe_err(1, "BZ2_bzReadOpen");
22
23	do {
24		n = BZ2_bzRead(&bzerror, b_in, buffer, sizeof (buffer));
25
26		switch (bzerror) {
27		case BZ_IO_ERROR:
28			maybe_errx(1, "bzip2 I/O error");
29		case BZ_UNEXPECTED_EOF:
30			maybe_errx(1, "bzip2 unexpected end of file");
31		case BZ_DATA_ERROR:
32			maybe_errx(1, "bzip2 data integrity error");
33		case BZ_DATA_ERROR_MAGIC:
34			maybe_errx(1, "bzip2 magic number error");
35		case BZ_MEM_ERROR:
36			maybe_errx(1, "bzip2 out of memory");
37		case BZ_OK:
38		case BZ_STREAM_END:
39			break;
40		default:
41			maybe_errx(1, "bzip2 unknown error");
42		}
43
44		if ((n = write(out, buffer, n)) < 0)
45			maybe_err(1, "write");
46		bytes_out += n;
47	} while (bzerror != BZ_STREAM_END);
48
49	(void)BZ2_bzReadClose(&bzerror, b_in);
50	(void)fclose(f_in);
51
52
53	return (bytes_out);
54}
55