| 1 |
//! Implementation for `zlib_rs` rust backend. |
| 2 |
//! |
| 3 |
//! Every backend must provide two types: |
| 4 |
//! |
| 5 |
//! - `Deflate` for compression, implements the `Backend` and `DeflateBackend` trait |
| 6 |
//! - `Inflate` for decompression, implements the `Backend` and `InflateBackend` trait |
| 7 |
//! |
| 8 |
//! Additionally the backend provides a number of constants, and a `ErrorMessage` type. |
| 9 |
//! |
| 10 |
//! ## Allocation |
| 11 |
//! |
| 12 |
//! The (de)compression state is not boxed. The C implementations require that the z_stream is |
| 13 |
//! pinned in memory (has a fixed address), because their z_stream is self-referential. The most |
| 14 |
//! convenient way in rust to guarantee a stable address is to `Box` the data, but it does add an |
| 15 |
//! additional allocation. |
| 16 |
//! |
| 17 |
//! With zlib_rs the state is not self-referential and hence no boxing is needed. The `new` methods |
| 18 |
//! internally do allocate space for the (de)compression state. |
| 19 |
|
| 20 |
use std::fmt; |
| 21 |
|
| 22 |
use ::zlib_rs::{DeflateFlush, InflateError, InflateFlush}; |
| 23 |
|
| 24 |
pub const MZ_NO_FLUSH: isize = DeflateFlush::NoFlush as isize; |
| 25 |
pub const MZ_PARTIAL_FLUSH: isize = DeflateFlush::PartialFlush as isize; |
| 26 |
pub const MZ_SYNC_FLUSH: isize = DeflateFlush::SyncFlush as isize; |
| 27 |
pub const MZ_FULL_FLUSH: isize = DeflateFlush::FullFlush as isize; |
| 28 |
pub const MZ_FINISH: isize = DeflateFlush::Finish as isize; |
| 29 |
|
| 30 |
pub const MZ_DEFAULT_WINDOW_BITS: core::ffi::c_int = 15; |
| 31 |
|
| 32 |
use super::*; |
| 33 |
use crate::mem::{compress_failed, decompress_failed}; |
| 34 |
|
| 35 |
impl From<::zlib_rs::Status> for crate::mem::Status { |
| 36 |
fn from(value: ::zlib_rs::Status) -> Self { |
| 37 |
match value { |
| 38 |
::zlib_rs::Status::Ok => crate::mem::Status::Ok, |
| 39 |
::zlib_rs::Status::BufError => crate::mem::Status::BufError, |
| 40 |
::zlib_rs::Status::StreamEnd => crate::mem::Status::StreamEnd, |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
#[derive(Clone, Default)] |
| 46 |
pub struct ErrorMessage(Option<&'static str>); |
| 47 |
|
| 48 |
impl ErrorMessage { |
| 49 |
pub fn get(&self) -> Option<&str> { |
| 50 |
self.0 |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
pub struct Inflate { |
| 55 |
pub(crate) inner: ::zlib_rs::Inflate, |
| 56 |
// NOTE: these counts do not count the dictionary. |
| 57 |
total_in: u64, |
| 58 |
total_out: u64, |
| 59 |
} |
| 60 |
|
| 61 |
impl fmt::Debug for Inflate { |
| 62 |
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { |
| 63 |
write!( |
| 64 |
f, |
| 65 |
"zlib_rs inflate internal state. total_in: {}, total_out: {}", |
| 66 |
self.total_in(), |
| 67 |
self.total_out(), |
| 68 |
) |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
impl From<FlushDecompress> for DeflateFlush { |
| 73 |
fn from(value: FlushDecompress) -> Self { |
| 74 |
match value { |
| 75 |
FlushDecompress::None => Self::NoFlush, |
| 76 |
FlushDecompress::Sync => Self::SyncFlush, |
| 77 |
FlushDecompress::Finish => Self::Finish, |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
impl InflateBackend for Inflate { |
| 83 |
fn make(zlib_header: bool, window_bits: u8) -> Self { |
| 84 |
Inflate { |
| 85 |
inner: ::zlib_rs::Inflate::new(zlib_header, window_bits), |
| 86 |
total_in: 0, |
| 87 |
total_out: 0, |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
fn decompress( |
| 92 |
&mut self, |
| 93 |
input: &[u8], |
| 94 |
output: &mut [u8], |
| 95 |
flush: FlushDecompress, |
| 96 |
) -> Result<Status, DecompressError> { |
| 97 |
let flush = match flush { |
| 98 |
FlushDecompress::None => InflateFlush::NoFlush, |
| 99 |
FlushDecompress::Sync => InflateFlush::SyncFlush, |
| 100 |
FlushDecompress::Finish => InflateFlush::Finish, |
| 101 |
}; |
| 102 |
|
| 103 |
let total_in_start = self.inner.total_in(); |
| 104 |
let total_out_start = self.inner.total_out(); |
| 105 |
|
| 106 |
let result = self.inner.decompress(input, output, flush); |
| 107 |
|
| 108 |
self.total_in += self.inner.total_in() - total_in_start; |
| 109 |
self.total_out += self.inner.total_out() - total_out_start; |
| 110 |
|
| 111 |
match result { |
| 112 |
Ok(status) => Ok(status.into()), |
| 113 |
Err(InflateError::NeedDict { dict_id }) => crate::mem::decompress_need_dict(dict_id), |
| 114 |
Err(_) => self.decompress_error(), |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
fn reset(&mut self, zlib_header: bool) { |
| 119 |
self.total_in = 0; |
| 120 |
self.total_out = 0; |
| 121 |
self.inner.reset(zlib_header); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
impl Backend for Inflate { |
| 126 |
#[inline] |
| 127 |
fn total_in(&self) -> u64 { |
| 128 |
self.total_in |
| 129 |
} |
| 130 |
|
| 131 |
#[inline] |
| 132 |
fn total_out(&self) -> u64 { |
| 133 |
self.total_out |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
impl Inflate { |
| 138 |
fn decompress_error<T>(&self) -> Result<T, DecompressError> { |
| 139 |
decompress_failed(ErrorMessage(self.inner.error_message())) |
| 140 |
} |
| 141 |
|
| 142 |
pub fn set_dictionary(&mut self, dictionary: &[u8]) -> Result<u32, DecompressError> { |
| 143 |
match self.inner.set_dictionary(dictionary) { |
| 144 |
Ok(v) => Ok(v), |
| 145 |
Err(_) => self.decompress_error(), |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
pub struct Deflate { |
| 151 |
pub(crate) inner: ::zlib_rs::Deflate, |
| 152 |
// NOTE: these counts do not count the dictionary. |
| 153 |
total_in: u64, |
| 154 |
total_out: u64, |
| 155 |
} |
| 156 |
|
| 157 |
impl fmt::Debug for Deflate { |
| 158 |
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { |
| 159 |
write!( |
| 160 |
f, |
| 161 |
"zlib_rs deflate internal state. total_in: {}, total_out: {}", |
| 162 |
self.total_in(), |
| 163 |
self.total_out(), |
| 164 |
) |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
impl DeflateBackend for Deflate { |
| 169 |
fn make(level: Compression, zlib_header: bool, window_bits: u8) -> Self { |
| 170 |
// Check in case the integer value changes at some point. |
| 171 |
debug_assert!(level.level() <= 9); |
| 172 |
|
| 173 |
Deflate { |
| 174 |
inner: ::zlib_rs::Deflate::new(level.level() as i32, zlib_header, window_bits), |
| 175 |
total_in: 0, |
| 176 |
total_out: 0, |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
fn compress( |
| 181 |
&mut self, |
| 182 |
input: &[u8], |
| 183 |
output: &mut [u8], |
| 184 |
flush: FlushCompress, |
| 185 |
) -> Result<Status, CompressError> { |
| 186 |
let flush = match flush { |
| 187 |
FlushCompress::None => DeflateFlush::NoFlush, |
| 188 |
FlushCompress::Partial => DeflateFlush::PartialFlush, |
| 189 |
FlushCompress::Sync => DeflateFlush::SyncFlush, |
| 190 |
FlushCompress::Full => DeflateFlush::FullFlush, |
| 191 |
FlushCompress::Finish => DeflateFlush::Finish, |
| 192 |
}; |
| 193 |
|
| 194 |
let total_in_start = self.inner.total_in(); |
| 195 |
let total_out_start = self.inner.total_out(); |
| 196 |
|
| 197 |
let result = self.inner.compress(input, output, flush); |
| 198 |
|
| 199 |
self.total_in += self.inner.total_in() - total_in_start; |
| 200 |
self.total_out += self.inner.total_out() - total_out_start; |
| 201 |
|
| 202 |
match result { |
| 203 |
Ok(status) => Ok(status.into()), |
| 204 |
Err(_) => self.compress_error(), |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
fn reset(&mut self) { |
| 209 |
self.total_in = 0; |
| 210 |
self.total_out = 0; |
| 211 |
self.inner.reset(); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
impl Backend for Deflate { |
| 216 |
#[inline] |
| 217 |
fn total_in(&self) -> u64 { |
| 218 |
self.total_in |
| 219 |
} |
| 220 |
|
| 221 |
#[inline] |
| 222 |
fn total_out(&self) -> u64 { |
| 223 |
self.total_out |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
impl Deflate { |
| 228 |
fn compress_error<T>(&self) -> Result<T, CompressError> { |
| 229 |
compress_failed(ErrorMessage(self.inner.error_message())) |
| 230 |
} |
| 231 |
|
| 232 |
pub fn set_dictionary(&mut self, dictionary: &[u8]) -> Result<u32, CompressError> { |
| 233 |
match self.inner.set_dictionary(dictionary) { |
| 234 |
Ok(v) => Ok(v), |
| 235 |
Err(_) => self.compress_error(), |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
pub fn set_level(&mut self, level: Compression) -> Result<(), CompressError> { |
| 240 |
use ::zlib_rs::Status; |
| 241 |
|
| 242 |
match self.inner.set_level(level.level() as i32) { |
| 243 |
Ok(status) => match status { |
| 244 |
Status::Ok => Ok(()), |
| 245 |
Status::BufError => compress_failed(ErrorMessage(Some("insufficient space"))), |
| 246 |
Status::StreamEnd => { |
| 247 |
unreachable!("zlib-rs is known to never return the StreamEnd status") |
| 248 |
} |
| 249 |
}, |
| 250 |
Err(_) => self.compress_error(), |
| 251 |
} |
| 252 |
} |
| 253 |
} |