|
| 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 | + |
| 34 | +impl From<::zlib_rs::Status> for crate::mem::Status { |
| 35 | + fn from(value: ::zlib_rs::Status) -> Self { |
| 36 | + match value { |
| 37 | + ::zlib_rs::Status::Ok => crate::mem::Status::Ok, |
| 38 | + ::zlib_rs::Status::BufError => crate::mem::Status::BufError, |
| 39 | + ::zlib_rs::Status::StreamEnd => crate::mem::Status::StreamEnd, |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +#[derive(Clone, Default)] |
| 45 | +pub struct ErrorMessage(Option<&'static str>); |
| 46 | + |
| 47 | +impl ErrorMessage { |
| 48 | + pub fn get(&self) -> Option<&str> { |
| 49 | + self.0 |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +pub struct Inflate { |
| 54 | + pub(crate) inner: ::zlib_rs::Inflate, |
| 55 | +} |
| 56 | + |
| 57 | +impl fmt::Debug for Inflate { |
| 58 | + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { |
| 59 | + write!( |
| 60 | + f, |
| 61 | + "zlib_rs inflate internal state. total_in: {}, total_out: {}", |
| 62 | + self.inner.total_in(), |
| 63 | + self.inner.total_out(), |
| 64 | + ) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +impl From<FlushDecompress> for DeflateFlush { |
| 69 | + fn from(value: FlushDecompress) -> Self { |
| 70 | + match value { |
| 71 | + FlushDecompress::None => Self::NoFlush, |
| 72 | + FlushDecompress::Sync => Self::SyncFlush, |
| 73 | + FlushDecompress::Finish => Self::Finish, |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +impl InflateBackend for Inflate { |
| 79 | + fn make(zlib_header: bool, window_bits: u8) -> Self { |
| 80 | + Inflate { |
| 81 | + inner: ::zlib_rs::Inflate::new(zlib_header, window_bits), |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + fn decompress( |
| 86 | + &mut self, |
| 87 | + input: &[u8], |
| 88 | + output: &mut [u8], |
| 89 | + flush: FlushDecompress, |
| 90 | + ) -> Result<Status, DecompressError> { |
| 91 | + let flush = match flush { |
| 92 | + FlushDecompress::None => InflateFlush::NoFlush, |
| 93 | + FlushDecompress::Sync => InflateFlush::SyncFlush, |
| 94 | + FlushDecompress::Finish => InflateFlush::Finish, |
| 95 | + }; |
| 96 | + |
| 97 | + match self.inner.decompress(input, output, flush) { |
| 98 | + Ok(status) => Ok(status.into()), |
| 99 | + Err(InflateError::NeedDict { dict_id }) => crate::mem::decompress_need_dict(dict_id), |
| 100 | + Err(e) => crate::mem::decompress_failed(ErrorMessage(Some(e.as_str()))), |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + fn reset(&mut self, zlib_header: bool) { |
| 105 | + self.inner.reset(zlib_header); |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +impl Backend for Inflate { |
| 110 | + #[inline] |
| 111 | + fn total_in(&self) -> u64 { |
| 112 | + self.inner.total_in() |
| 113 | + } |
| 114 | + |
| 115 | + #[inline] |
| 116 | + fn total_out(&self) -> u64 { |
| 117 | + self.inner.total_out() |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +pub struct Deflate { |
| 122 | + pub(crate) inner: ::zlib_rs::Deflate, |
| 123 | +} |
| 124 | + |
| 125 | +impl fmt::Debug for Deflate { |
| 126 | + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { |
| 127 | + write!( |
| 128 | + f, |
| 129 | + "zlib_rs deflate internal state. total_in: {}, total_out: {}", |
| 130 | + self.inner.total_in(), |
| 131 | + self.inner.total_out(), |
| 132 | + ) |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +impl DeflateBackend for Deflate { |
| 137 | + fn make(level: Compression, zlib_header: bool, window_bits: u8) -> Self { |
| 138 | + // Check in case the integer value changes at some point. |
| 139 | + debug_assert!(level.level() <= 9); |
| 140 | + |
| 141 | + Deflate { |
| 142 | + inner: ::zlib_rs::Deflate::new(level.level() as i32, zlib_header, window_bits), |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + fn compress( |
| 147 | + &mut self, |
| 148 | + input: &[u8], |
| 149 | + output: &mut [u8], |
| 150 | + flush: FlushCompress, |
| 151 | + ) -> Result<Status, CompressError> { |
| 152 | + let flush = match flush { |
| 153 | + FlushCompress::None => DeflateFlush::NoFlush, |
| 154 | + FlushCompress::Partial => DeflateFlush::PartialFlush, |
| 155 | + FlushCompress::Sync => DeflateFlush::SyncFlush, |
| 156 | + FlushCompress::Full => DeflateFlush::FullFlush, |
| 157 | + FlushCompress::Finish => DeflateFlush::Finish, |
| 158 | + }; |
| 159 | + |
| 160 | + match self.inner.compress(input, output, flush) { |
| 161 | + Ok(status) => Ok(status.into()), |
| 162 | + Err(e) => crate::mem::compress_failed(ErrorMessage(Some(e.as_str()))), |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + fn reset(&mut self) { |
| 167 | + self.inner.reset(); |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +impl Backend for Deflate { |
| 172 | + #[inline] |
| 173 | + fn total_in(&self) -> u64 { |
| 174 | + self.inner.total_in() |
| 175 | + } |
| 176 | + |
| 177 | + #[inline] |
| 178 | + fn total_out(&self) -> u64 { |
| 179 | + self.inner.total_out() |
| 180 | + } |
| 181 | +} |
0 commit comments