miniz_oxide/
lib.rs

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.
6//!
7//! # Usage
8//! ## Simple compression/decompression:
9//! ``` rust
10//!
11//! use miniz_oxide::inflate::decompress_to_vec;
12//! use miniz_oxide::deflate::compress_to_vec;
13//!
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;
18//! }
19//!
20//! # roundtrip(b"Test_data test data lalalal blabla");
21//!
22//! ```
23
24#![forbid(unsafe_code)]
25
26extern crate adler32;
27
28pub mod deflate;
29pub mod inflate;
30mod shared;
31
32pub use crate::shared::update_adler32 as mz_adler32_oxide;
33pub use crate::shared::{MZ_ADLER32_INIT, MZ_DEFAULT_WINDOW_BITS};
34
35/// A list of flush types.
36///
37/// See [http://www.bolet.org/~pornin/deflate-flush.html] for more in-depth info.
38#[repr(i32)]
39#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
40pub enum MZFlush {
41    /// Don't force any flushing.
42    /// Used when more input data is expected.
43    None = 0,
44    /// Zlib partial flush.
45    /// Currently treated as `Sync`.
46    Partial = 1,
47    /// Finish compressing the currently buffered data, and output an empty raw block.
48    /// Has no use in decompression.
49    Sync = 2,
50    /// Same as `Sync`, but resets the compression dictionary so that further compressed
51    /// data does not depend on data compressed before the flush.
52    /// Has no use in decompression.
53    Full = 3,
54    /// Attempt to flush the remaining data and end the stream.
55    Finish = 4,
56    /// Not implemented.
57    Block = 5,
58}
59
60impl MZFlush {
61    /// Create an MZFlush value from an integer value.
62    ///
63    /// Returns `MZError::Param` on invalid values.
64    pub fn new(flush: i32) -> Result<Self, MZError> {
65        match flush {
66            0 => Ok(MZFlush::None),
67            1 | 2 => Ok(MZFlush::Sync),
68            3 => Ok(MZFlush::Full),
69            4 => Ok(MZFlush::Finish),
70            _ => Err(MZError::Param),
71        }
72    }
73}
74
75/// A list of miniz successful status codes.
76#[repr(i32)]
77#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
78pub enum MZStatus {
79    Ok = 0,
80    StreamEnd = 1,
81    NeedDict = 2,
82}
83
84/// A list of miniz failed status codes.
85#[repr(i32)]
86#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
87pub enum MZError {
88    ErrNo = -1,
89    Stream = -2,
90    Data = -3,
91    Mem = -4,
92    Buf = -5,
93    Version = -6,
94    Param = -10_000,
95}
96
97/// How compressed data is wrapped.
98#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
99pub enum DataFormat {
100    /// Wrapped using the [zlib](http://www.zlib.org/rfc-zlib.html) format.
101    Zlib,
102    /// Raw DEFLATE.
103    Raw,
104}
105
106impl DataFormat {
107    pub(crate) fn from_window_bits(window_bits: i32) -> DataFormat {
108        if window_bits > 0 {
109            DataFormat::Zlib
110        } else {
111            DataFormat::Raw
112        }
113    }
114
115    pub(crate) fn to_window_bits(self) -> i32 {
116        match self {
117            DataFormat::Zlib => shared::MZ_DEFAULT_WINDOW_BITS,
118            DataFormat::Raw => -shared::MZ_DEFAULT_WINDOW_BITS,
119        }
120    }
121}
122
123/// `Result` alias for all miniz status codes both successful and failed.
124pub type MZResult = Result<MZStatus, MZError>;
125
126/// A structure containg the result of a call to the inflate or deflate streaming functions.
127#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
128pub struct StreamResult {
129    /// The number of bytes consumed from the input slice.
130    pub bytes_consumed: usize,
131    /// The number of bytes written to the output slice.
132    pub bytes_written: usize,
133    /// The return status of the call.
134    pub status: MZResult,
135}
136
137impl StreamResult {
138    #[inline]
139    pub(crate) fn error(error: MZError) -> StreamResult {
140        StreamResult {
141            bytes_consumed: 0,
142            bytes_written: 0,
143            status: Err(error),
144        }
145    }
146}
147
148impl std::convert::From<StreamResult> for MZResult {
149    fn from(res: StreamResult) -> Self {
150        res.status
151    }
152}
153
154impl std::convert::From<&StreamResult> for MZResult {
155    fn from(res: &StreamResult) -> Self {
156        res.status
157    }
158}