1#![no_std]
2#![allow(non_snake_case)]
3#![allow(unused_parens)]
4#![allow(unused_imports)]
5#![allow(non_camel_case_types)]
6#![allow(non_snake_case)]
7#![allow(non_upper_case_globals)]
8#![cfg_attr(feature="no-stdlib-ffi-binding",cfg_attr(not(feature="std"), feature(lang_items)))]
9#![cfg_attr(feature="no-stdlib-ffi-binding",cfg_attr(not(feature="std"), feature(panic_handler)))]
10#![cfg_attr(not(any(feature="unsafe", feature="ffi-api")), forbid(unsafe_code))]
13
14
15#[macro_use]
16#[cfg(feature="std")]
18extern crate std;
19#[cfg(feature="std")]
20use std::io::{self, Error, ErrorKind, Read, Write};
21#[cfg(feature="std")]
22extern crate alloc_stdlib;
23#[macro_use]
24extern crate alloc_no_stdlib as alloc;
25pub use alloc::{AllocatedStackMemory, Allocator, SliceWrapper, SliceWrapperMut, StackAllocator, bzero};
26use core::ops;
27
28#[cfg(feature="std")]
29pub use alloc_stdlib::StandardAlloc;
30#[cfg(all(feature="unsafe",feature="std"))]
31pub use alloc_stdlib::HeapAlloc;
32#[macro_use]
33mod memory;
34pub mod dictionary;
35mod brotli_alloc;
36#[macro_use]
37mod bit_reader;
38mod huffman;
39mod state;
40mod prefix;
41mod context;
42mod shared_dictionary;
43pub mod transform;
44mod test;
45mod test_metadata;
46mod decode;
47pub mod io_wrappers;
48pub mod reader;
49pub mod writer;
50pub use huffman::{HuffmanCode, HuffmanTreeGroup};
51pub use state::BrotliState;
52pub use state::BrotliDecoderErrorCode;
53pub use state::BrotliDecoderParameter;
54pub use shared_dictionary::BrotliSharedDictionary;
55#[cfg(feature="ffi-api")]
56pub mod ffi;
57pub use reader::{DecompressorCustomIo};
58
59#[cfg(feature="std")]
60pub use reader::{Decompressor};
61
62pub use writer::{DecompressorWriterCustomIo};
63#[cfg(feature="std")]
64pub use writer::{DecompressorWriter};
65
66pub use io_wrappers::{CustomRead, CustomWrite};
68#[cfg(feature="std")]
69pub use io_wrappers::{IntoIoReader, IoReaderWrapper, IntoIoWriter, IoWriterWrapper};
70
71pub use decode::{BrotliDecompressStream, BrotliDecompressStreamWithDictionary, BrotliResult, BrotliDecoderHasMoreOutput, BrotliDecoderIsFinished, BrotliDecoderTakeOutput};
82
83
84
85
86#[cfg(not(any(feature="unsafe", not(feature="std"))))]
87pub fn BrotliDecompress<InputType, OutputType>(r: &mut InputType,
88 w: &mut OutputType)
89 -> Result<(), io::Error>
90 where InputType: Read,
91 OutputType: Write
92{
93 let mut input_buffer: [u8; 4096] = [0; 4096];
94 let mut output_buffer: [u8; 4096] = [0; 4096];
95 BrotliDecompressCustomAlloc(r,
96 w,
97 &mut input_buffer[..],
98 &mut output_buffer[..],
99 StandardAlloc::default(),
100 StandardAlloc::default(),
101 StandardAlloc::default(),
102 )
103}
104
105#[cfg(feature="std")]
106pub fn BrotliDecompressCustomDict<InputType, OutputType>(r: &mut InputType,
107 w: &mut OutputType,
108 input_buffer:&mut [u8],
109 output_buffer:&mut [u8],
110 custom_dictionary:std::vec::Vec<u8>)
111 -> Result<(), io::Error>
112 where InputType: Read,
113 OutputType: Write
114{
115 let mut alloc_u8 = brotli_alloc::BrotliAlloc::<u8>::new();
116 let mut input_buffer_backing;
117 let mut output_buffer_backing;
118 {
119 let mut borrowed_input_buffer = input_buffer;
120 let mut borrowed_output_buffer = output_buffer;
121 if borrowed_input_buffer.len() == 0 {
122 input_buffer_backing = alloc_u8.alloc_cell(4096);
123 borrowed_input_buffer = input_buffer_backing.slice_mut();
124 }
125 if borrowed_output_buffer.len() == 0 {
126 output_buffer_backing = alloc_u8.alloc_cell(4096);
127 borrowed_output_buffer = output_buffer_backing.slice_mut();
128 }
129 let dict = alloc_u8.take_ownership(custom_dictionary);
130 BrotliDecompressCustomIoCustomDict(&mut IoReaderWrapper::<InputType>(r),
131 &mut IoWriterWrapper::<OutputType>(w),
132 borrowed_input_buffer,
133 borrowed_output_buffer,
134 alloc_u8,
135 brotli_alloc::BrotliAlloc::<u32>::new(),
136 brotli_alloc::BrotliAlloc::<HuffmanCode>::new(),
137 dict,
138 Error::new(ErrorKind::UnexpectedEof, "Unexpected EOF"))
139 }
140}
141
142#[cfg(all(feature="unsafe",feature="std"))]
143pub fn BrotliDecompress<InputType, OutputType>(r: &mut InputType,
144 w: &mut OutputType)
145 -> Result<(), io::Error>
146 where InputType: Read,
147 OutputType: Write
148{
149 let mut input_buffer: [u8; 4096] = [0; 4096];
150 let mut output_buffer: [u8; 4096] = [0; 4096];
151 BrotliDecompressCustomAlloc(r,
152 w,
153 &mut input_buffer[..],
154 &mut output_buffer[..],
155 HeapAlloc::<u8>::new(0),
156 HeapAlloc::<u32>::new(0),
157 HeapAlloc::<HuffmanCode>::new(HuffmanCode{ bits:2, value: 1}))
158}
159
160
161#[cfg(feature="std")]
162pub fn BrotliDecompressCustomAlloc<InputType,
163 OutputType,
164 AllocU8: Allocator<u8>,
165 AllocU32: Allocator<u32>,
166 AllocHC: Allocator<HuffmanCode>>
167 (r: &mut InputType,
168 w: &mut OutputType,
169 input_buffer: &mut [u8],
170 output_buffer: &mut [u8],
171 alloc_u8: AllocU8,
172 alloc_u32: AllocU32,
173 alloc_hc: AllocHC)
174 -> Result<(), io::Error>
175 where InputType: Read,
176 OutputType: Write
177{
178 BrotliDecompressCustomIo(&mut IoReaderWrapper::<InputType>(r),
179 &mut IoWriterWrapper::<OutputType>(w),
180 input_buffer,
181 output_buffer,
182 alloc_u8,
183 alloc_u32,
184 alloc_hc,
185 Error::new(ErrorKind::UnexpectedEof, "Unexpected EOF"))
186}
187pub fn BrotliDecompressCustomIo<ErrType,
188 InputType,
189 OutputType,
190 AllocU8: Allocator<u8>,
191 AllocU32: Allocator<u32>,
192 AllocHC: Allocator<HuffmanCode>>
193 (r: &mut InputType,
194 w: &mut OutputType,
195 input_buffer: &mut [u8],
196 output_buffer: &mut [u8],
197 alloc_u8: AllocU8,
198 alloc_u32: AllocU32,
199 alloc_hc: AllocHC,
200 unexpected_eof_error_constant: ErrType)
201 -> Result<(), ErrType>
202 where InputType: CustomRead<ErrType>,
203 OutputType: CustomWrite<ErrType>
204{
205 BrotliDecompressCustomIoCustomDict(r, w, input_buffer, output_buffer, alloc_u8, alloc_u32, alloc_hc, AllocU8::AllocatedMemory::default(), unexpected_eof_error_constant)
206}
207pub fn BrotliDecompressCustomIoCustomDict<ErrType,
208 InputType,
209 OutputType,
210 AllocU8: Allocator<u8>,
211 AllocU32: Allocator<u32>,
212 AllocHC: Allocator<HuffmanCode>>
213 (r: &mut InputType,
214 w: &mut OutputType,
215 input_buffer: &mut [u8],
216 output_buffer: &mut [u8],
217 alloc_u8: AllocU8,
218 alloc_u32: AllocU32,
219 alloc_hc: AllocHC,
220 custom_dictionary: AllocU8::AllocatedMemory,
221 unexpected_eof_error_constant: ErrType)
222 -> Result<(), ErrType>
223 where InputType: CustomRead<ErrType>,
224 OutputType: CustomWrite<ErrType>
225{
226 let mut brotli_state = BrotliState::new_with_custom_dictionary(alloc_u8, alloc_u32, alloc_hc, custom_dictionary);
227 assert!(input_buffer.len() != 0);
228 assert!(output_buffer.len() != 0);
229 let mut available_out: usize = output_buffer.len();
230
231 let mut available_in: usize = 0;
232 let mut input_offset: usize = 0;
233 let mut output_offset: usize = 0;
234 let mut result: BrotliResult = BrotliResult::NeedsMoreInput;
235 loop {
236 match result {
237 BrotliResult::NeedsMoreInput => {
238 input_offset = 0;
239 match r.read(input_buffer) {
240 Err(e) => {
241 return Err(e);
242 },
243 Ok(size) => {
244 if size == 0 {
245 return Err(unexpected_eof_error_constant);
246 }
247 available_in = size;
248 }
249 }
250 }
251 BrotliResult::NeedsMoreOutput => {
252 let mut total_written: usize = 0;
253 while total_written < output_offset {
254 match w.write(&output_buffer[total_written..output_offset]) {
256 Err(e) => {
257 return Result::Err(e);
258 },
259 Ok(0) => {
260 return Result::Err(unexpected_eof_error_constant);
261 }
262 Ok(cur_written) => {
263 total_written += cur_written;
264 }
265 }
266 }
267
268 output_offset = 0;
269 }
270 BrotliResult::ResultSuccess => break,
271 BrotliResult::ResultFailure => {
272 return Err(unexpected_eof_error_constant);
273 }
274 }
275 let mut written: usize = 0;
276 result = BrotliDecompressStream(&mut available_in,
277 &mut input_offset,
278 input_buffer,
279 &mut available_out,
280 &mut output_offset,
281 output_buffer,
282 &mut written,
283 &mut brotli_state);
284
285 if output_offset != 0 {
286 let mut total_written: usize = 0;
287 while total_written < output_offset {
288 match w.write(&output_buffer[total_written..output_offset]) {
289 Err(e) => {
290 return Result::Err(e);
291 },
292 Ok(0) => {
294 return Result::Err(unexpected_eof_error_constant);
295 }
296 Ok(cur_written) => {
297 total_written += cur_written;
298 }
299 }
300 }
301 output_offset = 0;
302 available_out = output_buffer.len()
303 }
304 }
305 Ok(())
306}
307
308
309#[cfg(feature="std")]
310pub fn copy_from_to<R: io::Read, W: io::Write>(mut r: R, mut w: W) -> io::Result<usize> {
311 let mut buffer: [u8; 65536] = [0; 65536];
312 let mut out_size: usize = 0;
313 loop {
314 match r.read(&mut buffer[..]) {
315 Err(e) => {
316 if let io::ErrorKind::Interrupted = e.kind() {
317 continue
318 }
319 return Err(e);
320 }
321 Ok(size) => {
322 if size == 0 {
323 break;
324 } else {
325 match w.write_all(&buffer[..size]) {
326 Err(e) => {
327 if let io::ErrorKind::Interrupted = e.kind() {
328 continue
329 }
330 return Err(e);
331 }
332 Ok(_) => out_size += size,
333 }
334 }
335 }
336 }
337 }
338 Ok(out_size)
339}
340
341#[repr(C)]
342pub struct BrotliDecoderReturnInfo {
343 pub decoded_size: usize,
344 pub error_string: [u8;256],
345 pub result: BrotliResult,
346 pub error_code: state::BrotliDecoderErrorCode,
347}
348impl BrotliDecoderReturnInfo {
349 fn new<AllocU8: Allocator<u8>,
350 AllocU32: Allocator<u32>,
351 AllocHC: Allocator<HuffmanCode>>(
352 state: &BrotliState<AllocU8, AllocU32, AllocHC>,
353 result: BrotliResult,
354 output_size: usize,
355 ) -> Self {
356 let mut ret = BrotliDecoderReturnInfo{
357 result: result,
358 decoded_size: output_size,
359 error_code: decode::BrotliDecoderGetErrorCode(&state),
360 error_string: if let &Err(msg) = &state.mtf_or_error_string {
361 msg
362 } else {
363 [0u8;256]
364 },
365 };
366 if ret.error_string[0] == 0 {
367 let error_string = state::BrotliDecoderErrorStr(ret.error_code);
368 let to_copy = core::cmp::min(error_string.len(), ret.error_string.len() - 1);
369 for (dst, src) in ret.error_string[..to_copy].iter_mut().zip(error_string[..to_copy].bytes()) {
370 *dst = src;
371 }
372 }
373 ret
374 }
375}
376
377declare_stack_allocator_struct!(MemPool, 512, stack);
378
379pub fn brotli_decode_prealloc(
380 input: &[u8],
381 mut output: &mut[u8],
382 scratch_u8: &mut [u8],
383 scratch_u32: &mut [u32],
384 scratch_hc: &mut [HuffmanCode],
385) -> BrotliDecoderReturnInfo {
386 let u8_pool_size = scratch_u8.len();
387 let stack_u8_allocator = MemPool::<u8>::new_allocator(scratch_u8, bzero);
388 let stack_u32_allocator = MemPool::<u32>::new_allocator(scratch_u32, bzero);
389 let stack_hc_allocator = MemPool::<HuffmanCode>::new_allocator(scratch_hc, bzero);
390 let mut available_out = output.len();
391 let mut available_in: usize = input.len();
392 let mut input_offset: usize = 0;
393 let mut output_offset: usize = 0;
394 let mut written: usize = 0;
395 let mut brotli_state =
396 BrotliState::new(stack_u8_allocator, stack_u32_allocator, stack_hc_allocator);
397 brotli_state.u8_pool_size = u8_pool_size;
399 let result = ::BrotliDecompressStream(&mut available_in,
400 &mut input_offset,
401 &input[..],
402 &mut available_out,
403 &mut output_offset,
404 &mut output,
405 &mut written,
406 &mut brotli_state);
407 let return_info = BrotliDecoderReturnInfo::new(&brotli_state, result.into(), output_offset);
408 return_info
409}
410
411#[cfg(not(feature="std"))]
412pub fn brotli_decode(
413 input: &[u8],
414 output_and_scratch: &mut[u8],
415) -> BrotliDecoderReturnInfo {
416 let mut stack_u32_buffer = [0u32; 12 * 1024 * 6];
417 let mut stack_hc_buffer = [HuffmanCode::default(); 128 * (decode::kNumInsertAndCopyCodes as usize + decode::kNumLiteralCodes as usize) + 6 * decode::kNumBlockLengthCodes as usize * huffman::INSERT_COPY_TABLE_WINDOW];
418 let mut guessed_output_size = core::cmp::min(
419 core::cmp::max(input.len(), output_and_scratch.len() / 3),
421 output_and_scratch.len());
422 if input.len() > 2 {
423 let scratch_len = output_and_scratch.len() - guessed_output_size;
424 if let Ok(lgwin) = decode::lg_window_size(input[0], input[1]) {
425 let extra_window_size = 65536 + (decode::kNumLiteralCodes + decode::kNumInsertAndCopyCodes) as usize * 256 + (1usize << lgwin.0) * 5 / 4;
426 if extra_window_size < scratch_len {
427 guessed_output_size += (scratch_len - extra_window_size) * 3/4;
428 }
429 }
430 }
431 let (mut output, mut scratch_space) = output_and_scratch.split_at_mut(guessed_output_size);
432 let u8_pool_size = scratch_space.len();
433 let stack_u8_allocator = MemPool::<u8>::new_allocator(&mut scratch_space, bzero);
434 let stack_u32_allocator = MemPool::<u32>::new_allocator(&mut stack_u32_buffer, bzero);
435 let stack_hc_allocator = MemPool::<HuffmanCode>::new_allocator(&mut stack_hc_buffer, bzero);
436 let mut available_out = output.len();
437 let mut available_in: usize = input.len();
438 let mut input_offset: usize = 0;
439 let mut output_offset: usize = 0;
440 let mut written: usize = 0;
441 let mut brotli_state =
442 BrotliState::new(stack_u8_allocator, stack_u32_allocator, stack_hc_allocator);
443 brotli_state.u8_pool_size = u8_pool_size;
445 let result = ::BrotliDecompressStream(&mut available_in,
446 &mut input_offset,
447 &input[..],
448 &mut available_out,
449 &mut output_offset,
450 &mut output,
451 &mut written,
452 &mut brotli_state);
453 let return_info = BrotliDecoderReturnInfo::new(&brotli_state, result.into(), output_offset);
454 return_info
455}
456
457#[cfg(feature="std")]
458pub fn brotli_decode(
459 input: &[u8],
460 output: &mut[u8],
461) -> BrotliDecoderReturnInfo {
462 brotli_decode_with_allocators(input, output,
463 StandardAlloc::default(),
464 StandardAlloc::default(),
465 StandardAlloc::default())
466}
467
468#[cfg(feature="std")]
469pub(crate) fn brotli_decode_with_allocators<AllocU8: Allocator<u8>,
470 AllocU32: Allocator<u32>,
471 AllocHC: Allocator<HuffmanCode>>(
472 input: &[u8],
473 mut output: &mut[u8],
474 alloc_u8: AllocU8,
475 alloc_u32: AllocU32,
476 alloc_hc: AllocHC,
477) -> BrotliDecoderReturnInfo {
478 let mut available_out = output.len();
479 let mut available_in: usize = input.len();
480 let mut input_offset: usize = 0;
481 let mut output_offset: usize = 0;
482 let mut written: usize = 0;
483 let mut brotli_state = BrotliState::new(alloc_u8, alloc_u32, alloc_hc);
484 brotli_state.one_shot_output_size =
488 core::cmp::min(available_out, u32::MAX as usize) as u32;
489 let result = ::BrotliDecompressStream(&mut available_in,
490 &mut input_offset,
491 &input[..],
492 &mut available_out,
493 &mut output_offset,
494 &mut output,
495 &mut written,
496 &mut brotli_state);
497 let return_info = BrotliDecoderReturnInfo::new(&brotli_state, result.into(), output_offset);
498 return_info
499}