1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
/* unbzip2.c -- decompress files in bzip2 format.
*/
#ifdef RCSID
static char rcsid[] = "$Id: unbzip2.c,v 1.4 2001/06/03 13:43:01 simonb Exp $";
#endif
#define BZ_NO_STDIO
#include <bzlib.h>
#include <stddef.h>
#include "gzip.h"
/* ============================================================================
* Bunzip2 in to out.
*/
int unbzip2(in, out)
int in, out; /* input and output file descriptors */
{
int n, ret, end_of_file;
bz_stream bzs;
bzs.bzalloc = NULL;
bzs.bzfree = NULL;
bzs.opaque = NULL;
end_of_file = 0;
if (BZ2_bzDecompressInit(&bzs, 0, 0) != BZ_OK)
return(ERROR);
/* Use up the remainder of "inbuf" that's been read in already */
bzs.next_in = inbuf;
bzs.avail_in = insize;
while (1) {
if (bzs.avail_in == 0 && !end_of_file) {
n = read(in, inbuf, INBUFSIZ);
if (n < 0)
read_error();
if (n == 0)
end_of_file = 1;
bzs.next_in = inbuf;
bzs.avail_in = n;
}
bzs.next_out = outbuf;
bzs.avail_out = OUTBUFSIZ;
ret = BZ2_bzDecompress(&bzs);
if (ret == BZ_STREAM_END) {
if (!test) {
n = write(out, outbuf, OUTBUFSIZ - bzs.avail_out);
if (n < 0)
write_error();
bytes_out += n;
}
break;
}
else if (ret == BZ_OK) {
if (end_of_file)
read_error();
if (!test)
n = write(out, outbuf, OUTBUFSIZ - bzs.avail_out);
bytes_out += n;
}
else {
switch (ret) {
case BZ_DATA_ERROR:
error("bzip2 data integrity error");
case BZ_DATA_ERROR_MAGIC:
error("bzip2 magic number error");
case BZ_MEM_ERROR:
error("bzip2 out of memory");
}
}
}
if (BZ2_bzDecompressEnd(&bzs) != BZ_OK)
return(ERROR);
return(OK);
}
|