1use alloc::{Allocator, SliceWrapperMut};
2#[cfg(feature = "std")]
3use std::io;
4#[cfg(feature = "std")]
5use std::io::{Error, ErrorKind, Write};
6
7#[cfg(feature = "std")]
8pub use alloc_stdlib::StandardAlloc;
9use brotli_decompressor::CustomWrite;
10#[cfg(feature = "std")]
11pub use brotli_decompressor::{IntoIoWriter, IoWriterWrapper};
12
13use super::backward_references::BrotliEncoderParams;
14use super::combined_alloc::BrotliAlloc;
15use super::encode::{
16 BrotliEncoderDestroyInstance, BrotliEncoderOperation, BrotliEncoderParameter,
17 BrotliEncoderStateStruct,
18};
19use super::interface;
20use crate::enc::combined_alloc::allocate;
21
22#[cfg(feature = "std")]
23pub struct CompressorWriterCustomAlloc<
24 W: Write,
25 BufferType: SliceWrapperMut<u8>,
26 Alloc: BrotliAlloc,
27>(CompressorWriterCustomIo<io::Error, IntoIoWriter<W>, BufferType, Alloc>);
28
29#[cfg(feature = "std")]
30impl<W: Write, BufferType: SliceWrapperMut<u8>, Alloc: BrotliAlloc>
31 CompressorWriterCustomAlloc<W, BufferType, Alloc>
32{
33 pub fn new(w: W, buffer: BufferType, alloc: Alloc, q: u32, lgwin: u32) -> Self {
34 CompressorWriterCustomAlloc::<W, BufferType, Alloc>(CompressorWriterCustomIo::<
35 Error,
36 IntoIoWriter<W>,
37 BufferType,
38 Alloc,
39 >::new(
40 IntoIoWriter::<W>(w),
41 buffer,
42 alloc,
43 Error::new(ErrorKind::InvalidData, "Invalid Data"),
44 Error::new(ErrorKind::WriteZero, "No room in output."),
45 q,
46 lgwin,
47 ))
48 }
49
50 pub fn get_ref(&self) -> &W {
51 &self.0.get_ref().0
52 }
53 pub fn get_mut(&mut self) -> &mut W {
54 &mut self.0.get_mut().0
55 }
56 pub fn into_inner(self) -> W {
57 self.0.into_inner().0
58 }
59}
60
61#[cfg(feature = "std")]
62impl<W: Write, BufferType: SliceWrapperMut<u8>, Alloc: BrotliAlloc> Write
63 for CompressorWriterCustomAlloc<W, BufferType, Alloc>
64{
65 fn write(&mut self, buf: &[u8]) -> Result<usize, Error> {
66 self.0.write(buf)
67 }
68 fn flush(&mut self) -> Result<(), Error> {
69 self.0.flush()
70 }
71}
72
73#[cfg(feature = "std")]
74pub struct CompressorWriter<W: Write>(
75 CompressorWriterCustomAlloc<
76 W,
77 <StandardAlloc as Allocator<u8>>::AllocatedMemory,
78 StandardAlloc,
79 >,
80);
81
82#[cfg(feature = "std")]
83impl<W: Write> CompressorWriter<W> {
84 pub fn new(w: W, buffer_size: usize, q: u32, lgwin: u32) -> Self {
85 let mut alloc = StandardAlloc::default();
86 let buffer = allocate::<u8, _>(
87 &mut alloc,
88 if buffer_size == 0 { 4096 } else { buffer_size },
89 );
90 CompressorWriter::<W>(CompressorWriterCustomAlloc::new(w, buffer, alloc, q, lgwin))
91 }
92
93 pub fn with_params(w: W, buffer_size: usize, params: &BrotliEncoderParams) -> Self {
94 let mut writer = Self::new(w, buffer_size, params.quality as u32, params.lgwin as u32);
95 (writer.0).0.state.params = params.clone();
96 writer
97 }
98
99 pub fn get_ref(&self) -> &W {
100 self.0.get_ref()
101 }
102 pub fn get_mut(&mut self) -> &mut W {
103 self.0.get_mut()
104 }
105 pub fn into_inner(self) -> W {
106 self.0.into_inner()
107 }
108}
109
110#[cfg(feature = "std")]
111impl<W: Write> Write for CompressorWriter<W> {
112 fn write(&mut self, buf: &[u8]) -> Result<usize, Error> {
113 self.0.write(buf)
114 }
115 fn flush(&mut self) -> Result<(), Error> {
116 self.0.flush()
117 }
118}
119
120pub struct CompressorWriterCustomIo<
121 ErrType,
122 W: CustomWrite<ErrType>,
123 BufferType: SliceWrapperMut<u8>,
124 Alloc: BrotliAlloc,
125> {
126 output_buffer: BufferType,
127 total_out: Option<usize>,
128 output: Option<W>,
129 error_if_invalid_data: Option<ErrType>,
130 state: BrotliEncoderStateStruct<Alloc>,
131 error_if_zero_bytes_written: Option<ErrType>,
132}
133pub fn write_all<ErrType, W: CustomWrite<ErrType>, ErrMaker: FnMut() -> Option<ErrType>>(
134 writer: &mut W,
135 mut buf: &[u8],
136 mut error_to_return_if_zero_bytes_written: ErrMaker,
137) -> Result<(), ErrType> {
138 while !buf.is_empty() {
139 match writer.write(buf) {
140 Ok(bytes_written) => if bytes_written != 0 {
141 buf = &buf[bytes_written..]
142 } else {
143 if let Some(err) = error_to_return_if_zero_bytes_written() {
144 return Err(err);
145 } else {
146 return Ok(());
147 }
148 },
149 Err(e) => return Err(e),
150 }
151 }
152 Ok(())
153}
154impl<ErrType, W: CustomWrite<ErrType>, BufferType: SliceWrapperMut<u8>, Alloc: BrotliAlloc>
155 CompressorWriterCustomIo<ErrType, W, BufferType, Alloc>
156{
157 pub fn new(
158 w: W,
159 buffer: BufferType,
160 alloc: Alloc,
161 invalid_data_error_type: ErrType,
162 error_if_zero_bytes_written: ErrType,
163 q: u32,
164 lgwin: u32,
165 ) -> Self {
166 let mut ret = CompressorWriterCustomIo {
167 output_buffer: buffer,
168 total_out: Some(0),
169 output: Some(w),
170 state: BrotliEncoderStateStruct::new(alloc),
171 error_if_invalid_data: Some(invalid_data_error_type),
172 error_if_zero_bytes_written: Some(error_if_zero_bytes_written),
173 };
174 ret.state
175 .set_parameter(BrotliEncoderParameter::BROTLI_PARAM_QUALITY, q);
176 ret.state
177 .set_parameter(BrotliEncoderParameter::BROTLI_PARAM_LGWIN, lgwin);
178
179 ret
180 }
181 fn flush_or_close(&mut self, op: BrotliEncoderOperation) -> Result<(), ErrType> {
182 let mut nop_callback =
183 |_data: &mut interface::PredictionModeContextMap<interface::InputReferenceMut>,
184 _cmds: &mut [interface::StaticCommand],
185 _mb: interface::InputPair,
186 _mfv: &mut Alloc| ();
187
188 loop {
189 let mut avail_in: usize = 0;
190 let mut input_offset: usize = 0;
191 let mut avail_out: usize = self.output_buffer.slice_mut().len();
192 let mut output_offset: usize = 0;
193 let ret = self.state.compress_stream(
194 op,
195 &mut avail_in,
196 &[],
197 &mut input_offset,
198 &mut avail_out,
199 self.output_buffer.slice_mut(),
200 &mut output_offset,
201 &mut self.total_out,
202 &mut nop_callback,
203 );
204 if output_offset > 0 {
205 let zero_err = &mut self.error_if_zero_bytes_written;
206 let fallback = &mut self.error_if_invalid_data;
207 match write_all(
208 self.output.as_mut().unwrap(),
209 &self.output_buffer.slice_mut()[..output_offset],
210 || {
211 if let Some(err) = zero_err.take() {
212 return Some(err);
213 }
214 fallback.take()
215 },
216 ) {
217 Ok(_) => {}
218 Err(e) => return Err(e),
219 }
220 }
221 if !ret {
222 return Err(self.error_if_invalid_data.take().unwrap());
223 }
224 if let BrotliEncoderOperation::BROTLI_OPERATION_FLUSH = op {
225 if self.state.has_more_output() {
226 continue;
227 }
228 return Ok(());
229 }
230 if self.state.is_finished() {
231 return Ok(());
232 }
233 }
234 }
235
236 pub fn get_ref(&self) -> &W {
237 self.output.as_ref().unwrap()
238 }
239 pub fn get_mut(&mut self) -> &mut W {
240 self.output.as_mut().unwrap()
241 }
242 pub fn into_inner(mut self) -> W {
243 match self.flush_or_close(BrotliEncoderOperation::BROTLI_OPERATION_FINISH) {
244 Ok(_) => {}
245 Err(_) => {}
246 }
247 self.output.take().unwrap()
248 }
249}
250
251impl<ErrType, W: CustomWrite<ErrType>, BufferType: SliceWrapperMut<u8>, Alloc: BrotliAlloc> Drop
252 for CompressorWriterCustomIo<ErrType, W, BufferType, Alloc>
253{
254 fn drop(&mut self) {
255 if self.output.is_some() {
256 match self.flush_or_close(BrotliEncoderOperation::BROTLI_OPERATION_FINISH) {
257 Ok(_) => {}
258 Err(_) => {}
259 }
260 }
261 BrotliEncoderDestroyInstance(&mut self.state);
262 }
263}
264impl<ErrType, W: CustomWrite<ErrType>, BufferType: SliceWrapperMut<u8>, Alloc: BrotliAlloc>
265 CustomWrite<ErrType> for CompressorWriterCustomIo<ErrType, W, BufferType, Alloc>
266{
267 fn write(&mut self, buf: &[u8]) -> Result<usize, ErrType> {
268 let mut nop_callback =
269 |_data: &mut interface::PredictionModeContextMap<interface::InputReferenceMut>,
270 _cmds: &mut [interface::StaticCommand],
271 _mb: interface::InputPair,
272 _mfv: &mut Alloc| ();
273 let mut avail_in = buf.len();
274 let mut input_offset: usize = 0;
275 while avail_in != 0 {
276 let mut output_offset = 0;
277 let mut avail_out = self.output_buffer.slice_mut().len();
278 let ret = self.state.compress_stream(
279 BrotliEncoderOperation::BROTLI_OPERATION_PROCESS,
280 &mut avail_in,
281 buf,
282 &mut input_offset,
283 &mut avail_out,
284 self.output_buffer.slice_mut(),
285 &mut output_offset,
286 &mut self.total_out,
287 &mut nop_callback,
288 );
289 if output_offset > 0 {
290 let zero_err = &mut self.error_if_zero_bytes_written;
291 let fallback = &mut self.error_if_invalid_data;
292 match write_all(
293 self.output.as_mut().unwrap(),
294 &self.output_buffer.slice_mut()[..output_offset],
295 || {
296 if let Some(err) = zero_err.take() {
297 return Some(err);
298 }
299 fallback.take()
300 },
301
302 ) {
303 Ok(_) => {}
304 Err(e) => {
305 return Err(e)
306 },
307 }
308 }
309 if !ret {
310 return Err(self.error_if_invalid_data.take().unwrap());
311 }
312 }
313 Ok(buf.len())
314 }
315 fn flush(&mut self) -> Result<(), ErrType> {
316 match self.flush_or_close(BrotliEncoderOperation::BROTLI_OPERATION_FLUSH) {
317 Ok(_) => {}
318 Err(e) => return Err(e),
319 }
320 self.output.as_mut().unwrap().flush()
321 }
322}