nish/compress/zlib

The zlib format (RFC 1950) around nish/compress/deflate: a two-byte header, the DEFLATE stream and the Adler-32 of the data, big-endian. What HTTP's Content-Encoding: deflate means (RFC 9110 §8.4.1.2), and what PNG and many protocols carry.

import {
  ZLIB_ERR_ADLER,
  ZLIB_ERR_DICTIONARY,
  ZLIB_ERR_HEADER,
  ZlibCompressor,
  ZlibDecompressor,
  zlibCompress,
  zlibDecompress,
} from "nish/compress/zlib";
import { DEFLATE_DEFAULT } from "nish/compress/deflate";
import { ZlibCompressor, ZlibDecompressor } from "nish/compress/zlib";

const z = new ZlibCompressor(DEFLATE_DEFAULT);
const end: i32 = z.compress(src, 0, n, out, 0); // -1: no room
const got: i32 = new ZlibDecompressor().decompress(out, 0, end, back, 0, n); // < 0: an error
Allocated once

The objects allocate their tables once and nothing per call; the free functions build one for a single call.

What is written and what is refused. The header written is 0x78 and the FLEVEL of the level, with no preset dictionary. Reading refuses a method other than 8, a window over 32 KB, a header whose check fails (ZLIB_ERR_HEADER) and a preset dictionary (ZLIB_ERR_DICTIONARY, since none can be supplied); a wrong Adler-32 is ZLIB_ERR_ADLER, and bytes after the trailer are DEFLATE_ERR_TRAILING.

Written from RFC 1950.

Functions

zlibCompress

ZlibCompressor.compress at level, building the compressor for this call alone.

const zlibCompress: (
  src: u8[],
  off: i32,
  len: i32,
  out: u8[],
  at: i32,
  level: i32,
) => i32
srcu8[]required

The array that holds the data to compress.

offi32required

Where in src the data starts.

leni32required

How many bytes of data.

outu8[]required

The caller's array the stream is written into.

ati32required

Where in out the stream starts.

leveli32required

How hard to compress, 0 to 9; another level panics.

returnsi32

The index in out one past the stream's last byte, or DEFLATE_NO_ROOM, as ZlibCompressor.compress answers.

zlibDecompress

ZlibDecompressor.decompress, building the decompressor for this call alone.

const zlibDecompress: (
  src: u8[],
  off: i32,
  len: i32,
  out: u8[],
  at: i32,
  limit: i32,
) => i32
srcu8[]required

The array that holds the zlib stream.

offi32required

Where in src the stream starts.

leni32required

How many bytes the stream is, trailer included.

outu8[]required

The caller's array the data is written into.

ati32required

Where in out the data starts.

limiti32required

The most bytes of data to accept.

returnsi32

The index in out one past the data's last byte, or a negative code, as ZlibDecompressor.decompress answers.

Classes

ZlibCompressor

A zlib compressor at one level, allocated once. Not shared between threads.

class ZlibCompressor
Member
new ZlibCompressor(level: i32) A compressor at level, 0 to 9; another level panics.
compress(src: u8[], off: i32, len: i32, out: u8[], at: i32): i32 Compresses src[off .. off + len) into out from at as one zlib stream and answers where it ends, or DEFLATE_NO_ROOM (-1) when out cannot hold it; compressBound(len) bytes always can. A window outside its array panics. Allocates nothing.

ZlibDecompressor

A zlib decompressor, allocated once. Not shared between threads.

class ZlibDecompressor
Member
new ZlibDecompressor() Builds the tables.
decompress(src: u8[], off: i32, len: i32, out: u8[], at: i32, limit: i32): i32 Decompresses the zlib stream src[off .. off + len) into out from at, writing at most limit bytes and never past out's end, and answers where the output ends, or a negative ZLIB_ERR_* or DEFLATE_ERR_*. A window outside its array, or a negative limit, panics. Allocates nothing.

Constants

-13

ZLIB_ERR_ADLER: i32

The trailer's Adler-32 is not the Adler-32 of the data.

-14

ZLIB_ERR_DICTIONARY: i32

The stream needs a preset dictionary (FDICT), which this reader has no way to be given.

-12

ZLIB_ERR_HEADER: i32

A zlib header that is not one: the wrong method, a window over 32 KB, or a failed check.