Skip to content
REFERENCE / ZLIB × LEAN

compress2_z

Compresses the input into one complete zlib stream at the requested level.

Full chapter and shared rules ↗
Source snapshot · 767fa1407a7fe3d1 · Source at the matching commit ↗

Read the function’s preconditions and results first. Expand the shared rules below for buffer and ownership requirements used throughout this API group.

int ZEXPORT compress2_z(Bytef *dest, z_size_t *destLen, const Bytef *source,
                        z_size_t sourceLen, int level);

Compresses the input into one complete zlib stream at the requested level.

Domain Restricted domain.

Pre Restricted buffer domain above. source may be NULL for zero input or checked rejection. destLen may be NULL for checked rejection. dest may be NULL for checked rejection, including the zero-capacity case below. Levels -1 and 0–9 are valid; -1 selects level 6.

Post Z_OK means the output is a complete zlib encoding of the input and *destLen is its length. Z_BUF_ERROR means the capacity was filled with a prefix of that encoding. Z_MEM_ERROR means allocation failed and no output was written.

Effects Stateful; common effects apply. A NULL source with nonzero input, NULL destLen, or NULL destination with nonzero capacity gives Z_STREAM_ERROR before any length write. Otherwise *destLen is cleared before initialisation. A bad level returns Z_STREAM_ERROR with length zero before any allocation. Later returns store the number of bytes written.

Edges NULL destination with zero capacity reaches initialisation: it returns Z_MEM_ERROR if allocation fails, otherwise Z_STREAM_ERROR, with length zero. A non-NULL destination with zero capacity gives Z_BUF_ERROR after successful initialisation. Empty input is valid.

Shared conditions and scope

API manual · Shared stream rules · Checksums

Use the buffer utilities when the complete input and output capacity are known at the call. They manage a temporary stream internally. Use the stream pages when you need incremental I/O, a different wrapper, a dictionary or explicit flush boundaries.

Task Entries
Compress a buffer Choose a level, default level, size the destination
Decompress a buffer Report input consumed, output only
Inspect the library Version, compile flags, error text

The focused contracts follow compress.c, uncompr.c, zutil.c and zlib.h in the default LP64 Linux build. _z forms use z_size_t; plain forms use uLong. Both are 64-bit here. Source readings are to confirm by proof. They do not narrow the registered targets in Utilities.lean and Queries.lean. Calls.lean fixes translated arguments and results. See contract conventions for domain/effect labels and progress for proof status and remaining work.

The buffer utilities create and release a local stream with the zlib wrapper and default settings.

flowchart LR
    B["compressBound"] --> C["compress / compress2"]
    C --> Z["zlib stream"] --> U["uncompress / uncompress2"]
    U --> D["decoded bytes"]

Common conventions

For buffer calls, the domain is restricted to valid memory: non-NULL sources cover the stated readable input; non-NULL destinations cover the stated writable capacity. Non-NULL length pointers name readable, writable scalar cells. Input, output and length cells are separate, including the two length cells when both occur. The caller keeps those objects live and does not modify them during the call. Each entry states its allowed NULL cases. Other invalid pointers are outside this scope. Decompression error handling needs no valid compressed-data assumption.

Common effects. Every in-scope call terminates without a fault. Buffer calls preserve input and memory outside the output and length cells. They write at most the original capacity. All internal allocations are released before return, including on allocation failure; no local stream or owned allocation is returned. Pointer-valued queries return borrowed immutable storage. These are contract requirements, not claims that the implementation proofs are complete.

Formal contract

ZlibF.Spec.Public.Goal.compress2_z

Proof status: Not completed · Evidence and remaining work

/-- `int compress2_z(Bytef *dest, z_size_t *destLen, const Bytef *source, z_size_t sourceLen, int level)`.
Includes null-cell rejection, unchanged capacity on argument rejection, exact stored length,
valid compressed data, buffer exhaustion, allocation failure, frame and complete cleanup. -/
def Goal.compress2_z : Prop :=
  ∀ level, CompressorContract (fun d c s n => Zlib.Compress.compress2_z d c s n level) level true
Related definitions and source ↗
ZLIB × LEAN

Global contents

READING NOWcompress2_z