~/bend-docscommunity

zlib.bend relies on unsafe/foreign

raw source on the hub · import bend-kit-zlib@0.2.0.0/zlib.bend as Zlib

DEFLATE, gzip, and zlib encoding and decoding (RFC 1951, 1952, 1950) over byte strings, plus gzip, zstd, and brotli through the C libraries. Source: https://github.com/paymog/bend-kit/tree/main/zlib

2 imports
import Base
import bend-kit-hash@0.1.0.0/hash.bend as Hash

Types

type Br source · line 18 · raw

Data

Bit reader. over counts bytes read past the end; any over means the input was cut short.

type Ht source · line 91 · raw

Data

Huffman codes as a trie: one step per bit. A read bit picks o (1) or z (0). Bad: two codes collided, so the code set was over-subscribed.

type Ow source · line 294 · raw

Data

Output: the 32 KiB window (indexes wrap), the write position, the byte count, and the bytes, reversed.

type Mo source · line 301 · raw

Data

type Is source · line 307 · raw

Data

type Cd source · line 390 · raw

Data

Lengths so far (reversed), how many, the last one (for code 16), and whether it went wrong.

type Inflated source · line 621 · raw

Data

out: the bytes; rest: the input after the stream; n: len(out) mod 2^32.

type Bw source · line 825 · raw

Data

Bit writer: bits wait in buf, least significant first; racc holds the bytes out, reversed.

type Cx source · line 901 · raw

Data

The last index whose base is at most v, with that base and its extra-bit count; bases ascend.

type Lz source · line 930 · raw

Type

Match finder: the input, the newest position + 1 per hash, and per position the previous one with its hash (0: none).

type Ml source · line 974 · raw

Type

Match length: bytes at a + k and b + k compared so far, and whether the last pair was equal.

type Ch source · line 1007 · raw

Type

Chain walk at i: candidate c (position + 1), the longest match so far, and whether to go on.

type Es source · line 1038 · raw

Type

Encoder: match finder, bit writer, position, input length, and whether bytes are left.

type Decoder source · line 1189 · raw

Type

A live native decoder. Call dec.finish even after a failed feed to release it.

Definitions

def byte source · line 7 · raw

@h:Char -> U32

def bits source · line 11 · raw

@+n:U32 -> Nat

def mask source · line 14 · raw

@+n:U32 -> U32

def br.new source · line 21 · raw

@s:String -> Br

def br.pull source · line 24 · raw

@b:Br -> Br

def br.pull.if source · line 32 · raw

@b:Br -> @short:Bool -> Br

def br.short source · line 39 · raw

@+b:Br -> @+n:U32 -> Bool

def br.fill source · line 44 · raw

@fuel:Nat -> @+b:Br -> @+n:U32 -> Br

n is at most 16, so three bytes always fill the buffer.

def br.cut source · line 51 · raw

@b:Br -> @+n:U32 -> Pair(Br, U32)

def br.take source · line 55 · raw

@b:Br -> @+n:U32 -> Pair(Br, U32)

def br.drop.of source · line 58 · raw

@r:Pair(Br, U32) -> Br

def br.drop source · line 62 · raw

@b:Br -> @+n:U32 -> Br

def br.align source · line 66 · raw

@+b:Br -> Br

RFC 1951 §3.2.4: a stored block starts on a byte boundary.

def br.ok source · line 70 · raw

@+b:Br -> Bool

def br.bytes source · line 74 · raw

@k:Nat -> @+buf:U32 -> String

def br.rest.of source · line 81 · raw

@b:Br -> String

def br.rest source · line 86 · raw

@+b:Br -> String

Whole bytes still in the buffer go back in front of the unread input.

def ht.left source · line 97 · raw

@t:Ht -> Ht

def ht.right source · line 108 · raw

@t:Ht -> Ht

def ht.leaf source · line 119 · raw

@t:Ht -> @+sym:U32 -> Ht

def ht.ins source · line 130 · raw

@path:List<&2, Bool> -> @+t:Ht -> @+sym:U32 -> Ht

def ht.path source · line 142 · raw

@n:Nat -> @+code:U32 -> @acc:List<&2, Bool> -> List<&2, Bool>

The code's bits, most significant first: the order the stream sends them.

def cnt.add source · line 150 · raw

@ar:Pair(Array<U32>, U32) -> @+len:U32 -> Array<U32>

Canonical codes (RFC 1951 §3.2.2): count lengths, find each length's first code, then assign in symbol order.

def cnt.go source · line 154 · raw

@xs:List<&2, U32> -> @a:Array<U32> -> Array<U32>

def nx.set source · line 161 · raw

@+i:U32 -> @cv:Pair(Array<U32>, U32) -> @n:Array<U32> -> @+code:U32 -> Pair(Array<U32>, Pair(Array<U32>, U32))

def nx.step source · line 166 · raw

@+i:U32 -> @st:Pair(Array<U32>, Pair(Array<U32>, U32)) -> Pair(Array<U32>, Pair(Array<U32>, U32))

def nx.go source · line 170 · raw

@k:Nat -> @+i:U32 -> @st:Pair(Array<U32>, Pair(Array<U32>, U32)) -> Pair(Array<U32>, Pair(Array<U32>, U32))

def as.put source · line 177 · raw

@+l:U32 -> @+sym:U32 -> @nv:Pair(Array<U32>, U32) -> @t:Ht -> Pair(Array<U32>, Ht)

def as.one.nz source · line 181 · raw

@+l:U32 -> @+sym:U32 -> @n:Array<U32> -> @t:Ht -> @zero:Bool -> Pair(Array<U32>, Ht)

def as.one source · line 188 · raw

@+l:U32 -> @+sym:U32 -> @st:Pair(Array<U32>, Ht) -> Pair(Array<U32>, Ht)

def as.go source · line 192 · raw

@xs:List<&2, U32> -> @+sym:U32 -> @st:Pair(Array<U32>, Ht) -> Pair(Array<U32>, Ht)

def ht.of source · line 199 · raw

@st:Pair(Array<U32>, Ht) -> Ht

def ht.assign source · line 203 · raw

@xs:List<&2, U32> -> @st:Pair(Array<U32>, Pair(Array<U32>, U32)) -> Ht

def ht.build source · line 208 · raw

@+lens:List<&2, U32> -> Ht

lens[i] is the code length of symbol i; 0 means the symbol is unused.

def ht.step source · line 212 · raw

@z:Ht -> @o:Ht -> @bv:Pair(Br, U32) -> Pair(Ht, Br)

No code: 9999, never a symbol.

def ht.dec source · line 217 · raw

@fuel:Nat -> @st:Pair(Ht, Br) -> Pair(Br, U32)

A code is at most 15 bits (§3.2.7), so 16 steps always reach a leaf or a miss.

def ht.decode source · line 234 · raw

@t:Ht -> @b:Br -> Pair(Br, U32)

def nth source · line 237 · raw

@xs:List<&2, U32> -> @+i:U32 -> U32

def rep source · line 244 · raw

@k:Nat -> @+v:U32 -> @acc:List<&2, U32> -> List<&2, U32>

def take.n source · line 251 · raw

@k:Nat -> @xs:List<&2, U32> -> List<&2, U32>

def drop.n source · line 262 · raw

@k:Nat -> @xs:List<&2, U32> -> List<&2, U32>

def len.base source · line 274 · raw

List<&2, U32>

RFC 1951 §3.2.5

def len.extra source · line 277 · raw

List<&2, U32>

def dist.base source · line 280 · raw

List<&2, U32>

def dist.extra source · line 283 · raw

List<&2, U32>

def fixed.lit source · line 287 · raw

Ht

RFC 1951 §3.2.6

def fixed.dist source · line 290 · raw

Ht

def out.put source · line 297 · raw

@win:Array<U32> -> @+c:U32 -> @o:Ow -> Pair(Array<U32>, Ow)

def inf.next source · line 310 · raw

@last:Bool -> Mo

def inf.bad source · line 317 · raw

@win:Array<U32> -> @b:Br -> @o:Ow -> Pair(Array<U32>, Is)

def sb.put2 source · line 321 · raw

@p:Pair(Array<U32>, Ow) -> @b:Br -> Pair(Array<U32>, Pair(Br, Ow))

Stored block (§3.2.4): LEN, then NLEN (its complement), then LEN raw bytes.

def sb.put source · line 325 · raw

@win:Array<U32> -> @bv:Pair(Br, U32) -> @o:Ow -> Pair(Array<U32>, Pair(Br, Ow))

def sb.byte source · line 329 · raw

@st:Pair(Array<U32>, Pair(Br, Ow)) -> Pair(Array<U32>, Pair(Br, Ow))

def sb.go source · line 333 · raw

@k:Nat -> @st:Pair(Array<U32>, Pair(Br, Ow)) -> Pair(Array<U32>, Pair(Br, Ow))

def sb.done source · line 340 · raw

@t:Pair(Array<U32>, Pair(Br, Ow)) -> @+last:Bool -> Pair(Array<U32>, Is)

def sb.ok source · line 344 · raw

@win:Array<U32> -> @b:Br -> @+len:U32 -> @o:Ow -> @+last:Bool -> @ok:Bool -> Pair(Array<U32>, Is)

def sb.nlen source · line 351 · raw

@win:Array<U32> -> @bv:Pair(Br, U32) -> @+len:U32 -> @o:Ow -> @+last:Bool -> Pair(Array<U32>, Is)

def sb.len source · line 355 · raw

@win:Array<U32> -> @bv:Pair(Br, U32) -> @o:Ow -> @+last:Bool -> Pair(Array<U32>, Is)

def inf.stored source · line 359 · raw

@win:Array<U32> -> @b:Br -> @o:Ow -> @+last:Bool -> Pair(Array<U32>, Is)

def cl.slot source · line 364 · raw

List<&2, U32>

Dynamic block (§3.2.7): code lengths for the code-length code, then the lengths themselves. cl.slot[i]: where symbol i's length sits in the order the stream sends them.

def cl.place source · line 367 · raw

@xs:List<&2, U32> -> @+vals:List<&2, U32> -> List<&2, U32>

def cl.push source · line 374 · raw

@bv:Pair(Br, U32) -> @acc:List<&2, U32> -> Pair(Br, List<&2, U32>)

def cl.one source · line 378 · raw

@st:Pair(Br, List<&2, U32>) -> Pair(Br, List<&2, U32>)

def cl.read source · line 382 · raw

@k:Nat -> @st:Pair(Br, List<&2, U32>) -> Pair(Br, List<&2, U32>)

def cd.fill source · line 393 · raw

@+b:Br -> @+acc:List<&2, U32> -> @+n:U32 -> @+v:U32 -> @+cnt:U32 -> @+total:U32 -> Cd

def cd.rep source · line 396 · raw

@bv:Pair(Br, U32) -> @+v:U32 -> @+base:U32 -> @acc:List<&2, U32> -> @+n:U32 -> @+total:U32 -> Cd

def cd.prev source · line 400 · raw

@b:Br -> @acc:List<&2, U32> -> @+n:U32 -> @+prev:U32 -> @+total:U32 -> @ok:Bool -> Cd

def cd.z18 source · line 407 · raw

@b:Br -> @acc:List<&2, U32> -> @+n:U32 -> @+total:U32 -> @ok:Bool -> Cd

def cd.z17 source · line 414 · raw

@b:Br -> @acc:List<&2, U32> -> @+n:U32 -> @+s:U32 -> @+total:U32 -> @ok:Bool -> Cd

def cd.c16 source · line 421 · raw

@b:Br -> @acc:List<&2, U32> -> @+n:U32 -> @+prev:U32 -> @+s:U32 -> @+total:U32 -> @ok:Bool -> Cd

def cd.lit source · line 428 · raw

@b:Br -> @acc:List<&2, U32> -> @+n:U32 -> @+prev:U32 -> @+s:U32 -> @+total:U32 -> @ok:Bool -> Cd

def cd.sym source · line 435 · raw

@bv:Pair(Br, U32) -> @acc:List<&2, U32> -> @+n:U32 -> @+prev:U32 -> @+total:U32 -> Cd

def cd.step.go source · line 439 · raw

@+cl:Ht -> @+total:U32 -> @stop:Bool -> @cs:Cd -> Cd

def cd.stop source · line 448 · raw

@+st:Cd -> @+total:U32 -> Bool

def cd.go source · line 453 · raw

@k:Nat -> @+cl:Ht -> @+total:U32 -> @+st:Cd -> Cd

Each step adds at least one length, so total steps suffice.

def dyn.tables source · line 460 · raw

@b:Br -> @+lens:List<&2, U32> -> @+hlit:U32 -> @bad:Bool -> Pair(Br, Mo)

def dyn.done source · line 467 · raw

@st:Cd -> @+hlit:U32 -> @+total:U32 -> Pair(Br, Mo)

def dyn.lens source · line 471 · raw

@st:Pair(Br, List<&2, U32>) -> @+hlit:U32 -> @+hdist:U32 -> Pair(Br, Mo)

def dyn.clen source · line 477 · raw

@bv:Pair(Br, U32) -> @+hlit:U32 -> @+hdist:U32 -> Pair(Br, Mo)

def dyn.hdist source · line 481 · raw

@bv:Pair(Br, U32) -> @+hlit:U32 -> Pair(Br, Mo)

def dyn.hlit source · line 485 · raw

@bv:Pair(Br, U32) -> Pair(Br, Mo)

def dyn.read source · line 489 · raw

@b:Br -> Pair(Br, Mo)

def cp.put source · line 493 · raw

@av:Pair(Array<U32>, U32) -> @o:Ow -> Pair(Array<U32>, Ow)

A match copies len bytes from d back; the window's indexes wrap, so w - d needs no mask.

def cp.one source · line 497 · raw

@+d:U32 -> @st:Pair(Array<U32>, Ow) -> Pair(Array<U32>, Ow)

def cp.go source · line 502 · raw

@k:Nat -> @+d:U32 -> @st:Pair(Array<U32>, Ow) -> Pair(Array<U32>, Ow)

def inf.codes source · line 509 · raw

@p:Pair(Array<U32>, Ow) -> @b:Br -> @+lit:Ht -> @+dist:Ht -> @+last:Bool -> Pair(Array<U32>, Is)

def ow.n source · line 513 · raw

@+o:Ow -> U32

def inf.far source · line 518 · raw

@win:Array<U32> -> @b:Br -> @+o:Ow -> @+len:U32 -> @+d:U32 -> @+lit:Ht -> @+dist:Ht -> @+last:Bool -> @ok:Bool -> Pair(Array<U32>, Is)

RFC 1951 §3.2.5: a distance may not reach before the first byte out.

def inf.dx source · line 525 · raw

@win:Array<U32> -> @bv:Pair(Br, U32) -> @+db:U32 -> @+len:U32 -> @+o:Ow -> @+lit:Ht -> @+dist:Ht -> @+last:Bool -> Pair(Array<U32>, Is)

def inf.ds source · line 530 · raw

@win:Array<U32> -> @b:Br -> @+ds:U32 -> @+len:U32 -> @+o:Ow -> @+lit:Ht -> @+dist:Ht -> @+last:Bool -> @ok:Bool -> Pair(Array<U32>, Is)

def inf.d source · line 537 · raw

@win:Array<U32> -> @bv:Pair(Br, U32) -> @+len:U32 -> @+o:Ow -> @+lit:Ht -> @+dist:Ht -> @+last:Bool -> Pair(Array<U32>, Is)

def inf.lx source · line 541 · raw

@win:Array<U32> -> @bv:Pair(Br, U32) -> @+lb:U32 -> @+o:Ow -> @+lit:Ht -> @+dist:Ht -> @+last:Bool -> Pair(Array<U32>, Is)

def inf.ls source · line 545 · raw

@win:Array<U32> -> @b:Br -> @+s:U32 -> @+o:Ow -> @+lit:Ht -> @+dist:Ht -> @+last:Bool -> @ok:Bool -> Pair(Array<U32>, Is)

def inf.end source · line 553 · raw

@win:Array<U32> -> @b:Br -> @+s:U32 -> @+o:Ow -> @+lit:Ht -> @+dist:Ht -> @+last:Bool -> @eob:Bool -> Pair(Array<U32>, Is)

def inf.lit source · line 560 · raw

@win:Array<U32> -> @b:Br -> @+s:U32 -> @+o:Ow -> @+lit:Ht -> @+dist:Ht -> @+last:Bool -> @ok:Bool -> Pair(Array<U32>, Is)

def inf.sym source · line 567 · raw

@win:Array<U32> -> @bv:Pair(Br, U32) -> @+o:Ow -> @+lit:Ht -> @+dist:Ht -> @+last:Bool -> Pair(Array<U32>, Is)

def inf.dyn source · line 571 · raw

@win:Array<U32> -> @bm:Pair(Br, Mo) -> @o:Ow -> @+last:Bool -> Pair(Array<U32>, Is)

def inf.t2 source · line 575 · raw

@win:Array<U32> -> @b:Br -> @o:Ow -> @+last:Bool -> @is2:Bool -> Pair(Array<U32>, Is)

def inf.t1 source · line 582 · raw

@win:Array<U32> -> @b:Br -> @o:Ow -> @+last:Bool -> @+t:U32 -> @is1:Bool -> Pair(Array<U32>, Is)

def inf.t0 source · line 589 · raw

@win:Array<U32> -> @b:Br -> @o:Ow -> @+last:Bool -> @+t:U32 -> @is0:Bool -> Pair(Array<U32>, Is)

def inf.head source · line 597 · raw

@win:Array<U32> -> @bv:Pair(Br, U32) -> @o:Ow -> Pair(Array<U32>, Is)

§3.2.3: BFINAL, then BTYPE.

def inf.go source · line 603 · raw

@k:Nat -> @st:Pair(Array<U32>, Is) -> Pair(Array<U32>, Is)

Every step reads at least one bit, so 8 steps per input byte always finish a valid stream.

def inf.fin source · line 624 · raw

@ok:Bool -> @b:Br -> @o:Ow -> Maybe<&2, Inflated>

def inf.result source · line 632 · raw

@st:Pair(Array<U32>, Is) -> Maybe<&2, Inflated>

def inflate.rest source · line 645 · raw

@+s:String -> Maybe<&2, Inflated>

def inflate.out source · line 648 · raw

@m:Maybe<&2, Inflated> -> Maybe<&2, String>

def inflate source · line 656 · raw

@+s:String -> Maybe<&2, String>

Raw DEFLATE. None: malformed or cut short.

def crc32 source · line 660 · raw

@s:String -> U32

Checksum views for byte strings; hash owns the algorithms.

def adler32 source · line 663 · raw

@s:String -> U32

def le32 source · line 667 · raw

@s:String -> Maybe<&2, U32>

Little- and big-endian 32-bit words at the front of s.

def be32 source · line 676 · raw

@s:String -> Maybe<&2, U32>

def flag source · line 685 · raw

@+f:U32 -> @+bit:U32 -> Bool

def check source · line 688 · raw

@ok:Bool -> @+out:String -> Maybe<&2, String>

def gz.zstr source · line 697 · raw

@s:String -> @hit:Bool -> Maybe<&2, String>

RFC 1952 §2.3: after the fixed 10 bytes, optional FEXTRA, FNAME, FCOMMENT, FHCRC. hit: the byte just read was the terminating zero.

def gz.skip source · line 712 · raw

@k:Nat -> @s:String -> Maybe<&2, String>

def gz.extra.len source · line 723 · raw

@s:String -> Maybe<&2, String>

def gz.fextra source · line 732 · raw

@on:Bool -> @+s:String -> Maybe<&2, String>

def gz.fstr source · line 739 · raw

@on:Bool -> @+s:String -> Maybe<&2, String>

def gz.fhcrc source · line 746 · raw

@on:Bool -> @+s:String -> Maybe<&2, String>

def gz.flags source · line 753 · raw

@+f:U32 -> @s:String -> Maybe<&2, String>

def gz.head source · line 760 · raw

@s:String -> Maybe<&2, String>

def gz.trail2 source · line 769 · raw

@+out:String -> @+n:U32 -> @crc:Maybe<&2, U32> -> @size:Maybe<&2, U32> -> Maybe<&2, String>

def gz.trail source · line 775 · raw

@m:Maybe<&2, Inflated> -> Maybe<&2, String>

def gz.body source · line 782 · raw

@m:Maybe<&2, String> -> Maybe<&2, String>

def gunzip source · line 791 · raw

@s:String -> Maybe<&2, String>

One gzip member; the CRC-32 and ISIZE must match. None: malformed, cut short, or corrupt. ponytail: bytes after the first member are ignored; loop over members if a server concatenates them.

def zl.sum source · line 795 · raw

@+out:String -> @a:Maybe<&2, U32> -> Maybe<&2, String>

RFC 1950: CMF and FLG, then DEFLATE, then Adler-32, big-endian.

def zl.trail source · line 802 · raw

@m:Maybe<&2, Inflated> -> Maybe<&2, String>

def zl.ok source · line 809 · raw

@+cmf:U32 -> @+flg:U32 -> Bool

def unzlib source · line 812 · raw

@s:String -> Maybe<&2, String>

def bw.emit source · line 828 · raw

@w:Bw -> Bw

def bw.emit.if source · line 832 · raw

@w:Bw -> @full:Bool -> Bw

def bw.full source · line 839 · raw

@+w:Bw -> Bool

def bw.drain source · line 844 · raw

@fuel:Nat -> @+w:Bw -> Bw

A put adds at most 16 bits to at most 7, so two emits drain every whole byte.

def bw.put source · line 851 · raw

@w:Bw -> @+v:U32 -> @+n:U32 -> Bw

def bw.out source · line 855 · raw

@w:Bw -> String

def bw.done source · line 860 · raw

@w:Bw -> String

Seven zero bits push out a partial last byte.

def rev source · line 864 · raw

@k:Nat -> @+v:U32 -> @+acc:U32 -> U32

A Huffman code goes out most significant bit first, so it is reversed before the put.

def bw.code source · line 871 · raw

@w:Bw -> @cl:Pair(U32, U32) -> Bw

def fx.hi source · line 876 · raw

@+s:U32 -> @lo:Bool -> Pair(U32, U32)

RFC 1951 §3.2.6: the fixed code of literal/length symbol s, and its length.

def fx.mid source · line 883 · raw

@+s:U32 -> @lo:Bool -> Pair(U32, U32)

def fx.low source · line 890 · raw

@+s:U32 -> @lo:Bool -> Pair(U32, U32)

def fx.lit source · line 897 · raw

@+s:U32 -> Pair(U32, U32)

def cx.go source · line 904 · raw

@bs:List<&2, U32> -> @es:List<&2, U32> -> @+v:U32 -> @+i:U32 -> @+best:Cx -> Cx

def em.len source · line 915 · raw

@w:Bw -> @+len:U32 -> @c:Cx -> Bw

def em.dist source · line 919 · raw

@w:Bw -> @+d:U32 -> @c:Cx -> Bw

def em.match source · line 923 · raw

@w:Bw -> @+len:U32 -> @+d:U32 -> Bw

def em.lit source · line 926 · raw

@w:Bw -> @+c:U32 -> Bw

def lz.at.of source · line 933 · raw

@r:Pair(Array<U32>, U32) -> @head:Array<U32> -> @prev:Array<U32> -> Pair(Lz, U32)

def lz.at source · line 937 · raw

@z:Lz -> @+i:U32 -> Pair(Lz, U32)

def lz.drop source · line 941 · raw

@r:Pair(Lz, U32) -> Lz

def lz.h3 source · line 945 · raw

@r:Pair(Lz, U32) -> @+a:U32 -> @+b:U32 -> Pair(Lz, U32)

def lz.h2 source · line 949 · raw

@r:Pair(Lz, U32) -> @+i:U32 -> @+a:U32 -> Pair(Lz, U32)

def lz.h1 source · line 953 · raw

@r:Pair(Lz, U32) -> @+i:U32 -> Pair(Lz, U32)

def lz.hash source · line 957 · raw

@z:Lz -> @+i:U32 -> Pair(Lz, U32)

def lz.ins.of source · line 964 · raw

@r:Pair(Lz, U32) -> @+i:U32 -> Pair(Lz, U32)

def lz.insert source · line 970 · raw

@z:Lz -> @+i:U32 -> Pair(Lz, U32)

Links position i into the chain for its next three bytes; answers the chain's previous head.

def ml.cmp source · line 977 · raw

@r:Pair(Lz, U32) -> @+a:U32 -> @+b:U32 -> @+k:U32 -> @+x:U32 -> Ml

def ml.read source · line 981 · raw

@r:Pair(Lz, U32) -> @+a:U32 -> @+b:U32 -> @+k:U32 -> Ml

def ml.len source · line 985 · raw

@+k:U32 -> @eq:Bool -> U32

def ml.go source · line 993 · raw

@fuel:Nat -> @st:Ml -> Pair(Lz, U32)

Counts equal bytes from a and from b, up to fuel.

def ch.ok source · line 1011 · raw

@+i:U32 -> @+c:U32 -> @+max:U32 -> @+blen:U32 -> Bool

RFC 1951 §3.2.5: a distance is at most 32768.

def ch.next source · line 1014 · raw

@r:Pair(Array<U32>, U32) -> @src:Array<U32> -> @head:Array<U32> -> @+i:U32 -> @+max:U32 -> @+blen:U32 -> @+bdist:U32 -> Ch

def ch.best source · line 1018 · raw

@r:Pair(Lz, U32) -> @+i:U32 -> @+p:U32 -> @+max:U32 -> @+blen:U32 -> @+bdist:U32 -> Ch

def ch.go source · line 1024 · raw

@fuel:Nat -> @st:Ch -> Ch

def es.lit.of source · line 1041 · raw

@r:Pair(Lz, U32) -> @w:Bw -> @+i:U32 -> @+n:U32 -> Es

def es.lit source · line 1046 · raw

@z:Lz -> @w:Bw -> @+i:U32 -> @+n:U32 -> Es

def ins.one source · line 1049 · raw

@ok:Bool -> @z:Lz -> @+j:U32 -> Lz

def ins.go source · line 1057 · raw

@k:Nat -> @z:Lz -> @+j:U32 -> @+n:U32 -> Lz

Positions inside a match join their chains too, when three bytes remain.

def es.emit source · line 1064 · raw

@hit:Bool -> @z:Lz -> @w:Bw -> @+i:U32 -> @+n:U32 -> @+blen:U32 -> @+bdist:U32 -> Es

def es.pick source · line 1072 · raw

@st:Ch -> @w:Bw -> @+n:U32 -> Es

def es.chain source · line 1077 · raw

@r:Pair(Lz, U32) -> @w:Bw -> @+i:U32 -> @+n:U32 -> Es

ponytail: greedy parse, 32 chain steps; lazy matching would compress text further.

def es.find source · line 1083 · raw

@short:Bool -> @z:Lz -> @w:Bw -> @+i:U32 -> @+n:U32 -> Es

def es.go source · line 1091 · raw

@fuel:Nat -> @st:Es -> Es

Each step consumes at least one byte, so n steps finish.

def es.out source · line 1103 · raw

@st:Es -> String

def slen source · line 1107 · raw

@s:String -> @+n:U32 -> U32

def src.fill source · line 1114 · raw

@s:String -> @+i:U32 -> @a:Array<U32> -> Array<U32>

def depth source · line 1122 · raw

@k:Nat -> @+d:Nat -> @+n:U32 -> Nat

The smallest depth whose 2^depth slots hold n.

def deflate source · line 1130 · raw

@+s:String -> String

Raw DEFLATE. The header bits are BFINAL = 1 and BTYPE = 01.

def str.of source · line 1135 · raw

@xs:List<&2, U32> -> @t:String -> String

def le32.put source · line 1142 · raw

@+v:U32 -> @t:String -> String

def be32.put source · line 1145 · raw

@+v:U32 -> @t:String -> String

def gzip source · line 1149 · raw

@+s:String -> String

One gzip member (RFC 1952): no flags, mtime 0, OS unknown.

def zlib source · line 1153 · raw

@+s:String -> String

RFC 1950: CMF 0x78 (32 KiB window), FLG 0x01 (fastest), then DEFLATE, then Adler-32.

def dec.new.got source · line 1196 · raw

@r:Result<&1, &1, Pair(U32, String), U32> -> Result<&1, &1, Pair(U32, String), Decoder>

def dec.new source · line 1203 · raw

@+kind:U32 -> IO(Result<&1, &1, Pair(U32, String), Decoder>)

def inflate.new source · line 1208 · raw

IO(Result<&1, &1, Pair(U32, String), Decoder>)

def brotli.new source · line 1211 · raw

IO(Result<&1, &1, Pair(U32, String), Decoder>)

def zstd.new source · line 1214 · raw

IO(Result<&1, &1, Pair(U32, String), Decoder>)

def dec.feed.words source · line 1223 · raw

@d:Decoder -> @+max:U32 -> @+len:U32 -> @words:Array<U32> -> IO(Pair(Decoder, Result<&1, &1, Pair(U32, String), Pair(U32, Array<U32>)>))

max caps the output of this call, not the total output of the stream.

def dec.finish source · line 1235 · raw

@d:Decoder -> IO(Result<&1, &1, Pair(U32, String), Unit>)

Finish validates the end marker and always releases the decoder.

Effects (foreign code)

effect zstd.words source · line 1158 · raw

@max:U32 -> @len:U32 -> @words:Array<U32> -> IO(Result<&1, &1, Pair(U32, String), Pair(U32, Array<U32>)>)

Zstandard frames (RFC 8878) through libzstd, as (len, words) like Wire.recv.words. Fails with EFBIG when the output passes max bytes, and ENOENT without libzstd.

foreign: effs/zlib.c, effs/zlib.js

effect inflate.words source · line 1165 · raw

@max:U32 -> @len:U32 -> @words:Array<U32> -> IO(Result<&1, &1, Pair(U32, String), Pair(U32, Array<U32>)>)

gzip, zlib, or raw DEFLATE data through libz, selected from the first two bytes. Every gzip member is decoded in turn, as gzip -d does. Fails with EFBIG when the output passes max bytes, and ENOENT without libz.

foreign: effs/zlib.c, effs/zlib.js

effect inflate.raw.words source · line 1171 · raw

@max:U32 -> @len:U32 -> @words:Array<U32> -> IO(Result<&1, &1, Pair(U32, String), Pair(U32, Array<U32>)>)

One raw DEFLATE stream through libz. Trailing bytes, truncated input and output above max fail.

foreign: effs/zlib.c, effs/zlib.js

effect gzip.words source · line 1177 · raw

@len:U32 -> @words:Array<U32> -> IO(Result<&1, &1, Pair(U32, String), Pair(U32, Array<U32>)>)

One gzip member at level 6 through libz.

foreign: effs/zlib.c, effs/zlib.js

effect brotli.words source · line 1183 · raw

@max:U32 -> @len:U32 -> @words:Array<U32> -> IO(Result<&1, &1, Pair(U32, String), Pair(U32, Array<U32>)>)

Brotli (RFC 7932) through libbrotlidec. Fails as zstd.words does.

foreign: effs/zlib.c, effs/zlib.js

effect dec.open source · line 1192 · raw

@kind:U32 -> IO(Result<&1, &1, Pair(U32, String), U32>)

foreign: effs/zlib.c, effs/zlib.js

effect dec.feed source · line 1217 · raw

@id:U32 -> @max:U32 -> @len:U32 -> @words:Array<U32> -> IO(Result<&1, &1, Pair(U32, String), Pair(U32, Array<U32>)>)

foreign: effs/zlib.c, effs/zlib.js

effect dec.finish.id source · line 1230 · raw

@id:U32 -> IO(Result<&1, &1, Pair(U32, String), Unit>)

foreign: effs/zlib.c, effs/zlib.js