deflatePrime
Prefix raw compression output with a caller-supplied bit fragment.
Full chapter and shared rules ↗39ab1ace4bbe0b0e · 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 deflatePrime(z_streamp strm, int bits, int value);
Prefix raw compression output with a caller-supplied bit fragment.
Domain Restricted domain. Effect: append bits to pending output.
Pre live raw stream before its first deflate after init or deflateReset;
bits is 0..16 and value is any int. The caller accounts for these prefix bits
when interpreting the later compressed stream.
Post Z_OK appends the low bits bits of value, least significant first,
to the logical pending bit sequence. Complete bytes join pending output and fewer than
8 bits remain buffered. No bytes are delivered to next_out; no allocation occurs.
Z_BUF_ERROR inserts no bits when the pending buffer has insufficient space.
Existing pending bytes may be compacted within their owned buffer before that refusal.
Outside focused scope: counts outside 0..16 give Z_BUF_ERROR before compaction;
a failed stream check gives Z_STREAM_ERROR. The implementation does not check wrapper
or first-call timing. Its public goal covers exact pending-bit effects there, without
promising ordinary stream validity for arbitrary inserted fragments.
Shared conditions and scope
API manual · Shared stream rules · Stream decompression
Use a z_stream compressor when input arrives in pieces, output space is limited, or you
need a chosen wrapper, dictionary or flush boundary. 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 |
These focused contracts follow the pinned deflate.c, applied patches and zlib.h in the
default LP64 Linux build. Source readings are to confirm by proof. They leave the broader
registered public goals unchanged. See contract conventions
for domain/effect labels and progress for proof status and remaining work.
Shared conventions
Each stated Pre requires termination without runtime faults and the stated Post. A call that retains a live stream preserves its representation invariant.
- Live stream: a writable
z_streamwith 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_inreadable bytes; output coversavail_outwritable 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 explicit focused assumptions; see safety.md. - 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
deflateEndfree 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
nameandcommenthave a terminating zero;extracovers its stated length. Focused format contracts useextra_len <= 65535. - Checked refusals: except for init and bounds, a failed stream check returns
Z_STREAM_ERRORbefore normal operation, without writes.deflateCopyadditionally 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 readable checked states, not arbitrary invalid addresses.
Formal contract
ZlibF.Spec.Public.Goal.deflatePrime
Proof status: Not completed · Evidence and remaining work
/-- `int deflatePrime(z_streamp strm, int bits, int value)`.
Append the requested low bits in transmission order. Compaction can occur before a buffer
error; invalid bit counts are refused before compaction. No allocation occurs. -/
def Goal.deflatePrime : Prop :=
∀ h r i z s count value old,
Zlib.Deflate.Spec.DGood h r i z s → PendingBits h s old →
∃ rc after s', C.run (Zlib.Deflate.deflatePrime (some r) count value) h = .ok (rc, after) ∧
Zlib.Deflate.Spec.DGood after r i z s' ∧
(count.toInt < 0 ∨ 16 < count.toInt → rc = Z_BUF_ERROR ∧ after = h) ∧
(0 ≤ count.toInt → count.toInt ≤ 16 →
s'.pending_out = s'.pending_buf ∧
if s.pending_buf_size.toNat - s.pending.toNat < (s.bi_valid.toInt.toNat + count.toInt.toNat) / 8 then
rc = Z_BUF_ERROR ∧ PendingBits after s' old
else rc = Z_OK ∧ PendingBits after s'
(old ++ (List.range count.toInt.toNat).map (fun j => decide (value.toUInt32.toNat / 2 ^ j % 2 = 1)))) ∧
Frame h after (DeflateOwned i s) ∧ LiveChange h after (fun _ => False) (fun _ => False) ∧
after.nalloc = h.nallocRelated definitions and source ↗