Compress and decompress
zlib is the compression inside PNG files, gzip, HTTP and many other formats. libstb writes and reads zlib streams (RFC 1950) with no library behind it: the same code that makes PNG files, set free for your own data. What it writes, any zlib implementation reads, and what any zlib implementation writes, libstb reads.
import libstb as stb
# Numbers are compressible, but not as much as a repeated sentence.
text = ",".join(str(i * i % 9973) for i in range(20000)).encode()
# The level is zlib's: 1 is the fastest, 9 the smallest, 0 stores without compressing, -1 is the default (6).
for level in (1, 6, 9):
packed = stb.zlib_compress(text, level=level)
print(f"level {level}: {len(text)} -> {len(packed)} bytes")
# Decompression gives the bytes back, exactly.
packed = stb.zlib_compress(text)
print("round trip exact:", "yes" if stb.zlib_decompress(packed) == text else "no")
# A stream that is cut short, or damaged, is a DecodeError.
try:
stb.zlib_decompress(packed[:-4])
except stb.DecodeError:
print("truncated stream: decode error")
# The output is bounded: 64 MiB of zeros packs to a few dozen KiB, and a limit says no before the memory is used.
bomb = stb.zlib_compress(bytes(64 << 20), level=9)
print(f"64 MiB of zeros packs to {len(bomb) >> 10} KiB")
try:
stb.zlib_decompress(bomb, max_bytes=1 << 20)
except stb.LimitError:
print("with a 1 MiB limit: limit error")
level 1: 97707 -> 44971 bytes
level 6: 97707 -> 43759 bytes
level 9: 97707 -> 43759 bytes
round trip exact: yes
truncated stream: decode error
64 MiB of zeros packs to 63 KiB
with a 1 MiB limit: limit error
Compress
zlib_compress(data, level) returns the compressed stream. The level is zlib's: 1 is the fastest, 9 the smallest and slowest, 0 stores the data without compressing it, and -1 is the default, 6. The numbers match zlib's but the algorithm is not zlib's. On the data libstb measured (source code, a binary, image pixels), its default level was 1.2 to 1.9 times faster than zlib's level 6, with output 0.4 % to 3.3 % larger. Measure on your own data before you rely on that. Empty input gives a valid stream, and in Python the GIL is released while it runs.
Decompress
zlib_decompress(stream, max_bytes) expects exactly one stream. A bad header, corrupt or truncated data, a wrong Adler-32 checksum, or bytes after the end of the stream are a decode error. It is correct and bounded, but it is not faster than other inflate code: about the same on binary data and about 2.5 times slower on very compressible text, in libstb's measurement.
The bound
A compressed stream can be a thousand times smaller than what it holds: 64 MiB of zeros is a few dozen KiB. A program that inflates whatever it is sent without a limit can be made to run out of memory by a file the size of a thumbnail. max_bytes bounds the output, 512 MiB by default. Memory is grown in steps and never past the limit, nothing is reserved up front, and a stream that would produce more is a limit error. This is the same rule as untrusted input for pictures.
Errors
| What happened | Python | C++ |
|---|---|---|
| Corrupt, truncated or extra data | stb.DecodeError | stb::decode_error |
| Output over the limit, or input over 2 GiB | stb.LimitError | stb::limit_error |
| A level outside -1 to 9 | ValueError | std::invalid_argument |
API at a glance
| Compress | stb.zlib_compress(data, level=-1) |
|---|---|
| Decompress | stb.zlib_decompress(data, max_bytes=...) |
| The data is | any bytes-like object, a bytes out |