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 */
7 unbzip2(int in
, int out
, char *pre
, size_t prelen
, off_t
*bytes_in
)
13 static char *inbuf
, *outbuf
;
16 inbuf
= malloc(BUFLEN
);
18 outbuf
= malloc(BUFLEN
);
19 if (inbuf
== NULL
|| outbuf
== NULL
)
27 ret
= BZ2_bzDecompressInit(&bzs
, 0, 0);
29 maybe_errx("bzip2 init");
32 bzs
.avail_in
= prelen
;
38 while (ret
!= BZ_STREAM_END
) {
39 if (bzs
.avail_in
== 0 && !end_of_file
) {
40 n
= read(in
, inbuf
, BUFLEN
);
51 bzs
.next_out
= outbuf
;
52 bzs
.avail_out
= BUFLEN
;
53 ret
= BZ2_bzDecompress(&bzs
);
58 if (ret
== BZ_OK
&& end_of_file
)
61 n
= write(out
, outbuf
, BUFLEN
- bzs
.avail_out
);
69 maybe_warnx("bzip2 data integrity error");
72 case BZ_DATA_ERROR_MAGIC
:
73 maybe_warnx("bzip2 magic number error");
77 maybe_warnx("bzip2 out of memory");
83 if (ret
!= BZ_STREAM_END
|| BZ2_bzDecompressEnd(&bzs
) != BZ_OK
)