/src/flate2-rs/src/zio.rs
Line | Count | Source |
1 | | use std::io; |
2 | | use std::io::prelude::*; |
3 | | use std::mem; |
4 | | |
5 | | use crate::{ |
6 | | Compress, CompressError, Decompress, DecompressError, FlushCompress, FlushDecompress, Status, |
7 | | }; |
8 | | |
9 | | #[derive(Debug)] |
10 | | pub struct Writer<W: Write, D: Ops> { |
11 | | obj: Option<W>, |
12 | | pub data: D, |
13 | | buf: Vec<u8>, |
14 | | } |
15 | | |
16 | | pub trait Ops { |
17 | | type Error: Into<io::Error>; |
18 | | type Flush: Flush; |
19 | | fn total_in(&self) -> u64; |
20 | | fn total_out(&self) -> u64; |
21 | | fn run( |
22 | | &mut self, |
23 | | input: &[u8], |
24 | | output: &mut [u8], |
25 | | flush: Self::Flush, |
26 | | ) -> Result<Status, Self::Error>; |
27 | | fn run_vec( |
28 | | &mut self, |
29 | | input: &[u8], |
30 | | output: &mut Vec<u8>, |
31 | | flush: Self::Flush, |
32 | | ) -> Result<Status, Self::Error>; |
33 | | } |
34 | | |
35 | | impl Ops for Compress { |
36 | | type Error = CompressError; |
37 | | type Flush = FlushCompress; |
38 | 16.8k | fn total_in(&self) -> u64 { |
39 | 16.8k | self.total_in() |
40 | 16.8k | } |
41 | 12.6k | fn total_out(&self) -> u64 { |
42 | 12.6k | self.total_out() |
43 | 12.6k | } |
44 | 0 | fn run( |
45 | 0 | &mut self, |
46 | 0 | input: &[u8], |
47 | 0 | output: &mut [u8], |
48 | 0 | flush: FlushCompress, |
49 | 0 | ) -> Result<Status, CompressError> { |
50 | 0 | self.compress(input, output, flush) |
51 | 0 | } |
52 | 14.7k | fn run_vec( |
53 | 14.7k | &mut self, |
54 | 14.7k | input: &[u8], |
55 | 14.7k | output: &mut Vec<u8>, |
56 | 14.7k | flush: FlushCompress, |
57 | 14.7k | ) -> Result<Status, CompressError> { |
58 | 14.7k | self.compress_vec(input, output, flush) |
59 | 14.7k | } |
60 | | } |
61 | | |
62 | | impl Ops for Decompress { |
63 | | type Error = DecompressError; |
64 | | type Flush = FlushDecompress; |
65 | 68.9k | fn total_in(&self) -> u64 { |
66 | 68.9k | self.total_in() |
67 | 68.9k | } |
68 | 68.9k | fn total_out(&self) -> u64 { |
69 | 68.9k | self.total_out() |
70 | 68.9k | } |
71 | 34.4k | fn run( |
72 | 34.4k | &mut self, |
73 | 34.4k | input: &[u8], |
74 | 34.4k | output: &mut [u8], |
75 | 34.4k | flush: FlushDecompress, |
76 | 34.4k | ) -> Result<Status, DecompressError> { |
77 | 34.4k | self.decompress(input, output, flush) |
78 | 34.4k | } |
79 | 0 | fn run_vec( |
80 | 0 | &mut self, |
81 | 0 | input: &[u8], |
82 | 0 | output: &mut Vec<u8>, |
83 | 0 | flush: FlushDecompress, |
84 | 0 | ) -> Result<Status, DecompressError> { |
85 | 0 | self.decompress_vec(input, output, flush) |
86 | 0 | } |
87 | | } |
88 | | |
89 | | pub trait Flush { |
90 | | fn none() -> Self; |
91 | | fn sync() -> Self; |
92 | | fn finish() -> Self; |
93 | | } |
94 | | |
95 | | impl Flush for FlushCompress { |
96 | 8.41k | fn none() -> Self { |
97 | 8.41k | FlushCompress::None |
98 | 8.41k | } |
99 | | |
100 | 0 | fn sync() -> Self { |
101 | 0 | FlushCompress::Sync |
102 | 0 | } |
103 | | |
104 | 6.31k | fn finish() -> Self { |
105 | 6.31k | FlushCompress::Finish |
106 | 6.31k | } |
107 | | } |
108 | | |
109 | | impl Flush for FlushDecompress { |
110 | 34.4k | fn none() -> Self { |
111 | 34.4k | FlushDecompress::None |
112 | 34.4k | } |
113 | | |
114 | 0 | fn sync() -> Self { |
115 | 0 | FlushDecompress::Sync |
116 | 0 | } |
117 | | |
118 | 0 | fn finish() -> Self { |
119 | 0 | FlushDecompress::Finish |
120 | 0 | } |
121 | | } |
122 | | |
123 | 34.4k | pub fn read<R, D>(obj: &mut R, data: &mut D, dst: &mut [u8]) -> io::Result<usize> |
124 | 34.4k | where |
125 | 34.4k | R: BufRead, |
126 | 34.4k | D: Ops, |
127 | | { |
128 | | loop { |
129 | | let (read, consumed, ret, eof); |
130 | | { |
131 | 34.4k | let input = obj.fill_buf()?; |
132 | 34.4k | eof = input.is_empty(); |
133 | 34.4k | let before_out = data.total_out(); |
134 | 34.4k | let before_in = data.total_in(); |
135 | 34.4k | let flush = if eof { |
136 | 0 | D::Flush::finish() |
137 | | } else { |
138 | 34.4k | D::Flush::none() |
139 | | }; |
140 | 34.4k | ret = data.run(input, dst, flush); |
141 | 34.4k | read = (data.total_out() - before_out) as usize; |
142 | 34.4k | consumed = (data.total_in() - before_in) as usize; |
143 | | } |
144 | 34.4k | obj.consume(consumed); |
145 | | |
146 | 28.3k | match ret { |
147 | | // If we haven't ready any data and we haven't hit EOF yet, |
148 | | // then we need to keep asking for more data because if we |
149 | | // return that 0 bytes of data have been read then it will |
150 | | // be interpreted as EOF. |
151 | 28.3k | Ok(Status::Ok | Status::BufError) if read == 0 && !eof && !dst.is_empty() => continue, |
152 | 34.4k | Ok(Status::Ok | Status::BufError | Status::StreamEnd) => return Ok(read), |
153 | | |
154 | | Err(..) => { |
155 | 0 | return Err(io::Error::new( |
156 | 0 | io::ErrorKind::InvalidInput, |
157 | 0 | "corrupt deflate stream", |
158 | 0 | )) |
159 | | } |
160 | | } |
161 | | } |
162 | 34.4k | } |
163 | | |
164 | | impl<W: Write, D: Ops> Writer<W, D> { |
165 | 3.07k | pub fn new(w: W, d: D) -> Writer<W, D> { |
166 | 3.07k | Writer { |
167 | 3.07k | obj: Some(w), |
168 | 3.07k | data: d, |
169 | 3.07k | buf: Vec::with_capacity(32 * 1024), |
170 | 3.07k | } |
171 | 3.07k | } |
172 | | |
173 | 3.07k | pub fn finish(&mut self) -> io::Result<()> { |
174 | | loop { |
175 | 6.31k | self.dump()?; |
176 | | |
177 | 6.31k | let before = self.data.total_out(); |
178 | 6.31k | self.data |
179 | 6.31k | .run_vec(&[], &mut self.buf, Flush::finish()) |
180 | 6.31k | .map_err(Into::into)?; |
181 | 6.31k | if before == self.data.total_out() { |
182 | 3.07k | return Ok(()); |
183 | 3.24k | } |
184 | | } |
185 | 3.07k | } |
186 | | |
187 | | pub fn replace(&mut self, w: W) -> W { |
188 | | self.buf.truncate(0); |
189 | | mem::replace(self.get_mut(), w) |
190 | | } |
191 | | |
192 | | pub fn get_ref(&self) -> &W { |
193 | | self.obj.as_ref().unwrap() |
194 | | } |
195 | | |
196 | 6.14k | pub fn get_mut(&mut self) -> &mut W { |
197 | 6.14k | self.obj.as_mut().unwrap() |
198 | 6.14k | } |
199 | | |
200 | | // Note that this should only be called if the outer object is just about |
201 | | // to be consumed! |
202 | | // |
203 | | // (e.g. an implementation of `into_inner`) |
204 | 3.07k | pub fn take_inner(&mut self) -> W { |
205 | 3.07k | self.obj.take().unwrap() |
206 | 3.07k | } |
207 | | |
208 | 3.07k | pub fn is_present(&self) -> bool { |
209 | 3.07k | self.obj.is_some() |
210 | 3.07k | } |
211 | | |
212 | | // Returns total written bytes and status of underlying codec |
213 | 7.94k | pub(crate) fn write_with_status(&mut self, buf: &[u8]) -> io::Result<(usize, Status)> { |
214 | | // miniz isn't guaranteed to actually write any of the buffer provided, |
215 | | // it may be in a flushing mode where it's just giving us data before |
216 | | // we're actually giving it any data. We don't want to spuriously return |
217 | | // `Ok(0)` when possible as it will cause calls to write_all() to fail. |
218 | | // As a result we execute this in a loop to ensure that we try our |
219 | | // darndest to write the data. |
220 | | loop { |
221 | 8.41k | self.dump()?; |
222 | | |
223 | 8.41k | let before_in = self.data.total_in(); |
224 | 8.41k | let ret = self.data.run_vec(buf, &mut self.buf, D::Flush::none()); |
225 | 8.41k | let written = (self.data.total_in() - before_in) as usize; |
226 | 8.41k | let is_stream_end = matches!(ret, Ok(Status::StreamEnd)); |
227 | | |
228 | 8.41k | if !buf.is_empty() && written == 0 && ret.is_ok() && !is_stream_end { |
229 | 473 | continue; |
230 | 7.94k | } |
231 | 7.94k | return match ret { |
232 | 7.94k | Ok(st) => match st { |
233 | 7.94k | Status::Ok | Status::BufError | Status::StreamEnd => Ok((written, st)), |
234 | | }, |
235 | 0 | Err(..) => Err(io::Error::new( |
236 | 0 | io::ErrorKind::InvalidInput, |
237 | 0 | "corrupt deflate stream", |
238 | 0 | )), |
239 | | }; |
240 | | } |
241 | 7.94k | } |
242 | | |
243 | 14.7k | fn dump(&mut self) -> io::Result<()> { |
244 | | // TODO: should manage this buffer not with `drain` but probably more of |
245 | | // a deque-like strategy. |
246 | 24.1k | while !self.buf.is_empty() { |
247 | 9.45k | let n = self.obj.as_mut().unwrap().write(&self.buf)?; |
248 | 9.45k | if n == 0 { |
249 | 0 | return Err(io::ErrorKind::WriteZero.into()); |
250 | 9.45k | } |
251 | 9.45k | self.buf.drain(..n); |
252 | | } |
253 | 14.7k | Ok(()) |
254 | 14.7k | } |
255 | | } |
256 | | |
257 | | impl<W: Write, D: Ops> Write for Writer<W, D> { |
258 | 7.94k | fn write(&mut self, buf: &[u8]) -> io::Result<usize> { |
259 | 7.94k | self.write_with_status(buf).map(|res| res.0) |
260 | 7.94k | } |
261 | | |
262 | | fn flush(&mut self) -> io::Result<()> { |
263 | | self.data |
264 | | .run_vec(&[], &mut self.buf, Flush::sync()) |
265 | | .map_err(Into::into)?; |
266 | | |
267 | | // Unfortunately miniz doesn't actually tell us when we're done with |
268 | | // pulling out all the data from the internal stream. To remedy this we |
269 | | // have to continually ask the stream for more memory until it doesn't |
270 | | // give us a chunk of memory the same size as our own internal buffer, |
271 | | // at which point we assume it's reached the end. |
272 | | loop { |
273 | | self.dump()?; |
274 | | let before = self.data.total_out(); |
275 | | self.data |
276 | | .run_vec(&[], &mut self.buf, Flush::none()) |
277 | | .map_err(Into::into)?; |
278 | | if before == self.data.total_out() { |
279 | | break; |
280 | | } |
281 | | } |
282 | | |
283 | | self.obj.as_mut().unwrap().flush() |
284 | | } |
285 | | } |
286 | | |
287 | | impl<W: Write, D: Ops> Drop for Writer<W, D> { |
288 | 3.07k | fn drop(&mut self) { |
289 | 3.07k | if self.obj.is_some() { |
290 | 0 | let _ = self.finish(); |
291 | 3.07k | } |
292 | 3.07k | } |
293 | | } |