Skip to content
REFERENCE / ZLIB × LEAN

deflatePending

Report output waiting inside the compressor.

Full chapter and shared rules ↗
int ZEXPORT deflatePending(z_streamp strm, unsigned *pending, int *bits);

Report output waiting inside the compressor.

Return values and next calls

Return type. int — zlib status code

The status says whether the requested values fit their output types; pending bytes and bits are output parameters, not the return value.

deflatePending()
Returned value Meaning and conditions Caller’s next step
Z_OK
Query complete
The stream is live, and pending is omitted or its whole-byte count fits unsigned. Requested pending and bits outputs receive current counts; the stream and buffers are unchanged. Use the requested results and continue legal stream operations; either or both outputs may be NULL.
Z_BUF_ERROR
Requested byte count saturated
pending is non-NULL and the internal whole-byte count exceeds unsigned; *pending is set to UINT_MAX. Requested bits is still written and the stream is unchanged. Treat the byte result as saturated, not exact; repeating the same query does not recover precision. The stream remains usable under legal calls.
Z_STREAM_ERROR
Stream check failed
The stream check fails before normal operation; neither the stream nor result cells are written. Establish a live stream and the call preconditions; this result does not complete the operation.

Reading the result.

  • bits is the current pending bit count, not the last-flush count reported by deflateUsed.

Calling conditions and effects

Domain Restricted domain. Effect: query with optional scalar results.

Pre live stream; each result is NULL or a valid separate output cell.

Proof goals.

  • Goal. Goal.deflatePending_safety (Safety/Streams.lean); required. Theorem: ZlibF.Spec.Public.StreamCallSafetyProof.deflatePending_safety (public-stream-call-safety.lean). The goal has two clauses: the refusal of a refused stream, and one call on a reachable stream.
  • Applies when. The refusal asks that strm is a refused stream (RefusedDeflate). The call on a reachable stream asks (StreamCallDomain; this call has no argument condition):
    1. strm is a reachable stream (StreamReachable);
    2. the platform size bound holds (ByteObjectsFit).
  • Covered without condition. Every reachable stream, in every phase: before the first deflate, mid-stream, while finishing, and after Z_STREAM_END. The result pointers are values in the translation, not memory (shared conventions): the goal asks nothing of them, while Pre asks for writable cells. In the refusal: every refused stream and every value of the other arguments.
  • Guarantees (StreamCallSafe for the call on a reachable stream).
    • Returns. On a refused stream the call returns Z_STREAM_ERROR and stores neither count. On a reachable stream the call returns.
    • State. On a reachable stream, the stream keeps its invariant (StreamInvariant).
    • Writes. On a refused stream, nothing: the heap is unchanged. On a reachable stream, only the stream record and the stream's own blocks.
    • Allocations. On a reachable stream, the exact allocation account (LiveChange).
  • Not in this goal. A stream record with zalloc and zfree set whose state is a garbage pointer, points to a freed state, or points to an inflate state: no goal covers this call on such a record (deviations). The return value and the two counts: functional goal Goal.deflatePending (not planned; a theorem proves it).
Complete return conditions and boundaries

Post return Z_OK. If requested, pending receives the undelivered whole-byte count and bits the remaining bit count (0..7 for legal compression history). The stream and its buffers are unchanged. Omitted results are not written.

If a represented pending count exceeds unsigned, the requested byte count saturates and return is Z_BUF_ERROR; a requested bit result is still written. The functional goal includes this numerical refusal; the pending buffer of the default build is smaller.

Shared conditions and scope

Use a z_stream compressor when input arrives in pieces, output space is limited, or you need a chosen wrapper, dictionary or flush boundary. A wrapper is the header, trailer and integrity information added around the DEFLATE data; raw DEFLATE has none. Initialize once, call deflate with successive buffers, and end the stream even if compression is abandoned. For a single buffer, see compress and compress2.

Task Entries
Start or reuse Init, configured init, reset, reset retaining history
Supply metadata or history Dictionary, read history, gzip header
Compress and size output Deflate, bound, pending output, last flush bits
Adjust or branch Parameters, tuning, priming, copy
Release storage End

The contracts follow zlib.h and deflate.c of the pinned zlib with the patches applied, in the default LP64 Linux build. Of the seven patches only patches 0006 and 0007 touch deflate.c. Both change deflateBound_z, and with it the result of deflateBound, which calls deflateBound_z: 0006 one condition, 0007 the length of the gzip extra field in the wrapper size. The entry deflateBound_z states both changes. The patches change no other function. These are focused contracts: Pre describes ordinary correct use and is deliberately narrower than what the function accepts. Cases outside Pre that the C still handles appear under Errors or Outside focused scope. A call outside Pre is not thereby undefined, and it need not return an error code. See how to read a contract for the domain and effect labels.

What is proved. Safety only: each call returns without undefined behaviour or a memory error, writes only what its goal names, and leaks nothing (safety). Each entry gives its goal under Proof goals, in the five items and with the fixed phrases that Reading a Proof goals paragraph defines: the goal and its theorem; the conditions under which it applies, in the order of the Lean statement; what it covers for every value; what it guarantees (returns, state, writes, allocations); and what it leaves to another goal. The proof assumes the conditions listed there, not the documentary Pre. Post, the return tables and the notes describe functional behaviour. They are read from the C, are to confirm where no theorem states them, and are not required for the safety results. Progress says which goals are done.

Shared conventions

Under each stated Pre, the documented behaviour is that the call terminates without a runtime fault and that Post holds. A call that leaves the stream live preserves its representation invariant. The part of this that is proved is the safety goal of the entry.

First, how data changes hands in one call. next_in points to the start of the input not yet read, and avail_in is the number of bytes readable from there. next_out points to the next writable position, and avail_out is the output room that remains, also in bytes. One call may consume only part of the input and deliver only part of the output. Read the updated pointers and counts, take the bytes delivered, then give the same stream more input or more output room.

  • Live stream: a writable z_stream with a compressor state established by successful init or copy, preserved by legal calls, and not ended. The caller does not alter its internal state or allocator fields. Calls on one stream are sequential.
  • Buffers: input covers avail_in readable bytes; output covers avail_out writable bytes. They do not overlap each other, the stream or its owned state. A non-NULL scalar result points to a writable cell separate from other results and stream memory. These separation conditions are assumptions of the contracts. Where a safety goal assumes one too, its Proof goals says so and deviations has a row.
  • Allocator: the default allocator/free pair is used, or both fields are NULL before init. Allocation may fail on any attempt. Custom allocator callbacks are outside this page's scope.
  • Frame and ownership: calls change only their stream, its owned state and stated output regions. Caller input is read-only. Caller buffers and header records remain caller-owned. Only init and copy allocate; only their failure cleanup and deflateEnd free memory. Other calls preserve the stream's allocation set.
  • Header lifetime: a retained gzip header and its optional fields remain readable and unchanged while used. Non-NULL name and comment have a terminating zero; extra covers its stated length. The contracts on this page take extra_len <= 65535.
  • Checked refusals: except for init and bounds, a failed stream check returns Z_STREAM_ERROR before normal operation, without writes. deflateCopy additionally clears a non-NULL destination state. Query output cells are untouched on this refusal.
  • Restricted domain means these explicit memory, lifetime and call-order conditions, plus the entry's Pre. It does not mean every representable pointer or integer is accepted. Error summaries cover stream records and states that the checks can read, not arbitrary invalid addresses.
  • Reachable stream: the proof goals speak of a reachable stream where the contracts say live stream (how to read a contract). A call that a goal covers extends the stream's history: when it returns, with any code, the stream is reachable again and the goal of the next call applies. deflateEnd ends the history.
  • Result cells as values: deflateGetDictionary, deflatePending and deflateUsed report results through pointers (uInt *dictLength, unsigned *pending, int *bits). The translation does not model these cells as memory: each function takes only the fact that a pointer is NULL or not, and returns the values. So the goals say nothing about the address of a result cell, and a cell that overlaps another object cannot be expressed.

Safety goal

ZlibF.Spec.Public.Goal.deflatePending_safety

State: done · Status of the results

/-- `int deflatePending(z_streamp strm, unsigned *pending, int *bits)`.
**Safety of `deflatePending`**: a `NULL` or inconsistent stream (`RefusedDeflate`) is refused with
`Z_STREAM_ERROR` and nothing changed; and on every reachable deflate stream, with the caller's
arguments as `zlib.h` asks, the call is safe (`StreamCallSafe`). -/
def Goal.deflatePending_safety : Prop :=
  (∀ h p hasPending hasBits, RefusedDeflate h p →
    C.run (Zlib.Deflate.deflatePending p hasPending hasBits) h =
      .ok ((Z_STREAM_ERROR, none, none), h)) ∧
  (∀ h r hasPending hasBits,
     StreamCallDomain .deflate h r (.deflatePending (some r) hasPending hasBits) →
     StreamCallSafe .deflate h r (.deflatePending (some r) hasPending hasBits))
Related definitions and source ↗

Functional contract (beyond safety)

ZlibF.Spec.Public.Goal.deflatePending

State: done · Status of the results

/-- `int deflatePending(z_streamp strm, unsigned *pending, int *bits)`.
Optional output cells are independent; omitted cells remain absent. -/
def Goal.deflatePending : Prop :=
  ∀ pending bits, CheckedDeflate (fun p => Zlib.Deflate.deflatePending p pending bits)
    (Z_STREAM_ERROR, none, none) <|
  ∀ h r i z s, DeflateView h r i z s →
    C.run (Zlib.Deflate.deflatePending (some r) pending bits) h =
      .ok (((if pending ∧ 2 ^ 32 ≤ s.pending.toNat then Z_BUF_ERROR else Z_OK),
        if pending then some (if 2 ^ 32 ≤ s.pending.toNat then 0xffffffff else s.pending.toUInt32) else none,
        if bits then some s.bi_valid else none), h)
Related definitions and source ↗
ZLIB × LEAN

Contents

READING NOWdeflatePending