#reference #bindings #api-bindings #scratch #byte #input

lzfse

Rust bindings for the LZFSE reference implementation

2 unstable releases

Uses old Rust 2015

0.2.0 Nov 14, 2024
0.1.0 Jul 26, 2020

#266 in Compression

Download history 3/week @ 2024-08-26 35/week @ 2024-09-23 13/week @ 2024-10-07 9/week @ 2024-10-14 12/week @ 2024-10-21 8/week @ 2024-10-28 37/week @ 2024-11-04 200/week @ 2024-11-11 53/week @ 2024-11-18 30/week @ 2024-11-25 213/week @ 2024-12-02 370/week @ 2024-12-09

674 downloads per month
Used in dmgwiz

MIT license

145KB
2.5K SLoC

C 2.5K SLoC // 0.3% comments Rust 260 SLoC // 0.1% comments Shell 6 SLoC

lzfse-rs

Build and Testcrates.io

https://img.shields.io/crates/v/lzfse

Rust bindings for the LZFSE reference implementation at https://github.com/lzfse/lzfse.

Documentation

# Cargo.toml
[dependencies]
lzfse = "0.2.0"

lib.rs:

Rust bindings for the LZFSE reference implementation

https://github.com/lzfse/lzfse

Example

use lzfse::{decode_buffer, encode_buffer};

let input: &[u8] = &[0xF, 0xE, 0xE, 0xD, 0xF, 0xA, 0xC, 0xE,
                     0xF, 0xE, 0xE, 0xD, 0xF, 0xA, 0xC, 0xE,
                     0xF, 0xE, 0xE, 0xD, 0xF, 0xA, 0xC, 0xE,
                     0xF, 0xE, 0xE, 0xD, 0xF, 0xA, 0xC, 0xE,
                     0xF, 0xE, 0xE, 0xD, 0xF, 0xA, 0xC, 0xE,
                     0xF, 0xE, 0xE, 0xD, 0xF, 0xA, 0xC, 0xE];

// compression
// in the worst case lzfse will fallback to return the input uncompressed
// and add a magic header to indicate this. that requires 12 bytes (see lzfse_encode.c)
let max_outlen = input.len() + 12;
let mut compressed = vec![0; max_outlen];

let bytes_out = encode_buffer(&input[..], &mut compressed[..]).unwrap();
assert!(bytes_out < input.len());

// decompression
// need to allocate 1 byte more since lzfse returns input.len() if the buffer is too small
let mut uncompressed = vec![0; input.len() + 1];
let bytes_in = decode_buffer(&compressed[0..bytes_out], &mut uncompressed[..]).unwrap();

assert_eq!(bytes_in, input.len());
assert_eq!(input[..], uncompressed[..bytes_in]);

Scratch Buffer Example

The encode_buffer_scratch and decode_buffer_scratch functions allow for encoding and decoding using a scratch buffer to avoid allocations by the underlying library. This makes it possible to use the Rust crate with no_std and a custom allocator.

use lzfse::{encode_buffer_scratch, decode_buffer_scratch, Scratch};

let input: &[u8] = &[0xF, 0xE, 0xE, 0xD, 0xF, 0xA, 0xC, 0xE,
                     0xF, 0xE, 0xE, 0xD, 0xF, 0xA, 0xC, 0xE,
                     0xF, 0xE, 0xE, 0xD, 0xF, 0xA, 0xC, 0xE,
                     0xF, 0xE, 0xE, 0xD, 0xF, 0xA, 0xC, 0xE,
                     0xF, 0xE, 0xE, 0xD, 0xF, 0xA, 0xC, 0xE,
                     0xF, 0xE, 0xE, 0xD, 0xF, 0xA, 0xC, 0xE];

let mut compressed = vec![0; input.len() + 12];
let mut scratch = Scratch::new();

let bytes_out = encode_buffer_scratch(&input, &mut compressed, &mut scratch).unwrap();
assert!(bytes_out < input.len());

let mut uncompressed = vec![0; input.len() + 1];
let bytes_in = decode_buffer_scratch(&compressed[0..bytes_out], &mut uncompressed, &mut scratch).unwrap();

assert_eq!(bytes_in, input.len());
assert_eq!(input[..], uncompressed[..bytes_in]);

Dependencies

~0–300KB