Crates.io | heatshrink-bin |
lib.rs | heatshrink-bin |
version | 0.4.1 |
source | src |
created_at | 2023-11-13 20:49:08.742517 |
updated_at | 2023-11-13 20:49:08.742517 |
description | A minimal library implementing the heatshrink compression algorithm for no_std environments |
homepage | |
repository | https://github.com/jcdubois/heatshrink-rs |
max_upload_size | |
id | 1034023 |
size | 23,152 |
Minimal no_std implementation of Heatshrink compression & decompression for embedded systems
This library is a "crude" port to RUST of the original heatshrink C library.
The port is limited to the "static" version of the C library which means heatshrink parameters are hardcoded to window_sz2 = 8 and lookahead_sz2 = 4.
It is not (yet?) possible to choose its own compression parameters (mainly because it is a no_std library and therefore it does not support dynamic allocation).
Allocate a heatshrink encoder or heatshrink decoder state machine using
either HeatshrinkEncoder::new
or HeatshrinkDecoder::new
. You can also
reset an existing state machine by calling the reset
function on the state
machine.
Use sink
to sink an input buffer into the state machine. In the
returned result you get a CR code and the amount of bytes that were actually
consumed (If 0 bytes were conusmed, the buffer is full.).
Use poll
to move output from the state machine into an output
buffer. In the returned result you get a CR code and the amount bytes
that were writen to the provided buffer.
Repeat steps 2 and 3 to stream data through the state machine. Since it's doing data compression, the input and output sizes can vary significantly. Looping will be necessary to buffer the input and output as the data is processed.
finish
to notify
the state machine that no more input is available. The return value from
finish
will indicate whether any output remains. if so, call poll
to
get more.Continue calling finish
and poll
ing to flush remaining output until
finish
indicates that the output has been exhausted.
Sinking more data after finish
has been called will not work without
calling reset
on the state machine.
No configuration is needed (for now) on this RUST implementation as parameters are not user defined (they are hardcoded).
On the cargo build command you can choose to enable the lookup table to speed up the compression phase by selecting --features "heatshrink-use-index" on the cargo command line.
heatshrink is based on LZSS, since it's particularly suitable for compression in small amounts of memory. It can use an optional, small index to make compression significantly faster, but otherwise can run in under 100 bytes of memory. The index currently adds 2^(window size+1) bytes to memory usage for compression, and temporarily allocates 512 bytes on the stack during index construction (if the index is enabled).
For more information, see the blog post for an overview.