flate2/mem.rs
1use std::error::Error;
2use std::fmt;
3use std::io;
4use std::mem::MaybeUninit;
5
6use crate::ffi::{self, Backend, Deflate, DeflateBackend, ErrorMessage, Inflate, InflateBackend};
7use 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)]
22pub 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)]
39pub 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)]
48pub 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)]
92pub 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)]
116pub(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)]
124pub struct DecompressError(pub(crate) DecompressErrorInner);
125
126impl 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 pub fn needs_dictionary(&self) -> Option<u32> {
132 match self.0 {
133 DecompressErrorInner::NeedsDictionary(adler) => Some(adler),
134 _ => None,
135 }
136 }
137}
138
139#[inline]
140pub(crate) fn decompress_failed<T>(msg: ErrorMessage) -> Result<T, DecompressError> {
141 Err(DecompressError(DecompressErrorInner::General { msg }))
142}
143
144#[inline]
145pub(crate) fn decompress_need_dict<T>(adler: u32) -> Result<T, DecompressError> {
146 Err(DecompressError(DecompressErrorInner::NeedsDictionary(
147 adler,
148 )))
149}
150
151/// Error returned when a compression object is used incorrectly or otherwise
152/// generates an error.
153#[derive(Clone, Debug)]
154pub struct CompressError {
155 pub(crate) msg: ErrorMessage,
156}
157
158#[inline]
159pub(crate) fn compress_failed<T>(msg: ErrorMessage) -> Result<T, CompressError> {
160 Err(CompressError { msg })
161}
162
163/// Possible status results of compressing some data or successfully
164/// decompressing a block of data.
165#[derive(Copy, Clone, PartialEq, Eq, Debug)]
166pub 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
192impl 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 pub fn new(level: Compression, zlib_header: bool) -> Compress {
199 Compress {
200 inner: Deflate::make(level, zlib_header, ffi::MZ_DEFAULT_WINDOW_BITS as u8),
201 }
202 }
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 pub fn total_in(&self) -> u64 {
257 self.inner.total_in()
258 }
259
260 /// Returns the total number of output bytes which have been produced by
261 /// this compression object.
262 pub fn total_out(&self) -> u64 {
263 self.inner.total_out()
264 }
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 pub fn reset(&mut self) {
293 self.inner.reset();
294 }
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 pub fn compress(
333 &mut self,
334 input: &[u8],
335 output: &mut [u8],
336 flush: FlushCompress,
337 ) -> Result<Status, CompressError> {
338 self.inner.compress(input, output, flush)
339 }
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 pub fn compress_uninit(
347 &mut self,
348 input: &[u8],
349 output: &mut [MaybeUninit<u8>],
350 flush: FlushCompress,
351 ) -> Result<Status, CompressError> {
352 self.inner.compress_uninit(input, output, flush)
353 }
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 pub fn compress_vec(
364 &mut self,
365 input: &[u8],
366 output: &mut Vec<u8>,
367 flush: FlushCompress,
368 ) -> Result<Status, CompressError> {
369 // SAFETY: bytes_written is the number of bytes written into `out`
370 unsafe {
371 write_to_spare_capacity_of_vec(output, |out| {
372 let before = self.total_out();
373 let ret = self.compress_uninit(input, out, flush);
374 let bytes_written = self.total_out() - before;
375 (bytes_written as usize, ret)
376 })
377 }
378 }
379}
380
381impl 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 pub fn new(zlib_header: bool) -> Decompress {
387 Decompress {
388 inner: Inflate::make(zlib_header, ffi::MZ_DEFAULT_WINDOW_BITS as u8),
389 }
390 }
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 pub fn total_in(&self) -> u64 {
436 self.inner.total_in()
437 }
438
439 /// Returns the total number of output bytes which have been produced by
440 /// this decompression object.
441 pub fn total_out(&self) -> u64 {
442 self.inner.total_out()
443 }
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 pub fn decompress(
468 &mut self,
469 input: &[u8],
470 output: &mut [u8],
471 flush: FlushDecompress,
472 ) -> Result<Status, DecompressError> {
473 self.inner.decompress(input, output, flush)
474 }
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 pub fn decompress_uninit(
482 &mut self,
483 input: &[u8],
484 output: &mut [MaybeUninit<u8>],
485 flush: FlushDecompress,
486 ) -> Result<Status, DecompressError> {
487 self.inner.decompress_uninit(input, output, flush)
488 }
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 pub fn decompress_vec(
505 &mut self,
506 input: &[u8],
507 output: &mut Vec<u8>,
508 flush: FlushDecompress,
509 ) -> Result<Status, DecompressError> {
510 // SAFETY: bytes_written is the number of bytes written into `out`
511 unsafe {
512 write_to_spare_capacity_of_vec(output, |out| {
513 let before = self.total_out();
514 let ret = self.decompress_uninit(input, out, flush);
515 let bytes_written = self.total_out() - before;
516 (bytes_written as usize, ret)
517 })
518 }
519 }
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 pub fn reset(&mut self, zlib_header: bool) {
552 self.inner.reset(zlib_header);
553 }
554}
555
556impl Error for DecompressError {}
557
558impl DecompressError {
559 /// Retrieve the implementation's message about why the operation failed, if one exists.
560 pub fn message(&self) -> Option<&str> {
561 match &self.0 {
562 DecompressErrorInner::General { msg } => msg.get(),
563 _ => None,
564 }
565 }
566}
567
568impl From<DecompressError> for io::Error {
569 fn from(data: DecompressError) -> io::Error {
570 io::Error::new(io::ErrorKind::Other, data)
571 }
572}
573
574impl fmt::Display for DecompressError {
575 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
576 let msg = match &self.0 {
577 DecompressErrorInner::General { msg } => msg.get(),
578 DecompressErrorInner::NeedsDictionary { .. } => Some("requires a dictionary"),
579 };
580 match msg {
581 Some(msg) => write!(f, "deflate decompression error: {msg}"),
582 None => write!(f, "deflate decompression error"),
583 }
584 }
585}
586
587impl Error for CompressError {}
588
589impl CompressError {
590 /// Retrieve the implementation's message about why the operation failed, if one exists.
591 pub fn message(&self) -> Option<&str> {
592 self.msg.get()
593 }
594}
595
596impl From<CompressError> for io::Error {
597 fn from(data: CompressError) -> io::Error {
598 io::Error::new(io::ErrorKind::Other, data)
599 }
600}
601
602impl fmt::Display for CompressError {
603 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
604 match self.msg.get() {
605 Some(msg) => write!(f, "deflate compression error: {msg}"),
606 None => write!(f, "deflate compression error"),
607 }
608 }
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.
623unsafe fn write_to_spare_capacity_of_vec<T>(
624 output: &mut Vec<u8>,
625 writer: impl FnOnce(&mut [MaybeUninit<u8>]) -> (usize, T),
626) -> T {
627 let cap = output.capacity();
628 let len = output.len();
629
630 let (bytes_written, ret) = writer(output.spare_capacity_mut());
631 output.set_len(cap.min(len + bytes_written)); // Sanitizes `bytes_written`.
632
633 ret
634}
635
636#[cfg(test)]
637mod 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}