-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathbzip2_compress.c
233 lines (211 loc) · 5.41 KB
/
bzip2_compress.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
* This file is a part of Pcompress, a chunked parallel multi-
* algorithm lossless compression and decompression program.
*
* Copyright (C) 2012-2013 Moinak Ghosh. All rights reserved.
* Use is subject to license terms.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program.
* If not, see <http://www.gnu.org/licenses/>.
*
* moinakg@belenix.org, http://moinakg.wordpress.com/
*/
#include <sys/types.h>
#include <stdio.h>
#include <strings.h>
#include <bzlib.h>
#include <utils.h>
#include <pcompress.h>
#include <allocator.h>
/*
* Max buffer size allowed for a single bzip2 compress/decompress call.
*/
#define SINGLE_CALL_MAX (2147483648UL)
static void *
slab_alloc_i(void *p, int items, int size) {
void *ptr;
uint64_t tot = (uint64_t)items * (uint64_t)size;
ptr = slab_alloc(p, tot);
return (ptr);
}
void
bzip2_stats(int show)
{
}
void
bzip2_props(algo_props_t *data, int level, uint64_t chunksize) {
data->delta2_span = 200;
data->deltac_min_distance = FOURM;
}
int
bzip2_init(void **data, int *level, int nthreads, uint64_t chunksize,
int file_version, compress_op_t op)
{
if (*level > 9) *level = 9;
return (0);
}
static void
bzerr(int err)
{
switch (err) {
case BZ_SEQUENCE_ERROR:
log_msg(LOG_ERR, 0, "Bzip2: Call sequence error, buggy code ?\n");
break;
case BZ_PARAM_ERROR:
log_msg(LOG_ERR, 0, "Bzip2: Invalid parameter\n");
break;
case BZ_MEM_ERROR:
log_msg(LOG_ERR, 0, "Bzip2: Out of memory\n");
break;
case BZ_DATA_ERROR:
log_msg(LOG_ERR, 0, "Bzip2: Data integrity checksum error\n");
break;
case BZ_DATA_ERROR_MAGIC:
log_msg(LOG_ERR, 0, "Bzip2: Invalid magic number in compressed buf\n");
break;
case BZ_OUTBUFF_FULL:
log_msg(LOG_ERR, 0, "Bzip2: Output buffer overflow\n");
break;
case BZ_CONFIG_ERROR:
log_msg(LOG_ERR, 0, "Bzip2: Improper library config on platform\n");
break;
default:
log_msg(LOG_ERR, 0, "Bzip2: Unknown error code: %d\n", err);
}
}
int
bzip2_compress(void *src, uint64_t srclen, void *dst, uint64_t *dstlen,
int level, uchar_t chdr, int btype, void *data)
{
bz_stream bzs;
int ret, ending;
unsigned int slen, dlen;
uint64_t _srclen = srclen;
uint64_t _dstlen = *dstlen;
char *dst1 = (char *)dst;
char *src1 = (char *)src;
/*
* If the data is known to be compressed then certain types less compressed data
* can be attempted to be compressed again for a possible gain. For others it is
* a waste of time.
*/
if (PC_TYPE(btype) & TYPE_COMPRESSED && level < 7) {
int subtype = PC_SUBTYPE(btype);
if (subtype != TYPE_COMPRESSED_LZW && subtype != TYPE_COMPRESSED_GZ &&
subtype != TYPE_COMPRESSED_LZ && subtype != TYPE_COMPRESSED_LZO) {
return (-1);
}
}
bzs.bzalloc = slab_alloc_i;
bzs.bzfree = slab_free;
bzs.opaque = NULL;
ret = BZ2_bzCompressInit(&bzs, level, 0, 30);
if (ret != BZ_OK) {
bzerr(ret);
return (-1);
}
ending = 0;
while (_srclen > 0) {
if (_srclen > SINGLE_CALL_MAX) {
slen = SINGLE_CALL_MAX;
} else {
slen = _srclen;
ending = 1;
}
if (_dstlen > SINGLE_CALL_MAX) {
dlen = SINGLE_CALL_MAX;
} else {
dlen = _dstlen;
}
bzs.next_in = src1;
bzs.avail_in = slen;
bzs.next_out = dst1;
bzs.avail_out = dlen;
if (!ending) {
ret = BZ2_bzCompress(&bzs, BZ_RUN);
if (ret != BZ_RUN_OK) {
BZ2_bzCompressEnd(&bzs);
return (-1);
}
} else {
ret = BZ2_bzCompress(&bzs, BZ_FINISH);
if (ret == BZ_FINISH_OK) {
BZ2_bzCompressEnd(&bzs);
return (-1);
}
if (ret != BZ_STREAM_END) {
BZ2_bzCompressEnd(&bzs);
return (-1);
}
}
dst1 += (dlen - bzs.avail_out);
_dstlen -= (dlen - bzs.avail_out);
src1 += slen;
_srclen -= slen;
}
/* normal termination */
*dstlen = *dstlen - _dstlen;
BZ2_bzCompressEnd(&bzs);
return (0);
}
int
bzip2_decompress(void *src, uint64_t srclen, void *dst, uint64_t *dstlen,
int level, uchar_t chdr, int btype, void *data)
{
bz_stream bzs;
int ret;
unsigned int slen, dlen;
uint64_t _srclen = srclen;
uint64_t _dstlen = *dstlen;
char *dst1 = (char *)dst;
char *src1 = (char *)src;
bzs.bzalloc = slab_alloc_i;
bzs.bzfree = slab_free;
bzs.opaque = NULL;
ret = BZ2_bzDecompressInit(&bzs, 0, 0);
if (ret != BZ_OK) {
bzerr(ret);
return (-1);
}
while (_srclen > 0) {
if (_srclen > SINGLE_CALL_MAX) {
slen = SINGLE_CALL_MAX;
} else {
slen = _srclen;
}
if (_dstlen > SINGLE_CALL_MAX) {
dlen = SINGLE_CALL_MAX;
} else {
dlen = _dstlen;
}
bzs.next_in = src1;
bzs.avail_in = slen;
bzs.next_out = dst1;
bzs.avail_out = dlen;
ret = BZ2_bzDecompress(&bzs);
if (ret != BZ_OK && ret != BZ_STREAM_END) {
BZ2_bzDecompressEnd(&bzs);
bzerr(ret);
return (-1);
}
dst1 += (dlen - bzs.avail_out);
_dstlen -= (dlen - bzs.avail_out);
src1 += (slen - bzs.avail_in);
_srclen -= (slen - bzs.avail_in);
}
/* normal termination */
*dstlen = *dstlen - _dstlen;
BZ2_bzDecompressEnd(&bzs);
return (0);
}