gzip
stdlib/extra/gzip.hk: gzip members (RFC 1952) around DEFLATE.
A ten-byte header, the DEFLATE stream of extra/deflate, and an eight-byte trailer carrying the CRC-32 of the decoded bytes and their length. That is the file format, the Content-Encoding: gzip body and the transfer coding a .tar.gz bundle is built from.
compress writes one member with a zero modification time, no file name and no comment. Identical input and level write identical bytes. decompress reads every member in the input, which is what a concatenation of gzip files is, refuses a header it does not read and a trailer whose CRC-32 or length does not match, and takes the same bounded deflate.Caps as the raw decoder. max_output_bytes applies to the whole decode: each member sees what the earlier ones left of it, which bounds peak memory by the caller's budget whatever the member count. max_ratio is measured against the input from a member's body to the end of data, which is an upper bound on that member's own length and charges later members' framing to the earlier one.
_at
def _at(data: bytes, i: int) -> int
Byte i of data, or 0 past the end.
crc32
def crc32(data: bytes) -> int
CRC-32 (RFC 1952 8) of data, the trailer's checksum.
crc32("123456789".to_bytes()) => 3421780262
crc32("".to_bytes()) => 0
The reflected polynomial, one bit per round and no table: the trailer costs one pass per member, and a table would be 1 KiB of the baked archive to save time no caller measures.
_le32
def _le32(data: bytes, at: int) -> int
A little-endian 32-bit field.
le32bytes
def _le32_bytes(value: int) -> bytes
value as four little-endian bytes.
_header
def _header() -> bytes
The ten-byte member header: DEFLATE, no flags, no modification time, no operating-system claim.
compress
def compress(data: bytes, level: deflate.Level) -> bytes
Compress data as one gzip member.
caps = deflate.Caps(max_output_bytes=1024, max_ratio=100)
member = compress("hello".to_bytes(), deflate.Stored)
decompress(member, caps).unwrap_or("".to_bytes()).to_string().unwrap_or("") => "hello"
skipzero
def _skip_zero(data: bytes, at: int) -> Result<int, deflate.InflateError>
The offset of the first byte past a NUL-terminated field starting at at.
decompress
def decompress(data: bytes, caps: deflate.Caps) -> Result<bytes, deflate.InflateError>
Inflate every gzip member in data, within caps.
caps = deflate.Caps(max_output_bytes=4096, max_ratio=100)
twice = "".to_bytes().join([compress("a".to_bytes(), deflate.Fixed), compress("b".to_bytes(), deflate.Fixed)])
decompress(twice, caps).unwrap_or("".to_bytes()).to_string().unwrap_or("") => "ab"
_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.