Modules

Deflate

moduleSource
macOSLinuxWindowswasm

since 0.28.0

#import "Extensions/Deflate";

The DEFLATE compressed format (RFC 1951) with its zlib (RFC 1950) and gzip (RFC 1952) wrappers, in plain Jai. A jaic extension, not official Jai. See docs/extensions/deflate.md.

ExampleOpen in playground ↗
#import "Extensions/Deflate";

bytes := cast([] u8) "hello hello hello hello";
packed := deflate(bytes, .GZIP);
unpacked, ok, error := inflate(packed, .GZIP);
Show moreShow less

Results are allocated with context.allocator and owned by the caller. Errors are static strings.

Procedures#

inflatesince 0.28.0
(data: [] u8, container := Deflate_Container.ZLIB, max_output: s64 = DEFAULT_MAX_OUTPUT) -> (output: [] u8, ok: bool, error: string)

Decompresses data into a new buffer. max_output bounds the result (a compressed bomb fails instead of exhausting memory). A GZIP stream is a single member; bytes after the stream are ignored. On failure the output is empty and error says why.

inflate_intosince 0.28.0
(data: [] u8, output: [] u8, container := Deflate_Container.ZLIB) -> (written: s64, ok: bool, error: string)

Like inflate, into a buffer the caller sized. Fails with "output is too small" when the stream expands past output; nothing is written beyond it.

deflatesince 0.28.0
(data: [] u8, container := Deflate_Container.ZLIB, level: s32 = 6) -> [] u8

Compresses data. level runs from 0 (stored blocks only) to 9 (smallest); 6 is the usual balance. Out-of-range levels are clamped.

crc32since 0.28.0
(data: [] u8, crc: u32 = 0) -> u32

CRC-32 (the checksum of gzip and PNG). To continue over more data, pass the previous result.

adler32since 0.28.0
(data: [] u8, adler: u32 = 1) -> u32

Types#

Constants#

GZIP_MIN_SIZEsince 0.28.0
:: 18

A gzip member is at least this long: 10 bytes of header and 8 of trailer.

MAX_RATIOsince 0.28.0
:: 1032

DEFLATE cannot expand a byte past 1032 bytes of output.

DEFAULT_MAX_OUTPUTsince 0.28.0
:: 1 << 30

inflate refuses to produce more than this many bytes unless told otherwise.