Skip to main content

Crate compress_lib

Crate compress_lib 

Source
Expand description

§compress_lib

DEFLATE (RFC 1951), zlib (RFC 1950), and gzip (RFC 1952) for Rust services, with the per-message rules of WebSocket permessage-deflate (RFC 7692).

The engine is sans-IO: the caller pushes input and pulls output through buffers it owns. Memory is fixed when an engine is built and never grows with input, and every decoder enforces an absolute output cap while it decodes, so a decompression bomb costs at most the cap. There is no unsafe code and no third-party runtime dependency.

§Tier 1

Add it with Cargo’s rename, compress = { package = "compress-lib", version = "0.5" }, and code reads compress::. One call each way; decoding is capped at 64 MiB of output by default:

use compress_lib as compress;

let packed = compress::gzip::compress(b"hello, hello, hello");
let body = compress::gzip::decompress(&packed)?;
assert_eq!(body, b"hello, hello, hello");

let z = compress::zlib::compress(b"zlib, which HTTP calls deflate");
let raw = compress::deflate::compress_with(b"raw DEFLATE", compress::Level::FAST);
assert_eq!(compress::decompress(&z)?, b"zlib, which HTTP calls deflate"); // gzip or zlib, detected

let small = compress::Limits::new().with_max_output(1 << 20);
assert_eq!(compress::deflate::decompress_with(&raw, small)?, b"raw DEFLATE");

§WebSocket permessage-deflate

message has the RFC 7692 rules: the sync-flush tail removed and put back, context takeover on or off, window bits 8 to 15, and the output budget checked while inflating.

use compress_lib::message::{Compressor, Decompressor, MessageConfig};

let config = MessageConfig::new().with_window_bits(10);
let mut tx = Compressor::new(config)?;
let mut rx = Decompressor::new(config)?;
let mut wire = Vec::new();
tx.compress_message(b"{\"op\":\"ping\"}", &mut wire)?;
let mut text = Vec::new();
rx.decompress_message(&wire, &mut text, 1 << 20)?;
assert_eq!(text, b"{\"op\":\"ping\"}");

§Streaming

deflate::Inflater and deflate::Deflater take buffers you own and say where they stopped (Status); memory is fixed when they are built, and every decoder limit is checked before output is written.

use compress_lib::deflate::{Deflater, Flush, Inflater, Status};

let mut deflater = Deflater::default();
let mut packed = [0u8; 256];
let p = deflater.compress(b"streamed", &mut packed, Flush::Finish)?;
assert_eq!(p.status, Status::Done);

let mut inflater = Inflater::new().with_window_bits(12)?;
let mut out = [0u8; 64];
let q = inflater.finish(&packed[..p.produced], &mut out)?;
assert_eq!(&out[..q.produced], b"streamed");

Decoder and Encoder take a Format chosen at run time (an HTTP layer picking by Content-Encoding); with the std feature, io wraps any of them around Read and Write.

Every failure is an Error with a matchable ErrorKind and a stable code for logs and metrics. checksum has CRC-32 and Adler-32.

§Status

0.5.0, feature complete for 1.0: raw DEFLATE, zlib, and gzip both ways, the RFC 7692 helpers, format detection, and the std::io adapters. Optimisation, hardening, and the 1.0 audit follow; the complete planned API, each item with its version, is docs/API.md in the repository.

Modules§

checksum
CRC-32 and Adler-32.
deflate
Raw DEFLATE (RFC 1951).
gzip
gzip framing (RFC 1952): a header, raw DEFLATE, and a CRC-32 and length of the data; several members may follow one another.
iostd
std::io adapters over the streaming engines (feature std).
message
Per-message compression for WebSocket permessage-deflate (RFC 7692).
zlib
zlib framing (RFC 1950): a two-byte header, raw DEFLATE, and an Adler-32 of the data.

Structs§

Decoder
A streaming decoder for any Format.
Encoder
A streaming encoder for any Format.
Error
A codec error: its ErrorKind and the input offset where it was found.
Level
A compression level, 0 to 9, with zlib’s meaning: 0 stores without compressing, 1 is the fastest, 9 compresses best, 6 is the default.
Limits
Caps a decoder enforces while it decodes.
Progress
What one streaming call did.

Enums§

ErrorKind
What went wrong, as a stable, matchable value.
Format
A compressed format.
Status
Where a streaming engine stopped.

Constants§

VERSION
Crate version string, populated by Cargo at build time.

Functions§

decompress
Decompresses a complete gzip or zlib stream, detected from its first bytes, with the default Limits.
decompress_with
As decompress, with the given limits.