Skip to content
REFERENCE / ZLIB × LEAN

inflatePrime

Supply bits before the next input byte, or clear buffered bits.

Full chapter and shared rules ↗
Source snapshot · ecafc184cdf44b7f · 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 inflatePrime(z_streamp strm, int bits, int value);

Supply bits before the next input byte, or clear buffered bits.

Domain Restricted domain: live raw streams before first decoding or at a reported block/header boundary; every signed bits and value.

Effects Mutates state only.

Pre The common live-stream conditions and the stated priming call order hold.

Post Zero bits returns Z_OK unchanged. Negative bits empties the bit buffer. For 1 through 16 bits that fit within the 32-bit buffer, returns Z_OK, appending the low bits bits of value above existing bits. These bits precede next_in.

Errors More than 16 bits, insufficient bit-buffer space or a failed stream check returns Z_STREAM_ERROR unchanged. Does not update data_type. The C does not enforce the raw-stream or call-order restriction.

Shared conditions and scope

API manual · Shared stream rules · Stream compression

Use an inflate stream to decode input in pieces while controlling output space. Select zlib, gzip or raw DEFLATE at initialization, then continue with more input or room until completion, a dictionary request or an error. End releases the decoder after any of those outcomes. For whole-buffer zlib decoding, see uncompress.

Task Entries
Start or reuse Init, configured init, reset, new configuration, keep history
Decode or provide metadata Inflate, set dictionary, read history, gzip header
Recover or inspect Sync search, sync point, mark, table use
Adjust or branch Prime bits, validation, distance option, copy
Release storage End

The focused contracts follow the pinned inflate.c and zlib.h in the default LP64 Linux build, with gzip enabled and without optional invalid-distance or strict-distance modes. Source readings are to confirm by proof. The broader registered public goals retain their domains. See contract conventions for terminology and progress for proof status and remaining work.

Common conditions

All contracts below have a Restricted domain. A C pointer type alone does not supply the storage, lifetime or call history required here.

  • A live stream was successfully initialised or copied, has not been ended, retains its library-owned state, and is used without concurrent calls. Use the default allocator and free function. Init takes a writable, non-live stream with allocator fields set to NULL or the default functions.
  • A readable or writable range has the stated capacity for the whole call. Caller ranges are separate from the stream and its owned storage. Input and output ranges do not overlap. Dictionary destinations, the registered header record and its destination buffers are separate from each other and from the stream. Registered header storage is also separate from input/output ranges on calls using it. These are explicit focused-domain restrictions.
  • The caller owns its stream object and buffers. The library owns the state and optional window. A call changes only its stream, owned storage, stated output ranges and any registered header destination. It never frees caller buffers.
  • A rejected stream check returns Z_STREAM_ERROR without changes, unless a query or copy contract says otherwise. This covers NULL and safely readable records rejected by the check; it does not cover dangling or arbitrary pointers. The check requires allocators, state, the state's back-pointer and a valid mode.
  • Counters use the C unsigned widths and wrap. No contract assumes allocation succeeds. End remains available after decoding, dictionary or allocation errors.
flowchart LR
    I["Init"] --> F["inflate"]
    F -->|"Z_NEED_DICT"| D["SetDictionary"] --> F
    F -->|"more room or input"| F
    F -->|"end or abandon"| E["End"]
    F -->|"data error"| R["Reset or Sync"] --> F

Formal contract

ZlibF.Spec.Public.Goal.inflatePrime

Proof status: Registered proof · Evidence and remaining work

/-- `int inflatePrime(z_streamp strm, int bits, int value)`.
Negative counts clear pending bits. Counts above 16 or a total above 32 are refused.
Otherwise append exactly the requested low bits, without allocation. -/
def Goal.inflatePrime : Prop :=
  ∀ bits value, CheckedInflate (fun p => Zlib.Inflate.inflatePrime p bits value) Z_STREAM_ERROR <|
  ∀ h r i z s, InflateView h r i z s → s.bits.toNat ≤ 39 →
    C.run (Zlib.Inflate.inflatePrime (some r) bits value) h =
      if bits = 0 then .ok (Z_OK, h)
      else if bits.toInt < 0 then .ok (Z_OK, putInflate h i { s with hold := 0, bits := 0 })
      else if 16 < bits.toInt ∨ 32 < s.bits.toNat + bits.toInt.toNat then .ok (Z_STREAM_ERROR, h)
      else .ok (Z_OK, putInflate h i { s with
        hold := s.hold + ((value.toUInt32 &&& ((1 <<< bits.toUInt32) - 1)).toUInt64 <<< s.bits.toUInt64)
        bits := s.bits + bits.toUInt32 })
Related definitions and source ↗
ZLIB × LEAN

Global contents

READING NOWinflatePrime