Coverage Report

Created: 2025-08-29 07:17

/rust/registry/src/index.crates.io-6f17d22bba15001f/brotli-decompressor-5.0.0/src/writer.rs
Line
Count
Source (jump to first uncovered line)
1
use core;
2
#[cfg(feature="std")]
3
use std::io::{self, Error, ErrorKind, Write};
4
#[cfg(feature="std")]
5
pub use alloc_stdlib::StandardAlloc;
6
#[cfg(all(feature="unsafe",feature="std"))]
7
pub use alloc_stdlib::HeapAlloc;
8
pub use huffman::{HuffmanCode, HuffmanTreeGroup};
9
pub use state::BrotliState;
10
// use io_wrappers::write_all;
11
pub use io_wrappers::{CustomWrite};
12
#[cfg(feature="std")]
13
pub use io_wrappers::{IntoIoWriter, IoWriterWrapper};
14
pub use super::decode::{BrotliDecompressStream, BrotliResult};
15
pub use alloc::{AllocatedStackMemory, Allocator, SliceWrapper, SliceWrapperMut, StackAllocator};
16
17
#[cfg(feature="std")]
18
pub struct DecompressorWriterCustomAlloc<W: Write,
19
     BufferType : SliceWrapperMut<u8>,
20
     AllocU8 : Allocator<u8>,
21
     AllocU32 : Allocator<u32>,
22
     AllocHC : Allocator<HuffmanCode> >(DecompressorWriterCustomIo<io::Error,
23
                                                             IntoIoWriter<W>,
24
                                                             BufferType,
25
                                                             AllocU8, AllocU32, AllocHC>);
26
27
28
#[cfg(feature="std")]
29
impl<W: Write,
30
     BufferType : SliceWrapperMut<u8>,
31
     AllocU8,
32
     AllocU32,
33
     AllocHC> DecompressorWriterCustomAlloc<W, BufferType, AllocU8, AllocU32, AllocHC>
34
 where AllocU8 : Allocator<u8>, AllocU32 : Allocator<u32>, AllocHC : Allocator<HuffmanCode>
35
    {
36
0
    pub fn new(w: W, buffer : BufferType,
37
0
               alloc_u8 : AllocU8, alloc_u32 : AllocU32, alloc_hc : AllocHC) -> Self {
38
0
     let dict = AllocU8::AllocatedMemory::default();
39
0
     Self::new_with_custom_dictionary(w, buffer, alloc_u8, alloc_u32, alloc_hc, dict)
40
0
41
0
    }
42
4.76k
    pub fn new_with_custom_dictionary(w: W, buffer : BufferType,
43
4.76k
               alloc_u8 : AllocU8, alloc_u32 : AllocU32, alloc_hc : AllocHC, dict: AllocU8::AllocatedMemory) -> Self {
44
4.76k
        DecompressorWriterCustomAlloc::<W, BufferType, AllocU8, AllocU32, AllocHC>(
45
4.76k
          DecompressorWriterCustomIo::<Error,
46
4.76k
                                 IntoIoWriter<W>,
47
4.76k
                                 BufferType,
48
4.76k
                                 AllocU8, AllocU32, AllocHC>::new_with_custom_dictionary(IntoIoWriter::<W>(w),
49
4.76k
                                                                  buffer,
50
4.76k
                                                                  alloc_u8, alloc_u32, alloc_hc,
51
4.76k
                                                                  dict,
52
4.76k
                                                                  Error::new(ErrorKind::InvalidData,
53
4.76k
                                                                             "Invalid Data")))
54
4.76k
    }
Unexecuted instantiation: <brotli_decompressor::writer::DecompressorWriterCustomAlloc<_, _, _, _, _>>::new_with_custom_dictionary
<brotli_decompressor::writer::DecompressorWriterCustomAlloc<std::io::util::Sink, alloc_stdlib::heap_alloc::WrapBox<u8>, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc>>::new_with_custom_dictionary
Line
Count
Source
42
4.76k
    pub fn new_with_custom_dictionary(w: W, buffer : BufferType,
43
4.76k
               alloc_u8 : AllocU8, alloc_u32 : AllocU32, alloc_hc : AllocHC, dict: AllocU8::AllocatedMemory) -> Self {
44
4.76k
        DecompressorWriterCustomAlloc::<W, BufferType, AllocU8, AllocU32, AllocHC>(
45
4.76k
          DecompressorWriterCustomIo::<Error,
46
4.76k
                                 IntoIoWriter<W>,
47
4.76k
                                 BufferType,
48
4.76k
                                 AllocU8, AllocU32, AllocHC>::new_with_custom_dictionary(IntoIoWriter::<W>(w),
49
4.76k
                                                                  buffer,
50
4.76k
                                                                  alloc_u8, alloc_u32, alloc_hc,
51
4.76k
                                                                  dict,
52
4.76k
                                                                  Error::new(ErrorKind::InvalidData,
53
4.76k
                                                                             "Invalid Data")))
54
4.76k
    }
55
56
0
    pub fn get_ref(&self) -> &W {
57
0
        &self.0.get_ref().0
58
0
    }
59
0
    pub fn get_mut(&mut self) -> &mut W {
60
0
        &mut self.0.get_mut().0
61
0
    }
62
0
    pub fn into_inner(self) -> Result<W, W> {
63
0
        match self.0.into_inner() {
64
0
            Ok(w) => Ok(w.0),
65
0
            Err(w) => Err(w.0),
66
        }
67
0
    }
68
}
69
#[cfg(feature="std")]
70
impl<W: Write,
71
     BufferType : SliceWrapperMut<u8>,
72
     AllocU8 : Allocator<u8>,
73
     AllocU32 : Allocator<u32>,
74
     AllocHC : Allocator<HuffmanCode> > Write for DecompressorWriterCustomAlloc<W,
75
                                                                         BufferType,
76
                                                                         AllocU8,
77
                                                                         AllocU32,
78
                                                                         AllocHC> {
79
4.82k
    fn write(&mut self, buf: &[u8]) -> Result<usize, Error> {
80
4.82k
       self.0.write(buf)
81
4.82k
    }
Unexecuted instantiation: <brotli_decompressor::writer::DecompressorWriterCustomAlloc<_, _, _, _, _> as std::io::Write>::write
<brotli_decompressor::writer::DecompressorWriterCustomAlloc<std::io::util::Sink, alloc_stdlib::heap_alloc::WrapBox<u8>, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc> as std::io::Write>::write
Line
Count
Source
79
4.82k
    fn write(&mut self, buf: &[u8]) -> Result<usize, Error> {
80
4.82k
       self.0.write(buf)
81
4.82k
    }
82
0
    fn flush(&mut self) -> Result<(), Error> {
83
0
       self.0.flush()
84
0
    }
85
}
86
87
#[cfg(feature="std")]
88
impl<W: Write,
89
     BufferType : SliceWrapperMut<u8>,
90
     AllocU8 : Allocator<u8>,
91
     AllocU32 : Allocator<u32>,
92
     AllocHC : Allocator<HuffmanCode> > DecompressorWriterCustomAlloc<W,
93
                                                                         BufferType,
94
                                                                         AllocU8,
95
                                                                         AllocU32,
96
                                                                         AllocHC> {
97
0
    pub fn close(&mut self) -> Result<(), Error>{
98
0
        self.0.close()
99
0
    }
100
}
101
102
103
#[cfg(not(any(feature="unsafe", not(feature="std"))))]
104
pub struct DecompressorWriter<W: Write>(DecompressorWriterCustomAlloc<W,
105
                                                         <StandardAlloc
106
                                                          as Allocator<u8>>::AllocatedMemory,
107
                                                         StandardAlloc,
108
                                                         StandardAlloc,
109
                                                         StandardAlloc>);
110
111
112
#[cfg(not(any(feature="unsafe", not(feature="std"))))]
113
impl<W: Write> DecompressorWriter<W> {
114
4.76k
  pub fn new(w: W, buffer_size: usize) -> Self {
115
4.76k
      Self::new_with_custom_dictionary(w, buffer_size, <StandardAlloc as Allocator<u8>>::AllocatedMemory::default())
116
4.76k
  }
Unexecuted instantiation: <brotli_decompressor::writer::DecompressorWriter<_>>::new
<brotli_decompressor::writer::DecompressorWriter<std::io::util::Sink>>::new
Line
Count
Source
114
4.76k
  pub fn new(w: W, buffer_size: usize) -> Self {
115
4.76k
      Self::new_with_custom_dictionary(w, buffer_size, <StandardAlloc as Allocator<u8>>::AllocatedMemory::default())
116
4.76k
  }
117
4.76k
  pub fn new_with_custom_dictionary(w: W, buffer_size: usize, dict: <StandardAlloc as Allocator<u8>>::AllocatedMemory) -> Self {
118
4.76k
    let mut alloc = StandardAlloc::default();
119
4.76k
    let buffer = <StandardAlloc as Allocator<u8>>::alloc_cell(&mut alloc, if buffer_size == 0 {4096} else {buffer_size});
120
4.76k
    DecompressorWriter::<W>(DecompressorWriterCustomAlloc::<W,
121
4.76k
                                                <StandardAlloc
122
4.76k
                                                 as Allocator<u8>>::AllocatedMemory,
123
4.76k
                                                StandardAlloc,
124
4.76k
                                                StandardAlloc,
125
4.76k
                                                StandardAlloc>::new_with_custom_dictionary(w,
126
4.76k
                                                                              buffer,
127
4.76k
                                                                              alloc,
128
4.76k
                                                                              StandardAlloc::default(),
129
4.76k
                                                                              StandardAlloc::default(),
130
4.76k
                                                                              dict))
131
4.76k
  }
Unexecuted instantiation: <brotli_decompressor::writer::DecompressorWriter<_>>::new_with_custom_dictionary
<brotli_decompressor::writer::DecompressorWriter<std::io::util::Sink>>::new_with_custom_dictionary
Line
Count
Source
117
4.76k
  pub fn new_with_custom_dictionary(w: W, buffer_size: usize, dict: <StandardAlloc as Allocator<u8>>::AllocatedMemory) -> Self {
118
4.76k
    let mut alloc = StandardAlloc::default();
119
4.76k
    let buffer = <StandardAlloc as Allocator<u8>>::alloc_cell(&mut alloc, if buffer_size == 0 {4096} else {buffer_size});
120
4.76k
    DecompressorWriter::<W>(DecompressorWriterCustomAlloc::<W,
121
4.76k
                                                <StandardAlloc
122
4.76k
                                                 as Allocator<u8>>::AllocatedMemory,
123
4.76k
                                                StandardAlloc,
124
4.76k
                                                StandardAlloc,
125
4.76k
                                                StandardAlloc>::new_with_custom_dictionary(w,
126
4.76k
                                                                              buffer,
127
4.76k
                                                                              alloc,
128
4.76k
                                                                              StandardAlloc::default(),
129
4.76k
                                                                              StandardAlloc::default(),
130
4.76k
                                                                              dict))
131
4.76k
  }
132
133
0
  pub fn get_ref(&self) -> &W {
134
0
      self.0.get_ref()
135
0
  }
136
0
  pub fn get_mut(&mut self) -> &mut W {
137
0
      self.0.get_mut()
138
0
  }
139
0
  pub fn into_inner(self) -> Result<W, W> {
140
0
    self.0.into_inner()
141
0
  }
142
}
143
144
145
#[cfg(all(feature="unsafe", feature="std"))]
146
pub struct DecompressorWriter<W: Write>(DecompressorWriterCustomAlloc<W,
147
                                                         <HeapAlloc<u8>
148
                                                          as Allocator<u8>>::AllocatedMemory,
149
                                                         HeapAlloc<u8>,
150
                                                         HeapAlloc<u32>,
151
                                                         HeapAlloc<HuffmanCode> >);
152
153
154
#[cfg(all(feature="unsafe", feature="std"))]
155
impl<W: Write> DecompressorWriter<W> {
156
  pub fn new(w: W, buffer_size: usize) -> Self {
157
    let dict = <HeapAlloc<u8> as Allocator<u8>>::AllocatedMemory::default();
158
    Self::new_with_custom_dictionary(w, buffer_size, dict)
159
  }
160
  pub fn new_with_custom_dictionary(w: W, buffer_size: usize, dict: <HeapAlloc<u8> as Allocator<u8>>::AllocatedMemory) -> Self {
161
    let mut alloc_u8 = HeapAlloc::<u8>::new(0);
162
    let buffer = alloc_u8.alloc_cell(buffer_size);
163
    let alloc_u32 = HeapAlloc::<u32>::new(0);
164
    let alloc_hc = HeapAlloc::<HuffmanCode>::new(HuffmanCode{bits:2, value: 1});
165
    DecompressorWriter::<W>(DecompressorWriterCustomAlloc::<W,
166
                                                <HeapAlloc<u8>
167
                                                 as Allocator<u8>>::AllocatedMemory,
168
                                                HeapAlloc<u8>,
169
                                                HeapAlloc<u32>,
170
                                                HeapAlloc<HuffmanCode> >
171
      ::new_with_custom_dictionary(w, buffer, alloc_u8, alloc_u32, alloc_hc, dict))
172
  }
173
174
  pub fn get_ref(&self) -> &W {
175
      self.0.get_ref()
176
  }
177
  pub fn get_mut(&mut self) -> &mut W {
178
    &mut (self.0).0.get_mut().0
179
  }
180
  pub fn into_inner(self) -> Result<W, W> {
181
    self.0.into_inner()
182
  }
183
}
184
185
#[cfg(feature="std")]
186
impl<W: Write> DecompressorWriter<W> {
187
0
    pub fn close(&mut self) -> Result<(), Error>{
188
0
        self.0.close()
189
0
    }
190
}
191
#[cfg(feature="std")]
192
impl<W: Write> Write for DecompressorWriter<W> {
193
4.82k
    fn write(&mut self, buf: &[u8]) -> Result<usize, Error> {
194
4.82k
       self.0.write(buf)
195
4.82k
    }
Unexecuted instantiation: <brotli_decompressor::writer::DecompressorWriter<_> as std::io::Write>::write
<brotli_decompressor::writer::DecompressorWriter<std::io::util::Sink> as std::io::Write>::write
Line
Count
Source
193
4.82k
    fn write(&mut self, buf: &[u8]) -> Result<usize, Error> {
194
4.82k
       self.0.write(buf)
195
4.82k
    }
196
0
    fn flush(&mut self) -> Result<(), Error> {
197
0
       self.0.flush()
198
0
    }
199
}
200
201
pub struct DecompressorWriterCustomIo<ErrType,
202
                                W: CustomWrite<ErrType>,
203
                                BufferType: SliceWrapperMut<u8>,
204
                                AllocU8: Allocator<u8>,
205
                                AllocU32: Allocator<u32>,
206
                                AllocHC: Allocator<HuffmanCode>>
207
{
208
  output_buffer: BufferType,
209
  total_out: usize,
210
  output: Option<W>,
211
  error_if_invalid_data: Option<ErrType>,
212
  state: BrotliState<AllocU8, AllocU32, AllocHC>,
213
}
214
215
216
303M
pub fn write_all<ErrType, W: CustomWrite<ErrType>>(writer: &mut W, mut buf : &[u8]) -> Result<(), ErrType> {
217
607M
    while buf.len() != 0 {
218
303M
          match writer.write(buf) {
219
303M
                Ok(bytes_written) => buf = &buf[bytes_written..],
220
0
                Err(e) => return Err(e),
221
          }
222
    }
223
303M
    Ok(())
224
303M
}
Unexecuted instantiation: brotli_decompressor::writer::write_all::<_, _>
brotli_decompressor::writer::write_all::<std::io::error::Error, brotli_decompressor::io_wrappers::IntoIoWriter<std::io::util::Sink>>
Line
Count
Source
216
303M
pub fn write_all<ErrType, W: CustomWrite<ErrType>>(writer: &mut W, mut buf : &[u8]) -> Result<(), ErrType> {
217
607M
    while buf.len() != 0 {
218
303M
          match writer.write(buf) {
219
303M
                Ok(bytes_written) => buf = &buf[bytes_written..],
220
0
                Err(e) => return Err(e),
221
          }
222
    }
223
303M
    Ok(())
224
303M
}
225
226
227
impl<ErrType,
228
     W: CustomWrite<ErrType>,
229
     BufferType : SliceWrapperMut<u8>,
230
     AllocU8,
231
     AllocU32,
232
     AllocHC> DecompressorWriterCustomIo<ErrType, W, BufferType, AllocU8, AllocU32, AllocHC>
233
 where AllocU8 : Allocator<u8>, AllocU32 : Allocator<u32>, AllocHC : Allocator<HuffmanCode>
234
{
235
236
0
    pub fn new(w: W, buffer : BufferType,
237
0
               alloc_u8 : AllocU8, alloc_u32 : AllocU32, alloc_hc : AllocHC,
238
0
               invalid_data_error_type : ErrType) -> Self {
239
0
           let dict = AllocU8::AllocatedMemory::default();
240
0
           Self::new_with_custom_dictionary(w, buffer, alloc_u8, alloc_u32, alloc_hc, dict, invalid_data_error_type)
241
0
    }
242
4.76k
    pub fn new_with_custom_dictionary(w: W, buffer : BufferType,
243
4.76k
               alloc_u8 : AllocU8, alloc_u32 : AllocU32, alloc_hc : AllocHC,
244
4.76k
               dict: AllocU8::AllocatedMemory,
245
4.76k
               invalid_data_error_type : ErrType) -> Self {
246
4.76k
        DecompressorWriterCustomIo::<ErrType, W, BufferType, AllocU8, AllocU32, AllocHC>{
247
4.76k
            output_buffer : buffer,
248
4.76k
            total_out : 0,
249
4.76k
            output: Some(w),
250
4.76k
            state : BrotliState::new_with_custom_dictionary(alloc_u8,
251
4.76k
                                                                 alloc_u32,
252
4.76k
                                                                 alloc_hc,
253
4.76k
                                                                 dict),
254
4.76k
            error_if_invalid_data : Some(invalid_data_error_type),
255
4.76k
        }
256
4.76k
    }
Unexecuted instantiation: <brotli_decompressor::writer::DecompressorWriterCustomIo<_, _, _, _, _, _>>::new_with_custom_dictionary
<brotli_decompressor::writer::DecompressorWriterCustomIo<std::io::error::Error, brotli_decompressor::io_wrappers::IntoIoWriter<std::io::util::Sink>, alloc_stdlib::heap_alloc::WrapBox<u8>, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc>>::new_with_custom_dictionary
Line
Count
Source
242
4.76k
    pub fn new_with_custom_dictionary(w: W, buffer : BufferType,
243
4.76k
               alloc_u8 : AllocU8, alloc_u32 : AllocU32, alloc_hc : AllocHC,
244
4.76k
               dict: AllocU8::AllocatedMemory,
245
4.76k
               invalid_data_error_type : ErrType) -> Self {
246
4.76k
        DecompressorWriterCustomIo::<ErrType, W, BufferType, AllocU8, AllocU32, AllocHC>{
247
4.76k
            output_buffer : buffer,
248
4.76k
            total_out : 0,
249
4.76k
            output: Some(w),
250
4.76k
            state : BrotliState::new_with_custom_dictionary(alloc_u8,
251
4.76k
                                                                 alloc_u32,
252
4.76k
                                                                 alloc_hc,
253
4.76k
                                                                 dict),
254
4.76k
            error_if_invalid_data : Some(invalid_data_error_type),
255
4.76k
        }
256
4.76k
    }
257
4.76k
    pub fn close(&mut self) -> Result<(), ErrType>{
258
        loop {
259
4.76k
            let mut avail_in : usize = 0;
260
4.76k
            let mut input_offset : usize = 0;
261
4.76k
            let mut avail_out : usize = self.output_buffer.slice_mut().len();
262
4.76k
            let mut output_offset : usize = 0;
263
4.76k
            let ret = BrotliDecompressStream(
264
4.76k
                &mut avail_in,
265
4.76k
                &mut input_offset,
266
4.76k
                &[],
267
4.76k
                &mut avail_out,
268
4.76k
                &mut output_offset,
269
4.76k
                self.output_buffer.slice_mut(),                
270
4.76k
                &mut self.total_out,
271
4.76k
                &mut self.state);
272
4.76k
          // already closed.
273
4.76k
          if self.error_if_invalid_data.is_none() {
274
1.07k
              return Ok(());
275
3.69k
          }
276
3.69k
          match write_all(self.output.as_mut().unwrap(), &self.output_buffer.slice_mut()[..output_offset]) {
277
3.69k
            Ok(_) => {},
278
0
            Err(e) => return Err(e),
279
           }
280
3.69k
           match ret {
281
3.51k
           BrotliResult::NeedsMoreInput => return self.error_if_invalid_data.take().map(|e|Err(e)).unwrap_or(Ok(())),
Unexecuted instantiation: <brotli_decompressor::writer::DecompressorWriterCustomIo<_, _, _, _, _, _>>::close::{closure#0}
<brotli_decompressor::writer::DecompressorWriterCustomIo<std::io::error::Error, brotli_decompressor::io_wrappers::IntoIoWriter<std::io::util::Sink>, alloc_stdlib::heap_alloc::WrapBox<u8>, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc>>::close::{closure#0}
Line
Count
Source
281
3.51k
           BrotliResult::NeedsMoreInput => return self.error_if_invalid_data.take().map(|e|Err(e)).unwrap_or(Ok(())),
282
0
           BrotliResult::NeedsMoreOutput => {},
283
           BrotliResult::ResultSuccess => {
284
178
               return Ok(());
285
           },
286
0
           BrotliResult::ResultFailure => return self.error_if_invalid_data.take().map(|e|Err(e)).unwrap_or(Ok(()))
Unexecuted instantiation: <brotli_decompressor::writer::DecompressorWriterCustomIo<_, _, _, _, _, _>>::close::{closure#1}
Unexecuted instantiation: <brotli_decompressor::writer::DecompressorWriterCustomIo<std::io::error::Error, brotli_decompressor::io_wrappers::IntoIoWriter<std::io::util::Sink>, alloc_stdlib::heap_alloc::WrapBox<u8>, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc>>::close::{closure#1}
287
           }
288
        }
289
4.76k
    }
Unexecuted instantiation: <brotli_decompressor::writer::DecompressorWriterCustomIo<_, _, _, _, _, _>>::close
<brotli_decompressor::writer::DecompressorWriterCustomIo<std::io::error::Error, brotli_decompressor::io_wrappers::IntoIoWriter<std::io::util::Sink>, alloc_stdlib::heap_alloc::WrapBox<u8>, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc>>::close
Line
Count
Source
257
4.76k
    pub fn close(&mut self) -> Result<(), ErrType>{
258
        loop {
259
4.76k
            let mut avail_in : usize = 0;
260
4.76k
            let mut input_offset : usize = 0;
261
4.76k
            let mut avail_out : usize = self.output_buffer.slice_mut().len();
262
4.76k
            let mut output_offset : usize = 0;
263
4.76k
            let ret = BrotliDecompressStream(
264
4.76k
                &mut avail_in,
265
4.76k
                &mut input_offset,
266
4.76k
                &[],
267
4.76k
                &mut avail_out,
268
4.76k
                &mut output_offset,
269
4.76k
                self.output_buffer.slice_mut(),                
270
4.76k
                &mut self.total_out,
271
4.76k
                &mut self.state);
272
4.76k
          // already closed.
273
4.76k
          if self.error_if_invalid_data.is_none() {
274
1.07k
              return Ok(());
275
3.69k
          }
276
3.69k
          match write_all(self.output.as_mut().unwrap(), &self.output_buffer.slice_mut()[..output_offset]) {
277
3.69k
            Ok(_) => {},
278
0
            Err(e) => return Err(e),
279
           }
280
3.69k
           match ret {
281
3.51k
           BrotliResult::NeedsMoreInput => return self.error_if_invalid_data.take().map(|e|Err(e)).unwrap_or(Ok(())),
282
0
           BrotliResult::NeedsMoreOutput => {},
283
           BrotliResult::ResultSuccess => {
284
178
               return Ok(());
285
           },
286
0
           BrotliResult::ResultFailure => return self.error_if_invalid_data.take().map(|e|Err(e)).unwrap_or(Ok(()))
287
           }
288
        }
289
4.76k
    }
290
291
0
    pub fn get_ref(&self) -> &W {
292
0
        self.output.as_ref().unwrap()
293
0
    }
294
0
    pub fn get_mut(&mut self) -> &mut W {
295
0
        self.output.as_mut().unwrap()
296
0
    }
297
0
    pub fn into_inner(mut self) -> Result<W, W> {
298
0
        match self.close() {
299
0
            Ok(_) => Ok((core::mem::replace(&mut self.output, None).unwrap())),
300
0
            Err(_) => Err((core::mem::replace(&mut self.output, None).unwrap())),
301
        }
302
0
    }
303
}
304
305
impl<ErrType,
306
     W: CustomWrite<ErrType>,
307
     BufferType : SliceWrapperMut<u8>,
308
     AllocU8 : Allocator<u8>,
309
     AllocU32 : Allocator<u32>,
310
     AllocHC : Allocator<HuffmanCode> > Drop for DecompressorWriterCustomIo<ErrType,
311
                                                                                     W,
312
                                                                                     BufferType,
313
                                                                                     AllocU8,
314
                                                                                     AllocU32,
315
                                                                                     AllocHC> {
316
4.76k
    fn drop(&mut self) {
317
4.76k
        if self.output.is_some() {
318
4.76k
            match self.close() {
319
1.25k
                Ok(_) => {},
320
3.51k
                Err(_) => {},
321
            }
322
0
        }
323
4.76k
    }
Unexecuted instantiation: <brotli_decompressor::writer::DecompressorWriterCustomIo<_, _, _, _, _, _> as core::ops::drop::Drop>::drop
<brotli_decompressor::writer::DecompressorWriterCustomIo<std::io::error::Error, brotli_decompressor::io_wrappers::IntoIoWriter<std::io::util::Sink>, alloc_stdlib::heap_alloc::WrapBox<u8>, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc> as core::ops::drop::Drop>::drop
Line
Count
Source
316
4.76k
    fn drop(&mut self) {
317
4.76k
        if self.output.is_some() {
318
4.76k
            match self.close() {
319
1.25k
                Ok(_) => {},
320
3.51k
                Err(_) => {},
321
            }
322
0
        }
323
4.76k
    }
324
}
325
326
impl<ErrType,
327
     W: CustomWrite<ErrType>,
328
     BufferType : SliceWrapperMut<u8>,
329
     AllocU8 : Allocator<u8>,
330
     AllocU32 : Allocator<u32>,
331
     AllocHC : Allocator<HuffmanCode> > CustomWrite<ErrType> for DecompressorWriterCustomIo<ErrType,
332
                                                                                     W,
333
                                                                                     BufferType,
334
                                                                                     AllocU8,
335
                                                                                     AllocU32,
336
                                                                                     AllocHC> {
337
4.82k
  fn write(&mut self, buf: &[u8]) -> Result<usize, ErrType > {
338
4.82k
        let mut avail_in = buf.len();
339
4.82k
        let mut input_offset : usize = 0;
340
        loop {
341
303M
            let mut output_offset = 0;
342
303M
            let mut avail_out = self.output_buffer.slice_mut().len();
343
303M
            let op_result = BrotliDecompressStream(&mut avail_in,
344
303M
                                     &mut input_offset,
345
303M
                                     &buf[..],
346
303M
                                     &mut avail_out,
347
303M
                                     &mut output_offset,
348
303M
                                     self.output_buffer.slice_mut(),
349
303M
                                     &mut self.total_out,
350
303M
                                     &mut self.state);
351
303M
         match write_all(self.output.as_mut().unwrap(), &self.output_buffer.slice_mut()[..output_offset]) {
352
303M
          Ok(_) => {},
353
0
          Err(e) => return Err(e),
354
         }
355
303M
         match op_result {
356
3.49k
          BrotliResult::NeedsMoreInput => assert_eq!(avail_in, 0),
357
303M
          BrotliResult::NeedsMoreOutput => continue,
358
          BrotliResult::ResultSuccess => {
359
259
              return Ok(input_offset);
360
          }
361
1.07k
          BrotliResult::ResultFailure => return self.error_if_invalid_data.take().map(|e|Err(e)).unwrap_or(Ok(0)),
Unexecuted instantiation: <brotli_decompressor::writer::DecompressorWriterCustomIo<_, _, _, _, _, _> as brotli_decompressor::io_wrappers::CustomWrite<_>>::write::{closure#0}
<brotli_decompressor::writer::DecompressorWriterCustomIo<std::io::error::Error, brotli_decompressor::io_wrappers::IntoIoWriter<std::io::util::Sink>, alloc_stdlib::heap_alloc::WrapBox<u8>, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc> as brotli_decompressor::io_wrappers::CustomWrite<std::io::error::Error>>::write::{closure#0}
Line
Count
Source
361
1.07k
          BrotliResult::ResultFailure => return self.error_if_invalid_data.take().map(|e|Err(e)).unwrap_or(Ok(0)),
362
       }
363
3.49k
        if avail_in == 0 {
364
3.49k
           break
365
0
        }
366
      }
367
3.49k
      Ok(buf.len())
368
4.82k
    }
Unexecuted instantiation: <brotli_decompressor::writer::DecompressorWriterCustomIo<_, _, _, _, _, _> as brotli_decompressor::io_wrappers::CustomWrite<_>>::write
<brotli_decompressor::writer::DecompressorWriterCustomIo<std::io::error::Error, brotli_decompressor::io_wrappers::IntoIoWriter<std::io::util::Sink>, alloc_stdlib::heap_alloc::WrapBox<u8>, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc> as brotli_decompressor::io_wrappers::CustomWrite<std::io::error::Error>>::write
Line
Count
Source
337
4.82k
  fn write(&mut self, buf: &[u8]) -> Result<usize, ErrType > {
338
4.82k
        let mut avail_in = buf.len();
339
4.82k
        let mut input_offset : usize = 0;
340
        loop {
341
303M
            let mut output_offset = 0;
342
303M
            let mut avail_out = self.output_buffer.slice_mut().len();
343
303M
            let op_result = BrotliDecompressStream(&mut avail_in,
344
303M
                                     &mut input_offset,
345
303M
                                     &buf[..],
346
303M
                                     &mut avail_out,
347
303M
                                     &mut output_offset,
348
303M
                                     self.output_buffer.slice_mut(),
349
303M
                                     &mut self.total_out,
350
303M
                                     &mut self.state);
351
303M
         match write_all(self.output.as_mut().unwrap(), &self.output_buffer.slice_mut()[..output_offset]) {
352
303M
          Ok(_) => {},
353
0
          Err(e) => return Err(e),
354
         }
355
303M
         match op_result {
356
3.49k
          BrotliResult::NeedsMoreInput => assert_eq!(avail_in, 0),
357
303M
          BrotliResult::NeedsMoreOutput => continue,
358
          BrotliResult::ResultSuccess => {
359
259
              return Ok(input_offset);
360
          }
361
1.07k
          BrotliResult::ResultFailure => return self.error_if_invalid_data.take().map(|e|Err(e)).unwrap_or(Ok(0)),
362
       }
363
3.49k
        if avail_in == 0 {
364
3.49k
           break
365
0
        }
366
      }
367
3.49k
      Ok(buf.len())
368
4.82k
    }
369
0
    fn flush(&mut self) -> Result<(), ErrType> {
370
0
       self.output.as_mut().unwrap().flush()
371
0
    }
372
}
373
374
#[cfg(feature="std")]
375
#[cfg(test)]
376
mod test {
377
    use super::DecompressorWriter;
378
    use std::vec::Vec;
379
    use std::io::Write;
380
    // Brotli-compressed "hello\n" and 2 extra bytes
381
382
383
    #[test]
384
    fn write_extra() {
385
        let contents = b"\x8f\x02\x80\x68\x65\x6c\x6c\x6f\x0a\x03\x67\x6f\x6f\x64\x62\x79\x65\x0a";
386
        let mut decoder = DecompressorWriter::new(Vec::new(), 0);
387
        let n = decoder.write(contents).unwrap();
388
        assert_eq!(n, 10);
389
        // Ensure that we can continue to not send data to the writer
390
        // as it has consumed the entire file.
391
        let n = decoder.write(contents).unwrap();
392
        assert_eq!(n, 0);
393
394
        let mut decoder = DecompressorWriter::new(Vec::new(), 0);
395
        let e = decoder.write_all(contents).unwrap_err();
396
        assert!(e.kind() == std::io::ErrorKind::WriteZero);
397
        assert_eq!(decoder.get_ref().as_slice(), b"hello\n");
398
    }
399
}