zlib
stdlib/extra/zlib.hk: the zlib container (RFC 1950) around DEFLATE.
Two bytes of header, the DEFLATE stream of extra/deflate, and an Adler-32 trailer. That is the framing a PNG IDAT chunk and an HTTP Content-Encoding: deflate value carry, and it is what identifies the stream a deflate body and its checksum are.
compress takes a deflate.Level and writes the stream deflate.compress produces; decompress takes the same bounded deflate.Caps as the raw decoder and refuses a header it does not read, a preset dictionary, a stream that does not end where the trailer starts, and a trailer whose Adler-32 does not match the decoded bytes. Every refusal is a deflate.InflateError, and there is no uncapped form.
_at
def _at(data: bytes, i: int) -> int
Byte i of data, or 0 past the end.
_CMF
_CMF: int = 120
The two header bytes this module writes: DEFLATE with a 32 KiB window and a default-level hint, the pair every encoder emits.
_FLG
_FLG: int = 156
adler32
def adler32(data: bytes) -> int
Adler-32 (RFC 1950 8.2) of data, the trailer's checksum.
adler32("Wikipedia".to_bytes()) => 300286872
adler32("".to_bytes()) => 1
_header
def _header() -> bytes
The two header bytes.
_trailer
def _trailer(sum: int) -> bytes
The Adler-32 value as its four big-endian trailer bytes.
compress
def compress(data: bytes, level: deflate.Level) -> bytes
Compress data as one zlib stream.
The header is the fixed DEFLATE-and-32-KiB pair. Identical input and level write identical bytes. Stored and Fixed are the deflate writer's two strategies: Stored guarantees the body never grows, Fixed Huffman-codes each byte against the RFC 1951 fixed table.
caps = deflate.Caps(max_output_bytes=1024, max_ratio=100)
stream = compress("png data".to_bytes(), deflate.Stored)
decompress(stream, caps).unwrap_or("".to_bytes()).to_string().unwrap_or("") => "png data"
decompress
def decompress(data: bytes, caps: deflate.Caps) -> Result<bytes, deflate.InflateError>
Inflate one zlib stream in data, within caps.
The input is one stream: data must hold the header, the body and the four trailer bytes, and a byte after the trailer is BadHeader at that offset. A stream naming a preset dictionary is DictionaryRequired, since no extra caller produces one.
caps = deflate.Caps(max_output_bytes=1024, max_ratio=100)
stream = compress("second".to_bytes(), deflate.Fixed)
decompress(stream, caps).unwrap_or("".to_bytes()).to_string().unwrap_or("") => "second"
_without
def _without(data: bytes, at: int) -> bytes
data with the byte at at removed: the test-side truncation, which needs no way to write an arbitrary byte value.
_put
def _put(data: bytes, at: int, byte: bytes) -> bytes
data with the value at at replaced by byte, keeping the length.