inflate.bend checks
raw source on the hub · import element-compress@0.1.0.0/inflate.bend as Inflate
1 import
import Base
Types
type Tree source · line 23 · raw
Type
Tree@counts:Array<U32> -> @syms:Array<U32> -> @max:U32 -> @ok:U32 -> Tree
type St source · line 26 · raw
Type
St@inp:List<&2, U32> -> @tag:U32 -> @nbits:U32 -> @out:List<&2, U32> -> @ring:Array<U32> -> @opos:U32 -> @bad:U32 -> St
type Out source · line 701 · raw
Data
the bytes decoded, and the bytes after the final block (a & pair is Type:
Result wants Data)
Out@bytes:List<&2, U32> -> @rest:List<&2, U32> -> Out
Definitions
def bad_text source · line 31 · raw
@+code:U32 -> String
def mark source · line 42 · raw
@st:St -> @+code:U32 -> St
def mark_if source · line 47 · raw
@fail:Bool -> @st:St -> @+code:U32 -> St
def is_bad source · line 54 · raw
@st:St -> Pair(St, Bool)
def pull source · line 62 · raw
@st:St -> St
one more input byte into the tag (a missing byte is eight zero bits and bad 1, as tinf's overflow)
def pull_if source · line 71 · raw
@need:Bool -> @st:St -> St
def need source · line 78 · raw
@st:St -> @+num:U32 -> Pair(St, Bool)
def step source · line 83 · raw
@sn:Pair(St, Bool) -> St
def refill source · line 88 · raw
@st:St -> @+num:U32 -> St
at least num (<= 24) bits in the tag: three pulls at most
def take source · line 91 · raw
@st:St -> @+num:U32 -> Pair(St, U32)
def getbits source · line 99 · raw
@st:St -> @+num:U32 -> Pair(St, U32)
num bits (0 to 24), least significant first
def plus source · line 102 · raw
@+base:U32 -> @sb:Pair(St, U32) -> Pair(St, U32)
def align source · line 107 · raw
@st:St -> St
a stored block starts at a byte: the tag's partial byte is dropped
def emit source · line 114 · raw
@st:St -> @+b:U32 -> St
def emit_pair source · line 119 · raw
@sb:Pair(St, U32) -> St
def ring_back source · line 123 · raw
@r:Pair(Array<U32>, U32) -> @inp:List<&2, U32> -> @+tag:U32 -> @+nbits:U32 -> @out:List<&2, U32> -> @+opos:U32 -> @+bad:U32 -> Pair(St, U32)
def back source · line 128 · raw
@st:St -> @+dist:U32 -> Pair(St, U32)
the byte dist back
def copy source · line 134 · raw
@n:Nat -> @st:St -> @+dist:U32 -> St
n bytes from dist back (a run may overlap itself: byte by byte)
def copy_checked source · line 141 · raw
@fits:Bool -> @st:St -> @+len:U32 -> @+dist:U32 -> St
def opos_of source · line 148 · raw
@st:St -> Pair(St, U32)
def set_if source · line 156 · raw
@doit:Bool -> @a:Array<U32> -> @+i:U32 -> @+v:U32 -> Array<U32>
an Array is one owner: a conditional write is a match, not a pick
def tree_new source · line 163 · raw
Tree
def tree_bad source · line 166 · raw
@t:Tree -> Tree
def tree_bad_if source · line 171 · raw
@fail:Bool -> @t:Tree -> Tree
def tree_ok source · line 178 · raw
@t:Tree -> Pair(Tree, Bool)
def tree_max source · line 183 · raw
@t:Tree -> Pair(Tree, U32)
def with_counts source · line 188 · raw
@r:Pair(Array<U32>, U32) -> @syms:Array<U32> -> @+max:U32 -> @+ok:U32 -> Pair(Tree, U32)
def count_at source · line 192 · raw
@t:Tree -> @+i:U32 -> Pair(Tree, U32)
def with_syms source · line 197 · raw
@r:Pair(Array<U32>, U32) -> @counts:Array<U32> -> @+max:U32 -> @+ok:U32 -> Pair(Tree, U32)
def sym_at source · line 201 · raw
@t:Tree -> @+i:U32 -> Pair(Tree, U32)
def count_len_fin source · line 207 · raw
@r:Pair(Tree, U32) -> @+len:U32 -> @+i:U32 -> Tree
counts[len] += 1 and max = i, for a length that is not 0
def count_len source · line 214 · raw
@t:Tree -> @+len:U32 -> @+i:U32 -> Tree
def count_step source · line 218 · raw
@r:Pair(Array<U32>, U32) -> @+i:U32 -> @t:Tree -> Pair(Array<U32>, Tree)
pass 1: the counts of each length over lengths[0 .. num)
def count_go source · line 222 · raw
@n:Nat -> @+i:U32 -> @r:Pair(Array<U32>, Tree) -> Pair(Array<U32>, Tree)
def offs_step source · line 235 · raw
@r:Pair(Tree, U32) -> @+i:U32 -> @offs:Array<U32> -> @+avail:U32 -> @+ncodes:U32 -> @+over:U32 -> Pair(Pair(Tree, Array<U32>), Pair(U32, Pair(U32, U32)))
pass 2: offs[len] = the first code index of that length; avail the codes
left at this length (over-subscribed when used > avail); ncodes the total;
over counts 2 per over-subscribed length
the loop's state: the table, the offsets, (avail, (ncodes, over))
def offs_go source · line 241 · raw
@n:Nat -> @+i:U32 -> @r:Pair(Pair(Tree, Array<U32>), Pair(U32, Pair(U32, U32))) -> Pair(Pair(Tree, Array<U32>), Pair(U32, Pair(U32, U32)))
def place_put source · line 251 · raw
@r:Pair(Array<U32>, U32) -> @+i:U32 -> @+len:U32 -> @ls:Array<U32> -> @t:Tree -> Pair(Pair(Array<U32>, Array<U32>), Tree)
pass 3: syms[offs[len]] = i, offs[len] += 1, for each non-zero length
def place_step source · line 259 · raw
@r:Pair(Array<U32>, U32) -> @+i:U32 -> @offs:Array<U32> -> @t:Tree -> Pair(Pair(Array<U32>, Array<U32>), Tree)
def place_go source · line 263 · raw
@n:Nat -> @+i:U32 -> @r:Pair(Pair(Array<U32>, Array<U32>), Tree) -> Pair(Pair(Array<U32>, Array<U32>), Tree)
def verdict_fin source · line 275 · raw
@r:Pair(Tree, U32) -> @+ncodes:U32 -> @+over:U32 -> Tree
over: 2 per over-subscribed length, 1 when codes are left unused; a single code of length 1 is fine, as tinf allows; an empty table is fine too, the caller decides
def verdict source · line 281 · raw
@t:Tree -> @+ncodes:U32 -> @+over:U32 -> Tree
def single source · line 286 · raw
@t:Tree -> @+ncodes:U32 -> Tree
tinf: one code (always 0) gets a sibling code 1 with a symbol past max, so a stream using it is caught as "outside the table"
def build_fin source · line 292 · raw
@r:Pair(Pair(Array<U32>, Array<U32>), Tree) -> @+ncodes:U32 -> @+over:U32 -> Pair(Array<U32>, Tree)
def build_place source · line 296 · raw
@r:Pair(Pair(Tree, Array<U32>), Pair(U32, Pair(U32, U32))) -> @ls:Array<U32> -> @+num:U32 -> Pair(Array<U32>, Tree)
def build_offs source · line 301 · raw
@r:Pair(Array<U32>, Tree) -> @+num:U32 -> Pair(Array<U32>, Tree)
def build source · line 307 · raw
@ls:Array<U32> -> @+num:U32 -> Pair(Array<U32>, Tree)
A table from code lengths (0 = no code) lengths[0 .. num), num <= 320. The lengths come back (the caller reads the distance lengths after).
def tree_of source · line 310 · raw
@r:Pair(Array<U32>, Tree) -> Tree
def dec_found source · line 316 · raw
@r:Pair(Tree, U32) -> @st:St -> Pair(Pair(St, Tree), U32)
def dec_count source · line 320 · raw
@r:Pair(Tree, U32) -> @st:St -> @+base:U32 -> @+offs2:U32 -> @+len2:U32 -> Pair(Pair(Pair(St, Tree), Pair(U32, Pair(U32, U32))), Bool)
def dec_bit source · line 325 · raw
@r:Pair(St, U32) -> @t:Tree -> @+base:U32 -> @+offs:U32 -> @+len:U32 -> Pair(Pair(Pair(St, Tree), Pair(U32, Pair(U32, U32))), Bool)
def dec_go source · line 332 · raw
@n:Nat -> @r:Pair(Pair(Pair(St, Tree), Pair(U32, Pair(U32, U32))), Bool) -> Pair(Pair(St, Tree), U32)
One bit a step: offs is the index among the codes of this length, base
the codes of shorter lengths; found was decided by the step before.
def decode source · line 347 · raw
@st:St -> @t:Tree -> Pair(Pair(St, Tree), U32)
def fixed_len source · line 352 · raw
@+i:U32 -> U32
def fill_go source · line 355 · raw
@n:Nat -> @+i:U32 -> @ls:Array<U32> -> @+fixed:Bool -> Array<U32>
def fixed_lit source · line 362 · raw
Tree
def fixed_dist source · line 365 · raw
Tree
def clc_index source · line 371 · raw
@+i:U32 -> U32
the order the code length code lengths come in
def clc_step source · line 379 · raw
@r:Pair(St, U32) -> @+i:U32 -> @ls:Array<U32> -> Pair(St, Array<U32>)
def clc_go source · line 384 · raw
@n:Nat -> @+i:U32 -> @r:Pair(St, Array<U32>) -> Pair(St, Array<U32>)
the hclen code length code lengths, 3 bits each
def rep_go source · line 394 · raw
@n:Nat -> @+at:U32 -> @ls:Array<U32> -> @+sym:U32 -> Array<U32>
writes sym len times from at
def lens_write source · line 402 · raw
@r:Pair(St, U32) -> @clt:Tree -> @ls:Array<U32> -> @+at:U32 -> @+total:U32 -> @+sym:U32 -> @+prev:U32 -> Pair(Pair(Pair(Pair(St, Tree), Array<U32>), U32), Bool)
the repeat count read, the lengths written, the next step decided
def lens_prev source · line 412 · raw
@r:Pair(Array<U32>, U32) -> @st:St -> @clt:Tree -> @+at:U32 -> @+total:U32 -> @+sym:U32 -> Pair(Pair(Pair(Pair(St, Tree), Array<U32>), U32), Bool)
def lens_sym source · line 421 · raw
@r:Pair(Pair(St, Tree), U32) -> @ls:Array<U32> -> @+at:U32 -> @+total:U32 -> Pair(Pair(Pair(Pair(St, Tree), Array<U32>), U32), Bool)
def lens_go source · line 429 · raw
@n:Nat -> @+total:U32 -> @r:Pair(Pair(Pair(Pair(St, Tree), Array<U32>), U32), Bool) -> Pair(Pair(St, Tree), Array<U32>)
The hlit + hdist code lengths, decoded with the code length table: a symbol 0-15 is a length, 16 repeats the previous 3-6 times, 17 writes 3-10 zeros, 18 writes 11-138 zeros. The state: stop (decided the step before), the decoder and table, the lengths, the write position.
def shift_step source · line 444 · raw
@r:Pair(Array<U32>, U32) -> @+i:U32 -> Array<U32>
def shift_go source · line 449 · raw
@n:Nat -> @+i:U32 -> @ls:Array<U32> -> @+from:U32 -> Array<U32>
the distance lengths follow the literal ones: moved down to 0
def dyn_dist_fin source · line 456 · raw
@r:Pair(Array<U32>, Tree) -> @st:St -> @lt:Tree -> Pair(Pair(St, Tree), Tree)
def dyn_dist source · line 460 · raw
@r:Pair(Array<U32>, Tree) -> @st:St -> @+hlit:U32 -> @+hdist:U32 -> Pair(Pair(St, Tree), Tree)
def dyn_eob source · line 464 · raw
@r:Pair(Array<U32>, U32) -> @st:St -> @+hlit:U32 -> @+hdist:U32 -> Pair(Pair(St, Tree), Tree)
def dyn_tables source · line 468 · raw
@r:Pair(Pair(St, Tree), Array<U32>) -> @+hlit:U32 -> @+hdist:U32 -> Pair(Pair(St, Tree), Tree)
def dyn_clt_ok source · line 472 · raw
@r:Pair(Tree, Bool) -> @st:St -> @+hlit:U32 -> @+hdist:U32 -> Pair(Pair(St, Tree), Tree)
def dyn_clt source · line 476 · raw
@r:Pair(Array<U32>, Tree) -> @st:St -> @+hlit:U32 -> @+hdist:U32 -> Pair(Pair(St, Tree), Tree)
def dyn_lens source · line 480 · raw
@r:Pair(St, Array<U32>) -> @+hlit:U32 -> @+hdist:U32 -> Pair(Pair(St, Tree), Tree)
def dyn_header source · line 484 · raw
@st:St -> @+hlit:U32 -> @+hdist:U32 -> @+hclen:U32 -> Pair(Pair(St, Tree), Tree)
def dyn_hclen source · line 488 · raw
@r:Pair(St, U32) -> @+hlit:U32 -> @+hdist:U32 -> Pair(Pair(St, Tree), Tree)
def dyn_hdist source · line 492 · raw
@r:Pair(St, U32) -> @+hlit:U32 -> Pair(Pair(St, Tree), Tree)
def dyn_hlit source · line 496 · raw
@r:Pair(St, U32) -> Pair(Pair(St, Tree), Tree)
def dynamic source · line 500 · raw
@st:St -> Pair(Pair(St, Tree), Tree)
def len_extra source · line 505 · raw
@+s:U32 -> U32
def len_base source · line 508 · raw
@+s:U32 -> U32
def dist_extra source · line 516 · raw
@+s:U32 -> U32
def dist_base source · line 519 · raw
@+s:U32 -> U32
def match_fits source · line 523 · raw
@r:Pair(St, U32) -> @+len:U32 -> @+dist:U32 -> St
def match_dist source · line 527 · raw
@r:Pair(St, U32) -> @lt:Tree -> @dt:Tree -> @+len:U32 -> @ok:Bool -> Pair(Pair(St, Tree), Tree)
def match_dmax source · line 531 · raw
@r:Pair(Tree, U32) -> @s:St -> @lt:Tree -> @+len:U32 -> @+dsym:U32 -> Pair(Pair(St, Tree), Tree)
def do_match source · line 536 · raw
@r:Pair(Pair(St, Tree), U32) -> @lt:Tree -> @+len:U32 -> Pair(Pair(St, Tree), Tree)
def code_len source · line 540 · raw
@r:Pair(St, U32) -> @dt:Tree -> @lt:Tree -> Pair(Pair(St, Tree), Tree)
def code_pick source · line 544 · raw
@lit:Bool -> @end:Bool -> @ok:Bool -> @s:St -> @lt:Tree -> @dt:Tree -> @+sym:U32 -> @+ls:U32 -> Pair(Pair(Pair(St, Tree), Tree), Bool)
def code_kind source · line 561 · raw
@r:Pair(Tree, U32) -> @s:St -> @dt:Tree -> @+sym:U32 -> Pair(Pair(Pair(St, Tree), Tree), Bool)
a literal is emitted, 256 ends the block (stop at the next step), 257-285 are a length whose distance code follows, anything else is an error
def one_code source · line 569 · raw
@r:Pair(Pair(St, Tree), U32) -> @dt:Tree -> Pair(Pair(Pair(St, Tree), Tree), Bool)
def codes_or source · line 573 · raw
@bad:Bool -> @s:St -> @lt:Tree -> @dt:Tree -> Pair(Pair(Pair(St, Tree), Tree), Bool)
def codes_bad source · line 581 · raw
@r:Pair(St, Bool) -> @lt:Tree -> @dt:Tree -> Pair(Pair(Pair(St, Tree), Tree), Bool)
a step: unless the decoder is already bad, one symbol
def codes_go source · line 585 · raw
@n:Nat -> @r:Pair(Pair(Pair(St, Tree), Tree), Bool) -> St
def fuel_of source · line 601 · raw
@st:St -> Pair(St, U32)
a bound on the symbols left: eight a byte of input, plus the tag's
def codes_fuel source · line 607 · raw
@r:Pair(St, U32) -> @lt:Tree -> @dt:Tree -> St
def codes source · line 611 · raw
@st:St -> @lt:Tree -> @dt:Tree -> St
def stored_go source · line 616 · raw
@n:Nat -> @st:St -> St
def stored_nlen source · line 623 · raw
@r:Pair(St, U32) -> @+len:U32 -> St
def stored_len source · line 628 · raw
@r:Pair(St, U32) -> St
def stored source · line 633 · raw
@st:St -> St
a stored block: LEN, NLEN, then LEN bytes (RFC 1951 3.2.4)
def dyn_ok_d source · line 636 · raw
@r:Pair(Tree, Bool) -> @s:St -> @lt:Tree -> @okl:Bool -> St
def dyn_ok_l source · line 640 · raw
@r:Pair(Tree, Bool) -> @s:St -> @dt:Tree -> St
def dyn_block source · line 644 · raw
@r:Pair(Pair(St, Tree), Tree) -> St
def block_pick source · line 648 · raw
@stored_:Bool -> @fixed:Bool -> @dyn:Bool -> @st:St -> St
def block source · line 663 · raw
@st:St -> @+btype:U32 -> St
def block_done source · line 666 · raw
@r:Pair(St, Bool) -> @+final:U32 -> Pair(St, Bool)
def block_type source · line 670 · raw
@r:Pair(St, U32) -> @+final:U32 -> Pair(St, Bool)
def block_header source · line 674 · raw
@r:Pair(St, U32) -> Pair(St, Bool)
def blocks_flip source · line 678 · raw
@r:Pair(St, Bool) -> Pair(St, Bool)
def blocks_go source · line 682 · raw
@n:Nat -> @r:Pair(St, Bool) -> St
blocks until the one marked final (or an error); last decided before
def finish source · line 704 · raw
@st:St -> Result<&2, &2, String, Out>
def inflate_rest source · line 711 · raw
@+bytes:List<&2, U32> -> Result<&2, &2, String, Out>
The bytes a raw DEFLATE stream (RFC 1951) encodes, beside the bytes after its final block (a gzip trailer, a zlib checksum), or why it cannot be read.
def only_bytes source · line 715 · raw
@r:Result<&2, &2, String, Out> -> Result<&2, &2, String, List<&2, U32>>
def inflate source · line 725 · raw
@+bytes:List<&2, U32> -> Result<&2, &2, String, List<&2, U32>>
The bytes alone; what follows the final block is ignored.