Module: compress.gzip

Source: ./compress/gzip.reef


Overview

compress/gzip - Gzip compression using zlib FFI

This module provides gzip compression/decompression using zlib's gzip functions. The gzip format includes a 10-byte header, compressed data, and 8-byte trailer.

IMPORTANT: This module requires the zlib library (-lz) to be linked.

Both the file-based API (gzopen/gzread/gzwrite, via zlib's own gz* family) and the in-memory API (gzip_compress/gzip_decompress, via zlib's stream API with windowBits = 15 + 16) produce and consume TRUE gzip framing (magic bytes 1f 8b) — output from either path is interoperable with standard gzip tooling (gunzip/zcat).

Usage: import compress.gzip unsafe // Compress a file let gz = gzip_open("/tmp/test.gz", "wb") gzip_write(gz, data, len) gzip_close(gz)

// Decompress a file let gz = gzip_open("/tmp/test.gz", "rb") let bytes_read = gzip_read(gz, buffer, buffer_size) gzip_close(gz) end unsafe


Functions

fn gzopen(path: pointer, mode: pointer): pointer

Zlib gzip FFI functions

fn gzclose(gz: pointer): int

fn gzread(gz: pointer, buf: pointer, len: int): int

fn gzwrite(gz: pointer, buf: pointer, len: int): int

fn gzerror(gz: pointer, errnum: pointer): pointer

fn compressBound(srcLen: int): int

fn zlibVersion(): pointer

In-memory gzip framing: zlib's stream API, with windowBits set to request gzip encapsulation instead of plain zlib framing. The _ suffix on the Init2 entry points is zlib's actual exported symbol name — they take the caller's zlib version string and sizeof(z_stream) so the library can detect an ABI mismatch between the headers a caller compiled against and the .so it links at runtime.

fn deflateInit2_(strm: pointer, level: int, method: int, windowBits: int, memLevel: int, strategy: int, version: pointer, stream_size: int): int

fn deflate(strm: pointer, flush: int): int

fn deflateEnd(strm: pointer): int

fn inflateInit2_(strm: pointer, windowBits: int, version: pointer, stream_size: int): int

fn inflate(strm: pointer, flush: int): int

fn inflateEnd(strm: pointer): int

fn Z_STREAM_SIZE(): int

sizeof(z_stream) on LP64 (verified against the system zlib.h: 14 fields, 112 bytes with natural alignment/padding — see the field-offset table on the FFI struct helpers below). deflateInit2_/inflateInit2_ use this to guard against a caller built against a different z_stream layout.

fn Z_NEXT_IN_OFFSET(): uint64

z_stream field byte offsets (LP64: 8-byte pointers/longs, 4-byte ints, natural alignment). Only the four fields the compress/decompress loops touch directly; msg/state/zalloc/zfree/opaque/data_type/adler/reserved are left zero-initialized (zalloc/zfree/opaque = NULL tells zlib to use its default allocator, which is the standard, supported way to skip providing custom allocation callbacks) and managed internally by zlib.

fn Z_AVAIL_IN_OFFSET(): uint64

fn Z_NEXT_OUT_OFFSET(): uint64

fn Z_AVAIL_OUT_OFFSET(): uint64

fn gzip_alloc_buffer(size: int): [uint8]

Allocate a byte buffer for use with gzip functions

fn gzip_compress_bound(src_len: int): int

Returns the maximum compressed size for gzip format (includes 10-byte header + compressed data + 8-byte trailer)

fn gzip_open(path: string, mode: string): pointer

Open a gzip file mode: "rb" for reading, "wb" for writing, "ab" for append

fn gzip_close(gz: pointer): int

Close a gzip file Returns 0 on success, error code on failure

fn gzip_read(gz: pointer, buf: [uint8], len: int): int

Read from a gzip file Returns number of uncompressed bytes read, or -1 on error

fn gzip_write(gz: pointer, buf: [uint8], len: int): int

Write to a gzip file Returns number of uncompressed bytes written, or -1 on error

fn gzip_error(gz: pointer): string

Get error message for last gzip operation

fn gzip_compress(src: [uint8], src_len: int, dest: [uint8], dest_len: int): int

Compress data into true gzip format (magic bytes 1f 8b, 10-byte header, CRC32 + size trailer) via zlib's stream API.

fn gzip_decompress(src: [uint8], src_len: int, dest: [uint8], dest_len: int): int

Decompress gzip- (or zlib-) framed data via zlib's stream API. windowBits = 47 (15 + 32) auto-detects gzip vs. zlib framing on the input, so this stays a drop-in decompressor for data produced by this module's own gzip_compress(), by the file-based gzip_open/gzip_write path, by real gzip(1)/zlib tooling, or by zlib-framed data.


Generated by reefc doc