/rust/registry/src/index.crates.io-1949cf8c6b5b557f/brotli-decompressor-5.0.0/src/reader.rs
Line | Count | Source |
1 | | #[cfg(feature="std")] |
2 | | use std::io::{self, Error, ErrorKind, Read}; |
3 | | #[cfg(feature="std")] |
4 | | pub use alloc_stdlib::StandardAlloc; |
5 | | #[cfg(all(feature="unsafe",feature="std"))] |
6 | | pub use alloc_stdlib::HeapAlloc; |
7 | | pub use huffman::{HuffmanCode, HuffmanTreeGroup}; |
8 | | pub use state::BrotliState; |
9 | | // use io_wrappers::write_all; |
10 | | pub use io_wrappers::{CustomRead, CustomWrite}; |
11 | | #[cfg(feature="std")] |
12 | | pub use io_wrappers::{IntoIoReader, IoReaderWrapper, IoWriterWrapper}; |
13 | | pub use super::decode::{BrotliDecompressStream, BrotliResult}; |
14 | | pub use alloc::{AllocatedStackMemory, Allocator, SliceWrapper, SliceWrapperMut, StackAllocator}; |
15 | | |
16 | | #[cfg(feature="std")] |
17 | | pub struct DecompressorCustomAlloc<R: Read, |
18 | | BufferType : SliceWrapperMut<u8>, |
19 | | AllocU8 : Allocator<u8>, |
20 | | AllocU32 : Allocator<u32>, |
21 | | AllocHC : Allocator<HuffmanCode> >(DecompressorCustomIo<io::Error, |
22 | | IntoIoReader<R>, |
23 | | BufferType, |
24 | | AllocU8, AllocU32, AllocHC>); |
25 | | |
26 | | |
27 | | #[cfg(feature="std")] |
28 | | impl<R: Read, |
29 | | BufferType : SliceWrapperMut<u8>, |
30 | | AllocU8, |
31 | | AllocU32, |
32 | | AllocHC> DecompressorCustomAlloc<R, BufferType, AllocU8, AllocU32, AllocHC> |
33 | | where AllocU8 : Allocator<u8>, AllocU32 : Allocator<u32>, AllocHC : Allocator<HuffmanCode> |
34 | | { |
35 | | |
36 | 0 | pub fn new(r: R, buffer : BufferType, |
37 | 0 | alloc_u8 : AllocU8, alloc_u32 : AllocU32, alloc_hc : AllocHC) -> Self { |
38 | 0 | DecompressorCustomAlloc::<R, BufferType, AllocU8, AllocU32, AllocHC>( |
39 | 0 | DecompressorCustomIo::<Error, |
40 | 0 | IntoIoReader<R>, |
41 | 0 | BufferType, |
42 | 0 | AllocU8, AllocU32, AllocHC>::new(IntoIoReader::<R>(r), |
43 | 0 | buffer, |
44 | 0 | alloc_u8, alloc_u32, alloc_hc, |
45 | 0 | Error::new(ErrorKind::InvalidData, |
46 | 0 | "Invalid Data"))) |
47 | 0 | } |
48 | | |
49 | 100k | pub fn new_with_custom_dictionary(r: R, buffer : BufferType, |
50 | 100k | alloc_u8 : AllocU8, alloc_u32 : AllocU32, alloc_hc : AllocHC, |
51 | 100k | dict: AllocU8::AllocatedMemory) -> Self { |
52 | 100k | DecompressorCustomAlloc::<R, BufferType, AllocU8, AllocU32, AllocHC>( |
53 | 100k | DecompressorCustomIo::<Error, |
54 | 100k | IntoIoReader<R>, |
55 | 100k | BufferType, |
56 | 100k | AllocU8, AllocU32, AllocHC>::new_with_custom_dictionary(IntoIoReader::<R>(r), |
57 | 100k | buffer, |
58 | 100k | alloc_u8, alloc_u32, alloc_hc, |
59 | 100k | dict, |
60 | 100k | Error::new(ErrorKind::InvalidData, |
61 | 100k | "Invalid Data"))) |
62 | 100k | } <brotli_decompressor::reader::DecompressorCustomAlloc<suricata::http2::decompression::HTTP2cursor, 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 | 49 | 58.9k | pub fn new_with_custom_dictionary(r: R, buffer : BufferType, | 50 | 58.9k | alloc_u8 : AllocU8, alloc_u32 : AllocU32, alloc_hc : AllocHC, | 51 | 58.9k | dict: AllocU8::AllocatedMemory) -> Self { | 52 | 58.9k | DecompressorCustomAlloc::<R, BufferType, AllocU8, AllocU32, AllocHC>( | 53 | 58.9k | DecompressorCustomIo::<Error, | 54 | 58.9k | IntoIoReader<R>, | 55 | 58.9k | BufferType, | 56 | 58.9k | AllocU8, AllocU32, AllocHC>::new_with_custom_dictionary(IntoIoReader::<R>(r), | 57 | 58.9k | buffer, | 58 | 58.9k | alloc_u8, alloc_u32, alloc_hc, | 59 | 58.9k | dict, | 60 | 58.9k | Error::new(ErrorKind::InvalidData, | 61 | 58.9k | "Invalid Data"))) | 62 | 58.9k | } |
Unexecuted instantiation: <brotli_decompressor::reader::DecompressorCustomAlloc<_, _, _, _, _>>::new_with_custom_dictionary <brotli_decompressor::reader::DecompressorCustomAlloc<suricata::http2::decompression::HTTP2cursor, 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 | 49 | 41.4k | pub fn new_with_custom_dictionary(r: R, buffer : BufferType, | 50 | 41.4k | alloc_u8 : AllocU8, alloc_u32 : AllocU32, alloc_hc : AllocHC, | 51 | 41.4k | dict: AllocU8::AllocatedMemory) -> Self { | 52 | 41.4k | DecompressorCustomAlloc::<R, BufferType, AllocU8, AllocU32, AllocHC>( | 53 | 41.4k | DecompressorCustomIo::<Error, | 54 | 41.4k | IntoIoReader<R>, | 55 | 41.4k | BufferType, | 56 | 41.4k | AllocU8, AllocU32, AllocHC>::new_with_custom_dictionary(IntoIoReader::<R>(r), | 57 | 41.4k | buffer, | 58 | 41.4k | alloc_u8, alloc_u32, alloc_hc, | 59 | 41.4k | dict, | 60 | 41.4k | Error::new(ErrorKind::InvalidData, | 61 | 41.4k | "Invalid Data"))) | 62 | 41.4k | } |
|
63 | | |
64 | 0 | pub fn get_ref(&self) -> &R { |
65 | 0 | &self.0.get_ref().0 |
66 | 0 | } |
67 | 0 | pub fn get_mut(&mut self) -> &mut R { |
68 | 0 | &mut self.0.get_mut().0 |
69 | 0 | } |
70 | 0 | pub fn into_inner(self) -> R { |
71 | 0 | self.0.into_inner().0 |
72 | 0 | } |
73 | | } |
74 | | #[cfg(feature="std")] |
75 | | impl<R: Read, |
76 | | BufferType : SliceWrapperMut<u8>, |
77 | | AllocU8 : Allocator<u8>, |
78 | | AllocU32 : Allocator<u32>, |
79 | | AllocHC : Allocator<HuffmanCode> > Read for DecompressorCustomAlloc<R, |
80 | | BufferType, |
81 | | AllocU8, |
82 | | AllocU32, |
83 | | AllocHC> { |
84 | 639k | fn read(&mut self, buf: &mut [u8]) -> Result<usize, Error> { |
85 | 639k | self.0.read(buf) |
86 | 639k | } <brotli_decompressor::reader::DecompressorCustomAlloc<suricata::http2::decompression::HTTP2cursor, alloc_stdlib::heap_alloc::WrapBox<u8>, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc> as std::io::Read>::read Line | Count | Source | 84 | 391k | fn read(&mut self, buf: &mut [u8]) -> Result<usize, Error> { | 85 | 391k | self.0.read(buf) | 86 | 391k | } |
Unexecuted instantiation: <brotli_decompressor::reader::DecompressorCustomAlloc<_, _, _, _, _> as std::io::Read>::read <brotli_decompressor::reader::DecompressorCustomAlloc<suricata::http2::decompression::HTTP2cursor, alloc_stdlib::heap_alloc::WrapBox<u8>, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc> as std::io::Read>::read Line | Count | Source | 84 | 247k | fn read(&mut self, buf: &mut [u8]) -> Result<usize, Error> { | 85 | 247k | self.0.read(buf) | 86 | 247k | } |
|
87 | | } |
88 | | |
89 | | |
90 | | #[cfg(not(any(feature="unsafe", not(feature="std"))))] |
91 | | pub struct Decompressor<R: Read>(DecompressorCustomAlloc<R, |
92 | | <StandardAlloc |
93 | | as Allocator<u8>>::AllocatedMemory, |
94 | | StandardAlloc, |
95 | | StandardAlloc, |
96 | | StandardAlloc>); |
97 | | |
98 | | |
99 | | #[cfg(not(any(feature="unsafe", not(feature="std"))))] |
100 | | impl<R: Read> Decompressor<R> { |
101 | 100k | pub fn new(r: R, buffer_size: usize) -> Self { |
102 | 100k | let dict = <StandardAlloc as Allocator<u8>>::AllocatedMemory::default(); |
103 | 100k | Self::new_with_custom_dict(r, buffer_size, dict) |
104 | 100k | } <brotli_decompressor::reader::Decompressor<suricata::http2::decompression::HTTP2cursor>>::new Line | Count | Source | 101 | 58.9k | pub fn new(r: R, buffer_size: usize) -> Self { | 102 | 58.9k | let dict = <StandardAlloc as Allocator<u8>>::AllocatedMemory::default(); | 103 | 58.9k | Self::new_with_custom_dict(r, buffer_size, dict) | 104 | 58.9k | } |
Unexecuted instantiation: <brotli_decompressor::reader::Decompressor<_>>::new <brotli_decompressor::reader::Decompressor<suricata::http2::decompression::HTTP2cursor>>::new Line | Count | Source | 101 | 41.4k | pub fn new(r: R, buffer_size: usize) -> Self { | 102 | 41.4k | let dict = <StandardAlloc as Allocator<u8>>::AllocatedMemory::default(); | 103 | 41.4k | Self::new_with_custom_dict(r, buffer_size, dict) | 104 | 41.4k | } |
|
105 | 100k | pub fn new_with_custom_dict(r: R, buffer_size: usize, dict: <StandardAlloc as Allocator<u8>>::AllocatedMemory) -> Self { |
106 | 100k | let mut alloc = StandardAlloc::default(); |
107 | 100k | let buffer = <StandardAlloc as Allocator<u8>>::alloc_cell(&mut alloc, if buffer_size == 0 {4096} else {buffer_size}); |
108 | 100k | Decompressor::<R>(DecompressorCustomAlloc::<R, |
109 | 100k | <StandardAlloc |
110 | 100k | as Allocator<u8>>::AllocatedMemory, |
111 | 100k | StandardAlloc, |
112 | 100k | StandardAlloc, |
113 | 100k | StandardAlloc>::new_with_custom_dictionary(r, |
114 | 100k | buffer, |
115 | 100k | alloc, |
116 | 100k | StandardAlloc::default(), |
117 | 100k | StandardAlloc::default(), |
118 | 100k | dict)) |
119 | 100k | } <brotli_decompressor::reader::Decompressor<suricata::http2::decompression::HTTP2cursor>>::new_with_custom_dict Line | Count | Source | 105 | 58.9k | pub fn new_with_custom_dict(r: R, buffer_size: usize, dict: <StandardAlloc as Allocator<u8>>::AllocatedMemory) -> Self { | 106 | 58.9k | let mut alloc = StandardAlloc::default(); | 107 | 58.9k | let buffer = <StandardAlloc as Allocator<u8>>::alloc_cell(&mut alloc, if buffer_size == 0 {4096} else {buffer_size}); | 108 | 58.9k | Decompressor::<R>(DecompressorCustomAlloc::<R, | 109 | 58.9k | <StandardAlloc | 110 | 58.9k | as Allocator<u8>>::AllocatedMemory, | 111 | 58.9k | StandardAlloc, | 112 | 58.9k | StandardAlloc, | 113 | 58.9k | StandardAlloc>::new_with_custom_dictionary(r, | 114 | 58.9k | buffer, | 115 | 58.9k | alloc, | 116 | 58.9k | StandardAlloc::default(), | 117 | 58.9k | StandardAlloc::default(), | 118 | 58.9k | dict)) | 119 | 58.9k | } |
Unexecuted instantiation: <brotli_decompressor::reader::Decompressor<_>>::new_with_custom_dict <brotli_decompressor::reader::Decompressor<suricata::http2::decompression::HTTP2cursor>>::new_with_custom_dict Line | Count | Source | 105 | 41.4k | pub fn new_with_custom_dict(r: R, buffer_size: usize, dict: <StandardAlloc as Allocator<u8>>::AllocatedMemory) -> Self { | 106 | 41.4k | let mut alloc = StandardAlloc::default(); | 107 | 41.4k | let buffer = <StandardAlloc as Allocator<u8>>::alloc_cell(&mut alloc, if buffer_size == 0 {4096} else {buffer_size}); | 108 | 41.4k | Decompressor::<R>(DecompressorCustomAlloc::<R, | 109 | 41.4k | <StandardAlloc | 110 | 41.4k | as Allocator<u8>>::AllocatedMemory, | 111 | 41.4k | StandardAlloc, | 112 | 41.4k | StandardAlloc, | 113 | 41.4k | StandardAlloc>::new_with_custom_dictionary(r, | 114 | 41.4k | buffer, | 115 | 41.4k | alloc, | 116 | 41.4k | StandardAlloc::default(), | 117 | 41.4k | StandardAlloc::default(), | 118 | 41.4k | dict)) | 119 | 41.4k | } |
|
120 | | |
121 | 0 | pub fn get_ref(&self) -> &R { |
122 | 0 | &self.0.get_ref() |
123 | 0 | } |
124 | 341k | pub fn get_mut(&mut self) -> &mut R { |
125 | 341k | &mut ((self.0).0).get_mut().0 |
126 | 341k | } <brotli_decompressor::reader::Decompressor<suricata::http2::decompression::HTTP2cursor>>::get_mut Line | Count | Source | 124 | 202k | pub fn get_mut(&mut self) -> &mut R { | 125 | 202k | &mut ((self.0).0).get_mut().0 | 126 | 202k | } |
Unexecuted instantiation: <brotli_decompressor::reader::Decompressor<_>>::get_mut <brotli_decompressor::reader::Decompressor<suricata::http2::decompression::HTTP2cursor>>::get_mut Line | Count | Source | 124 | 138k | pub fn get_mut(&mut self) -> &mut R { | 125 | 138k | &mut ((self.0).0).get_mut().0 | 126 | 138k | } |
|
127 | 0 | pub fn into_inner(self) -> R { |
128 | 0 | self.0.into_inner() |
129 | 0 | } |
130 | | } |
131 | | |
132 | | |
133 | | #[cfg(all(feature="unsafe", feature="std"))] |
134 | | pub struct Decompressor<R: Read>(DecompressorCustomAlloc<R, |
135 | | <HeapAlloc<u8> |
136 | | as Allocator<u8>>::AllocatedMemory, |
137 | | HeapAlloc<u8>, |
138 | | HeapAlloc<u32>, |
139 | | HeapAlloc<HuffmanCode> >); |
140 | | |
141 | | |
142 | | #[cfg(all(feature="unsafe", feature="std"))] |
143 | | impl<R: Read> Decompressor<R> { |
144 | | pub fn new(r: R, buffer_size: usize) -> Self { |
145 | | let dict = <HeapAlloc<u8> as Allocator<u8>>::AllocatedMemory::default(); |
146 | | Self::new_with_custom_dictionary(r, buffer_size, dict) |
147 | | } |
148 | | pub fn new_with_custom_dictionary(r: R, buffer_size: usize, dict: <HeapAlloc<u8> |
149 | | as Allocator<u8>>::AllocatedMemory) -> Self { |
150 | | let mut alloc_u8 = HeapAlloc::<u8>::new(0); |
151 | | let buffer = alloc_u8.alloc_cell(if buffer_size == 0 {4096} else {buffer_size}); |
152 | | let alloc_u32 = HeapAlloc::<u32>::new(0); |
153 | | let alloc_hc = HeapAlloc::<HuffmanCode>::new(HuffmanCode{ |
154 | | bits:0, value: 0, |
155 | | }); |
156 | | Decompressor::<R>(DecompressorCustomAlloc::<R, |
157 | | <HeapAlloc<u8> |
158 | | as Allocator<u8>>::AllocatedMemory, |
159 | | HeapAlloc<u8>, |
160 | | HeapAlloc<u32>, |
161 | | HeapAlloc<HuffmanCode> > |
162 | | ::new_with_custom_dictionary(r, buffer, alloc_u8, alloc_u32, alloc_hc, dict)) |
163 | | } |
164 | | |
165 | | pub fn get_ref(&self) -> &R { |
166 | | self.0.get_ref() |
167 | | } |
168 | | pub fn get_mut(&mut self) -> &mut R { |
169 | | &mut (self.0).0.get_mut().0 |
170 | | } |
171 | | pub fn into_inner(self) -> R { |
172 | | self.0.into_inner() |
173 | | } |
174 | | } |
175 | | |
176 | | |
177 | | #[cfg(feature="std")] |
178 | | impl<R: Read> Read for Decompressor<R> { |
179 | 639k | fn read(&mut self, buf: &mut [u8]) -> Result<usize, Error> { |
180 | 639k | self.0.read(buf) |
181 | 639k | } <brotli_decompressor::reader::Decompressor<suricata::http2::decompression::HTTP2cursor> as std::io::Read>::read Line | Count | Source | 179 | 391k | fn read(&mut self, buf: &mut [u8]) -> Result<usize, Error> { | 180 | 391k | self.0.read(buf) | 181 | 391k | } |
Unexecuted instantiation: <brotli_decompressor::reader::Decompressor<_> as std::io::Read>::read <brotli_decompressor::reader::Decompressor<suricata::http2::decompression::HTTP2cursor> as std::io::Read>::read Line | Count | Source | 179 | 247k | fn read(&mut self, buf: &mut [u8]) -> Result<usize, Error> { | 180 | 247k | self.0.read(buf) | 181 | 247k | } |
|
182 | | } |
183 | | |
184 | | pub struct DecompressorCustomIo<ErrType, |
185 | | R: CustomRead<ErrType>, |
186 | | BufferType: SliceWrapperMut<u8>, |
187 | | AllocU8: Allocator<u8>, |
188 | | AllocU32: Allocator<u32>, |
189 | | AllocHC: Allocator<HuffmanCode>> |
190 | | { |
191 | | input_buffer: BufferType, |
192 | | total_out: usize, |
193 | | input_offset: usize, |
194 | | input_len: usize, |
195 | | input: R, |
196 | | error_if_invalid_data: Option<ErrType>, |
197 | | state: BrotliState<AllocU8, AllocU32, AllocHC>, |
198 | | done: bool, |
199 | | } |
200 | | |
201 | | impl<ErrType, |
202 | | R: CustomRead<ErrType>, |
203 | | BufferType : SliceWrapperMut<u8>, |
204 | | AllocU8, |
205 | | AllocU32, |
206 | | AllocHC> DecompressorCustomIo<ErrType, R, BufferType, AllocU8, AllocU32, AllocHC> |
207 | | where AllocU8 : Allocator<u8>, AllocU32 : Allocator<u32>, AllocHC : Allocator<HuffmanCode> |
208 | | { |
209 | | |
210 | 0 | pub fn new(r: R, buffer : BufferType, |
211 | 0 | alloc_u8 : AllocU8, alloc_u32 : AllocU32, alloc_hc : AllocHC, |
212 | 0 | invalid_data_error_type : ErrType) -> Self { |
213 | 0 | let dict = AllocU8::AllocatedMemory::default(); |
214 | 0 | Self::new_with_custom_dictionary(r, buffer, alloc_u8, alloc_u32, alloc_hc, dict, invalid_data_error_type) |
215 | 0 | } |
216 | 100k | pub fn new_with_custom_dictionary(r: R, buffer : BufferType, |
217 | 100k | alloc_u8 : AllocU8, alloc_u32 : AllocU32, alloc_hc : AllocHC, |
218 | 100k | dict: AllocU8::AllocatedMemory, |
219 | 100k | invalid_data_error_type : ErrType) -> Self { |
220 | 100k | DecompressorCustomIo::<ErrType, R, BufferType, AllocU8, AllocU32, AllocHC>{ |
221 | 100k | input_buffer : buffer, |
222 | 100k | total_out : 0, |
223 | 100k | input_offset : 0, |
224 | 100k | input_len : 0, |
225 | 100k | input: r, |
226 | 100k | state : BrotliState::new_with_custom_dictionary(alloc_u8, |
227 | 100k | alloc_u32, |
228 | 100k | alloc_hc, |
229 | 100k | dict), |
230 | 100k | error_if_invalid_data : Some(invalid_data_error_type), |
231 | 100k | done: false, |
232 | 100k | } |
233 | 100k | } <brotli_decompressor::reader::DecompressorCustomIo<std::io::error::Error, brotli_decompressor::io_wrappers::IntoIoReader<suricata::http2::decompression::HTTP2cursor>, 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 | 216 | 58.9k | pub fn new_with_custom_dictionary(r: R, buffer : BufferType, | 217 | 58.9k | alloc_u8 : AllocU8, alloc_u32 : AllocU32, alloc_hc : AllocHC, | 218 | 58.9k | dict: AllocU8::AllocatedMemory, | 219 | 58.9k | invalid_data_error_type : ErrType) -> Self { | 220 | 58.9k | DecompressorCustomIo::<ErrType, R, BufferType, AllocU8, AllocU32, AllocHC>{ | 221 | 58.9k | input_buffer : buffer, | 222 | 58.9k | total_out : 0, | 223 | 58.9k | input_offset : 0, | 224 | 58.9k | input_len : 0, | 225 | 58.9k | input: r, | 226 | 58.9k | state : BrotliState::new_with_custom_dictionary(alloc_u8, | 227 | 58.9k | alloc_u32, | 228 | 58.9k | alloc_hc, | 229 | 58.9k | dict), | 230 | 58.9k | error_if_invalid_data : Some(invalid_data_error_type), | 231 | 58.9k | done: false, | 232 | 58.9k | } | 233 | 58.9k | } |
Unexecuted instantiation: <brotli_decompressor::reader::DecompressorCustomIo<_, _, _, _, _, _>>::new_with_custom_dictionary <brotli_decompressor::reader::DecompressorCustomIo<std::io::error::Error, brotli_decompressor::io_wrappers::IntoIoReader<suricata::http2::decompression::HTTP2cursor>, 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 | 216 | 41.4k | pub fn new_with_custom_dictionary(r: R, buffer : BufferType, | 217 | 41.4k | alloc_u8 : AllocU8, alloc_u32 : AllocU32, alloc_hc : AllocHC, | 218 | 41.4k | dict: AllocU8::AllocatedMemory, | 219 | 41.4k | invalid_data_error_type : ErrType) -> Self { | 220 | 41.4k | DecompressorCustomIo::<ErrType, R, BufferType, AllocU8, AllocU32, AllocHC>{ | 221 | 41.4k | input_buffer : buffer, | 222 | 41.4k | total_out : 0, | 223 | 41.4k | input_offset : 0, | 224 | 41.4k | input_len : 0, | 225 | 41.4k | input: r, | 226 | 41.4k | state : BrotliState::new_with_custom_dictionary(alloc_u8, | 227 | 41.4k | alloc_u32, | 228 | 41.4k | alloc_hc, | 229 | 41.4k | dict), | 230 | 41.4k | error_if_invalid_data : Some(invalid_data_error_type), | 231 | 41.4k | done: false, | 232 | 41.4k | } | 233 | 41.4k | } |
|
234 | | |
235 | 0 | pub fn get_ref(&self) -> &R { |
236 | 0 | &self.input |
237 | 0 | } |
238 | 341k | pub fn get_mut(&mut self) -> &mut R { |
239 | 341k | &mut self.input |
240 | 341k | } <brotli_decompressor::reader::DecompressorCustomIo<std::io::error::Error, brotli_decompressor::io_wrappers::IntoIoReader<suricata::http2::decompression::HTTP2cursor>, alloc_stdlib::heap_alloc::WrapBox<u8>, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc>>::get_mut Line | Count | Source | 238 | 202k | pub fn get_mut(&mut self) -> &mut R { | 239 | 202k | &mut self.input | 240 | 202k | } |
Unexecuted instantiation: <brotli_decompressor::reader::DecompressorCustomIo<_, _, _, _, _, _>>::get_mut <brotli_decompressor::reader::DecompressorCustomIo<std::io::error::Error, brotli_decompressor::io_wrappers::IntoIoReader<suricata::http2::decompression::HTTP2cursor>, alloc_stdlib::heap_alloc::WrapBox<u8>, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc>>::get_mut Line | Count | Source | 238 | 138k | pub fn get_mut(&mut self) -> &mut R { | 239 | 138k | &mut self.input | 240 | 138k | } |
|
241 | 0 | pub fn into_inner(self) -> R { |
242 | 0 | match self { |
243 | | DecompressorCustomIo { |
244 | 0 | input_buffer: _ib, |
245 | 0 | total_out: _to, |
246 | 0 | state: _state, |
247 | 0 | input_offset: _io, |
248 | 0 | input_len: _il, |
249 | 0 | error_if_invalid_data:_eiid, |
250 | 0 | input, |
251 | 0 | done: _done, |
252 | | } =>{ |
253 | 0 | input |
254 | | } |
255 | | } |
256 | 0 | } |
257 | | |
258 | 439k | pub fn copy_to_front(&mut self) { |
259 | 439k | let avail_in = self.input_len - self.input_offset; |
260 | 439k | if self.input_offset == self.input_buffer.slice_mut().len() { |
261 | 2.85k | self.input_offset = 0; |
262 | 2.85k | self.input_len = 0; |
263 | 437k | } else if self.input_offset + 256 > self.input_buffer.slice_mut().len() && avail_in < self.input_offset { |
264 | 248 | let (first, second) = self.input_buffer.slice_mut().split_at_mut(self.input_offset); |
265 | 248 | self.input_len -= self.input_offset; |
266 | 248 | first[0..avail_in].clone_from_slice(&second[0..avail_in]); |
267 | 248 | self.input_offset = 0; |
268 | 436k | } |
269 | 439k | } <brotli_decompressor::reader::DecompressorCustomIo<std::io::error::Error, brotli_decompressor::io_wrappers::IntoIoReader<suricata::http2::decompression::HTTP2cursor>, alloc_stdlib::heap_alloc::WrapBox<u8>, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc>>::copy_to_front Line | Count | Source | 258 | 317k | pub fn copy_to_front(&mut self) { | 259 | 317k | let avail_in = self.input_len - self.input_offset; | 260 | 317k | if self.input_offset == self.input_buffer.slice_mut().len() { | 261 | 2.12k | self.input_offset = 0; | 262 | 2.12k | self.input_len = 0; | 263 | 315k | } else if self.input_offset + 256 > self.input_buffer.slice_mut().len() && avail_in < self.input_offset { | 264 | 200 | let (first, second) = self.input_buffer.slice_mut().split_at_mut(self.input_offset); | 265 | 200 | self.input_len -= self.input_offset; | 266 | 200 | first[0..avail_in].clone_from_slice(&second[0..avail_in]); | 267 | 200 | self.input_offset = 0; | 268 | 315k | } | 269 | 317k | } |
Unexecuted instantiation: <brotli_decompressor::reader::DecompressorCustomIo<_, _, _, _, _, _>>::copy_to_front <brotli_decompressor::reader::DecompressorCustomIo<std::io::error::Error, brotli_decompressor::io_wrappers::IntoIoReader<suricata::http2::decompression::HTTP2cursor>, alloc_stdlib::heap_alloc::WrapBox<u8>, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc>>::copy_to_front Line | Count | Source | 258 | 122k | pub fn copy_to_front(&mut self) { | 259 | 122k | let avail_in = self.input_len - self.input_offset; | 260 | 122k | if self.input_offset == self.input_buffer.slice_mut().len() { | 261 | 731 | self.input_offset = 0; | 262 | 731 | self.input_len = 0; | 263 | 121k | } else if self.input_offset + 256 > self.input_buffer.slice_mut().len() && avail_in < self.input_offset { | 264 | 48 | let (first, second) = self.input_buffer.slice_mut().split_at_mut(self.input_offset); | 265 | 48 | self.input_len -= self.input_offset; | 266 | 48 | first[0..avail_in].clone_from_slice(&second[0..avail_in]); | 267 | 48 | self.input_offset = 0; | 268 | 121k | } | 269 | 122k | } |
|
270 | | } |
271 | | |
272 | | impl<ErrType, |
273 | | R: CustomRead<ErrType>, |
274 | | BufferType : SliceWrapperMut<u8>, |
275 | | AllocU8 : Allocator<u8>, |
276 | | AllocU32 : Allocator<u32>, |
277 | | AllocHC : Allocator<HuffmanCode> > CustomRead<ErrType> for DecompressorCustomIo<ErrType, |
278 | | R, |
279 | | BufferType, |
280 | | AllocU8, |
281 | | AllocU32, |
282 | | AllocHC> { |
283 | | /// This variant of read will return Ok(number of bytes read) until the file |
284 | | /// Is completed at which point it will return Ok(0). |
285 | | /// However if there are additional unconsumed bytes in the buffer, it will |
286 | | /// return Err(InvalidData) at that point. Otherwise it will keep returning |
287 | | /// Ok(0). |
288 | | /// |
289 | | /// # Arguments |
290 | | /// |
291 | | /// * `buf` - The buffer to read into |
292 | | /// |
293 | | /// # Errors |
294 | | /// |
295 | | /// Returns Ok(0) if the file has been fully decompressed. |
296 | | /// If the file has been fully decompressed but there are additional |
297 | | /// non-brotli bytes in the buffer, then return an InvalidData error. |
298 | | /// Also upstream errors from the reader are returned. |
299 | 639k | fn read(&mut self, buf: &mut [u8]) -> Result<usize, ErrType > { |
300 | 639k | let mut output_offset : usize = 0; |
301 | 639k | let mut avail_out = buf.len() - output_offset; |
302 | 639k | let mut avail_in = self.input_len - self.input_offset; |
303 | 764k | while avail_out == buf.len() { |
304 | 764k | match BrotliDecompressStream(&mut avail_in, |
305 | 764k | &mut self.input_offset, |
306 | 764k | &self.input_buffer.slice_mut()[..], |
307 | 764k | &mut avail_out, |
308 | 764k | &mut output_offset, |
309 | 764k | buf, |
310 | 764k | &mut self.total_out, |
311 | 764k | &mut self.state) { |
312 | | BrotliResult::NeedsMoreInput => { |
313 | 439k | self.copy_to_front(); |
314 | 439k | if output_offset != 0 { |
315 | | // The decompressor successfully decoded some bytes, but still requires more |
316 | | // we do not wish to risk self.input.read returning an error, so instead we |
317 | | // opt to return what we have and do not invoke the read trait method |
318 | 239k | return Ok(output_offset); |
319 | 200k | } |
320 | 200k | match self.input.read(&mut self.input_buffer.slice_mut()[self.input_len..]) { |
321 | 75.2k | Err(e) => { |
322 | 75.2k | return Err(e); |
323 | | }, |
324 | 125k | Ok(size) => if size == 0 { |
325 | 0 | return self.error_if_invalid_data.take().map(|e| Err(e)).unwrap_or(Ok(0)); Unexecuted instantiation: <brotli_decompressor::reader::DecompressorCustomIo<std::io::error::Error, brotli_decompressor::io_wrappers::IntoIoReader<suricata::http2::decompression::HTTP2cursor>, 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::CustomRead<std::io::error::Error>>::read::{closure#0}Unexecuted instantiation: <brotli_decompressor::reader::DecompressorCustomIo<_, _, _, _, _, _> as brotli_decompressor::io_wrappers::CustomRead<_>>::read::{closure#0}Unexecuted instantiation: <brotli_decompressor::reader::DecompressorCustomIo<std::io::error::Error, brotli_decompressor::io_wrappers::IntoIoReader<suricata::http2::decompression::HTTP2cursor>, 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::CustomRead<std::io::error::Error>>::read::{closure#0} |
326 | 125k | }else { |
327 | 125k | self.input_len += size; |
328 | 125k | avail_in = self.input_len - self.input_offset; |
329 | 125k | }, |
330 | | } |
331 | | }, |
332 | | BrotliResult::NeedsMoreOutput => { |
333 | 267k | break; |
334 | | }, |
335 | | BrotliResult::ResultSuccess => { |
336 | 10.8k | if output_offset == 0 { |
337 | 7.54k | if !self.done { |
338 | 5.87k | self.done = true; |
339 | 5.87k | } else if self.input_len != self.input_offset { |
340 | | // Did not consume entire input; report error. |
341 | 1.15k | return self.error_if_invalid_data.take().map(|e| Err(e)).unwrap_or(Ok(output_offset)); <brotli_decompressor::reader::DecompressorCustomIo<std::io::error::Error, brotli_decompressor::io_wrappers::IntoIoReader<suricata::http2::decompression::HTTP2cursor>, 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::CustomRead<std::io::error::Error>>::read::{closure#1}Line | Count | Source | 341 | 648 | return self.error_if_invalid_data.take().map(|e| Err(e)).unwrap_or(Ok(output_offset)); |
Unexecuted instantiation: <brotli_decompressor::reader::DecompressorCustomIo<_, _, _, _, _, _> as brotli_decompressor::io_wrappers::CustomRead<_>>::read::{closure#1}<brotli_decompressor::reader::DecompressorCustomIo<std::io::error::Error, brotli_decompressor::io_wrappers::IntoIoReader<suricata::http2::decompression::HTTP2cursor>, 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::CustomRead<std::io::error::Error>>::read::{closure#1}Line | Count | Source | 341 | 506 | return self.error_if_invalid_data.take().map(|e| Err(e)).unwrap_or(Ok(output_offset)); |
|
342 | 513 | } |
343 | 3.32k | } |
344 | 9.71k | return Ok(output_offset); |
345 | | } |
346 | 46.3k | BrotliResult::ResultFailure => return self.error_if_invalid_data.take().map(|e| Err(e)).unwrap_or(Ok(0)), <brotli_decompressor::reader::DecompressorCustomIo<std::io::error::Error, brotli_decompressor::io_wrappers::IntoIoReader<suricata::http2::decompression::HTTP2cursor>, 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::CustomRead<std::io::error::Error>>::read::{closure#2}Line | Count | Source | 346 | 24.9k | BrotliResult::ResultFailure => return self.error_if_invalid_data.take().map(|e| Err(e)).unwrap_or(Ok(0)), |
Unexecuted instantiation: <brotli_decompressor::reader::DecompressorCustomIo<_, _, _, _, _, _> as brotli_decompressor::io_wrappers::CustomRead<_>>::read::{closure#2}<brotli_decompressor::reader::DecompressorCustomIo<std::io::error::Error, brotli_decompressor::io_wrappers::IntoIoReader<suricata::http2::decompression::HTTP2cursor>, 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::CustomRead<std::io::error::Error>>::read::{closure#2}Line | Count | Source | 346 | 21.3k | BrotliResult::ResultFailure => return self.error_if_invalid_data.take().map(|e| Err(e)).unwrap_or(Ok(0)), |
|
347 | | } |
348 | | } |
349 | 267k | Ok(output_offset) |
350 | 639k | } <brotli_decompressor::reader::DecompressorCustomIo<std::io::error::Error, brotli_decompressor::io_wrappers::IntoIoReader<suricata::http2::decompression::HTTP2cursor>, 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::CustomRead<std::io::error::Error>>::read Line | Count | Source | 299 | 391k | fn read(&mut self, buf: &mut [u8]) -> Result<usize, ErrType > { | 300 | 391k | let mut output_offset : usize = 0; | 301 | 391k | let mut avail_out = buf.len() - output_offset; | 302 | 391k | let mut avail_in = self.input_len - self.input_offset; | 303 | 466k | while avail_out == buf.len() { | 304 | 466k | match BrotliDecompressStream(&mut avail_in, | 305 | 466k | &mut self.input_offset, | 306 | 466k | &self.input_buffer.slice_mut()[..], | 307 | 466k | &mut avail_out, | 308 | 466k | &mut output_offset, | 309 | 466k | buf, | 310 | 466k | &mut self.total_out, | 311 | 466k | &mut self.state) { | 312 | | BrotliResult::NeedsMoreInput => { | 313 | 317k | self.copy_to_front(); | 314 | 317k | if output_offset != 0 { | 315 | | // The decompressor successfully decoded some bytes, but still requires more | 316 | | // we do not wish to risk self.input.read returning an error, so instead we | 317 | | // opt to return what we have and do not invoke the read trait method | 318 | 196k | return Ok(output_offset); | 319 | 121k | } | 320 | 121k | match self.input.read(&mut self.input_buffer.slice_mut()[self.input_len..]) { | 321 | 46.3k | Err(e) => { | 322 | 46.3k | return Err(e); | 323 | | }, | 324 | 74.7k | Ok(size) => if size == 0 { | 325 | 0 | return self.error_if_invalid_data.take().map(|e| Err(e)).unwrap_or(Ok(0)); | 326 | 74.7k | }else { | 327 | 74.7k | self.input_len += size; | 328 | 74.7k | avail_in = self.input_len - self.input_offset; | 329 | 74.7k | }, | 330 | | } | 331 | | }, | 332 | | BrotliResult::NeedsMoreOutput => { | 333 | 118k | break; | 334 | | }, | 335 | | BrotliResult::ResultSuccess => { | 336 | 5.80k | if output_offset == 0 { | 337 | 4.33k | if !self.done { | 338 | 3.27k | self.done = true; | 339 | 3.27k | } else if self.input_len != self.input_offset { | 340 | | // Did not consume entire input; report error. | 341 | 648 | return self.error_if_invalid_data.take().map(|e| Err(e)).unwrap_or(Ok(output_offset)); | 342 | 409 | } | 343 | 1.46k | } | 344 | 5.15k | return Ok(output_offset); | 345 | | } | 346 | 24.9k | BrotliResult::ResultFailure => return self.error_if_invalid_data.take().map(|e| Err(e)).unwrap_or(Ok(0)), | 347 | | } | 348 | | } | 349 | 118k | Ok(output_offset) | 350 | 391k | } |
Unexecuted instantiation: <brotli_decompressor::reader::DecompressorCustomIo<_, _, _, _, _, _> as brotli_decompressor::io_wrappers::CustomRead<_>>::read <brotli_decompressor::reader::DecompressorCustomIo<std::io::error::Error, brotli_decompressor::io_wrappers::IntoIoReader<suricata::http2::decompression::HTTP2cursor>, 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::CustomRead<std::io::error::Error>>::read Line | Count | Source | 299 | 247k | fn read(&mut self, buf: &mut [u8]) -> Result<usize, ErrType > { | 300 | 247k | let mut output_offset : usize = 0; | 301 | 247k | let mut avail_out = buf.len() - output_offset; | 302 | 247k | let mut avail_in = self.input_len - self.input_offset; | 303 | 298k | while avail_out == buf.len() { | 304 | 298k | match BrotliDecompressStream(&mut avail_in, | 305 | 298k | &mut self.input_offset, | 306 | 298k | &self.input_buffer.slice_mut()[..], | 307 | 298k | &mut avail_out, | 308 | 298k | &mut output_offset, | 309 | 298k | buf, | 310 | 298k | &mut self.total_out, | 311 | 298k | &mut self.state) { | 312 | | BrotliResult::NeedsMoreInput => { | 313 | 122k | self.copy_to_front(); | 314 | 122k | if output_offset != 0 { | 315 | | // The decompressor successfully decoded some bytes, but still requires more | 316 | | // we do not wish to risk self.input.read returning an error, so instead we | 317 | | // opt to return what we have and do not invoke the read trait method | 318 | 42.9k | return Ok(output_offset); | 319 | 79.4k | } | 320 | 79.4k | match self.input.read(&mut self.input_buffer.slice_mut()[self.input_len..]) { | 321 | 28.9k | Err(e) => { | 322 | 28.9k | return Err(e); | 323 | | }, | 324 | 50.4k | Ok(size) => if size == 0 { | 325 | 0 | return self.error_if_invalid_data.take().map(|e| Err(e)).unwrap_or(Ok(0)); | 326 | 50.4k | }else { | 327 | 50.4k | self.input_len += size; | 328 | 50.4k | avail_in = self.input_len - self.input_offset; | 329 | 50.4k | }, | 330 | | } | 331 | | }, | 332 | | BrotliResult::NeedsMoreOutput => { | 333 | 149k | break; | 334 | | }, | 335 | | BrotliResult::ResultSuccess => { | 336 | 5.06k | if output_offset == 0 { | 337 | 3.21k | if !self.done { | 338 | 2.60k | self.done = true; | 339 | 2.60k | } else if self.input_len != self.input_offset { | 340 | | // Did not consume entire input; report error. | 341 | 506 | return self.error_if_invalid_data.take().map(|e| Err(e)).unwrap_or(Ok(output_offset)); | 342 | 104 | } | 343 | 1.85k | } | 344 | 4.56k | return Ok(output_offset); | 345 | | } | 346 | 21.3k | BrotliResult::ResultFailure => return self.error_if_invalid_data.take().map(|e| Err(e)).unwrap_or(Ok(0)), | 347 | | } | 348 | | } | 349 | 149k | Ok(output_offset) | 350 | 247k | } |
|
351 | | } |
352 | | |
353 | | #[cfg(feature="std")] |
354 | | #[test] |
355 | | fn test_no_vanishing_bytes() { |
356 | | use std::string::ToString; |
357 | | |
358 | | // Output from this command: |
359 | | let compressed_with_extra = b"\x8f\x02\x80\x68\x65\x6c\x6c\x6f\x0a\x03\x67\x6f\x6f\x64\x62\x79\x65\x0a"; |
360 | | // Make sure that read_to_string returns the data. |
361 | | let cursor = std::io::Cursor::new(compressed_with_extra); |
362 | | let mut reader = super::Decompressor::new(cursor, 8000); |
363 | | assert_eq!(std::io::read_to_string(&mut reader).unwrap(), "hello\n"); |
364 | | |
365 | | // However you can call read extra times to make sure there's no data. |
366 | | let cursor = std::io::Cursor::new(compressed_with_extra); |
367 | | let mut reader = super::Decompressor::new(cursor, 8000); |
368 | | let mut data = std::vec::Vec::<u8>::default(); |
369 | | loop { |
370 | | let mut buf = [0u8;5]; |
371 | | let offset = reader.read(&mut buf).unwrap(); |
372 | | if offset == 0 { |
373 | | break; |
374 | | } |
375 | | data.extend_from_slice(&buf[..offset]); |
376 | | } |
377 | | assert_eq!( |
378 | | &data, |
379 | | &['h' as u8, 'e' as u8, 'l' as u8, 'l' as u8, 'o' as u8, '\n' as u8]); |
380 | | |
381 | | // But calling read, one last time, results in an error because there |
382 | | // were leftover bytes in the buffer. |
383 | | let mut buf = [0u8;5]; |
384 | | assert_eq!(reader.read(&mut buf).unwrap_err().kind(), |
385 | | io::ErrorKind::InvalidData); |
386 | | data.clear(); |
387 | | |
388 | | |
389 | | } |
390 | | |
391 | | #[cfg(feature="std")] |
392 | | #[test] |
393 | | fn test_repeated_read_returns_zero() { |
394 | | use std::string::ToString; |
395 | | |
396 | | // Output from this command: |
397 | | let compressed_without_extra = b"\x8f\x02\x80\x68\x65\x6c\x6c\x6f\x0a\x03"; |
398 | | // Make sure that read_to_string returns the data. |
399 | | let cursor = std::io::Cursor::new(compressed_without_extra); |
400 | | let mut reader = super::Decompressor::new(cursor, 8000); |
401 | | assert_eq!(std::io::read_to_string(&mut reader).unwrap(), "hello\n"); |
402 | | |
403 | | // However you can call read extra times to make sure there's no data. |
404 | | let cursor = std::io::Cursor::new(compressed_without_extra); |
405 | | let mut reader = super::Decompressor::new(cursor, 8000); |
406 | | let mut data = std::vec::Vec::<u8>::default(); |
407 | | loop { |
408 | | let mut buf = [0u8;5]; |
409 | | let offset = reader.read(&mut buf).unwrap(); |
410 | | if offset == 0 { |
411 | | break; |
412 | | } |
413 | | data.extend_from_slice(&buf[..offset]); |
414 | | } |
415 | | assert_eq!(&data, &['h' as u8, 'e' as u8, 'l' as u8, 'l' as u8, 'o' as u8, '\n' as u8]); |
416 | | let mut buf = [0u8;5]; |
417 | | assert_eq!(reader.read(&mut buf).unwrap(), 0); |
418 | | data.clear(); |
419 | | |
420 | | |
421 | | } |
422 | | |