1 //! A pure rust replacement for the [miniz](https://github.com/richgel999/miniz)
2 //! DEFLATE/zlib encoder/decoder.
3 //! The plan for this crate is to be used as a back-end for the
4 //! [flate2](https://github.com/alexcrichton/flate2-rs) crate and eventually remove the
5 //! need to depend on a C library.
8 //! ## Simple compression/decompression:
11 //! use miniz_oxide::inflate::decompress_to_vec;
12 //! use miniz_oxide::deflate::compress_to_vec;
14 //! fn roundtrip(data: &[u8]) {
15 //! let compressed = compress_to_vec(data, 6);
16 //! let decompressed = decompress_to_vec(compressed.as_slice()).expect("Failed to decompress!");
17 //! # let _ = decompressed;
20 //! # roundtrip(b"Test_data test data lalalal blabla");
24 #![forbid(unsafe_code)]
33 pub use crate::shared
::update_adler32
as mz_adler32_oxide
;
34 pub use crate::shared
::{MZ_ADLER32_INIT, MZ_DEFAULT_WINDOW_BITS}
;
36 /// A list of flush types.
38 /// See <http://www.bolet.org/~pornin/deflate-flush.html> for more in-depth info.
40 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
42 /// Don't force any flushing.
43 /// Used when more input data is expected.
45 /// Zlib partial flush.
46 /// Currently treated as [`Sync`].
48 /// Finish compressing the currently buffered data, and output an empty raw block.
49 /// Has no use in decompression.
51 /// Same as [`Sync`], but resets the compression dictionary so that further compressed
52 /// data does not depend on data compressed before the flush.
54 /// Has no use in decompression, and is an error to supply in that case.
56 /// Attempt to flush the remaining data and end the stream.
63 /// Create an MZFlush value from an integer value.
65 /// Returns `MZError::Param` on invalid values.
66 pub fn new(flush
: i32) -> Result
<Self, MZError
> {
68 0 => Ok(MZFlush
::None
),
69 1 | 2 => Ok(MZFlush
::Sync
),
70 3 => Ok(MZFlush
::Full
),
71 4 => Ok(MZFlush
::Finish
),
72 _
=> Err(MZError
::Param
),
77 /// A list of miniz successful status codes.
79 /// These are emitted as the [`Ok`] side of a [`MZResult`] in the [`StreamResult`] returned from
80 /// [`deflate::stream::deflate()`] or [`inflate::stream::inflate()`].
82 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
84 /// Operation succeeded.
86 /// Some data was decompressed or compressed; see the byte counters in the [`StreamResult`] for
90 /// Operation succeeded and end of deflate stream was found.
92 /// X-ref [`TINFLStatus::Done`][inflate::TINFLStatus::Done] or
93 /// [`TDEFLStatus::Done`][deflate::core::TDEFLStatus::Done] for `inflate` or `deflate`
101 /// A list of miniz failed status codes.
103 /// These are emitted as the [`Err`] side of a [`MZResult`] in the [`StreamResult`] returned from
104 /// [`deflate::stream::deflate()`] or [`inflate::stream::inflate()`].
106 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
111 /// General stream error.
113 /// See [`inflate::stream::inflate()`] docs for details of how it can occur there.
115 /// See [`deflate::stream::deflate()`] docs for how it can in principle occur there, though it's
116 /// believed impossible in practice.
119 /// Error in inflation; see [`inflate::stream::inflate()`] for details.
121 /// Not returned from [`deflate::stream::deflate()`].
127 /// Buffer-related error.
129 /// See the docs of [`deflate::stream::deflate()`] or [`inflate::stream::inflate()`] for details
130 /// of when it would trigger in the one you're using.
138 /// This can be returned from [`deflate::stream::deflate()`] in the case of bad parameters. See
139 /// [`TDEFLStatus::BadParam`][deflate::core::TDEFLStatus::BadParam].
143 /// How compressed data is wrapped.
144 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
146 pub enum DataFormat
{
147 /// Wrapped using the [zlib](http://www.zlib.org/rfc-zlib.html) format.
149 /// Zlib wrapped but ignore and don't compute the adler32 checksum.
150 /// Currently only used for inflate, behaves the same as Zlib for compression.
157 pub(crate) fn from_window_bits(window_bits
: i32) -> DataFormat
{
165 pub(crate) fn to_window_bits(self) -> i32 {
167 DataFormat
::Zlib
| DataFormat
::ZLibIgnoreChecksum
=> shared
::MZ_DEFAULT_WINDOW_BITS
,
168 DataFormat
::Raw
=> -shared
::MZ_DEFAULT_WINDOW_BITS
,
173 /// `Result` alias for all miniz status codes both successful and failed.
174 pub type MZResult
= Result
<MZStatus
, MZError
>;
176 /// A structure containg the result of a call to the inflate or deflate streaming functions.
177 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
178 pub struct StreamResult
{
179 /// The number of bytes consumed from the input slice.
180 pub bytes_consumed
: usize,
181 /// The number of bytes written to the output slice.
182 pub bytes_written
: usize,
183 /// The return status of the call.
184 pub status
: MZResult
,
189 pub(crate) const fn error(error
: MZError
) -> StreamResult
{
198 impl core
::convert
::From
<StreamResult
> for MZResult
{
199 fn from(res
: StreamResult
) -> Self {
204 impl core
::convert
::From
<&StreamResult
> for MZResult
{
205 fn from(res
: &StreamResult
) -> Self {