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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
use std::error;
use std::fmt;
use std::marker;
use std::mem;
use std::slice;
use libc::{c_int, c_uint};
use {ffi, Compression};
pub struct Compress {
inner: Stream<DirCompress>,
}
pub struct Decompress {
inner: Stream<DirDecompress>,
}
struct Stream<D: Direction> {
raw: Box<ffi::bz_stream>,
_marker: marker::PhantomData<D>,
}
unsafe impl<D: Direction> Send for Stream<D> {}
unsafe impl<D: Direction> Sync for Stream<D> {}
trait Direction {
unsafe fn destroy(stream: *mut ffi::bz_stream) -> c_int;
}
enum DirCompress {}
enum DirDecompress {}
#[derive(PartialEq, Eq, Copy, Debug, Clone)]
pub enum Action {
Run = ffi::BZ_RUN as isize,
Flush = ffi::BZ_FLUSH as isize,
Finish = ffi::BZ_FINISH as isize,
}
#[derive(PartialEq, Eq, Copy, Debug, Clone)]
pub enum Status {
Ok,
FlushOk,
RunOk,
FinishOk,
StreamEnd,
MemNeeded,
}
#[derive(PartialEq, Eq, Copy, Debug, Clone)]
pub enum Error {
Sequence,
Data,
DataMagic,
Param,
}
impl Compress {
pub fn new(lvl: Compression, work_factor: u32) -> Compress {
unsafe {
let mut raw = Box::new(mem::zeroed());
assert_eq!(ffi::BZ2_bzCompressInit(&mut *raw, lvl as c_int, 0,
work_factor as c_int), 0);
Compress {
inner: Stream { raw: raw, _marker: marker::PhantomData },
}
}
}
pub fn compress(&mut self, input: &[u8], output: &mut [u8],
action: Action) -> Result<Status, Error> {
if input.len() == 0 && action == Action::Run {
return Ok(Status::RunOk)
}
self.inner.raw.next_in = input.as_ptr() as *mut _;
self.inner.raw.avail_in = input.len() as c_uint;
self.inner.raw.next_out = output.as_mut_ptr() as *mut _;
self.inner.raw.avail_out = output.len() as c_uint;
unsafe {
match ffi::BZ2_bzCompress(&mut *self.inner.raw, action as c_int) {
ffi::BZ_RUN_OK => Ok(Status::RunOk),
ffi::BZ_FLUSH_OK => Ok(Status::FlushOk),
ffi::BZ_FINISH_OK => Ok(Status::FinishOk),
ffi::BZ_STREAM_END => Ok(Status::StreamEnd),
ffi::BZ_SEQUENCE_ERROR => Err(Error::Sequence),
c => panic!("unknown return status: {}", c),
}
}
}
pub fn compress_vec(&mut self,
input: &[u8],
output: &mut Vec<u8>,
action: Action) -> Result<Status, Error> {
let cap = output.capacity();
let len = output.len();
unsafe {
let before = self.total_out();
let ret = {
let ptr = output.as_mut_ptr().offset(len as isize);
let out = slice::from_raw_parts_mut(ptr, cap - len);
self.compress(input, out, action)
};
output.set_len((self.total_out() - before) as usize + len);
return ret
}
}
pub fn total_in(&self) -> u64 {
self.inner.total_in()
}
pub fn total_out(&self) -> u64 {
self.inner.total_out()
}
}
impl Decompress {
pub fn new(small: bool) -> Decompress {
unsafe {
let mut raw = Box::new(mem::zeroed());
assert_eq!(ffi::BZ2_bzDecompressInit(&mut *raw, 0, small as c_int), 0);
Decompress {
inner: Stream { raw: raw, _marker: marker::PhantomData },
}
}
}
pub fn decompress(&mut self, input: &[u8], output: &mut [u8])
-> Result<Status, Error> {
self.inner.raw.next_in = input.as_ptr() as *mut _;
self.inner.raw.avail_in = input.len() as c_uint;
self.inner.raw.next_out = output.as_mut_ptr() as *mut _;
self.inner.raw.avail_out = output.len() as c_uint;
unsafe {
match ffi::BZ2_bzDecompress(&mut *self.inner.raw) {
ffi::BZ_OK => Ok(Status::Ok),
ffi::BZ_MEM_ERROR => Ok(Status::MemNeeded),
ffi::BZ_STREAM_END => Ok(Status::StreamEnd),
ffi::BZ_PARAM_ERROR => Err(Error::Param),
ffi::BZ_DATA_ERROR => Err(Error::Data),
ffi::BZ_DATA_ERROR_MAGIC => Err(Error::DataMagic),
ffi::BZ_SEQUENCE_ERROR => Err(Error::Sequence),
c => panic!("wut: {}", c),
}
}
}
pub fn decompress_vec(&mut self, input: &[u8], output: &mut Vec<u8>)
-> Result<Status, Error> {
let cap = output.capacity();
let len = output.len();
unsafe {
let before = self.total_out();
let ret = {
let ptr = output.as_mut_ptr().offset(len as isize);
let out = slice::from_raw_parts_mut(ptr, cap - len);
self.decompress(input, out)
};
output.set_len((self.total_out() - before) as usize + len);
return ret
}
}
pub fn total_in(&self) -> u64 {
self.inner.total_in()
}
pub fn total_out(&self) -> u64 {
self.inner.total_out()
}
}
impl<D: Direction> Stream<D> {
fn total_in(&self) -> u64 {
(self.raw.total_in_lo32 as u64) |
((self.raw.total_in_hi32 as u64) << 32)
}
fn total_out(&self) -> u64 {
(self.raw.total_out_lo32 as u64) |
((self.raw.total_out_hi32 as u64) << 32)
}
}
impl error::Error for Error {
fn description(&self) -> &str { "bz2 data error" }
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
error::Error::description(self).fmt(f)
}
}
impl Direction for DirCompress {
unsafe fn destroy(stream: *mut ffi::bz_stream) -> c_int {
ffi::BZ2_bzCompressEnd(stream)
}
}
impl Direction for DirDecompress {
unsafe fn destroy(stream: *mut ffi::bz_stream) -> c_int {
ffi::BZ2_bzDecompressEnd(stream)
}
}
impl<D: Direction> Drop for Stream<D> {
fn drop(&mut self) {
unsafe {
let _ = D::destroy(&mut *self.raw);
}
}
}