Direct no-stdlib port of the C brotli decompressor to Rust
no dependency on the Rust stdlib: this library would be ideal for decompressing within a rust kernel among other things.
This will be useful to see how C and Rust compare in an apples-to-apples comparison where the same algorithms and data structures and optimizations are employed.
The current expected performance losses come from
the system also enables all syscalls to be "frontloaded" in the initial generation of a memory pool for the allocator. Afterwards, SECCOMP can be activated or other mechanisms can be used to secure the application, if desired
This library has FFI exports which comply with the original C interfaces. To build them, enter the c directory and just type make there. That will build a small example program and the cdylib with the appropriate ffi in place to link against
the example, called c/main.c shows how to decompress a program using the streaming interface and the nonstreaming interface.
If a nostdlib version is desired, then an unstable rust must be used (to enable the custom panic handler) and then the BrotliDecoderDecompress function is deactivated since that has no facilities for specifying a custom malloc
a customized malloc must be used if a nostdlib build is chosen and additionally the no-stdlib-ffi-binding cargo feature must be set eg
cargo build --features='no-stdlib no-stdlib-ffi-binding' --release
rust
let mut input = brotli_decompressor::Decompressor::new(&mut io::stdin(), 4096 /* buffer size */);
then you can simply read input as you would any other io::Read class
rust
match brotli_decompressor::BrotliDecompress(&mut io::stdin(), &mut io::stdout(), 65536 /* buffer size */) {
Ok(_) => {},
Err(e) => panic!("Error {:?}", e),
}
There are 3 steps to using brotli without stdlib
in Detail
```rust // at global scope declare a MemPool type -- in this case we'll choose the heap to // avoid unsafe code, and avoid restrictions of the stack size
declarestackallocator_struct!(MemPool, heap);
// at local scope, make a heap allocated buffers to hold uint8's uint32's and huffman codes
let mut u8buffer = defineallocatormemorypool!(4096, u8, [0; 32 * 1024 * 1024], heap);
let mut u32buffer = defineallocatormemorypool!(4096, u32, [0; 1024 * 1024], heap);
let mut hcbuffer = defineallocatormemorypool!(4096, HuffmanCode, [0; 4 * 1024 * 1024], heap);
let heapu8allocator = HeapPrealloc::
// At this point no more syscalls are going to be needed since everything can come from the allocators.
// Feel free to activate SECCOMP jailing or other mechanisms to secure your application if you wish.
// Now it's possible to setup the decompressor state let mut brotlistate = BrotliState::new(heapu8allocator, heapu32allocator, heaphc_allocator);
// at this point the decompressor simply needs an input and output buffer and the ability to track // the available data left in each buffer loop { result = BrotliDecompressStream(&mut availablein, &mut inputoffset, &input.slice(), &mut availableout, &mut outputoffset, &mut output.slicemut(), &mut written, &mut brotlistate);
// just end the decompression if result is BrotliResult::ResultSuccess or BrotliResult::ResultFailure
} ```
This interface is the same interface that the C brotli decompressor uses
Also feel free to use custom allocators that invoke Box directly. This example illustrates a mechanism to avoid subsequent syscalls after the initial allocation