/rust/registry/src/index.crates.io-1949cf8c6b5b557f/flate2-1.1.4/src/mem.rs
Line | Count | Source |
1 | | use std::error::Error; |
2 | | use std::fmt; |
3 | | use std::io; |
4 | | use std::mem::MaybeUninit; |
5 | | |
6 | | use crate::ffi::{self, Backend, Deflate, DeflateBackend, ErrorMessage, Inflate, InflateBackend}; |
7 | | use crate::Compression; |
8 | | |
9 | | /// Raw in-memory compression stream for blocks of data. |
10 | | /// |
11 | | /// This type is the building block for the I/O streams in the rest of this |
12 | | /// crate. It requires more management than the [`Read`]/[`Write`] API but is |
13 | | /// maximally flexible in terms of accepting input from any source and being |
14 | | /// able to produce output to any memory location. |
15 | | /// |
16 | | /// It is recommended to use the I/O stream adaptors over this type as they're |
17 | | /// easier to use. |
18 | | /// |
19 | | /// [`Read`]: https://doc.rust-lang.org/std/io/trait.Read.html |
20 | | /// [`Write`]: https://doc.rust-lang.org/std/io/trait.Write.html |
21 | | #[derive(Debug)] |
22 | | pub struct Compress { |
23 | | inner: Deflate, |
24 | | } |
25 | | |
26 | | /// Raw in-memory decompression stream for blocks of data. |
27 | | /// |
28 | | /// This type is the building block for the I/O streams in the rest of this |
29 | | /// crate. It requires more management than the [`Read`]/[`Write`] API but is |
30 | | /// maximally flexible in terms of accepting input from any source and being |
31 | | /// able to produce output to any memory location. |
32 | | /// |
33 | | /// It is recommended to use the I/O stream adaptors over this type as they're |
34 | | /// easier to use. |
35 | | /// |
36 | | /// [`Read`]: https://doc.rust-lang.org/std/io/trait.Read.html |
37 | | /// [`Write`]: https://doc.rust-lang.org/std/io/trait.Write.html |
38 | | #[derive(Debug)] |
39 | | pub struct Decompress { |
40 | | inner: Inflate, |
41 | | } |
42 | | |
43 | | /// Values which indicate the form of flushing to be used when compressing |
44 | | /// in-memory data. |
45 | | #[derive(Copy, Clone, PartialEq, Eq, Debug)] |
46 | | #[non_exhaustive] |
47 | | #[allow(clippy::unnecessary_cast)] |
48 | | pub enum FlushCompress { |
49 | | /// A typical parameter for passing to compression/decompression functions, |
50 | | /// this indicates that the underlying stream to decide how much data to |
51 | | /// accumulate before producing output in order to maximize compression. |
52 | | None = ffi::MZ_NO_FLUSH as isize, |
53 | | |
54 | | /// All pending output is flushed to the output buffer, but the output is |
55 | | /// not aligned to a byte boundary. |
56 | | /// |
57 | | /// All input data so far will be available to the decompressor (as with |
58 | | /// `Flush::Sync`). This completes the current deflate block and follows it |
59 | | /// with an empty fixed codes block that is 10 bits long, and it assures |
60 | | /// that enough bytes are output in order for the decompressor to finish the |
61 | | /// block before the empty fixed code block. |
62 | | Partial = ffi::MZ_PARTIAL_FLUSH as isize, |
63 | | |
64 | | /// All pending output is flushed to the output buffer and the output is |
65 | | /// aligned on a byte boundary so that the decompressor can get all input |
66 | | /// data available so far. |
67 | | /// |
68 | | /// Flushing may degrade compression for some compression algorithms and so |
69 | | /// it should only be used when necessary. This will complete the current |
70 | | /// deflate block and follow it with an empty stored block. |
71 | | Sync = ffi::MZ_SYNC_FLUSH as isize, |
72 | | |
73 | | /// All output is flushed as with `Flush::Sync` and the compression state is |
74 | | /// reset so decompression can restart from this point if previous |
75 | | /// compressed data has been damaged or if random access is desired. |
76 | | /// |
77 | | /// Using this option too often can seriously degrade compression. |
78 | | Full = ffi::MZ_FULL_FLUSH as isize, |
79 | | |
80 | | /// Pending input is processed and pending output is flushed. |
81 | | /// |
82 | | /// The return value may indicate that the stream is not yet done and more |
83 | | /// data has yet to be processed. |
84 | | Finish = ffi::MZ_FINISH as isize, |
85 | | } |
86 | | |
87 | | /// Values which indicate the form of flushing to be used when |
88 | | /// decompressing in-memory data. |
89 | | #[derive(Copy, Clone, PartialEq, Eq, Debug)] |
90 | | #[non_exhaustive] |
91 | | #[allow(clippy::unnecessary_cast)] |
92 | | pub enum FlushDecompress { |
93 | | /// A typical parameter for passing to compression/decompression functions, |
94 | | /// this indicates that the underlying stream to decide how much data to |
95 | | /// accumulate before producing output in order to maximize compression. |
96 | | None = ffi::MZ_NO_FLUSH as isize, |
97 | | |
98 | | /// All pending output is flushed to the output buffer and the output is |
99 | | /// aligned on a byte boundary so that the decompressor can get all input |
100 | | /// data available so far. |
101 | | /// |
102 | | /// Flushing may degrade compression for some compression algorithms and so |
103 | | /// it should only be used when necessary. This will complete the current |
104 | | /// deflate block and follow it with an empty stored block. |
105 | | Sync = ffi::MZ_SYNC_FLUSH as isize, |
106 | | |
107 | | /// Pending input is processed and pending output is flushed. |
108 | | /// |
109 | | /// The return value may indicate that the stream is not yet done and more |
110 | | /// data has yet to be processed. |
111 | | Finish = ffi::MZ_FINISH as isize, |
112 | | } |
113 | | |
114 | | /// The inner state for an error when decompressing |
115 | | #[derive(Clone, Debug)] |
116 | | pub(crate) enum DecompressErrorInner { |
117 | | General { msg: ErrorMessage }, |
118 | | NeedsDictionary(u32), |
119 | | } |
120 | | |
121 | | /// Error returned when a decompression object finds that the input stream of |
122 | | /// bytes was not a valid input stream of bytes. |
123 | | #[derive(Clone, Debug)] |
124 | | pub struct DecompressError(pub(crate) DecompressErrorInner); |
125 | | |
126 | | impl DecompressError { |
127 | | /// Indicates whether decompression failed due to requiring a dictionary. |
128 | | /// |
129 | | /// The resulting integer is the Adler-32 checksum of the dictionary |
130 | | /// required. |
131 | 0 | pub fn needs_dictionary(&self) -> Option<u32> { |
132 | 0 | match self.0 { |
133 | 0 | DecompressErrorInner::NeedsDictionary(adler) => Some(adler), |
134 | 0 | _ => None, |
135 | | } |
136 | 0 | } |
137 | | } |
138 | | |
139 | | #[inline] |
140 | 77 | pub(crate) fn decompress_failed<T>(msg: ErrorMessage) -> Result<T, DecompressError> { |
141 | 77 | Err(DecompressError(DecompressErrorInner::General { msg })) |
142 | 77 | } |
143 | | |
144 | | #[inline] |
145 | 0 | pub(crate) fn decompress_need_dict<T>(adler: u32) -> Result<T, DecompressError> { |
146 | 0 | Err(DecompressError(DecompressErrorInner::NeedsDictionary( |
147 | 0 | adler, |
148 | 0 | ))) |
149 | 0 | } |
150 | | |
151 | | /// Error returned when a compression object is used incorrectly or otherwise |
152 | | /// generates an error. |
153 | | #[derive(Clone, Debug)] |
154 | | pub struct CompressError { |
155 | | pub(crate) msg: ErrorMessage, |
156 | | } |
157 | | |
158 | | #[inline] |
159 | 0 | pub(crate) fn compress_failed<T>(msg: ErrorMessage) -> Result<T, CompressError> { |
160 | 0 | Err(CompressError { msg }) |
161 | 0 | } |
162 | | |
163 | | /// Possible status results of compressing some data or successfully |
164 | | /// decompressing a block of data. |
165 | | #[derive(Copy, Clone, PartialEq, Eq, Debug)] |
166 | | pub enum Status { |
167 | | /// Indicates success. |
168 | | /// |
169 | | /// Means that more input may be needed but isn't available |
170 | | /// and/or there's more output to be written but the output buffer is full. |
171 | | Ok, |
172 | | |
173 | | /// Indicates that forward progress is not possible due to input or output |
174 | | /// buffers being empty. |
175 | | /// |
176 | | /// For compression it means the input buffer needs some more data or the |
177 | | /// output buffer needs to be freed up before trying again. |
178 | | /// |
179 | | /// For decompression this means that more input is needed to continue or |
180 | | /// the output buffer isn't large enough to contain the result. The function |
181 | | /// can be called again after fixing both. |
182 | | BufError, |
183 | | |
184 | | /// Indicates that all input has been consumed and all output bytes have |
185 | | /// been written. Decompression/compression should not be called again. |
186 | | /// |
187 | | /// For decompression with zlib streams the adler-32 of the decompressed |
188 | | /// data has also been verified. |
189 | | StreamEnd, |
190 | | } |
191 | | |
192 | | impl Compress { |
193 | | /// Creates a new object ready for compressing data that it's given. |
194 | | /// |
195 | | /// The `level` argument here indicates what level of compression is going |
196 | | /// to be performed, and the `zlib_header` argument indicates whether the |
197 | | /// output data should have a zlib header or not. |
198 | 0 | pub fn new(level: Compression, zlib_header: bool) -> Compress { |
199 | 0 | Compress { |
200 | 0 | inner: Deflate::make(level, zlib_header, ffi::MZ_DEFAULT_WINDOW_BITS as u8), |
201 | 0 | } |
202 | 0 | } |
203 | | |
204 | | /// Creates a new object ready for compressing data that it's given. |
205 | | /// |
206 | | /// The `level` argument here indicates what level of compression is going |
207 | | /// to be performed, and the `zlib_header` argument indicates whether the |
208 | | /// output data should have a zlib header or not. The `window_bits` parameter |
209 | | /// indicates the base-2 logarithm of the sliding window size and must be |
210 | | /// between 9 and 15. |
211 | | /// |
212 | | /// # Panics |
213 | | /// |
214 | | /// If `window_bits` does not fall into the range 9 ..= 15, |
215 | | /// `new_with_window_bits` will panic. |
216 | | #[cfg(feature = "any_zlib")] |
217 | | pub fn new_with_window_bits( |
218 | | level: Compression, |
219 | | zlib_header: bool, |
220 | | window_bits: u8, |
221 | | ) -> Compress { |
222 | | assert!( |
223 | | window_bits > 8 && window_bits < 16, |
224 | | "window_bits must be within 9 ..= 15" |
225 | | ); |
226 | | Compress { |
227 | | inner: Deflate::make(level, zlib_header, window_bits), |
228 | | } |
229 | | } |
230 | | |
231 | | /// Creates a new object ready for compressing data that it's given. |
232 | | /// |
233 | | /// The `level` argument here indicates what level of compression is going |
234 | | /// to be performed. |
235 | | /// |
236 | | /// The Compress object produced by this constructor outputs gzip headers |
237 | | /// for the compressed data. |
238 | | /// |
239 | | /// # Panics |
240 | | /// |
241 | | /// If `window_bits` does not fall into the range 9 ..= 15, |
242 | | /// `new_with_window_bits` will panic. |
243 | | #[cfg(feature = "any_zlib")] |
244 | | pub fn new_gzip(level: Compression, window_bits: u8) -> Compress { |
245 | | assert!( |
246 | | window_bits > 8 && window_bits < 16, |
247 | | "window_bits must be within 9 ..= 15" |
248 | | ); |
249 | | Compress { |
250 | | inner: Deflate::make(level, true, window_bits + 16), |
251 | | } |
252 | | } |
253 | | |
254 | | /// Returns the total number of input bytes which have been processed by |
255 | | /// this compression object. |
256 | 0 | pub fn total_in(&self) -> u64 { |
257 | 0 | self.inner.total_in() |
258 | 0 | } |
259 | | |
260 | | /// Returns the total number of output bytes which have been produced by |
261 | | /// this compression object. |
262 | 0 | pub fn total_out(&self) -> u64 { |
263 | 0 | self.inner.total_out() |
264 | 0 | } |
265 | | |
266 | | /// Specifies the compression dictionary to use. |
267 | | /// |
268 | | /// Returns the Adler-32 checksum of the dictionary. |
269 | | #[cfg(feature = "any_zlib")] |
270 | | pub fn set_dictionary(&mut self, dictionary: &[u8]) -> Result<u32, CompressError> { |
271 | | // SAFETY: The field `inner` must always be accessed as a raw pointer, |
272 | | // since it points to a cyclic structure. No copies of `inner` can be |
273 | | // retained for longer than the lifetime of `self.inner.inner.stream_wrapper`. |
274 | | let stream = self.inner.inner.stream_wrapper.inner; |
275 | | let rc = unsafe { |
276 | | (*stream).msg = std::ptr::null_mut(); |
277 | | assert!(dictionary.len() < ffi::uInt::MAX as usize); |
278 | | ffi::deflateSetDictionary(stream, dictionary.as_ptr(), dictionary.len() as ffi::uInt) |
279 | | }; |
280 | | |
281 | | match rc { |
282 | | ffi::MZ_STREAM_ERROR => compress_failed(self.inner.inner.msg()), |
283 | | #[allow(clippy::unnecessary_cast)] |
284 | | ffi::MZ_OK => Ok(unsafe { (*stream).adler } as u32), |
285 | | c => panic!("unknown return code: {}", c), |
286 | | } |
287 | | } |
288 | | |
289 | | /// Quickly resets this compressor without having to reallocate anything. |
290 | | /// |
291 | | /// This is equivalent to dropping this object and then creating a new one. |
292 | 0 | pub fn reset(&mut self) { |
293 | 0 | self.inner.reset(); |
294 | 0 | } |
295 | | |
296 | | /// Dynamically updates the compression level. |
297 | | /// |
298 | | /// This can be used to switch between compression levels for different |
299 | | /// kinds of data, or it can be used in conjunction with a call to reset |
300 | | /// to reuse the compressor. |
301 | | /// |
302 | | /// This may return an error if there wasn't enough output space to complete |
303 | | /// the compression of the available input data before changing the |
304 | | /// compression level. Flushing the stream before calling this method |
305 | | /// ensures that the function will succeed on the first call. |
306 | | #[cfg(feature = "any_zlib")] |
307 | | pub fn set_level(&mut self, level: Compression) -> Result<(), CompressError> { |
308 | | use std::os::raw::c_int; |
309 | | // SAFETY: The field `inner` must always be accessed as a raw pointer, |
310 | | // since it points to a cyclic structure. No copies of `inner` can be |
311 | | // retained for longer than the lifetime of `self.inner.inner.stream_wrapper`. |
312 | | let stream = self.inner.inner.stream_wrapper.inner; |
313 | | unsafe { |
314 | | (*stream).msg = std::ptr::null_mut(); |
315 | | } |
316 | | let rc = unsafe { ffi::deflateParams(stream, level.0 as c_int, ffi::MZ_DEFAULT_STRATEGY) }; |
317 | | |
318 | | match rc { |
319 | | ffi::MZ_OK => Ok(()), |
320 | | ffi::MZ_BUF_ERROR => compress_failed(self.inner.inner.msg()), |
321 | | c => panic!("unknown return code: {}", c), |
322 | | } |
323 | | } |
324 | | |
325 | | /// Compresses the input data into the output, consuming only as much |
326 | | /// input as needed and writing as much output as possible. |
327 | | /// |
328 | | /// The flush option can be any of the available `FlushCompress` parameters. |
329 | | /// |
330 | | /// To learn how much data was consumed or how much output was produced, use |
331 | | /// the `total_in` and `total_out` functions before/after this is called. |
332 | 0 | pub fn compress( |
333 | 0 | &mut self, |
334 | 0 | input: &[u8], |
335 | 0 | output: &mut [u8], |
336 | 0 | flush: FlushCompress, |
337 | 0 | ) -> Result<Status, CompressError> { |
338 | 0 | self.inner.compress(input, output, flush) |
339 | 0 | } |
340 | | |
341 | | /// Similar to [`Self::compress`] but accepts uninitialized buffer. |
342 | | /// |
343 | | /// If you want to avoid the overhead of zero initializing the |
344 | | /// buffer and you don't want to use a [`Vec`], then please use |
345 | | /// this API. |
346 | 0 | pub fn compress_uninit( |
347 | 0 | &mut self, |
348 | 0 | input: &[u8], |
349 | 0 | output: &mut [MaybeUninit<u8>], |
350 | 0 | flush: FlushCompress, |
351 | 0 | ) -> Result<Status, CompressError> { |
352 | 0 | self.inner.compress_uninit(input, output, flush) |
353 | 0 | } |
354 | | |
355 | | /// Compresses the input data into the extra space of the output, consuming |
356 | | /// only as much input as needed and writing as much output as possible. |
357 | | /// |
358 | | /// This function has the same semantics as `compress`, except that the |
359 | | /// length of `vec` is managed by this function. This will not reallocate |
360 | | /// the vector provided or attempt to grow it, so space for the output must |
361 | | /// be reserved in the output vector by the caller before calling this |
362 | | /// function. |
363 | 0 | pub fn compress_vec( |
364 | 0 | &mut self, |
365 | 0 | input: &[u8], |
366 | 0 | output: &mut Vec<u8>, |
367 | 0 | flush: FlushCompress, |
368 | 0 | ) -> Result<Status, CompressError> { |
369 | | // SAFETY: bytes_written is the number of bytes written into `out` |
370 | | unsafe { |
371 | 0 | write_to_spare_capacity_of_vec(output, |out| { |
372 | 0 | let before = self.total_out(); |
373 | 0 | let ret = self.compress_uninit(input, out, flush); |
374 | 0 | let bytes_written = self.total_out() - before; |
375 | 0 | (bytes_written as usize, ret) |
376 | 0 | }) |
377 | | } |
378 | 0 | } |
379 | | } |
380 | | |
381 | | impl Decompress { |
382 | | /// Creates a new object ready for decompressing data that it's given. |
383 | | /// |
384 | | /// The `zlib_header` argument indicates whether the input data is expected |
385 | | /// to have a zlib header or not. |
386 | 598 | pub fn new(zlib_header: bool) -> Decompress { |
387 | 598 | Decompress { |
388 | 598 | inner: Inflate::make(zlib_header, ffi::MZ_DEFAULT_WINDOW_BITS as u8), |
389 | 598 | } |
390 | 598 | } |
391 | | |
392 | | /// Creates a new object ready for decompressing data that it's given. |
393 | | /// |
394 | | /// The `zlib_header` argument indicates whether the input data is expected |
395 | | /// to have a zlib header or not. The `window_bits` parameter indicates the |
396 | | /// base-2 logarithm of the sliding window size and must be between 9 and 15. |
397 | | /// |
398 | | /// # Panics |
399 | | /// |
400 | | /// If `window_bits` does not fall into the range 9 ..= 15, |
401 | | /// `new_with_window_bits` will panic. |
402 | | #[cfg(feature = "any_zlib")] |
403 | | pub fn new_with_window_bits(zlib_header: bool, window_bits: u8) -> Decompress { |
404 | | assert!( |
405 | | window_bits > 8 && window_bits < 16, |
406 | | "window_bits must be within 9 ..= 15" |
407 | | ); |
408 | | Decompress { |
409 | | inner: Inflate::make(zlib_header, window_bits), |
410 | | } |
411 | | } |
412 | | |
413 | | /// Creates a new object ready for decompressing data that it's given. |
414 | | /// |
415 | | /// The Decompress object produced by this constructor expects gzip headers |
416 | | /// for the compressed data. |
417 | | /// |
418 | | /// # Panics |
419 | | /// |
420 | | /// If `window_bits` does not fall into the range 9 ..= 15, |
421 | | /// `new_with_window_bits` will panic. |
422 | | #[cfg(feature = "any_zlib")] |
423 | | pub fn new_gzip(window_bits: u8) -> Decompress { |
424 | | assert!( |
425 | | window_bits > 8 && window_bits < 16, |
426 | | "window_bits must be within 9 ..= 15" |
427 | | ); |
428 | | Decompress { |
429 | | inner: Inflate::make(true, window_bits + 16), |
430 | | } |
431 | | } |
432 | | |
433 | | /// Returns the total number of input bytes which have been processed by |
434 | | /// this decompression object. |
435 | 289k | pub fn total_in(&self) -> u64 { |
436 | 289k | self.inner.total_in() |
437 | 289k | } |
438 | | |
439 | | /// Returns the total number of output bytes which have been produced by |
440 | | /// this decompression object. |
441 | 289k | pub fn total_out(&self) -> u64 { |
442 | 289k | self.inner.total_out() |
443 | 289k | } |
444 | | |
445 | | /// Decompresses the input data into the output, consuming only as much |
446 | | /// input as needed and writing as much output as possible. |
447 | | /// |
448 | | /// The flush option can be any of the available `FlushDecompress` parameters. |
449 | | /// |
450 | | /// If the first call passes `FlushDecompress::Finish` it is assumed that |
451 | | /// the input and output buffers are both sized large enough to decompress |
452 | | /// the entire stream in a single call. |
453 | | /// |
454 | | /// A flush value of `FlushDecompress::Finish` indicates that there are no |
455 | | /// more source bytes available beside what's already in the input buffer, |
456 | | /// and the output buffer is large enough to hold the rest of the |
457 | | /// decompressed data. |
458 | | /// |
459 | | /// To learn how much data was consumed or how much output was produced, use |
460 | | /// the `total_in` and `total_out` functions before/after this is called. |
461 | | /// |
462 | | /// # Errors |
463 | | /// |
464 | | /// If the input data to this instance of `Decompress` is not a valid |
465 | | /// zlib/deflate stream then this function may return an instance of |
466 | | /// `DecompressError` to indicate that the stream of input bytes is corrupted. |
467 | 144k | pub fn decompress( |
468 | 144k | &mut self, |
469 | 144k | input: &[u8], |
470 | 144k | output: &mut [u8], |
471 | 144k | flush: FlushDecompress, |
472 | 144k | ) -> Result<Status, DecompressError> { |
473 | 144k | self.inner.decompress(input, output, flush) |
474 | 144k | } |
475 | | |
476 | | /// Similar to [`Self::decompress`] but accepts uninitialized buffer |
477 | | /// |
478 | | /// If you want to avoid the overhead of zero initializing the |
479 | | /// buffer and you don't want to use a [`Vec`], then please use |
480 | | /// this API. |
481 | 0 | pub fn decompress_uninit( |
482 | 0 | &mut self, |
483 | 0 | input: &[u8], |
484 | 0 | output: &mut [MaybeUninit<u8>], |
485 | 0 | flush: FlushDecompress, |
486 | 0 | ) -> Result<Status, DecompressError> { |
487 | 0 | self.inner.decompress_uninit(input, output, flush) |
488 | 0 | } |
489 | | |
490 | | /// Decompresses the input data into the extra space in the output vector |
491 | | /// specified by `output`. |
492 | | /// |
493 | | /// This function has the same semantics as `decompress`, except that the |
494 | | /// length of `vec` is managed by this function. This will not reallocate |
495 | | /// the vector provided or attempt to grow it, so space for the output must |
496 | | /// be reserved in the output vector by the caller before calling this |
497 | | /// function. |
498 | | /// |
499 | | /// # Errors |
500 | | /// |
501 | | /// If the input data to this instance of `Decompress` is not a valid |
502 | | /// zlib/deflate stream then this function may return an instance of |
503 | | /// `DecompressError` to indicate that the stream of input bytes is corrupted. |
504 | 0 | pub fn decompress_vec( |
505 | 0 | &mut self, |
506 | 0 | input: &[u8], |
507 | 0 | output: &mut Vec<u8>, |
508 | 0 | flush: FlushDecompress, |
509 | 0 | ) -> Result<Status, DecompressError> { |
510 | | // SAFETY: bytes_written is the number of bytes written into `out` |
511 | | unsafe { |
512 | 0 | write_to_spare_capacity_of_vec(output, |out| { |
513 | 0 | let before = self.total_out(); |
514 | 0 | let ret = self.decompress_uninit(input, out, flush); |
515 | 0 | let bytes_written = self.total_out() - before; |
516 | 0 | (bytes_written as usize, ret) |
517 | 0 | }) |
518 | | } |
519 | 0 | } |
520 | | |
521 | | /// Specifies the decompression dictionary to use. |
522 | | #[cfg(feature = "any_zlib")] |
523 | | pub fn set_dictionary(&mut self, dictionary: &[u8]) -> Result<u32, DecompressError> { |
524 | | // SAFETY: The field `inner` must always be accessed as a raw pointer, |
525 | | // since it points to a cyclic structure. No copies of `inner` can be |
526 | | // retained for longer than the lifetime of `self.inner.inner.stream_wrapper`. |
527 | | let stream = self.inner.inner.stream_wrapper.inner; |
528 | | let rc = unsafe { |
529 | | (*stream).msg = std::ptr::null_mut(); |
530 | | assert!(dictionary.len() < ffi::uInt::MAX as usize); |
531 | | ffi::inflateSetDictionary(stream, dictionary.as_ptr(), dictionary.len() as ffi::uInt) |
532 | | }; |
533 | | |
534 | | #[allow(clippy::unnecessary_cast)] |
535 | | match rc { |
536 | | ffi::MZ_STREAM_ERROR => decompress_failed(self.inner.inner.msg()), |
537 | | ffi::MZ_DATA_ERROR => decompress_need_dict(unsafe { (*stream).adler } as u32), |
538 | | ffi::MZ_OK => Ok(unsafe { (*stream).adler } as u32), |
539 | | c => panic!("unknown return code: {}", c), |
540 | | } |
541 | | } |
542 | | |
543 | | /// Performs the equivalent of replacing this decompression state with a |
544 | | /// freshly allocated copy. |
545 | | /// |
546 | | /// This function may not allocate memory, though, and attempts to reuse any |
547 | | /// previously existing resources. |
548 | | /// |
549 | | /// The argument provided here indicates whether the reset state will |
550 | | /// attempt to decode a zlib header first or not. |
551 | 0 | pub fn reset(&mut self, zlib_header: bool) { |
552 | 0 | self.inner.reset(zlib_header); |
553 | 0 | } |
554 | | } |
555 | | |
556 | | impl Error for DecompressError {} |
557 | | |
558 | | impl DecompressError { |
559 | | /// Retrieve the implementation's message about why the operation failed, if one exists. |
560 | 0 | pub fn message(&self) -> Option<&str> { |
561 | 0 | match &self.0 { |
562 | 0 | DecompressErrorInner::General { msg } => msg.get(), |
563 | 0 | _ => None, |
564 | | } |
565 | 0 | } |
566 | | } |
567 | | |
568 | | impl From<DecompressError> for io::Error { |
569 | 0 | fn from(data: DecompressError) -> io::Error { |
570 | 0 | io::Error::new(io::ErrorKind::Other, data) |
571 | 0 | } |
572 | | } |
573 | | |
574 | | impl fmt::Display for DecompressError { |
575 | 0 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
576 | 0 | let msg = match &self.0 { |
577 | 0 | DecompressErrorInner::General { msg } => msg.get(), |
578 | 0 | DecompressErrorInner::NeedsDictionary { .. } => Some("requires a dictionary"), |
579 | | }; |
580 | 0 | match msg { |
581 | 0 | Some(msg) => write!(f, "deflate decompression error: {msg}"), |
582 | 0 | None => write!(f, "deflate decompression error"), |
583 | | } |
584 | 0 | } |
585 | | } |
586 | | |
587 | | impl Error for CompressError {} |
588 | | |
589 | | impl CompressError { |
590 | | /// Retrieve the implementation's message about why the operation failed, if one exists. |
591 | 0 | pub fn message(&self) -> Option<&str> { |
592 | 0 | self.msg.get() |
593 | 0 | } |
594 | | } |
595 | | |
596 | | impl From<CompressError> for io::Error { |
597 | 0 | fn from(data: CompressError) -> io::Error { |
598 | 0 | io::Error::new(io::ErrorKind::Other, data) |
599 | 0 | } |
600 | | } |
601 | | |
602 | | impl fmt::Display for CompressError { |
603 | 0 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
604 | 0 | match self.msg.get() { |
605 | 0 | Some(msg) => write!(f, "deflate compression error: {msg}"), |
606 | 0 | None => write!(f, "deflate compression error"), |
607 | | } |
608 | 0 | } |
609 | | } |
610 | | |
611 | | /// Allows `writer` to write data into the spare capacity of the `output` vector. |
612 | | /// This will not reallocate the vector provided or attempt to grow it, so space |
613 | | /// for the `output` must be reserved by the caller before calling this |
614 | | /// function. |
615 | | /// |
616 | | /// `writer` needs to return the number of bytes written (and can also return |
617 | | /// another arbitrary return value). |
618 | | /// |
619 | | /// # Safety: |
620 | | /// |
621 | | /// The length returned by the `writer` must be equal to actual number of bytes written |
622 | | /// to the uninitialized slice passed in and initialized. |
623 | 0 | unsafe fn write_to_spare_capacity_of_vec<T>( |
624 | 0 | output: &mut Vec<u8>, |
625 | 0 | writer: impl FnOnce(&mut [MaybeUninit<u8>]) -> (usize, T), |
626 | 0 | ) -> T { |
627 | 0 | let cap = output.capacity(); |
628 | 0 | let len = output.len(); |
629 | | |
630 | 0 | let (bytes_written, ret) = writer(output.spare_capacity_mut()); |
631 | 0 | output.set_len(cap.min(len + bytes_written)); // Sanitizes `bytes_written`. |
632 | | |
633 | 0 | ret |
634 | 0 | } Unexecuted instantiation: flate2::mem::write_to_spare_capacity_of_vec::<core::result::Result<flate2::mem::Status, flate2::mem::CompressError>, <flate2::mem::Compress>::compress_vec::{closure#0}> Unexecuted instantiation: flate2::mem::write_to_spare_capacity_of_vec::<core::result::Result<flate2::mem::Status, flate2::mem::DecompressError>, <flate2::mem::Decompress>::decompress_vec::{closure#0}> |
635 | | |
636 | | #[cfg(test)] |
637 | | mod tests { |
638 | | use std::io::Write; |
639 | | |
640 | | use crate::write; |
641 | | use crate::{Compression, Decompress, FlushDecompress}; |
642 | | |
643 | | #[cfg(feature = "any_zlib")] |
644 | | use crate::{Compress, FlushCompress}; |
645 | | |
646 | | #[test] |
647 | | fn issue51() { |
648 | | let data = [ |
649 | | 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xb3, 0xc9, 0x28, 0xc9, |
650 | | 0xcd, 0xb1, 0xe3, 0xe5, 0xb2, 0xc9, 0x48, 0x4d, 0x4c, 0xb1, 0xb3, 0x29, 0xc9, 0x2c, |
651 | | 0xc9, 0x49, 0xb5, 0x33, 0x31, 0x30, 0x51, 0xf0, 0xcb, 0x2f, 0x51, 0x70, 0xcb, 0x2f, |
652 | | 0xcd, 0x4b, 0xb1, 0xd1, 0x87, 0x08, 0xda, 0xe8, 0x83, 0x95, 0x00, 0x95, 0x26, 0xe5, |
653 | | 0xa7, 0x54, 0x2a, 0x24, 0xa5, 0x27, 0xe7, 0xe7, 0xe4, 0x17, 0xd9, 0x2a, 0x95, 0x67, |
654 | | 0x64, 0x96, 0xa4, 0x2a, 0x81, 0x8c, 0x48, 0x4e, 0xcd, 0x2b, 0x49, 0x2d, 0xb2, 0xb3, |
655 | | 0xc9, 0x30, 0x44, 0x37, 0x01, 0x28, 0x62, 0xa3, 0x0f, 0x95, 0x06, 0xd9, 0x05, 0x54, |
656 | | 0x04, 0xe5, 0xe5, 0xa5, 0x67, 0xe6, 0x55, 0xe8, 0x1b, 0xea, 0x99, 0xe9, 0x19, 0x21, |
657 | | 0xab, 0xd0, 0x07, 0xd9, 0x01, 0x32, 0x53, 0x1f, 0xea, 0x3e, 0x00, 0x94, 0x85, 0xeb, |
658 | | 0xe4, 0xa8, 0x00, 0x00, 0x00, |
659 | | ]; |
660 | | |
661 | | let mut decoded = Vec::with_capacity(data.len() * 2); |
662 | | |
663 | | let mut d = Decompress::new(false); |
664 | | // decompressed whole deflate stream |
665 | | assert!(d |
666 | | .decompress_vec(&data[10..], &mut decoded, FlushDecompress::Finish) |
667 | | .is_ok()); |
668 | | |
669 | | // decompress data that has nothing to do with the deflate stream (this |
670 | | // used to panic) |
671 | | drop(d.decompress_vec(&[0], &mut decoded, FlushDecompress::None)); |
672 | | } |
673 | | |
674 | | #[test] |
675 | | fn reset() { |
676 | | let string = "hello world".as_bytes(); |
677 | | let mut zlib = Vec::new(); |
678 | | let mut deflate = Vec::new(); |
679 | | |
680 | | let comp = Compression::default(); |
681 | | write::ZlibEncoder::new(&mut zlib, comp) |
682 | | .write_all(string) |
683 | | .unwrap(); |
684 | | write::DeflateEncoder::new(&mut deflate, comp) |
685 | | .write_all(string) |
686 | | .unwrap(); |
687 | | |
688 | | let mut dst = [0; 1024]; |
689 | | let mut decoder = Decompress::new(true); |
690 | | decoder |
691 | | .decompress(&zlib, &mut dst, FlushDecompress::Finish) |
692 | | .unwrap(); |
693 | | assert_eq!(decoder.total_out(), string.len() as u64); |
694 | | assert!(dst.starts_with(string)); |
695 | | |
696 | | decoder.reset(false); |
697 | | decoder |
698 | | .decompress(&deflate, &mut dst, FlushDecompress::Finish) |
699 | | .unwrap(); |
700 | | assert_eq!(decoder.total_out(), string.len() as u64); |
701 | | assert!(dst.starts_with(string)); |
702 | | } |
703 | | |
704 | | #[cfg(feature = "any_zlib")] |
705 | | #[test] |
706 | | fn set_dictionary_with_zlib_header() { |
707 | | let string = "hello, hello!".as_bytes(); |
708 | | let dictionary = "hello".as_bytes(); |
709 | | |
710 | | let mut encoded = Vec::with_capacity(1024); |
711 | | |
712 | | let mut encoder = Compress::new(Compression::default(), true); |
713 | | |
714 | | let dictionary_adler = encoder.set_dictionary(&dictionary).unwrap(); |
715 | | |
716 | | encoder |
717 | | .compress_vec(string, &mut encoded, FlushCompress::Finish) |
718 | | .unwrap(); |
719 | | |
720 | | assert_eq!(encoder.total_in(), string.len() as u64); |
721 | | assert_eq!(encoder.total_out(), encoded.len() as u64); |
722 | | |
723 | | let mut decoder = Decompress::new(true); |
724 | | let mut decoded = [0; 1024]; |
725 | | let decompress_error = decoder |
726 | | .decompress(&encoded, &mut decoded, FlushDecompress::Finish) |
727 | | .expect_err("decompression should fail due to requiring a dictionary"); |
728 | | |
729 | | let required_adler = decompress_error.needs_dictionary() |
730 | | .expect("the first call to decompress should indicate a dictionary is required along with the required Adler-32 checksum"); |
731 | | |
732 | | assert_eq!(required_adler, dictionary_adler, |
733 | | "the Adler-32 checksum should match the value when the dictionary was set on the compressor"); |
734 | | |
735 | | let actual_adler = decoder.set_dictionary(&dictionary).unwrap(); |
736 | | |
737 | | assert_eq!(required_adler, actual_adler); |
738 | | |
739 | | // Decompress the rest of the input to the remainder of the output buffer |
740 | | let total_in = decoder.total_in(); |
741 | | let total_out = decoder.total_out(); |
742 | | |
743 | | let decompress_result = decoder.decompress( |
744 | | &encoded[total_in as usize..], |
745 | | &mut decoded[total_out as usize..], |
746 | | FlushDecompress::Finish, |
747 | | ); |
748 | | assert!(decompress_result.is_ok()); |
749 | | |
750 | | assert_eq!(&decoded[..decoder.total_out() as usize], string); |
751 | | } |
752 | | |
753 | | #[cfg(feature = "any_zlib")] |
754 | | #[test] |
755 | | fn set_dictionary_raw() { |
756 | | let string = "hello, hello!".as_bytes(); |
757 | | let dictionary = "hello".as_bytes(); |
758 | | |
759 | | let mut encoded = Vec::with_capacity(1024); |
760 | | |
761 | | let mut encoder = Compress::new(Compression::default(), false); |
762 | | |
763 | | encoder.set_dictionary(&dictionary).unwrap(); |
764 | | |
765 | | encoder |
766 | | .compress_vec(string, &mut encoded, FlushCompress::Finish) |
767 | | .unwrap(); |
768 | | |
769 | | assert_eq!(encoder.total_in(), string.len() as u64); |
770 | | assert_eq!(encoder.total_out(), encoded.len() as u64); |
771 | | |
772 | | let mut decoder = Decompress::new(false); |
773 | | |
774 | | decoder.set_dictionary(&dictionary).unwrap(); |
775 | | |
776 | | let mut decoded = [0; 1024]; |
777 | | let decompress_result = decoder.decompress(&encoded, &mut decoded, FlushDecompress::Finish); |
778 | | |
779 | | assert!(decompress_result.is_ok()); |
780 | | |
781 | | assert_eq!(&decoded[..decoder.total_out() as usize], string); |
782 | | } |
783 | | |
784 | | #[cfg(feature = "any_zlib")] |
785 | | #[test] |
786 | | fn test_gzip_flate() { |
787 | | let string = "hello, hello!".as_bytes(); |
788 | | |
789 | | let mut encoded = Vec::with_capacity(1024); |
790 | | |
791 | | let mut encoder = Compress::new_gzip(Compression::default(), 9); |
792 | | |
793 | | encoder |
794 | | .compress_vec(string, &mut encoded, FlushCompress::Finish) |
795 | | .unwrap(); |
796 | | |
797 | | assert_eq!(encoder.total_in(), string.len() as u64); |
798 | | assert_eq!(encoder.total_out(), encoded.len() as u64); |
799 | | |
800 | | let mut decoder = Decompress::new_gzip(9); |
801 | | |
802 | | let mut decoded = [0; 1024]; |
803 | | decoder |
804 | | .decompress(&encoded, &mut decoded, FlushDecompress::Finish) |
805 | | .unwrap(); |
806 | | |
807 | | assert_eq!(&decoded[..decoder.total_out() as usize], string); |
808 | | } |
809 | | |
810 | | #[cfg(feature = "any_zlib")] |
811 | | #[test] |
812 | | fn test_error_message() { |
813 | | let mut decoder = Decompress::new(false); |
814 | | let mut decoded = [0; 128]; |
815 | | let garbage = b"xbvxzi"; |
816 | | |
817 | | let err = decoder |
818 | | .decompress(&*garbage, &mut decoded, FlushDecompress::Finish) |
819 | | .unwrap_err(); |
820 | | |
821 | | assert_eq!(err.message(), Some("invalid stored block lengths")); |
822 | | } |
823 | | } |