Fix WARNS6 regression.
[dragonfly.git] / usr.bin / gzip / unbzip2.c
blob696cecaa2c8465ecfbd7d490327fde9c5f828f87
1 /* $NetBSD: unbzip2.c,v 1.5 2004/05/25 04:34:40 mrg Exp $ */
2 /* $DragonFly: src/usr.bin/gzip/unbzip2.c,v 1.1 2004/10/26 11:19:31 joerg Exp $ */
4 /* This file is #included by gzip.c */
6 static off_t
7 unbzip2(int in, int out, char *pre, size_t prelen, off_t *bytes_in)
9 int ret, end_of_file;
10 ssize_t n;
11 off_t bytes_out = 0;
12 bz_stream bzs;
13 static char *inbuf, *outbuf;
15 if (inbuf == NULL)
16 inbuf = malloc(BUFLEN);
17 if (outbuf == NULL)
18 outbuf = malloc(BUFLEN);
19 if (inbuf == NULL || outbuf == NULL)
20 maybe_err("malloc");
22 bzs.bzalloc = NULL;
23 bzs.bzfree = NULL;
24 bzs.opaque = NULL;
26 end_of_file = 0;
27 ret = BZ2_bzDecompressInit(&bzs, 0, 0);
28 if (ret != BZ_OK)
29 maybe_errx("bzip2 init");
31 /* Prepend. */
32 bzs.avail_in = prelen;
33 bzs.next_in = pre;
35 if (bytes_in)
36 *bytes_in = prelen;
38 while (ret != BZ_STREAM_END) {
39 if (bzs.avail_in == 0 && !end_of_file) {
40 n = read(in, inbuf, BUFLEN);
41 if (n < 0)
42 maybe_err("read");
43 if (n == 0)
44 end_of_file = 1;
45 bzs.next_in = inbuf;
46 bzs.avail_in = n;
47 if (bytes_in)
48 *bytes_in += n;
51 bzs.next_out = outbuf;
52 bzs.avail_out = BUFLEN;
53 ret = BZ2_bzDecompress(&bzs);
55 switch (ret) {
56 case BZ_STREAM_END:
57 case BZ_OK:
58 if (ret == BZ_OK && end_of_file)
59 maybe_err("read");
60 if (!tflag) {
61 n = write(out, outbuf, BUFLEN - bzs.avail_out);
62 if (n < 0)
63 maybe_err("write");
65 bytes_out += n;
66 break;
68 case BZ_DATA_ERROR:
69 maybe_warnx("bzip2 data integrity error");
70 break;
72 case BZ_DATA_ERROR_MAGIC:
73 maybe_warnx("bzip2 magic number error");
74 break;
76 case BZ_MEM_ERROR:
77 maybe_warnx("bzip2 out of memory");
78 break;
83 if (ret != BZ_STREAM_END || BZ2_bzDecompressEnd(&bzs) != BZ_OK)
84 return (-1);
86 return (bytes_out);