#![cfg_attr(
feature = "with-alloc",
doc = r##"
# Usage
## Simple compression/decompression:
``` rust
use miniz_oxide::inflate::decompress_to_vec;
use miniz_oxide::deflate::compress_to_vec;
fn roundtrip(data: &[u8]) {
let compressed = compress_to_vec(data, 6);
let decompressed = decompress_to_vec(compressed.as_slice()).expect("Failed to decompress!");
# let _ = decompressed;
}
# roundtrip(b"Test_data test data lalalal blabla");
"##
)]
#![forbid(unsafe_code)]
#![cfg_attr(all(not(feature = "std"), not(feature = "serde")), no_std)]
#[cfg(feature = "with-alloc")]
extern crate alloc;
#[cfg(feature = "with-alloc")]
pub mod deflate;
pub mod inflate;
#[cfg(feature = "serde")]
pub mod serde;
mod shared;
pub use crate::shared::update_adler32 as mz_adler32_oxide;
pub use crate::shared::{MZ_ADLER32_INIT, MZ_DEFAULT_WINDOW_BITS};
#[repr(i32)]
#[derive(Copy, Clone, PartialEq, Eq)]
#[cfg_attr(not(feature = "rustc-dep-of-std"), derive(Hash, Debug))]
pub enum MZFlush {
None = 0,
Partial = 1,
Sync = 2,
Full = 3,
Finish = 4,
Block = 5,
}
impl MZFlush {
pub fn new(flush: i32) -> Result<Self, MZError> {
match flush {
0 => Ok(MZFlush::None),
1 | 2 => Ok(MZFlush::Sync),
3 => Ok(MZFlush::Full),
4 => Ok(MZFlush::Finish),
_ => Err(MZError::Param),
}
}
}
#[repr(i32)]
#[derive(Copy, Clone, PartialEq, Eq)]
#[cfg_attr(not(feature = "rustc-dep-of-std"), derive(Hash, Debug))]
pub enum MZStatus {
Ok = 0,
StreamEnd = 1,
NeedDict = 2,
}
#[repr(i32)]
#[cfg_attr(not(feature = "rustc-dep-of-std"), derive(Hash, Debug))]
#[derive(Copy, Clone, PartialEq, Eq)]
pub enum MZError {
ErrNo = -1,
Stream = -2,
Data = -3,
Mem = -4,
Buf = -5,
Version = -6,
Param = -10_000,
}
#[derive(Copy, Clone, PartialEq, Eq)]
#[cfg_attr(not(feature = "rustc-dep-of-std"), derive(Hash, Debug))]
#[non_exhaustive]
pub enum DataFormat {
Zlib,
ZLibIgnoreChecksum,
Raw,
}
#[cfg(not(feature = "rustc-dep-of-std"))]
impl DataFormat {
pub fn from_window_bits(window_bits: i32) -> DataFormat {
if window_bits > 0 {
DataFormat::Zlib
} else {
DataFormat::Raw
}
}
pub fn to_window_bits(self) -> i32 {
match self {
DataFormat::Zlib | DataFormat::ZLibIgnoreChecksum => shared::MZ_DEFAULT_WINDOW_BITS,
DataFormat::Raw => -shared::MZ_DEFAULT_WINDOW_BITS,
}
}
}
pub type MZResult = Result<MZStatus, MZError>;
#[cfg(not(feature = "rustc-dep-of-std"))]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct StreamResult {
pub bytes_consumed: usize,
pub bytes_written: usize,
pub status: MZResult,
}
#[cfg(not(feature = "rustc-dep-of-std"))]
impl StreamResult {
#[inline]
pub const fn error(error: MZError) -> StreamResult {
StreamResult {
bytes_consumed: 0,
bytes_written: 0,
status: Err(error),
}
}
}
#[cfg(not(feature = "rustc-dep-of-std"))]
impl core::convert::From<StreamResult> for MZResult {
fn from(res: StreamResult) -> Self {
res.status
}
}
#[cfg(not(feature = "rustc-dep-of-std"))]
impl core::convert::From<&StreamResult> for MZResult {
fn from(res: &StreamResult) -> Self {
res.status
}
}