Skip to content

Brotli (compress/brotli)

Google's modern compression algorithm (RFC 7932). Brotli achieves the best compression ratio for web content, surpassing Gzip by 15–25% on typical HTML/CSS/JS payloads. It's the standard for HTTP Content-Encoding: br and WOFF2 web fonts.

Usage

rust
import { brotliCompress, brotliDecompress } from "compress/brotli";

let data = "Brotli in pure Vex!";
let ptr = data.as_ptr() as *void;
let len = data.len() as i64;

let outCap = len + (len / 65536 + 1) * 8 + 16;
let comp = alloc(outCap as u64);
let compLen = brotliCompress(ptr, len, comp, outCap);

let decomp = alloc(len as u64);
let decompLen = brotliDecompress(comp, compLen, decomp, len);

API

FunctionDescription
brotliCompress(src, srcLen, dst, dstCap): i64Compress into Brotli stream
brotliDecompress(src, srcLen, dst, dstCap): i64Decompress Brotli data

Internal Architecture

FilePurpose
compress.vxBrotli encoder with context modeling
decompress.vxBrotli decoder
prefix.vxPrefix code (Huffman) tables

The reference C Brotli implementation is ~15,000 lines. Vex achieves the same in ~660 lines — a 96% reduction.

Released under the MIT License.