Browse the docs

silk.compress.deflate.

Compress bytes as raw DEFLATE, zlib, or gzip with a bounded LZ77 match table.

When to use#

Use Deflate.encode to produce .gz files, zlib payloads, or raw DEFLATE streams. Select the container with silk.compress.inflate.Format. Use silk.compress.inflate.Decoder to decode them.

Details#

The encoder uses a 32 KiB history window and fixed Huffman codes. It emits deterministic output, with no timestamps or optional gzip headers. The caller owns the output buffer; Deflate.encodedBound gives a size that always fits. A table of 32768 usize entries is allocated through the ambient allocator and released before the call returns. Each input position considers one match of at most 258 bytes.

Import as Deflate with import silk.compress.deflate { Deflate }.

Public declarations: 2.

EncodeError#

silk
pub struct EncodeError

The output buffer was too small. Discard its partial compressed stream.

Field written#

silk
pub written: usize

Bytes written before the buffer filled. No write occurs beyond this offset.

Implementation Debug for EncodeError#

silk
impl Debug for EncodeError

Operation debug#

silk
effect fn debug(self: &Self, formatter: &mut Formatter) -> ()
    ! WriterError
    ? &mut Writer

Writes the number of bytes written before the output buffer filled.

Deflate#

silk
pub struct Deflate

A deterministic LZ77 and fixed-Huffman DEFLATE encoder for complete input buffers.

Implementation Deflate#

silk
impl Deflate

Associated function encodedBound#

silk
pub fn encodedBound(length: usize, format: Format) -> Option<usize>

Returns the most bytes Deflate.encode can write for length input bytes in format, or None if that count does not fit in usize.

Details#

An output buffer of this size never fails with EncodeError. The bound is length + (length + 17) / 8 plus the container: 0 bytes for raw DEFLATE, 6 for zlib, and 18 for gzip. It covers 9 bits per input byte, which no literal or match exceeds, the 3-bit block header, the 7-bit end-of-block code, and the final partial byte. Input of distinct bytes from 144 through 255 reaches it exactly.

Associated function encode#

silk
pub effect fn encode(input: &[u8], output: &mut [u8], format: Format) -> usize
    ! OutOfMemoryError | EncodeError
    ? &mut Allocator

Compresses all of input into the start of output and returns the encoded length.

Details#

Choose raw DEFLATE, zlib, or gzip with silk.compress.inflate.Format. Uses fixed Huffman codes, a 32 KiB match window, and one candidate per input position. It allocates exactly 32768 usize entries through the ambient allocator, excluding allocator overhead. Output is deterministic: gzip uses timestamp zero, no optional fields, and OS byte 255. Checksums and the gzip size trailer are included. No compression levels or preset dictionary are used. Input and output must not overlap, as enforced by their shared and exclusive borrows.

Errors#

EncodeError means the output buffer filled; discard its partial stream. It never occurs when output holds at least Deflate.encodedBound(input.length, format) bytes. OutOfMemoryError means the match table could not be allocated.

Category: Data formats.

Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, wasm32-wasip1, wasm32-wasip2.

View the source of this page