Learn/Remade with Rust

A Pure-Rust zlib for Edge Devices: Where rusty_zlib Fits

rusty_zlib is a pure-Rust zlib fork that runs on wasm32 and no_std and writes the same compressed bytes as upstream. Here is where it fits in a product, and where it does not.

Signed by Tim Almond·
A hydraulic baling press has compacted shredded paper into a dense strapped cube, with a loose pile of the same shredded paper beside it in a dim recycling plant
Is there a pure-Rust zlib I can use on small devices and in the browser?

Yes. rusty_zlib 0.1.1 is a fork of zlib-rs with no C in its shipped build. It builds for wasm32 and, with default features off, for no_std targets. It reads and writes zlib and gzip data, and its compressed output matches upstream byte for byte.

Compression code sits under almost everything a device does. PNG images, PDF pages, gzip downloads and backups all lean on it. For MATA's Home Computers and mesh nodes, I wanted a pure-Rust zlib I could point at, and rusty_zlib is that. A pure-Rust zlib also means one less C library to cross-compile, which anyone who has built for a browser or a tiny board will appreciate. It is on crates.io as version 0.1.1, with no C in the shipped build. The compressed bytes it writes are identical to its upstream's at every level.

Before the details, one point about honesty. This is a fork of someone else's good work, and the speed gains so far are modest. I'll say what is measured, and what is not.

Why compression code ends up in security advisories

zlib is the reference, and it is written in C. That is no insult to it. It dates from 1995 and runs nearly everywhere. It has still had memory bugs. CVE-2018-25032 is memory corruption when compressing input with many distant matches. CVE-2022-37434 is a heap overflow in inflate, reached through a large gzip header extra field.

Both live in code that handles bytes from outside. A device that unpacks an update, a sync packet or an uploaded file is running that code on input it does not control. CISA's memory-safety guidance makes the general case for moving that kind of parser to a memory-safe language.

A product team feels this in two places. The first is the advisory feed: every bundled C zlib is a line item to track, and old copies hide inside vendored trees. The second is the build. Cross-compiling C for wasm32 or a microcontroller needs a toolchain per target, while a Rust crate needs the target and nothing else.

For us the edge matters too. A mesh node has little memory and no spare hands to patch it, which is why the rust edge-node work cares about small, predictable code. The same thinking is behind our zstd crate and why the distributed cloud needs memory-safe Rust.

What rusty_zlib actually is

A fork, with credit

rusty_zlib is the Remade-With-Rust fork of zlib-rs, built by the Trifecta Tech Foundation. It starts from zlib-rs 0.6.9 plus one later upstream fix. The Zlib licence stays as upstream ships it, and a NOTICE file names the people whose work this is.

The repo's HOUSE.md lists every change on top: a benchmark harness, a rename, and the speed work in 0.1.1. I like that. A fork should be easy to audit against its parent.

What stays identical

Compressed output is byte for byte what upstream writes, at every level. Each speed change had to pass that check before it landed. The test suite has 92 tests in the crate, and another 280 in a test crate that compares results with a C zlib-ng. That C code is the referee only, and it is never published.

Where it runs

The crate builds for the host, for wasm32-unknown-unknown, and for a bare-metal RISC-V target with default features off. In practice that means a browser tab, a phone and a small board can all call the same Rust code, and a Home Computer in era 3 of the roadmap can use the same crate as a customer's laptop. Features matter here: the defaults pull in std and a C-style allocator hook, and turning them off is what makes the bare-metal build work. On a board, check which allocator you give it before you trust the memory numbers. Its minimum Rust version is 1.75, and the licence is Zlib.

Using it in a product

A round trip in a few lines

The API comes from the crate's own documentation. You size a buffer, then compress or decompress into it.

use rusty_zlib::*;

let mut buf = vec![0u8; compress_bound(input.len())];
let (compressed, rc) = compress_slice(&mut buf, input, DeflateConfig::default());

Decompression mirrors that with decompress_slice and an InflateConfig, and the return code tells you whether the stream was valid. Docs are on docs.rs.

What the numbers say, and what they don't

Against the fork's own 0.1.0, level 1 compression was 1.189 times faster on a whole file and 1.390 times faster on 4 KiB inputs. Decompression was 1.037 times faster. Levels 6 and 9 did not move, even though they ran about 10 percent fewer instructions. These come from the repo's own pinned, paired benchmark on one machine, 16 pairs per workload.

None of it is a comparison with C zlib. Treat 0.1.1 as "same output, a bit quicker at level 1".

What to wait for, and what to watch

Two things. Before those, a note on how I read claims about a pure-Rust zlib: they are a promise about the build, not about every line. First, the source still uses unsafe code, as upstream does, so "memory-safe" is a goal and not a proof. Second, no outside audit exists yet. If you use it, test your own data and keep your fallback. The Remade with Rust hub and the rusty_zlib page track the crate, and the Freedom Guide explains why we care. The remadewithrust.com notes cover the tuning in more depth.

FAQ

Short answers already in this article.

Is rusty_zlib the same thing as zlib-rs?

It is a fork. rusty_zlib starts from zlib-rs 0.6.9 by the Trifecta Tech Foundation and keeps its Zlib licence. The Remade-With-Rust team publishes it under its own name and version line and lists every change it makes in a HOUSE.md file.

Does it contain unsafe code?

Yes. Like upstream, the source uses unsafe blocks for speed, and the crate only denies unsafe operations inside unsafe functions. Pure Rust means no C in the shipped build. It does not mean a proof of memory safety, and no independent audit has been published.

Is it faster than C zlib?

No such claim is made. The published timing compares 0.1.1 with the fork's own 0.1.0 on one pinned core of one machine. Level 1 compressed 1.189 times faster, and levels 6 and 9 were flat. There is no head-to-head against C zlib or zlib-ng in the repo.

Can I use it as a drop-in C library?

Not from this crate. The C-compatible wrapper, libz-rs-sys, is deliberately not published under a house name. If a C program needs a Rust zlib today, upstream's libz-rs-sys is the thing to read about. rusty_zlib is for Rust callers.

What does the licence let me do?

rusty_zlib is under the Zlib licence, the same permissive licence upstream uses, with a NOTICE file crediting zlib-rs, zlib and zlib-ng. The licence allows commercial use in closed products. Keep the notices, and do not claim you wrote the original.