-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathbzstream.c
100 lines (88 loc) · 1.99 KB
/
bzstream.c
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
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "config.h"
#include <bzlib.h>
#include <stdlib.h>
#include "pandaseq.h"
#include "misc.h"
struct bz_stream_data {
bz_stream strm;
size_t buffer_length;
MANAGED_MEMBER(
PandaBufferRead,
source);
char buffer[];
};
static bool read_stream(
char *buffer,
size_t buffer_length,
size_t *read,
struct bz_stream_data *data) {
int ret;
while (true) {
if (data->strm.avail_in == 0) {
size_t avail_in;
data->strm.next_in = data->buffer;
if (!data->source(data->buffer, data->buffer_length, &avail_in, data->source_data)) {
*read = 0;
return false;
}
if (avail_in == 0) {
*read = 0;
return true;
}
data->strm.avail_in = avail_in;
}
data->strm.next_out = buffer;
data->strm.avail_out = buffer_length;
ret = BZ2_bzDecompress(&data->strm);
if (ret == BZ_OK || ret == BZ_STREAM_END) {
*read = buffer_length - data->strm.avail_out;
if (*read > 0) {
return true;
}
} else {
*read = 0;
return false;
}
}
}
static void destroy_stream(
struct bz_stream_data *data) {
BZ2_bzDecompressEnd(&data->strm);
DESTROY_MEMBER(data, source);
free(data);
}
#define BUFFER_SIZE 1024
PandaBufferRead panda_bz_decompress(
PandaBufferRead source,
void *source_data,
PandaDestroy source_destroy,
int verbosity,
void **user_data,
PandaDestroy *destroy) {
struct bz_stream_data *data;
if (source == NULL) {
*user_data = NULL;
*destroy = NULL;
return NULL;
}
data = malloc(sizeof(struct bz_stream_data) + BUFFER_SIZE);
data->buffer_length = BUFFER_SIZE;
data->strm.bzalloc = NULL;
data->strm.bzfree = NULL;
data->strm.opaque = NULL;
if (BZ2_bzDecompressInit(&data->strm, verbosity, 0) == BZ_OK) {
data->strm.avail_in = 0;
data->source = source;
data->source_data = source_data;
data->source_destroy = source_destroy;
*user_data = data;
*destroy = (PandaDestroy) destroy_stream;
return (PandaBufferRead) read_stream;
} else {
free(data);
source_destroy(source_data);
*user_data = NULL;
*destroy = NULL;
return NULL;
}
}