8 #define debug(...) fprintf(stderr, __VA_ARGS__);
10 char *bunzip2(char *input, int input_len, int *output_len)
12 bz_stream *stream = calloc(1, sizeof(bz_stream));
14 switch (BZ2_bzDecompressInit(stream, 0, 0)) {
15 case BZ_CONFIG_ERROR: err(1, "the library has been mis-compiled");
16 case BZ_PARAM_ERROR: err(1, "Parameter error");
17 case BZ_MEM_ERROR: err(1, "insufficient memory is available");
18 //case BZ_OK: debug("success\n"); break;
19 //default: debug("unknown\n"); break;
23 int output_size = 512;
27 stream->next_in = input + stream->total_in_lo32;
28 stream->avail_in = input_len - stream->total_in_lo32;
30 output = realloc(output, output_size);
31 //debug("alloc %d\n", output_size);
32 stream->next_out = output + stream->total_out_lo32;
33 stream->avail_out = output_size - stream->total_out_lo32;
34 //debug("decompressing..\n"
38 // " avail_out = %u\n",
42 // stream->avail_out);
43 } while ((status = BZ2_bzDecompress(stream)) == BZ_OK && output_size < 1024*1024);
45 // debug("done with status %d = %d\n", status, BZ_STREAM_END);
47 *output_len = stream->total_out_lo32;
48 BZ2_bzDecompressEnd(stream);
52 int main(int argc, char **argv)
55 printf("usage: ./dec <input> <output>\n");
59 FILE *input = fopen(argv[1], "r");
60 FILE *output = fopen(argv[2], "w+");
61 if (!input) err(1, "error opening input");
62 if (!output) err(1, "error opening output");
66 char *buf = malloc(24);
69 //debug("reading header\n");
70 fread (buf, 1, 24, input);
71 fwrite(buf, 1, 24, output);
73 //debug("reading body\n");
74 while ((st = fread(&size, 1, 4, input))) {
75 //debug("read %u bytes\n", st);
76 //fwrite(&size, 1, 4, output); // DEBUG
77 size = abs(ntohl(size));
80 //debug("size = %x\n", size);
81 if (size > 10*1024*1024)
82 err(1, "sanity check failed, buf is to big: %d", size);
83 buf = realloc(buf, size);
84 fread (buf, 1, size, input);
85 //fwrite(buf, 1, size, output); // DEBUG
88 char *dec = bunzip2(buf, size, &dec_len);
89 // debug("decompressed %u bytes\n", dec_len);
90 fwrite(dec, 1, dec_len, output);