stdlib.compress¶
Compression
Generated from
v0.60.1. 9 source files, 93 documented symbols.
brotli.xi¶
fn brotli_compress(data: &Vec[UInt8]) -> Vec[UInt8]¶
Compress at the default quality (11) and window exponent (20).
fn brotli_compress_quality(data: &Vec[UInt8], quality: Int) -> Vec[UInt8]¶
Compress with an explicit quality (0-11, clamped). The quality is recorded in the header and does not change the payload shape.
fn brotli_compress_window(data: &Vec[UInt8], window: Int) -> Vec[UInt8]¶
Compress with an explicit window size in bytes. The base-2 logarithm is stored in the header, clamped to 10..24 (window 1KiB..16MiB).
fn brotli_decompress(data: &Vec[UInt8]) -> Result[Vec[UInt8], Str]¶
Decompress a brotli_compress frame. Validates magic and the length trailer, then decompresses the payload. Returns Err on malformed input or a size mismatch.
fn brotli_decompress_stream(reader: Int, writer: Int) -> Result[Int, Str]¶
Stream-decompress between two file descriptors: reads all bytes from
reader, decompresses them, writes the result towriter, and returns the number of bytes written. Errors surface as Err.
compress.xi¶
type GzipCompressor¶
=== Gzip ===
| Field | Type |
|---|---|
level |
Int |
fn new() -> GzipCompressor¶
Streaming gzip compressor with the default level.
fn with_level(level: Int) -> GzipCompressor¶
Streaming gzip compressor with a level 0..9.
fn gzip_compress(data: &Vec[UInt8]) -> Result[Vec[UInt8], Str]¶
=== Gzip functions === Aggregate facade: delegate to the real sublib implementations (xiom.compress.gzip). The earlier RLE-based duplicates trapped on empty input (requires: data.len() > 0) and were not gzip-compatible.
- Postcondition:
result is Ok(_) => result.len() >= 18
fn gzip_compress_level(data: &Vec[UInt8], level: Int) -> Result[Vec[UInt8], Str]¶
Gzip-compress with a level 0..9; Err on failure.
- Postcondition:
result is Ok(_) => result.len() >= 18
fn gzip_decompress_capped(data: &Vec[UInt8], max_out: Int) -> Result[Vec[UInt8], Str]¶
Capped variant: hard ceiling on decompressed size (bomb guard).
fn deflate_decompress_capped(data: &Vec[UInt8], max_out: Int) -> Result[Vec[UInt8], Str]¶
Capped variant: hard ceiling on decompressed size (bomb guard).
fn gzip_decompress(data: &Vec[UInt8]) -> Result[Vec[UInt8], Str]¶
Decompress gzip data; Err on malformed input.
- Postcondition:
result is Ok(_) => result.len() >= 0
fn deflate_compress(data: &Vec[UInt8]) -> Result[Vec[UInt8], Str]¶
=== Deflate / Raw ===
- Postcondition:
result is Ok(_) => result.len() >= 0
fn deflate_compress_level(data: &Vec[UInt8], level: Int) -> Result[Vec[UInt8], Str]¶
Deflate-compress with a level 0..9; Err on failure.
- Postcondition:
result is Ok(_) => result.len() >= 0
fn deflate_decompress(data: &Vec[UInt8]) -> Result[Vec[UInt8], Str]¶
Decompress raw deflate data; Err on malformed input.
- Postcondition:
result is Ok(_) => result.len() >= 0
fn zlib_compress(data: &Vec[UInt8]) -> Result[Vec[UInt8], Str]¶
=== Zlib ===
- Postcondition:
result is Ok(_) => result.len() >= 6
fn zlib_compress_level(data: &Vec[UInt8], level: Int) -> Result[Vec[UInt8], Str]¶
Zlib-compress with a level 0..9; Err on failure.
- Postcondition:
result is Ok(_) => result.len() >= 6
fn zlib_decompress(data: &Vec[UInt8]) -> Result[Vec[UInt8], Str]¶
Decompress zlib data; Err on malformed input.
- Postcondition:
result is Ok(_) => result.len() >= 0
fn brotli_compress(data: &Vec[UInt8]) -> Result[Vec[UInt8], Str]¶
=== Brotli ===
- Postcondition:
result is Ok(_) => result.len() >= 8
fn brotli_compress_level(data: &Vec[UInt8], quality: Int) -> Result[Vec[UInt8], Str]¶
Brotli-compress with a quality 0..11; Err on failure.
- Postcondition:
result is Ok(_) => result.len() >= 8
fn brotli_decompress(data: &Vec[UInt8]) -> Result[Vec[UInt8], Str]¶
Decompress Brotli data; Err on malformed input.
- Postcondition:
result is Ok(_) => result.len() >= 0
fn lz4_compress(data: &Vec[UInt8]) -> Result[Vec[UInt8], Str]¶
=== LZ4 ===
- Postcondition:
result is Ok(_) => result.len() >= 1
fn lz4_decompress(data: &Vec[UInt8]) -> Result[Vec[UInt8], Str]¶
Decompress an LZ4 block; Err on malformed input.
- Postcondition:
result is Ok(_) => result.len() >= 0
fn snappy_compress(data: &Vec[UInt8]) -> Result[Vec[UInt8], Str]¶
=== Snappy ===
- Postcondition:
result is Ok(_) => result.len() >= 1
fn snappy_decompress(data: &Vec[UInt8]) -> Result[Vec[UInt8], Str]¶
Decompress Snappy data; Err on malformed input.
- Postcondition:
result is Ok(_) => result.len() >= 0
fn compression_ratio(original: Int, compressed: Int) -> Float64¶
compressed/original ratio (0 when original is 0).
- Precondition:
original >= 0 - Precondition:
compressed >= 0 - Postcondition:
result >= 0
fn is_compressed(data: &Vec[UInt8]) -> Bool¶
True when the bytes start with a recognized compression header.
- Precondition:
data.len() >= 0
fn detect_format(data: &Vec[UInt8]) -> Str¶
Name of the detected compression format, or "" when unrecognized.
- Precondition:
data.len() >= 0
fn lz77_compress(data: &Vec[UInt8], window: Int) -> Vec[Int]¶
LZ77 compression using a simple sliding-window search. Emits tokens as flat triples in a Vec[Int]: [literal_len, match_offset, match_length, ...]. A match_offset of 0 means no match found; literal_len bytes follow directly. Window size limits the backward search distance. Complexity: O(n * window), n = data length.
fn lz77_decompress(tokens: &Vec[Int]) -> Result[Vec[UInt8], Str]¶
LZ77 decompression of token triples produced by lz77_compress. Each triple: [literal_len, data, match_length_or_zero]. If literal_len == 0: data = match_offset, third = match_length. If literal_len == 1: data = literal byte, third = 0. Complexity: O(t), t = number of tokens.
fn rle_encode_bytes(data: &Vec[UInt8]) -> Vec[UInt8]¶
Simple byte-level run-length encoding. Format: [count: UInt8, byte: UInt8] for runs of identical bytes. Count represents the number of repetitions (1 means 1 byte). Differs from rle_encode which uses control-byte format with bit 7 markers. Complexity: O(n), n = data length.
fn huffman_freqs(data: &Vec[UInt8]) -> Vec[Int]¶
Builds a 256-slot frequency table for Huffman coding. Each slot i contains the count of byte value i in the input. Complexity: O(n), n = data length.
fn compress_gzip_str(s: Str) -> Result[Vec[UInt8], Str]¶
Compresses a UTF-8 string using gzip. Converts Str to bytes, then wraps gzip_compress. Complexity: O(n), n = string length.
fn decompress_gzip_str(data: &Vec[UInt8]) -> Result[Str, Str]¶
Decompresses gzip data to a UTF-8 string. Wraps gzip_decompress and converts the result to Str. Complexity: O(n), n = compressed data length.
fn compress_ratio(original: Int, compressed: Int) -> Float64¶
Alias for compression_ratio. Returns original / compressed as Float64. Complexity: O(1).
deflate.xi¶
fn deflate_compress(data: &Vec[UInt8]) -> Vec[UInt8]¶
Compress with the RFC 1951 fixed-Huffman encoding (matching the deterministic reference behavior of zlib Z_FIXED for greedy matchers).
fn deflate_compress_level(data: &Vec[UInt8], level: Int) -> Vec[UInt8]¶
level 0 => STORED blocks (real, uncompressed); levels 1-9 => FIXED Huffman blocks.
fn deflate_decompress(data: &Vec[UInt8]) -> Result[Vec[UInt8], Str]¶
Decompress raw deflate data; Err on malformed input.
fn deflate_decompress_capped(data: &Vec[UInt8], max_out: Int) -> Result[Vec[UInt8], Str]¶
Decompress a real RFC 1951 stream with a hard output ceiling.
fn deflate_bound(len: Int) -> Int¶
Worst-case compressed size for
leninput bytes under the fixed-Huffman encoder: per byte at most 9 code bits + headers + match overhead is always below this bound. O(1).
fn deflate_compress_stream(reader: Int, writer: Int) -> Result[Int, Str]¶
Stream-compress between two file descriptors: reads all bytes from
reader, compresses them, writes the container towriter, and returns the number of bytes written. Errors (empty input, read/write failures) are returned as Err.
gzip.xi¶
fn gzip_crc32(data: &Vec[UInt8]) -> UInt32¶
Compute the CRC-32 checksum (IEEE 802.3 polynomial) of
data. O(n).
fn gzip_header_new(mtime: Int, os: Int) -> Vec[UInt8]¶
Build a bare gzip header: magic (1F 8B), method (08), flags (00), MTIME (
mtime, LE, may be 0), XFL (00) and OS (os, masked to 8 bits; use 255 for unknown, 3 for Unix). Returns 10 bytes.
fn gzip_compress(data: &Vec[UInt8]) -> Vec[UInt8]¶
Wrap
datain a gzip stream: header (mtime 0, OS 255) + DEFLATE payload + CRC32 trailer + ISIZE trailer.
fn gzip_decompress(data: &Vec[UInt8]) -> Result[Vec[UInt8], Str]¶
Unwrap and validate a gzip stream. Parses the optional header fields, decompresses the payload, and verifies the CRC32 and ISIZE trailer values. Returns Err on any mismatch or malformed input.
fn gzip_decompress_capped(data: &Vec[UInt8], max_out: Int) -> Result[Vec[UInt8], Str]¶
Decompress with a hard ceiling on output size (decompression-bomb guard). The header's declared ISIZE is rejected up-front when it exceeds the cap; the payload cap is enforced inside the deflate stages.
fn gzip_compress_file(path: Str) -> Result[Unit, Str]¶
Gzip a file to
<path>.gz. Returns Err when the source cannot be read or the target cannot be written.
fn gzip_decompress_file(path: Str) -> Result[Vec[UInt8], Str]¶
Gunzip a file into raw bytes. Returns Err on read failures or invalid gzip data.
fn gzip_validate(data: &Vec[UInt8]) -> Bool¶
Sanity-check magic, method, and the minimum trailer footprint. O(1).
huffman.xi¶
type HuffmanTree¶
Huffman tree: per-symbol code lengths and canonical codes (index = byte value).
| Field | Type |
|---|---|
code_lengths |
Vec[Int] |
codes |
Vec[Int] |
max_len |
Int |
fn huffman_canonical(codelengths: &Vec[Int]) -> Vec[Int]¶
Derive the canonical prefix-free codes from per-symbol code lengths. Canonical order: shorter codes first, ties by symbol index. O(256).
fn huffman_build(frequencies: &Vec[Int]) -> HuffmanTree¶
Build a Huffman tree from a 256-slot frequency table (index = byte value). The tree holds per-symbol code lengths and canonical codes. Empty or single-symbol inputs are handled (single symbol gets length 1). Complexity: O(n^2) in the number of distinct symbols (<= 256).
fn huffman_code_lengths(tree: HuffmanTree) -> Vec[Int]¶
Extract the per-symbol code lengths as a fresh 256-slot Vec[Int].
fn huffman_table_new(codelengths: &Vec[Int]) -> Vec[Int]¶
Pack a flat canonical decode table: entries [0..255] are the canonical codes, entries [256..511] are the matching code lengths. O(512).
fn huffman_encode_symbol(tree: HuffmanTree, sym: Int) -> Vec[Bool]¶
Return the code bits of one symbol as booleans, MSB first. An uncovered symbol yields an empty Vec (documented).
fn huffman_encode(tree: HuffmanTree, data: &Vec[UInt8]) -> Vec[UInt8]¶
Pack
datainto an MSB-first bit stream using the tree's codes. O(n * avg code length). Requires the tree to cover every input byte.
fn huffman_decode(tree: HuffmanTree, bits: &Vec[UInt8], len: Int) -> Result[Vec[UInt8], Str]¶
Decode exactly
lenbits of the MSB-first stream back into bytes. Returns Err on invalid bit patterns or stream exhaustion.
fn huffman_compress(data: &Vec[UInt8]) -> Vec[UInt8]¶
One-shot Huffman compression: builds the frequency table and tree, packs the bits, and emits the self-describing container (header documented at the top of this module). Empty input yields a valid empty container.
fn huffman_decompress(data: &Vec[UInt8]) -> Result[Vec[UInt8], Str]¶
Decompress Huffman-coded data; Err on malformed input.
fn huffman_decompress_capped(data: &Vec[UInt8], max_out: Int) -> Result[Vec[UInt8], Str]¶
Decompress with a hard ceiling on output size. The container's declared length is rejected up-front when it exceeds the cap; the decode loop re-checks per symbol, so a lying header cannot allocate past the cap.
fn rle_compress(data: &Vec[UInt8]) -> Vec[UInt8]¶
Byte-level run-length encoding: [count: UInt8, byte: UInt8] pairs for runs of identical bytes (count 1..255; runs longer than 255 split). Complexity: O(n), n = data length.
fn rle_decompress(data: &Vec[UInt8]) -> Result[Vec[UInt8], Str]¶
Undo byte-level run-length encoding (rle_compress format). Returns Err on a truncated pair (odd trailing byte) or a zero count.
lz4.xi¶
fn lz4_compress(data: &Vec[UInt8]) -> Vec[UInt8]¶
Compress
datainto a full frame (magic, FLG/BD, block size, payload).
fn lz4_compress_frame(data: &Vec[UInt8]) -> Vec[UInt8]¶
Alias for lz4_compress: the frame IS the full container in this module.
fn lz4_compress_hc(data: &Vec[UInt8]) -> Vec[UInt8]¶
Compress
datainto a frame using the high-compression block encoder.
fn lz4_bound(len: Int) -> Int¶
Worst-case compressed size for
leninput bytes (literals expand by at most 1 byte per 255 input bytes plus token/header overhead). O(1).
fn lz4_decompress(data: &Vec[UInt8]) -> Result[Vec[UInt8], Str]¶
Decompress an LZ4 block; Err on malformed input.
fn lz4_decompress_capped(data: &Vec[UInt8], max_out: Int) -> Result[Vec[UInt8], Str]¶
Decompress with a hard ceiling on total output size (bomb guard). Checked after each block's expansion.
fn lz4_decompress_frame(data: &Vec[UInt8]) -> Result[Vec[UInt8], Str]¶
Alias for lz4_decompress: parses and decompresses an lz4_compress frame.
fn lz4_compress_block(data: &Vec[UInt8]) -> Vec[UInt8]¶
Compress a raw block payload (no frame header) using the LZ4 block format. Greedy search with a 4KiB window; matches need length >= 4. Complexity: O(n * 4096).
fn lz4_compress_hc_block(data: &Vec[UInt8]) -> Vec[UInt8]¶
High-compression block variant: 64KiB search window, longer match scan. Uses the same block format, so lz4_decompress_block decodes either output.
fn lz4_decompress_block(data: &Vec[UInt8]) -> Result[Vec[UInt8], Str]¶
Decompress a raw LZ4 block. Returns Err on truncation, an invalid offset, or malformed extended lengths.
lz77.xi¶
fn lz77_compress(data: &Vec[UInt8]) -> Vec[UInt8]¶
Compress
datawith a greedy 32KiB sliding-window search into the control-byte token stream documented above. O(n * 32768).
fn lz77_decompress(data: &Vec[UInt8]) -> Result[Vec[UInt8], Str]¶
Decompress LZ77 data; Err on malformed input.
fn lz77_decompress_capped(data: &Vec[UInt8], max_out: Int) -> Result[Vec[UInt8], Str]¶
Decompress with a hard ceiling on output size. Every expansion step is checked BEFORE writing, so a hostile token stream cannot allocate beyond the cap even momentarily. Returns Err("lz77: output exceeds cap") when exceeded.
fn lz77_find_longest_match(data: &Vec[UInt8], pos: Int, window: Int) -> (Int, Int)¶
Locate the longest match for the byte at
possearching back at mostwindowpositions. Returns (length, distance); (0, 0) when no match of length >= 3 exists. Out-of-range positions yield (0, 0). Complexity: O(window * match_length).
fn lz77_token_encode(length: Int, distance: Int) -> Int¶
Pack a (length, distance) pair into a single token: (length << 16) | distance. Both fields are clamped to 16 bits (0..65535). Pure-Int token.
fn lz77_token_decode(token: Int) -> (Int, Int)¶
Unpack a token produced by lz77_token_encode into (length, distance).
snappy.xi¶
fn snappy_uncompressed_len(data: &Vec[UInt8]) -> Result[Int, Str]¶
Read the varint uncompressed length that opens a raw snappy stream. Returns Err on a malformed header.
fn snappy_validate(data: &Vec[UInt8]) -> Bool¶
Sanity-check the varint header of a raw snappy stream. O(1)..O(10).
fn snappy_max_compressed_len(len: Int) -> Int¶
Upper bound on the compressed size of
leninput bytes (varint + up to 1/6 expansion for incompressible data). O(1).
fn snappy_compress(data: &Vec[UInt8]) -> Vec[UInt8]¶
Compress
datainto a raw snappy stream (varint length + elements). Greedy matches with a 4KiB window; literals batch until a match is found. Complexity: O(n * 4096).
fn snappy_decompress(data: &Vec[UInt8]) -> Result[Vec[UInt8], Str]¶
Decompress Snappy data; Err on malformed input.
fn snappy_decompress_capped(data: &Vec[UInt8], max_out: Int) -> Result[Vec[UInt8], Str]¶
Decompress with a hard ceiling on output size (bomb guard). The varint-declared length is rejected up-front when it exceeds the cap; every literal/copy element re-checks before writing.
fn snappy_compress_frame(data: &Vec[UInt8]) -> Vec[UInt8]¶
Compress
datainto a framed stream: varint(compressed payload length) followed by the raw snappy stream (which itself starts with the varint uncompressed length). The frame is self-delimiting.
fn snappy_decompress_frame(data: &Vec[UInt8]) -> Result[Vec[UInt8], Str]¶
Decompress a snappy_compress_frame stream. Validates the outer varint against the remaining bytes. Returns Err on any mismatch or malformed data.
zlib.xi¶
fn zlib_header_new(level: Int) -> Vec[UInt8]¶
Build the CMF/FLG header bytes for a level (0-9, clamped). Returns 2 bytes.
fn zlib_adler32(data: &Vec[UInt8]) -> UInt32¶
Compute the Adler-32 checksum of
data. O(n).
fn zlib_compress(data: &Vec[UInt8]) -> Vec[UInt8]¶
Wrap
datain a zlib stream at the default level (6).
fn zlib_compress_level(data: &Vec[UInt8], level: Int) -> Vec[UInt8]¶
Wrap
datain a zlib stream with an explicit level (0-9, clamped).
fn zlib_decompress(data: &Vec[UInt8]) -> Result[Vec[UInt8], Str]¶
Unwrap and validate a zlib stream: header method/checksum, payload decompression, and Adler32 trailer verification. Returns Err on any mismatch or malformed input.
fn zlib_validate(data: &Vec[UInt8]) -> Bool¶
Sanity-check the zlib header (method and CMF/FLG checksum). O(1).