Gzip

object Gzip

gzip (RFC 1952) / zlib (RFC 1950) wrapper handling on top of Inflate: a pure-Kotlin port of libtorrent's inflate_gzip + gzip_header (src/gzip.cpp).

BitTorrent HTTP trackers and scrape endpoints frequently return their bencoded responses Content-Encoding: gzip, so the client has to strip the wrapper and inflate the raw DEFLATE payload. That is the sole job of this object.

libtorrent only handles gzip. We additionally accept a bare zlib stream, because some servers send Content-Encoding: deflate as a zlib stream rather than raw DEFLATE, and detecting it is cheap and unambiguous (the zlib CMF/FLG pair has a defined check). This is a small, clearly-marked extension; the gzip path is byte-for-byte faithful to gzip_header.

Errors surface as InflateException carrying the faithful libtorrent error code, e.g. InflateError.INVALID_GZIP_HEADER for a malformed header, InflateError.INFLATED_DATA_TOO_LARGE when a size cap is exceeded.

Properties

Link copied to clipboard

Default cap mirroring libtorrent callers (tracker responses are small).

Functions

Link copied to clipboard
fun decompress(input: ByteArray, maximumSize: Int = DEFAULT_MAXIMUM_SIZE): ByteArray

Decompress a gzip (or zlib) wrapped DEFLATE buffer and return the inflated bytes.