inflateMark
Report the current code position for indexing.
Full chapter and shared rules ↗long ZEXPORT inflateMark(z_streamp strm);
Report the current code position for indexing.
The single integer packs two measures of progress. back is in bits: the distance from the
current bit position back to the start of the code being decoded. progress is in bytes: what
remains to copy of a stored block, or what has been written of the current match. The multiplier
65536 is 2^16; it only packs the two parts into one result and is not a window size.
Return values and next calls
Return type. long — Packed position and progress, or an overlapping error sentinel
This returns a packed position value rather than a zlib status code. Its error sentinel overlaps a valid position value.
inflateMark()| Returned value | Meaning and conditions | Caller’s next step |
|---|---|---|
back * 65536 + progressPacked code position |
For a valid stream, back is the number of bits back to the current code; between codes, in a header or in a stored block it is -1. progress is remaining stored-block bytes, emitted match bytes, or zero otherwise. | Interpret the two parts in bits and bytes respectively; the query does not change the stream. |
-65536Stream-check failure sentinel |
A failed stream check returns -65536; a valid stream with back -1 and progress zero returns the same value. | This value alone cannot distinguish an error; interpret it using the stream’s validity. |
Calling conditions and effects
Domain Restricted domain: live streams in any mode.
Effects Read-only query.
Pre The common live-stream conditions hold.
Proof goals.
- Goal.
Goal.inflateMark_safety(Safety/Streams.lean); required. Theorem:ZlibF.Spec.Public.StreamCallSafetyProof.inflateMark_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
strmis a refused stream (RefusedInflate). The call on a reachable stream asks (StreamCallDomain; this call has no argument condition):strmis a reachable stream (StreamReachable);- the platform size bound holds (
ByteObjectsFit).
- Covered without condition. Every reachable stream, in every mode: before decoding, mid-block, at stream end, and after a data or memory error. In the refusal: every refused stream.
- Guarantees (
StreamCallSafefor the call on a reachable stream).- Returns. On a refused stream the call returns
-65536. 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).
- Returns. On a refused stream the call returns
- Not in this goal. A stream record with
zallocandzfreeset whosestateis a garbage pointer, points to a freed state, or points to a deflate state: no goal covers this call on such a record (deviations). The value returned: functional goalGoal.inflateMark(not planned; a theorem proves it).
Complete return conditions and boundaries
Post Returns back * 65536 + progress: back is the bits back to the current
code, or -1 between codes/in a header or stored block. progress is remaining
stored-block bytes, emitted match bytes, or zero otherwise. Changes nothing and
allocates/frees nothing.
Edges A failed stream check returns -65536. That value also occurs on valid streams, so it does not by itself identify an error.
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_ERRORwithout 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.
inflateEndends the history. - Result cells as values.
inflateGetDictionaryreports a length throughuInt *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.inflateMark_safety
State: done · Status of the results
/-- `long inflateMark(z_streamp strm)`.
**Safety of `inflateMark`**: a `NULL` or inconsistent stream (`RefusedInflate`) is refused with
-65536 and nothing changed; and on every reachable inflate stream, with the caller's arguments as
`zlib.h` asks, the call is safe (`StreamCallSafe`). -/
def Goal.inflateMark_safety : Prop :=
(∀ h p, RefusedInflate h p → C.run (Zlib.Inflate.inflateMark p) h = .ok ((-65536), h)) ∧
(∀ h r,
StreamCallDomain .inflate h r (.inflateMark (some r)) →
StreamCallSafe .inflate h r (.inflateMark (some r)))Related definitions and source ↗Functional contract (beyond safety)
ZlibF.Spec.Public.Goal.inflateMark
State: done · Status of the results
/-- `long inflateMark(z_streamp strm)`.
The high half counts code bits; the low half counts remaining stored bytes or match bytes
already copied. The internal ranges are obligations of the stream invariant. -/
def Goal.inflateMark : Prop :=
CheckedInflate Zlib.Inflate.inflateMark (-65536) <|
∀ h r i z s, InflateView h r i z s →
C.run (Zlib.Inflate.inflateMark (some r)) h = .ok
(Int64.ofInt (s.back.toInt * 65536 +
(if s.mode = COPY then s.length.toNat else
if s.mode = MATCH then (s.was - s.length).toNat else 0)), h)Related definitions and source ↗