Standard library · Data formats
data/deflate
Imported as import "data/deflate" as deflate, its names are then deflate.…. Every signature below is the one the checker infers.
Types
Bits
st holds the position in the data, the bits read but not yet used, how many of them there are, and whether the data ran out.
Bits(data: Str, st: Buf(Int))
Huff
A canonical code as two tables: how many codes have each length, and the symbols in code order — enough to decode a bit at a time.
Huff(count: Buf(Int), symbol: Buf(Int))
Out
The output as it grows, and the last 32 KB of it in a ring for the copies to read back from.
Out(b: StrBuf, win: Buf(U8), st: Buf(Int))
Tables
Tables(lbase: Buf(Int), lext: Buf(Int), dbase: Buf(Int), dext: Buf(Int))
Wr
Bits go out least significant first; st is the bits not yet written and how many.
Wr(b: StrBuf, st: Buf(Int))
Functions
fn inflate(data: Str) -> Result(Str, Str)
The bytes a DEFLATE stream encodes, or what was wrong with it.
deflate.inflate(deflate.deflate("hello hello hello")) # => Ok(hello hello hello) deflate.inflate("not a stream") # => Err(not a deflate stream: a block of an unknown kind)
fn inflate_from(data: Str, at: Int) -> Result((Str, Int), Str)
The same from byte at, and where the stream ended — for an envelope with a trailer after it.
packed = deflate.deflate("abc") deflate.inflate_from("xx" + packed + "trailer", 2) # => Ok((abc, 7))
fn deflate(data: Str) -> Str
LZ77 over a 32 KB window: a hash of three bytes leads to the most recent position with that hash, and a chain leads from there to older ones.
#deflate.deflate("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") < 32 # => true
fn zlib(data: Str) -> Str
zlib: two header bytes, the stream, and the Adler-32 of the text.
import "text/str" as str str.hex_encode(deflate.zlib("a")) # => 789c4b040000620062
fn unzlib(data: Str) -> Result(Str, Str)
deflate.unzlib(deflate.zlib("round trip")) # => Ok(round trip)
fn gzip(data: Str) -> Str
gzip: the ten-byte header gzip writes with no name and no time, the stream, the CRC-32 and the length.
import "text/str" as str str.hex_encode(deflate.gzip("")) # => 1f8b08000000000000ff03000000000000000000
fn gunzip(data: Str) -> Result(Str, Str)
deflate.gunzip(deflate.gzip("round trip")) # => Ok(round trip) deflate.gunzip("plain") # => Err(not gzip)
Tests
test_inflate_what_zlib_wrote— Streams written by zlib: a fixed block, a stored block, a dynamic block.test_round_tripstest_it_compressestest_refusals