Skip to content
REFERENCE / ZLIB × LEAN

inflateInit_

Create a zlib decoder with a 32 KiB maximum window.

Full chapter and shared rules ↗
int ZEXPORT inflateInit_(z_streamp strm, const char *version, int stream_size);

Create a zlib decoder with a 32 KiB maximum window.

Return values and next calls

Return type. int — zlib status code

The result says whether a zlib decoder with a maximum 32 KiB window was created.

inflateInit_()
Returned value Meaning and conditions Caller’s next step
Z_OK
Zlib decoder created
This is inflateInit2_ with windowBits 15, and state allocation succeeded. Decode zlib input with inflate; call inflateEnd after completion or abandonment.
Z_VERSION_ERROR
Version or structure size mismatch
version is NULL, its first byte mismatches, or stream_size is wrong; the stream is not inspected and nothing changes. Correct the version and structure size before initializing again; this failure needs no inflateEnd.
Z_STREAM_ERROR
Stream pointer refused
strm is NULL after the version check passed. Provide a writable non-live stream before initializing again; this failure needs no end call.
Z_MEM_ERROR
State allocation failed
State could not be allocated; the old state remains, while msg and default allocator fields may have changed. Initialization may be tried again when resources are available; no new allocation was retained and no inflateEnd is needed.

Calling conditions and effects

Domain Restricted domain: the same storage, version and allocator domain as inflateInit2_.

Effects Allocates state.

Pre The inflateInit2_ preconditions hold.

Proof goals.

  • Goal. Goal.inflateInit__safety (Safety/Initialisation.lean); required. Theorem: ZlibF.Spec.Public.InitSafetyProof.inflateInit__safety (public-init-safety.lean).
  • Applies when (InitArguments):
    1. the heap is a library heap (Fresh, Uniq, LibraryGlobals);
    2. version is NULL or its first byte is readable (VersionValue);
    3. strm is NULL, or points to a readable stream record whose zalloc and zfree are each NULL or the default function (DefaultOrNull).
  • Covered without condition. Every stream_size; every value of the first byte of version; every value of the other fields of the record, state included; every allocation outcome. The goal is wider than Pre: it also covers a NULL strm, a wrong version or stream_size, and a record whose state is not NULL. If that record was a live stream, the call still returns, but the old state stays allocated and the record no longer points to it: the account records that leak, it does not exclude it.
  • Guarantees (InitResultSafe).
    • Returns. The call returns. With a NULL strm the code is Z_VERSION_ERROR or Z_STREAM_ERROR.
    • State. With Z_OK, strm is a reachable stream.
    • Writes. With a NULL strm, nothing: the heap is unchanged. Otherwise only the stream record and blocks the call itself allocates.
    • Allocations. With Z_OK, every block the call allocated is one of the stream's own blocks, and nothing is freed (LiveChange). With any other code the allocated blocks are the same as before the call.
  • Not in this goal. A stream whose zalloc or zfree is a custom function: outside every goal (deviations). Which code each refusal and each allocation failure returns, and the field values after the call: functional goal Goal.inflateInit_ (not planned; a theorem proves it). That some call does return Z_OK: Goal.inflate_initialisation_domain (Safety/Domains.lean), required.
Complete return conditions and boundaries

Post Exactly inflateInit2_(strm, 15, version, stream_size). On success it accepts zlib streams with a window up to 32 KiB, and owns one state allocation.

Errors The same version, NULL-stream and allocation failures and cleanup as inflateInit2_. Gzip and raw bytes are not accepted by later decoding as zlib.

Shared conditions and scope

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. inflateEnd 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 contracts follow the pinned inflate.c with patch 0001 applied and zlib.h, in the default LP64 Linux build, with gzip enabled and without the optional invalid-distance or strict-distance modes. Patch 0001 changes inflateCopy only (patches). 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 Edges. A call outside Pre is not thereby undefined, and it need not return an error code. See how to read a contract for the 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.

Common conditions

The caller and the decoder keep different things. The caller supplies the input and output buffers of each call. The decoder keeps the current parsing stage, the bits not yet used, and the history that later copies may need. Giving it a different output array does not start a new decoding task.

next_in is the start of the unread input and avail_in the number of readable bytes. next_out is the next output position and avail_out the number of writable bytes that remain from there. For example, a 5-byte output array that returns with 2 bytes of room has received 3 bytes. Take those bytes first, then set the pointer and the count to the next writable region.

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 restrictions are part of Pre; each entry's Proof goals says which of them its goal assumes.
  • 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.
  • 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. inflateEnd ends the history.
  • Result cells as values. inflateGetDictionary reports a length through uInt *dictLength. The translation does not model that cell as memory: the function takes the cell's old value, or the fact that the pointer is NULL, and returns the new value. So the goal says nothing about the address of the cell, and a cell that overlaps another object cannot be expressed.
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

Safety goal

ZlibF.Spec.Public.Goal.inflateInit__safety

State: done · Status of the results

/-- `int inflateInit_(z_streamp strm, const char *version, int stream_size)`.
**Safety of `inflateInit_`** (macro `inflateInit`): as `Goal.inflateInit2__safety` at window
bits 15. -/
def Goal.inflateInit__safety : Prop :=
  ∀ h strm version size, InitArguments h strm version →
    ∃ rc after, C.run (Zlib.Inflate.inflateInit_ strm version size) h = .ok (rc, after) ∧
      InitResultSafe .inflate h after strm rc
Related definitions and source ↗

Functional contract (beyond safety)

ZlibF.Spec.Public.Goal.inflateInit_

State: done · Status of the results

/-- `int inflateInit_(z_streamp strm, const char *version, int stream_size)`.
The same version, stream, allocation, cleanup and invariant contract at window bits 15. -/
def Goal.inflateInit_ : Prop := InflateInitContract Zlib.Inflate.inflateInit_ 15
Related definitions and source ↗
ZLIB × LEAN

Contents

READING NOWinflateInit_