Wado
<!-- Auto-generated by: wado doc -f markdown core:zlib --> <!-- Do not edit this file directly. -->

core:zlib

zlib compression and decompression.

A pure Wado port of zlib 1.3.1 (https://github.com/madler/zlib). This code is derived from the original zlib by Jean-loup Gailly and Mark Adler, and is licensed under the zlib License (https://github.com/madler/zlib/blob/develop/LICENSE).

Implements DEFLATE (RFC 1951) and gzip (RFC 1952) fully. RFC 1950 (zlib format) is supported except for preset dictionaries (FDICT); streams with FDICT set return ZlibError::PresetDictionaryNotSupported.

The decoder is annotated against the format specifications, whose text is kept under wado-compiler/ref/ (RFC 1950, RFC 1951, RFC 1952). Section references in the comments below point at those documents. The RFCs define only the bitstream formats, not encoder heuristics, and they ship no conformance vectors, so zlib_test.wado pairs hand-built, spec-anchored decode vectors (verified against a reference zlib) with cross-validation in tests/zlib_interop.rs.

Features:

All compression entry points share the same shape: the input buffer is required, and level and strategy default to Z_DEFAULT_COMPRESSION and Z_DEFAULT_STRATEGY respectively.

Compression:

let input: List<u8> = [...];
let compressed = zlib_compress(&input);                            // zlib, default level/strategy
let fast       = zlib_compress(&input, Z_BEST_SPEED);              // override level only
let best       = zlib_compress(&input, Z_BEST_COMPRESSION);
let gz         = gzip_compress(&input);                            // gzip wrapper
let raw        = deflate_raw(&input);                              // raw DEFLATE, no header/trailer

Decompression:

let z   = inflate_zlib(&compressed)?;                              // RFC 1950
let gz  = inflate_gzip(&gzipped)?;                                 // RFC 1952
let any = uncompress(&data)?;                                      // auto-detect zlib/gzip

Streaming (chunked input):

let mut stream = DeflateStream::new(Z_DEFAULT_COMPRESSION);
stream.update(&chunk1);
stream.update(&chunk2);
let compressed = stream.finish();

Synopsis

let data = b"wado wado wado wado";
let compressed = zlib_compress(&data);
assert uncompress(&compressed) matches { Ok(round) && round == data };

Globals

pub global Z_OK: i32

pub global Z_STREAM_END: i32

pub global Z_ERRNO: i32

pub global Z_STREAM_ERROR: i32

pub global Z_DATA_ERROR: i32

pub global Z_MEM_ERROR: i32

pub global Z_BUF_ERROR: i32

pub global Z_VERSION_ERROR: i32

pub global Z_NO_FLUSH: i32

pub global Z_PARTIAL_FLUSH: i32

pub global Z_SYNC_FLUSH: i32

pub global Z_FULL_FLUSH: i32

pub global Z_FINISH: i32

pub global Z_BLOCK: i32

pub global Z_TREES: i32

pub global Z_NO_COMPRESSION: i32

pub global Z_BEST_SPEED: i32

pub global Z_BEST_COMPRESSION: i32

pub global Z_DEFAULT_COMPRESSION: i32

pub global Z_DEFAULT_STRATEGY: i32

pub global Z_FILTERED: i32

pub global Z_HUFFMAN_ONLY: i32

pub global Z_RLE: i32

pub global Z_FIXED: i32

pub global Z_BINARY: i32

pub global Z_TEXT: i32

pub global Z_UNKNOWN: i32

pub global Z_DEFLATED: i32

pub global MAX_WBITS: i32

pub global ZLIB_FORMAT: i32

pub global RAW_FORMAT: i32

pub global GZIP_FORMAT: i32

pub global AUTO_FORMAT: i32

Format value that asks an InflateStream to auto-detect zlib or gzip framing from the magic bytes.

Functions

pub fn zlib_version() -> String

Returns the zlib version string.

pub fn adler32(adler: u32, buf: &List<u8>, offset: i32, len: i32) -> u32

Computes an Adler-32 checksum over buf[offset..offset+len], starting from adler.

pub fn adler32_init() -> u32

Returns the initial Adler-32 checksum value (1).

pub fn adler32_combine(adler1: u32, adler2: u32, len2: i32) -> u32

Combines two Adler-32 checksums into one for concatenated data.

adler1 and adler2 are the checksums of the first and second segments; len2 is the length of the second segment.

pub fn crc32(crc: u32, buf: &List<u8>, offset: i32, len: i32) -> u32

Computes a CRC-32 checksum over buf[offset..offset+len], starting from crc.

pub fn crc32_init() -> u32

Returns the initial CRC-32 checksum value (0).

pub fn crc32_combine(crc1: u32, crc2: u32, len2: i32) -> u32

Combines two CRC-32 checksums into one for concatenated data.

crc1 and crc2 are the checksums of the first and second segments; len2 is the length of the second segment.

pub fn compress_bound(source_len: i32) -> i32

Returns the maximum compressed size for a given source length.

Uses the larger of the stored-block worst case and the classic compressBound formula to match zlib-rs/zlib-ng behavior.

pub fn inflate_raw(input: &List<u8>) -> List<u8>

Decompresses raw DEFLATE data (no header/checksum).

pub fn inflate_zlib(input: &List<u8>) -> Result<List<u8>, ZlibError>

Decompresses zlib-wrapped data (RFC 1950: 2-byte header + DEFLATE + 4-byte Adler-32).

pub fn deflate_stored(input: &List<u8>) -> List<u8>

Compresses data into raw DEFLATE stored blocks (no actual compression).

pub fn zlib_compress_stored(input: &List<u8>) -> List<u8>

Wraps data in zlib format using stored blocks (no actual compression).

pub fn deflate_huffman(input: &List<u8>) -> List<u8>

Compresses data using DEFLATE with fixed Huffman codes and LZ77 matching.

pub fn deflate_with_level(input: &List<u8>, level: i32, strategy: i32) -> List<u8>

Compresses data using DEFLATE with the given compression level and strategy.

level is 0-9 (0 = stored, 1 = fastest, 9 = best compression). strategy is one of Z_DEFAULT_STRATEGY, Z_FILTERED, Z_HUFFMAN_ONLY, Z_RLE, or Z_FIXED.

pub fn zlib_compress(input: &List<u8>, level: i32 = Z_DEFAULT_COMPRESSION, strategy: i32 = Z_DEFAULT_STRATEGY) -> List<u8>

Compresses data in zlib format (RFC 1950).

level defaults to Z_DEFAULT_COMPRESSION and strategy defaults to Z_DEFAULT_STRATEGY.

pub fn inflate_gzip(input: &List<u8>) -> Result<List<u8>, ZlibError>

Decompresses gzip-wrapped data (RFC 1952).

Supports multi-member gzip streams; member outputs are concatenated.

pub fn gzip_compress(input: &List<u8>, level: i32 = Z_DEFAULT_COMPRESSION, strategy: i32 = Z_DEFAULT_STRATEGY) -> List<u8>

Compresses data in gzip format (RFC 1952) with a minimal header.

level defaults to Z_DEFAULT_COMPRESSION and strategy defaults to Z_DEFAULT_STRATEGY. Use gzip_compress_with_header to set MTIME, file name, comment, and other gzip header fields.

pub fn uncompress(input: &List<u8>, max_output: i32 = i32::MAX) -> Result<List<u8>, ZlibError>

Auto-detects the wrapper format (zlib or gzip) and decompresses.

Returns ZlibError::OutputExceedsMax if the decompressed size exceeds max_output (default: i32::MAX).

pub fn deflate_raw(input: &List<u8>, level: i32 = Z_DEFAULT_COMPRESSION, strategy: i32 = Z_DEFAULT_STRATEGY) -> List<u8>

Compresses data as raw DEFLATE (RFC 1951) with no wrapper header or trailer.

level defaults to Z_DEFAULT_COMPRESSION and strategy defaults to Z_DEFAULT_STRATEGY.

pub fn gzip_compress_with_header(input: &List<u8>, level: i32, header: &GzipHeader) -> List<u8>

Compresses data in gzip format using the given compression level and a caller-supplied header.

pub fn inflate_get_gzip_header(input: &List<u8>) -> Result<GzipHeader, ZlibError>

Parses and returns the gzip header without decompressing the body.

pub fn inflate_sync_find(input: &List<u8>, start: i32) -> i32

Finds the next DEFLATE sync point (00 00 FF FF) at or after start.

Returns the offset of the recovered block, or -1 if no sync point is found. Useful for recovering from corruption in a DEFLATE stream after a sync flush.

Structs

pub struct GzipHeader

A gzip header (RFC 1952) used by gzip_compress_with_header to set optional fields and by inflate_get_gzip_header to report them.

Fields are private.

pub fn new() -> GzipHeader

Creates a header with all fields cleared (os set to 0xFF = unknown).

pub fn text(&self) -> bool

Returns true if the FTEXT flag is set (payload is probably ASCII text).

pub fn time(&self) -> u32

Returns MTIME (modification time as a Unix timestamp; 0 = unset).

pub fn os(&self) -> i32

Returns the OS byte (0xFF = unknown).

pub fn extra(&self) -> List<u8>

Returns the FEXTRA payload (empty = none).

pub fn name(&self) -> String

Returns the FNAME (original file name; empty = none).

pub fn comment(&self) -> String

Returns the FCOMMENT (empty = none).

pub fn hcrc(&self) -> bool

Returns true if the FHCRC flag is set (header CRC16 is present).

pub fn set_text(&mut self, text: bool)

Sets the FTEXT flag, indicating the payload is probably ASCII text.

pub fn set_time(&mut self, time: u32)

Sets MTIME (modification time as a Unix timestamp; 0 = unset).

pub fn set_os(&mut self, os: i32)

Sets the OS byte (0xFF = unknown).

pub fn set_extra(&mut self, extra: &List<u8>)

Sets the FEXTRA payload. An empty array clears the field.

pub fn set_name(&mut self, name: String)

Sets the FNAME (original file name). An empty string clears the field.

pub fn set_comment(&mut self, comment: String)

Sets the FCOMMENT. An empty string clears the field.

pub fn set_hcrc(&mut self, hcrc: bool)

Enables the FHCRC flag, which appends a 16-bit CRC of the header.

pub struct DeflateStream

Streaming deflate compressor.

Buffer chunks via update, then call finish once to emit the compressed output. compress is a single-shot variant that compresses one input without buffering. The output wrapper is selected by format (defaults to ZLIB_FORMAT).

Fields are private.

pub fn new(level: i32) -> DeflateStream

Creates a stream that emits zlib format at the given compression level using Z_DEFAULT_STRATEGY.

pub fn new_with_strategy(level: i32, strategy: i32) -> DeflateStream

Creates a zlib-format stream with an explicit strategy.

pub fn new_with_format(level: i32, format: i32) -> DeflateStream

Creates a stream with the given output format (ZLIB_FORMAT, RAW_FORMAT, or GZIP_FORMAT).

pub fn new_full(level: i32, strategy: i32, format: i32) -> DeflateStream

Creates a stream with explicit level, strategy, and format.

pub fn set_header(&mut self, header: &GzipHeader)

Attaches a custom gzip header. The header is only emitted when the stream's format is GZIP_FORMAT; it is ignored for the zlib and raw formats.

pub fn params(&mut self, level: i32, strategy: i32)

Changes the compression level and strategy for subsequent input. The new values take effect when finish or compress is called.

pub fn bound(&self, source_len: i32) -> i32

Returns an upper bound on the compressed size for source_len input bytes (delegates to compress_bound).

pub fn pending(&self) -> i32

Returns the number of bytes currently buffered awaiting compression.

pub fn get_total_in(&self) -> i32

Returns the cumulative number of input bytes consumed across all finish/compress calls.

pub fn get_total_out(&self) -> i32

Returns the cumulative number of output bytes produced across all finish/compress calls.

pub fn reset(&mut self)

Discards buffered input and counters so the stream can be reused. level, strategy, format, and any custom gzip header are kept.

pub fn copy(&self) -> DeflateStream

Returns an independent copy of this stream, including buffered input and any attached gzip header.

pub fn update(&mut self, chunk: &List<u8>)

Appends chunk to the input buffer. Panics if the stream has already been finished.

pub fn finish(&mut self) -> List<u8>

Compresses all buffered input and returns the wrapped output. Marks the stream as finished; further update/finish/compress calls panic until reset is invoked.

pub fn compress(&mut self, input: &List<u8>) -> List<u8>

Compresses input in one shot, bypassing the internal buffer. Marks the stream as finished.

pub struct InflateStream

Streaming inflate decompressor.

Buffer compressed chunks via update, then call finish once to produce the decompressed output. decompress is a single-shot variant.

Fields are private.

pub fn new() -> InflateStream

Creates a stream that expects zlib-wrapped input.

pub fn new_with_format(format: i32) -> InflateStream

Creates a stream for the given input format (ZLIB_FORMAT, RAW_FORMAT, GZIP_FORMAT, or AUTO_FORMAT).

pub fn get_total_in(&self) -> i32

Returns the cumulative number of input bytes consumed across all finish calls.

pub fn get_total_out(&self) -> i32

Returns the cumulative number of output bytes produced across all finish calls.

pub fn reset(&mut self)

Discards buffered input and counters so the stream can be reused. format is preserved.

pub fn copy(&self) -> InflateStream

Returns an independent copy of this stream, including buffered input.

pub fn update(&mut self, chunk: &List<u8>)

Appends chunk to the buffered compressed input.

pub fn finish(&mut self) -> Result<List<u8>, ZlibError>

Decompresses the buffered input and clears the buffer. The stream may be reused for further input.

pub fn decompress(&self, input: &List<u8>) -> Result<List<u8>, ZlibError>

Decompresses input in one shot using the stream's format. Does not touch the internal buffer.

Enums

pub enum ZlibError

Error type for zlib/gzip decompression operations.

DataTooShort

InvalidHeader

InvalidMagicNumber

UnsupportedCompressionMethod

PresetDictionaryNotSupported

Adler32Mismatch

Crc32Mismatch

TruncatedExtraField

HeaderExtendsPastEnd

TruncatedTrailer

IsizeMismatch

OutputExceedsMax

InvalidDeflateData