flate2/ffi/
miniz_oxide.rs1use alloc::boxed::Box;
4use core::convert::TryInto;
5use core::fmt;
6
7use ::miniz_oxide::deflate::core::CompressorOxide;
8use ::miniz_oxide::inflate::stream::{FullReset, InflateState};
9pub use ::miniz_oxide::*;
10
11pub const MZ_NO_FLUSH: isize = MZFlush::None as isize;
12pub const MZ_PARTIAL_FLUSH: isize = MZFlush::Partial as isize;
13pub const MZ_SYNC_FLUSH: isize = MZFlush::Sync as isize;
14pub const MZ_FULL_FLUSH: isize = MZFlush::Full as isize;
15pub const MZ_FINISH: isize = MZFlush::Finish as isize;
16
17use super::*;
18use crate::mem;
19
20#[derive(Clone, Default)]
22pub struct ErrorMessage;
23
24impl ErrorMessage {
25 pub fn get(&self) -> Option<&str> {
26 None
27 }
28}
29
30fn format_from_bool(zlib_header: bool) -> DataFormat {
31 if zlib_header {
32 DataFormat::Zlib
33 } else {
34 DataFormat::Raw
35 }
36}
37
38pub struct Inflate {
39 inner: Box<InflateState>,
40 total_in: u64,
41 total_out: u64,
42}
43
44impl fmt::Debug for Inflate {
45 fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
46 write!(
47 f,
48 "miniz_oxide inflate internal state. total_in: {}, total_out: {}",
49 self.total_in, self.total_out,
50 )
51 }
52}
53
54impl From<FlushDecompress> for MZFlush {
55 fn from(value: FlushDecompress) -> Self {
56 match value {
57 FlushDecompress::None => Self::None,
58 FlushDecompress::Sync => Self::Sync,
59 FlushDecompress::Finish => Self::Finish,
60 }
61 }
62}
63
64impl InflateBackend for Inflate {
65 fn make(zlib_header: bool, _window_bits: u8) -> Self {
66 let format = format_from_bool(zlib_header);
67
68 Inflate {
69 inner: InflateState::new_boxed(format),
70 total_in: 0,
71 total_out: 0,
72 }
73 }
74
75 fn decompress(
76 &mut self,
77 input: &[u8],
78 output: &mut [u8],
79 flush: FlushDecompress,
80 ) -> Result<Status, DecompressError> {
81 let mz_flush = flush.into();
82 let res = inflate::stream::inflate(&mut self.inner, input, output, mz_flush);
83 self.total_in += res.bytes_consumed as u64;
84 self.total_out += res.bytes_written as u64;
85
86 match res.status {
87 Ok(status) => match status {
88 MZStatus::Ok => Ok(Status::Ok),
89 MZStatus::StreamEnd => Ok(Status::StreamEnd),
90 MZStatus::NeedDict => {
91 mem::decompress_need_dict(self.inner.decompressor().adler32().unwrap_or(0))
92 }
93 },
94 Err(status) => match status {
95 MZError::Buf => Ok(Status::BufError),
96 _ => mem::decompress_failed(ErrorMessage),
97 },
98 }
99 }
100
101 fn reset(&mut self, zlib_header: bool) {
102 self.inner
103 .reset_as(FullReset(format_from_bool(zlib_header)));
104 self.total_in = 0;
105 self.total_out = 0;
106 }
107}
108
109impl Backend for Inflate {
110 #[inline]
111 fn total_in(&self) -> u64 {
112 self.total_in
113 }
114
115 #[inline]
116 fn total_out(&self) -> u64 {
117 self.total_out
118 }
119}
120
121pub struct Deflate {
122 inner: Box<CompressorOxide>,
123 total_in: u64,
124 total_out: u64,
125}
126
127impl fmt::Debug for Deflate {
128 fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
129 write!(
130 f,
131 "miniz_oxide deflate internal state. total_in: {}, total_out: {}",
132 self.total_in, self.total_out,
133 )
134 }
135}
136
137impl From<FlushCompress> for MZFlush {
138 fn from(value: FlushCompress) -> Self {
139 match value {
140 FlushCompress::None => Self::None,
141 FlushCompress::Partial => Self::Partial,
142 FlushCompress::Sync => Self::Sync,
143 FlushCompress::Full => Self::Full,
144 FlushCompress::Finish => Self::Finish,
145 }
146 }
147}
148
149impl DeflateBackend for Deflate {
150 fn make(level: Compression, zlib_header: bool, _window_bits: u8) -> Self {
151 debug_assert!(level.level() <= 10);
154
155 let mut inner: Box<CompressorOxide> = Box::default();
156 let format = format_from_bool(zlib_header);
157 inner.set_format_and_level(format, level.level().try_into().unwrap_or(1));
158
159 Deflate {
160 inner,
161 total_in: 0,
162 total_out: 0,
163 }
164 }
165
166 fn compress(
167 &mut self,
168 input: &[u8],
169 output: &mut [u8],
170 flush: FlushCompress,
171 ) -> Result<Status, CompressError> {
172 let mz_flush = flush.into();
173 let res = deflate::stream::deflate(&mut self.inner, input, output, mz_flush);
174 self.total_in += res.bytes_consumed as u64;
175 self.total_out += res.bytes_written as u64;
176
177 match res.status {
178 Ok(status) => match status {
179 MZStatus::Ok => Ok(Status::Ok),
180 MZStatus::StreamEnd => Ok(Status::StreamEnd),
181 MZStatus::NeedDict => mem::compress_failed(ErrorMessage),
182 },
183 Err(status) => match status {
184 MZError::Buf => Ok(Status::BufError),
185 _ => mem::compress_failed(ErrorMessage),
186 },
187 }
188 }
189
190 fn reset(&mut self) {
191 self.total_in = 0;
192 self.total_out = 0;
193 self.inner.reset();
194 }
195}
196
197impl Backend for Deflate {
198 #[inline]
199 fn total_in(&self) -> u64 {
200 self.total_in
201 }
202
203 #[inline]
204 fn total_out(&self) -> u64 {
205 self.total_out
206 }
207}