blob: b5fae5c40b5dddec3fb2012d0ef0b6f8f58e126d (
plain)
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
84
85
86
87
|
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
* bzip2.c
*
* Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
*/
#include "../internal.h"
#include <bzlib.h>
typedef struct {
istream_comp_t base;
bz_stream strm;
} istream_bzip2_t;
static int precache(istream_t *base)
{
istream_bzip2_t *bzip2 = (istream_bzip2_t *)base;
istream_t *wrapped = ((istream_comp_t *)base)->wrapped;
int ret;
for (;;) {
ret = istream_precache(wrapped);
if (ret != 0)
return ret;
bzip2->strm.next_in = (char *)wrapped->buffer;
bzip2->strm.avail_in = wrapped->buffer_used;
bzip2->strm.next_out = (char *)base->buffer + base->buffer_used;
bzip2->strm.avail_out = BUFSZ - base->buffer_used;
if (bzip2->strm.avail_out < 1)
break;
ret = BZ2_bzDecompress(&bzip2->strm);
if (ret < 0) {
fprintf(stderr, "%s: internal error in bzip2 "
"decompressor.\n",
wrapped->get_filename(wrapped));
return -1;
}
base->buffer_used = BUFSZ - bzip2->strm.avail_out;
wrapped->buffer_offset = wrapped->buffer_used -
bzip2->strm.avail_in;
if (ret == BZ_STREAM_END) {
base->eof = true;
break;
}
}
return 0;
}
static void cleanup(istream_comp_t *base)
{
istream_bzip2_t *bzip2 = (istream_bzip2_t *)base;
BZ2_bzDecompressEnd(&bzip2->strm);
}
istream_comp_t *istream_bzip2_create(const char *filename)
{
istream_bzip2_t *bzip2 = calloc(1, sizeof(*bzip2));
istream_comp_t *base = (istream_comp_t *)bzip2;
if (bzip2 == NULL) {
fprintf(stderr, "%s: creating bzip2 compressor: %s.\n",
filename, strerror(errno));
return NULL;
}
if (BZ2_bzDecompressInit(&bzip2->strm, 0, 0) != BZ_OK) {
fprintf(stderr, "%s: error initializing bzip2 decompressor.\n",
filename);
free(bzip2);
return NULL;
}
((istream_t *)base)->precache = precache;
base->cleanup = cleanup;
return base;
}
|