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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
use std::io::{self, BufRead, Read, BufReader, Error, ErrorKind, Write};
use std::{cmp,mem};
use super::InflateStream;
#[inline]
fn copy_from_slice(mut to: &mut [u8], from: &[u8]) {
assert_eq!(to.len(), from.len());
to.write_all(from).unwrap();
}
pub struct DeflateDecoderBuf<R> {
reader: R,
decompressor: InflateStream,
pending_output_bytes: usize,
total_in: u64,
total_out: u64,
}
impl<R: BufRead> DeflateDecoderBuf<R> {
pub fn new(reader: R) -> DeflateDecoderBuf<R> {
DeflateDecoderBuf {
reader: reader,
decompressor: InflateStream::new(),
pending_output_bytes: 0,
total_in: 0,
total_out: 0,
}
}
pub fn from_zlib(reader: R) -> DeflateDecoderBuf<R> {
DeflateDecoderBuf {
reader: reader,
decompressor: InflateStream::from_zlib(),
pending_output_bytes: 0,
total_in: 0,
total_out: 0,
}
}
pub fn from_zlib_no_checksum(reader: R) -> DeflateDecoderBuf<R> {
DeflateDecoderBuf {
reader: reader,
decompressor: InflateStream::from_zlib_no_checksum(),
pending_output_bytes: 0,
total_in: 0,
total_out: 0,
}
}
}
impl<R> DeflateDecoderBuf<R> {
#[inline]
pub fn reset(&mut self, r: R) -> R {
self.decompressor.reset();
mem::replace(&mut self.reader, r)
}
#[inline]
pub fn reset_data(&mut self) {
self.decompressor.reset()
}
#[inline]
pub fn get_ref(&self) -> &R {
&self.reader
}
#[inline]
pub fn get_mut(&mut self) -> &mut R {
&mut self.reader
}
#[inline]
pub fn into_inner(self) -> R {
self.reader
}
#[inline]
pub fn total_in(&self) -> u64 {
self.total_in
}
#[inline]
pub fn total_out(&self) -> u64 {
self.total_out
}
#[inline]
pub fn current_checksum(&self) -> u32 {
self.decompressor.current_checksum()
}
}
impl<R: BufRead> Read for DeflateDecoderBuf<R> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
let mut bytes_out = 0;
if self.pending_output_bytes != 0 {
let start = if self.decompressor.pos != 0 {
self.decompressor.pos as usize - self.pending_output_bytes
} else {
self.decompressor.buffer.len() - self.pending_output_bytes
};
let bytes_to_copy = cmp::min(buf.len(), self.pending_output_bytes);
let pending_data =
&self.decompressor.buffer[start..
start + bytes_to_copy];
copy_from_slice(&mut buf[..bytes_to_copy],pending_data);
bytes_out += bytes_to_copy;
self.pending_output_bytes -= bytes_to_copy;
if self.pending_output_bytes != 0 {
self.total_out += bytes_out as u64;
return Ok(bytes_out);
}
}
let (input_bytes_read, remaining_bytes) = {
self.pending_output_bytes = 0;
let input = try!(self.reader.fill_buf());
if input.len() == 0 {
self.total_out += bytes_out as u64;
return Ok(bytes_out);
}
let (input_bytes_read, data) =
match self.decompressor.update(&input) {
Ok(res) => res,
Err(m) => return Err(Error::new(ErrorKind::Other, m))
};
let space_left = buf.len() - bytes_out;
let bytes_to_copy = cmp::min(space_left, data.len());
copy_from_slice(&mut buf[bytes_out..bytes_out + bytes_to_copy], &data[..bytes_to_copy]);
bytes_out += bytes_to_copy;
(input_bytes_read, data.len() - bytes_to_copy)
};
self.pending_output_bytes += remaining_bytes;
self.total_in += input_bytes_read as u64;
self.total_out += bytes_out as u64;
self.reader.consume(input_bytes_read);
Ok(bytes_out)
}
}
pub struct DeflateDecoder<R> {
inner: DeflateDecoderBuf<BufReader<R>>
}
impl<R: Read> DeflateDecoder<R> {
pub fn new(reader: R) -> DeflateDecoder<R> {
DeflateDecoder {
inner: DeflateDecoderBuf::new(BufReader::new(reader))
}
}
pub fn from_zlib(reader: R) -> DeflateDecoder<R> {
DeflateDecoder {
inner: DeflateDecoderBuf::from_zlib(BufReader::new(reader))
}
}
pub fn from_zlib_no_checksum(reader: R) -> DeflateDecoder<R> {
DeflateDecoder {
inner: DeflateDecoderBuf::from_zlib_no_checksum(BufReader::new(reader))
}
}
#[inline]
pub fn reset(&mut self, r: R) -> R {
self.inner.reset(BufReader::new(r)).into_inner()
}
#[inline]
pub fn get_ref(&self) -> &R {
self.inner.get_ref().get_ref()
}
#[inline]
pub fn get_mut(&mut self) -> &mut R {
self.inner.get_mut().get_mut()
}
#[inline]
pub fn into_inner(self) -> R {
self.inner.into_inner().into_inner()
}
}
impl<R> DeflateDecoder<R> {
#[inline]
pub fn reset_data(&mut self) {
self.inner.reset_data()
}
#[inline]
pub fn total_in(&self) -> u64 {
self.inner.total_in
}
#[inline]
pub fn total_out(&self) -> u64 {
self.inner.total_out
}
#[inline]
pub fn current_checksum(&self) -> u32 {
self.inner.current_checksum()
}
}
impl<R: Read> Read for DeflateDecoder<R> {
#[inline]
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.inner.read(buf)
}
}
#[cfg(test)]
mod test {
use super::{DeflateDecoder};
use std::io::Read;
#[test]
fn deflate_reader() {
const TEST_STRING: &'static str = "Hello, world";
let encoded = vec![243, 72, 205, 201, 201, 215, 81, 40, 207, 47, 202, 73, 1, 0];
let mut decoder = DeflateDecoder::new(&encoded[..]);
let mut output = Vec::new();
decoder.read_to_end(&mut output).unwrap();
assert_eq!(String::from_utf8(output).unwrap(), TEST_STRING);
assert_eq!(decoder.total_in(), encoded.len() as u64);
assert_eq!(decoder.total_out(), TEST_STRING.len() as u64);
}
#[test]
fn zlib_reader() {
const TEST_STRING: &'static str = "Hello, zlib!";
let encoded = vec![120, 156, 243, 72, 205, 201, 201, 215, 81, 168, 202, 201,
76, 82, 4, 0, 27, 101, 4, 19];
let mut decoder = DeflateDecoder::from_zlib(&encoded[..]);
let mut output = Vec::new();
decoder.read_to_end(&mut output).unwrap();
assert_eq!(String::from_utf8(output).unwrap(), TEST_STRING);
assert_eq!(decoder.total_in(), encoded.len() as u64);
assert_eq!(decoder.total_out(), TEST_STRING.len() as u64);
}
}