~/bend-docscommunity

src/inflate.bend checks

raw source on the hub · import emerging-ezimg@1.5.0.0/src/inflate.bend as Inflate

src/inflate: zlib-wrapped DEFLATE for a PNG IDAT stream (compression method 0). Blocks are stored, fixed Huffman, or dynamic Huffman.

Trusted, not proved: decode_png reads the fixed and dynamic Huffman DEFLATE streams other encoders (zlib, libpng) write. Only stored blocks are proved (laws/InflateReadsStoredBlocks.bend); a proof for dynamic Huffman needs a reference compressor. The Pillow check in bench/ exercises it, which is a test. Of inflate.upto's budget, what it answers inflate answers (laws/InflateUptoKeepsBudget.bend), and it answers the stored streams ezimg writes (laws/InflateUptoReadsStoredBlocks.bend).

The reader takes one bit at a time, eight per input byte. A Huffman code is read the canonical way (RFC 1951 3.2.2, as zlib's puff.c does): per length the reader keeps the code so far, the first code of that length and the index of that code among the symbols, so a symbol costs one step per bit and one walk of a 512-leaf tree. Back-references read a 32768-byte window kept as a tree by position, so a copy costs the same at any distance. Code lengths are checked as zlib's inflate checks them: no over-subscribed set, no incomplete set but a single code of length 1 or no code at all for the literal/length and distance codes, an end-of-block code, at most 286 literal/length and 30 distance codes.

1 import
import Base

Types

type Ph source · line 25 · raw

Data

where the bit reader is

type Md source · line 33 · raw

Data

which integer the bit reader is filling

type Wh source · line 50 · raw

Data

which Huffman table a symbol is read from

type Tr source · line 57 · raw

Data

a complete binary tree of U32 leaves, indexed from the root by the top bits of an index. TNone is a subtree of zeros, so an empty one is one cell.

type Hc source · line 64 · raw

Data

one Huffman table: how many codes have each length from 1 to 15, and the symbols in canonical order (by length, then by symbol) in a 512-leaf tree

type Hs source · line 70 · raw

Data

the Huffman tables and the dynamic header being read: the code lengths so far (newest first), how many are still to come, the length a repeat code repeats and its base count, HLIT and HDIST

type Cx source · line 79 · raw

Data

what the fields and symbols read so far leave: BFINAL of this block; the stored bytes, Adler bytes or 3-bit code lengths left, or a match length; a distance base; the bytes emitted, newest first; the window of the last 32768 of them by position, and how many bytes it holds (mod 2^32); the stored bytes not yet copied into the window; CMF, then the Adler-32 read so far; the Huffman state

type St source · line 86 · raw

Data

one inflate reader. Reading an integer (PByte): its bits so far, how many, and its width. Reading a Huffman code (PSym): the code so far, the first code of its length, the index of that first code among the symbols, and the counts of the lengths still to try. skew is the bit count mod 8.

type lens.Split source · line 290 · raw

Data

the two halves of a code-length list

type Walk source · line 1139 · raw

Data

where a canonical Huffman walk stands: the bits read so far, the first code of their length and that code's index in canonical order

Definitions

def hc.none source · line 90 · raw

Hc

an empty Huffman table

def hs.none source · line 94 · raw

Hs

no Huffman tables yet

def cx.none source · line 98 · raw

Cx

nothing read yet

def st.fail source · line 102 · raw

St

a reader that has failed

def st.empty source · line 106 · raw

St

the reader before the first byte

def st.byte source · line 110 · raw

@md:Md -> @+need:U32 -> @+skew:U32 -> @cx:Cx -> St

a fresh integer field of need bits

def tr.lo source · line 117 · raw

@+ix:U32 -> Bool

whether the walk from here goes to the low half: the top bit of the index

def tr.get source · line 121 · raw

@dd:Nat -> @tt:Tr -> @zz:Bool -> @+ix:U32 -> U32

the leaf at the index whose top dd bits are the path; zz is tr.lo(ix)

def tr.set source · line 137 · raw

@dd:Nat -> @tt:Tr -> @zz:Bool -> @+ix:U32 -> @+val:U32 -> Tr

write the leaf at the index whose top dd bits are the path

def tr.leaves source · line 161 · raw

@xs:List<&2, U32> -> List<&2, Tr>

one leaf per value

def tr.pairs source · line 169 · raw

@xs:List<&2, Tr> -> List<&2, Tr>

pair neighbouring trees into the next level up

def tr.top source · line 179 · raw

@xs:List<&2, Tr> -> Tr

the one tree left at the top

def tr.up source · line 187 · raw

@dd:Nat -> @xs:List<&2, Tr> -> Tr

a tree of depth dd whose leaves, in order, are these trees

def win.get source · line 195 · raw

@win:Tr -> @+pos:U32 -> U32

the byte at a position of the window: the low 15 bits pick the leaf

def win.put source · line 200 · raw

@win:Tr -> @+pos:U32 -> @+bb:U32 -> Tr

write the byte at a position of the window

def win.sync source · line 206 · raw

@nn:Nat -> @xs:List<&2, U32> -> @win:Tr -> @+pos:U32 -> Tr

copy the newest nn bytes of a newest-first list into the window, the head at pos and each older one just before it

def huff.zeros source · line 219 · raw

@nn:Nat -> List<&2, U32>

zeros: a code-length list of zeros

def tab.at source · line 228 · raw

@xs:List<&2, U32> -> @zz:Bool -> @+ii:U32 -> U32

the value at an index, or zero past the end. zz is whether ii is zero: the walk stops at that value

def lens.set source · line 238 · raw

@xs:List<&2, U32> -> @zz:Bool -> @+ii:U32 -> @+vv:U32 -> List<&2, U32>

write an index. zz is "this is the slot".

def len.base source · line 251 · raw

List<&2, U32>

length bases for symbols 257..285

def len.extra source · line 255 · raw

List<&2, U32>

extra bits for symbols 257..285

def dist.base source · line 259 · raw

List<&2, U32>

distance bases

def dist.extra source · line 264 · raw

List<&2, U32>

extra bits for distances

def clen.order source · line 268 · raw

List<&2, U32>

code-length symbol order

def clen.place source · line 272 · raw

@order:List<&2, U32> -> @lens:List<&2, U32> -> @acc:List<&2, U32> -> List<&2, U32>

drop code-length values into the 19 symbol slots

def lens.rep source · line 282 · raw

@kk:Nat -> @+vv:U32 -> @lens:List<&2, U32> -> List<&2, U32>

repeat a length n times onto the front of the list

def lens.split source · line 294 · raw

@xs:List<&2, U32> -> @zz:Bool -> @+nn:U32 -> @acc:List<&2, U32> -> Maybe<&2, lens.Split>

split a list after n elements. The Bool is "n is zero".

def hc.inc source · line 304 · raw

@zz:Bool -> @+nn:U32 -> U32

one more when zz

def hc.nz.keep source · line 312 · raw

@zz:Bool -> @+ee:U32 -> @acc:List<&2, U32> -> List<&2, U32>

keep a code that has a length

def hc.nz source · line 321 · raw

@ls:List<&2, U32> -> @+sym:U32 -> @acc:List<&2, U32> -> List<&2, U32>

the symbols whose length is not zero, each packed as len << 16 | sym, newest first: a sparse table is built from a short list

def hc.num source · line 329 · raw

@ls:List<&2, U32> -> @+ll:U32 -> @+nn:U32 -> U32

how many of the packed codes have length ll

def hc.cnts source · line 337 · raw

@kk:Nat -> @+ll:U32 -> @+ls:List<&2, U32> -> List<&2, U32>

bl_count for the lengths ll to ll + kk - 1, from the packed codes

def hc.keep source · line 345 · raw

@zz:Bool -> @+sym:U32 -> @acc:List<&2, U32> -> List<&2, U32>

keep the symbol when its length matches

def hc.pick source · line 353 · raw

@ls:List<&2, U32> -> @+ll:U32 -> @acc:List<&2, U32> -> List<&2, U32>

the symbols of the packed codes of length ll, in order, onto a newest-first list

def hc.sorted source · line 361 · raw

@kk:Nat -> @+ll:U32 -> @+ls:List<&2, U32> -> @acc:List<&2, U32> -> List<&2, U32>

the symbols of the packed codes (in symbol order), by length and then by symbol

def hc.room source · line 370 · raw

@cs:List<&2, U32> -> @zz:Bool -> @+left:U32 -> Maybe<&2, U32>

the codes left unused once lengths 1 to 15 are counted (puff's left), or none when the lengths ask for more codes than there are. zz is "too many".

def hc.sum source · line 381 · raw

@cs:List<&2, U32> -> @+nn:U32 -> U32

the sum of the counts

def hc.first source · line 389 · raw

@cs:List<&2, U32> -> U32

the count of length 1

def hc.valid source · line 398 · raw

@rr:Maybe<&2, U32> -> @+total:U32 -> @+one:U32 -> @single:Bool -> Bool

zlib's inflate_table: refuse an over-subscribed set; refuse an incomplete one unless it has no code, or single allows one code of length 1

def hc.build.ok source · line 407 · raw

@zz:Bool -> @cs:List<&2, U32> -> @+ls:List<&2, U32> -> Maybe<&2, Hc>

the table once its counts are checked

def hc.build source · line 416 · raw

@ls:List<&2, U32> -> @single:Bool -> Maybe<&2, Hc>

the canonical Huffman table of a length per symbol, if the lengths are a set zlib accepts

def seq source · line 422 · raw

@nn:Nat -> @+from:U32 -> @acc:List<&2, U32> -> List<&2, U32>

nn symbols from from on, onto a list

def lit.fixed source · line 431 · raw

Hc

the fixed literal/length table (RFC 1951 3.2.6): 256-279 have length 7, 0-143 and 280-287 length 8, 144-255 length 9

def dist.fixed source · line 436 · raw

Hc

the fixed distance table: thirty-two codes of length 5

def hc.sym source · line 440 · raw

@hc:Hc -> @+ix:U32 -> U32

the symbol at an index of a table's canonical order

def hc.cnt source · line 447 · raw

@hc:Hc -> List<&2, U32>

the counts of a table

def hs.cnt source · line 453 · raw

@wh:Wh -> @hs:Hs -> List<&2, U32>

the counts of the table wh names

def hs.sym source · line 463 · raw

@wh:Wh -> @hs:Hs -> @+ix:U32 -> U32

a symbol of the table wh names

def hs.fixed source · line 473 · raw

@hs:Hs -> Hs

fixed literal/length and distance tables

def hs.hlit source · line 479 · raw

@hs:Hs -> @+val:U32 -> Hs

HLIT

def hs.hdist source · line 485 · raw

@hs:Hs -> @+val:U32 -> Hs

HDIST

def hs.clear source · line 491 · raw

@hs:Hs -> Hs

no code lengths yet

def hs.push source · line 497 · raw

@hs:Hs -> @+val:U32 -> Hs

one more code length, newest first

def hs.store source · line 503 · raw

@hs:Hs -> @+val:U32 -> Hs

one more literal/length or distance code length, one fewer to come

def hs.clen source · line 509 · raw

@hs:Hs -> @ct:Hc -> Hs

the code-length table is built: HLIT + HDIST + 258 code lengths to come

def hs.rep source · line 515 · raw

@hs:Hs -> @+prev:U32 -> @+rep:U32 -> Hs

a repeat code: the length it repeats and its base count

def hs.fill source · line 521 · raw

@hs:Hs -> @+nn:U32 -> Hs

a repeat of nn lengths

def hs.tables source · line 527 · raw

@hs:Hs -> @lt:Hc -> @dt:Hc -> Hs

both tables of a dynamic block

def hs.lens source · line 533 · raw

@hs:Hs -> List<&2, U32>

the code lengths so far

def hs.nleft source · line 539 · raw

@hs:Hs -> U32

the code lengths still to come

def hs.rep.of source · line 545 · raw

@hs:Hs -> U32

the base count of the repeat code being read

def hs.hlit.of source · line 551 · raw

@hs:Hs -> U32

HLIT

def adler.go source · line 560 · raw

@xs:List<&2, U32> -> @+aa:U32 -> @+bb:U32 -> Pair(U32, U32)

Adler-32, one byte at a time

def adler.fin source · line 570 · raw

@pp:Pair(U32, U32) -> U32

pack the two Adler sums

def adler.of source · line 576 · raw

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

Adler-32 of chronological bytes

def cx.final source · line 583 · raw

@cx:Cx -> Bool

BFINAL

def cx.left source · line 589 · raw

@cx:Cx -> U32

left

def cx.dist source · line 595 · raw

@cx:Cx -> U32

the distance base

def cx.wpos source · line 601 · raw

@cx:Cx -> U32

the bytes the window holds

def cx.hs source · line 607 · raw

@cx:Cx -> Hs

the Huffman state

def cx.with source · line 613 · raw

@cx:Cx -> @hs:Hs -> Cx

another Huffman state

def cx.set.left source · line 619 · raw

@cx:Cx -> @+val:U32 -> Cx

another left

def cx.set.dist source · line 625 · raw

@cx:Cx -> @+val:U32 -> Cx

another distance base

def cx.cnt source · line 631 · raw

@wh:Wh -> @cx:Cx -> List<&2, U32>

the counts of the table wh names

def cx.sym source · line 637 · raw

@wh:Wh -> @cx:Cx -> @+ix:U32 -> U32

a symbol of the table wh names

def cx.emit source · line 643 · raw

@cx:Cx -> @+bb:U32 -> Cx

emit one byte: onto the output and into the window

def cx.copy1 source · line 649 · raw

@cx:Cx -> @+dd:U32 -> Cx

copy one byte from dd back

def copy.run source · line 656 · raw

@nn:Nat -> @+dd:U32 -> @cx:Cx -> Cx

copy nn bytes from dd back

def win.cap source · line 664 · raw

@zz:Bool -> @+pend:U32 -> U32

the most stored bytes the window can take

def cx.sync source · line 672 · raw

@cx:Cx -> Cx

a Huffman block starts: the stored bytes since the last one go into the window

def cx.fixed source · line 680 · raw

@cx:Cx -> Cx

the fixed tables

def st.sym source · line 689 · raw

@+wh:Wh -> @+skew:U32 -> @+cx:Cx -> St

a Huffman code starts in the table wh

def rd.begin source · line 693 · raw

@md:Md -> @+skew:U32 -> @cx:Cx -> St

start the field md asks for, now that the stream is aligned

def rd.align source · line 703 · raw

@zz:Bool -> @md:Md -> @+skew:U32 -> @cx:Cx -> St

a byte-aligned field starts, or a padding bit is still waiting

def rd.to_align source · line 711 · raw

@md:Md -> @+skew:U32 -> @cx:Cx -> St

skip bits up to the next byte, then read md

def rd.after.at source · line 715 · raw

@zz:Bool -> @+skew:U32 -> @cx:Cx -> St

the next block, or the Adler checksum when this was the last block

def rd.after source · line 723 · raw

@+skew:U32 -> @+cx:Cx -> St

a block has ended

def rd.cmf.ok source · line 727 · raw

@zz:Bool -> @+val:U32 -> @+skew:U32 -> @cx:Cx -> St

CMF checked: keep it in run for FLG

def rd.cmf source · line 735 · raw

@+val:U32 -> @+skew:U32 -> @cx:Cx -> St

CMF: method 8, window at most 32K

def rd.flg.ok source · line 739 · raw

@zz:Bool -> @+skew:U32 -> @cx:Cx -> St

FLG checked

def rd.flg source · line 747 · raw

@+val:U32 -> @+skew:U32 -> @cx:Cx -> St

FLG: the header check, and no preset dictionary

def rd.blk.dyn source · line 755 · raw

@z2:Bool -> @+skew:U32 -> @cx:Cx -> St

a dynamic header, or the reserved block type

def rd.blk.huff source · line 763 · raw

@z1:Bool -> @z2:Bool -> @+skew:U32 -> @cx:Cx -> St

fixed or dynamic Huffman

def rd.blk.ty source · line 771 · raw

@z0:Bool -> @z1:Bool -> @z2:Bool -> @+skew:U32 -> @cx:Cx -> St

a stored block, or a Huffman block with the window brought up to date

def rd.blk.go source · line 779 · raw

@fin:Bool -> @z0:Bool -> @z1:Bool -> @z2:Bool -> @+skew:U32 -> @cx:Cx -> St

BFINAL is kept; BTYPE picks the block

def rd.blk source · line 785 · raw

@+val:U32 -> @+skew:U32 -> @cx:Cx -> St

BFINAL is the low bit. BTYPE is the next two.

def rd.len source · line 790 · raw

@+val:U32 -> @+skew:U32 -> @cx:Cx -> St

stored LEN, then NLEN. The LEN bytes are owed to the window.

def rd.nlen.ok source · line 796 · raw

@zz:Bool -> @+skew:U32 -> @cx:Cx -> St

an empty stored block ends the block; otherwise the bytes follow

def rd.nlen.chk source · line 804 · raw

@zz:Bool -> @+skew:U32 -> @+cx:Cx -> St

NLEN checked

def rd.nlen source · line 812 · raw

@+val:U32 -> @+skew:U32 -> @+cx:Cx -> St

NLEN is the one's complement of LEN

def rd.data.z source · line 816 · raw

@zz:Bool -> @+skew:U32 -> @cx:Cx -> St

the stored block's end, or its next byte

def rd.data source · line 824 · raw

@+val:U32 -> @+skew:U32 -> @cx:Cx -> St

consume one stored byte

def rd.hlit.ok source · line 831 · raw

@zz:Bool -> @+val:U32 -> @+skew:U32 -> @+cx:Cx -> St

HLIT checked: at most 286 literal/length codes

def rd.hlit source · line 839 · raw

@+val:U32 -> @+skew:U32 -> @cx:Cx -> St

HLIT, then HDIST

def rd.hdist.ok source · line 843 · raw

@zz:Bool -> @+val:U32 -> @+skew:U32 -> @+cx:Cx -> St

HDIST checked: at most 30 distance codes

def rd.hdist source · line 851 · raw

@+val:U32 -> @+skew:U32 -> @cx:Cx -> St

HDIST, then HCLEN

def rd.hclen source · line 855 · raw

@+val:U32 -> @+skew:U32 -> @cx:Cx -> St

HCLEN, then that many 3-bit code lengths

def rd.clen.got source · line 861 · raw

@mm:Maybe<&2, Hc> -> @+skew:U32 -> @+cx:Cx -> St

the code-length table, if its lengths are complete

def rd.clen.z source · line 869 · raw

@zz:Bool -> @+skew:U32 -> @+cx:Cx -> St

code lengths are in. Build the code-length table and read the rest.

def rd.clen source · line 878 · raw

@+val:U32 -> @+skew:U32 -> @cx:Cx -> St

one 3-bit code length

def rd.finish.dist source · line 885 · raw

@mm:Maybe<&2, Hc> -> @lt:Hc -> @+skew:U32 -> @+cx:Cx -> St

the distance table, if its lengths are a set zlib accepts

def rd.finish.lit source · line 893 · raw

@mm:Maybe<&2, Hc> -> @+dists:List<&2, U32> -> @+skew:U32 -> @+cx:Cx -> St

the literal/length table, if its lengths are a set zlib accepts

def rd.finish.eob source · line 901 · raw

@zz:Bool -> @+lits:List<&2, U32> -> @+dists:List<&2, U32> -> @+skew:U32 -> @+cx:Cx -> St

the end-of-block code must have a length

def rd.finish.at source · line 909 · raw

@mm:Maybe<&2, lens.Split> -> @+skew:U32 -> @+cx:Cx -> St

both length lists are in

def rd.finish source · line 917 · raw

@+skew:U32 -> @+cx:Cx -> St

split the length list into literal and distance tables

def rd.clen.more source · line 924 · raw

@zz:Bool -> @+skew:U32 -> @cx:Cx -> St

all the code lengths are in, or more follow

def rd.push source · line 932 · raw

@+sy:U32 -> @+skew:U32 -> @+cx:Cx -> St

a code length of 0 to 15

def rd.rep16 source · line 937 · raw

@ls:List<&2, U32> -> @+skew:U32 -> @+cx:Cx -> St

code 16 repeats the last length stored, so one must be

def rd.clen.t source · line 945 · raw

@z18:Bool -> @+skew:U32 -> @+cx:Cx -> St

code 18, or an illegal code-length symbol

def rd.clen.s source · line 953 · raw

@z17:Bool -> @z18:Bool -> @+skew:U32 -> @+cx:Cx -> St

codes 17 and 18 repeat zero

def rd.clen.r source · line 961 · raw

@z16:Bool -> @z17:Bool -> @z18:Bool -> @+skew:U32 -> @+cx:Cx -> St

repeat codes 16, 17, and 18

def rd.clens source · line 969 · raw

@z15:Bool -> @z16:Bool -> @z17:Bool -> @z18:Bool -> @+sy:U32 -> @+skew:U32 -> @cx:Cx -> St

one code-length symbol

def rd.mrep.z source · line 977 · raw

@zbad:Bool -> @+nn:U32 -> @+skew:U32 -> @+cx:Cx -> St

reject a repeat that runs past the list

def rd.mrep source · line 986 · raw

@+val:U32 -> @+skew:U32 -> @+cx:Cx -> St

extra bits of a repeat code

def rd.copy.at source · line 992 · raw

@zz:Bool -> @+dd:U32 -> @+skew:U32 -> @+cx:Cx -> St

copy left bytes from dd back, if the output reaches that far

def rd.copy source · line 1000 · raw

@+dd:U32 -> @+skew:U32 -> @+cx:Cx -> St

a back-reference of left bytes at distance dd

def rd.xdist source · line 1004 · raw

@+val:U32 -> @+skew:U32 -> @+cx:Cx -> St

extra distance bits are added to the base in dist

def rd.xdist0 source · line 1008 · raw

@zz:Bool -> @+extra:U32 -> @+base:U32 -> @+skew:U32 -> @cx:Cx -> St

no extra distance bits: the base is the distance

def rd.distsym source · line 1016 · raw

@zz:Bool -> @+sy:U32 -> @+skew:U32 -> @cx:Cx -> St

a distance symbol

def rd.xlen source · line 1025 · raw

@+val:U32 -> @+skew:U32 -> @+cx:Cx -> St

extra length bits added to the base kept in left

def rd.xlen0 source · line 1029 · raw

@zz:Bool -> @+extra:U32 -> @+base:U32 -> @+skew:U32 -> @cx:Cx -> St

no extra length bits

def rd.lit.n source · line 1037 · raw

@zz:Bool -> @+sy:U32 -> @+skew:U32 -> @cx:Cx -> St

a length symbol, or an illegal one

def rd.lit.e source · line 1047 · raw

@zend:Bool -> @zlen:Bool -> @+sy:U32 -> @+skew:U32 -> @cx:Cx -> St

end of block, or a length code

def rd.lit source · line 1055 · raw

@zlit:Bool -> @zend:Bool -> @zlen:Bool -> @+sy:U32 -> @+skew:U32 -> @cx:Cx -> St

a literal byte

def rd.symbol source · line 1063 · raw

@wh:Wh -> @+sy:U32 -> @+skew:U32 -> @cx:Cx -> St

dispatch a decoded symbol

def rd.adler.eq source · line 1073 · raw

@zz:Bool -> @+skew:U32 -> @cx:Cx -> St

Adler matches the bytes

def rd.adler.z source · line 1081 · raw

@zz:Bool -> @+skew:U32 -> @cx:Cx -> St

one more Adler byte, or the check

def rd.adler source · line 1090 · raw

@+val:U32 -> @+skew:U32 -> @cx:Cx -> St

one big-endian Adler byte

def rd.field source · line 1098 · raw

@md:Md -> @+val:U32 -> @+skew:U32 -> @cx:Cx -> St

a finished integer field

def rd.raw source · line 1130 · raw

@zz:Bool -> @md:Md -> @+acc:U32 -> @+got:U32 -> @+need:U32 -> @+skew:U32 -> @cx:Cx -> St

keep a partial integer, or dispatch it

def rd.hbit.at source · line 1144 · raw

@zz:Bool -> @rest:List<&2, U32> -> @+wh:Wh -> @wk:Walk -> @+cnt:U32 -> @+skew:U32 -> @+cx:Cx -> St

the code is index + code - first in canonical order when it is below first + count; otherwise try the next length

def rd.hbit source · line 1153 · raw

@cur:List<&2, U32> -> @+wh:Wh -> @+code:U32 -> @+first:U32 -> @+index:U32 -> @+skew:U32 -> @cx:Cx -> St

one Huffman bit is in code. Past length 15 no symbol is an error.

def rd.skew source · line 1161 · raw

@+skew:U32 -> U32

the bit count mod 8 after one more bit

def rd.step source · line 1166 · raw

@st:St -> @+bit:U32 -> St

advance one bit, in the phase the reader is in. The bit count mod 8 tracks byte alignment.

def rd.step8 source · line 1184 · raw

@st:St -> @+bb:U32 -> St

advance the eight bits of a byte, low bit first

def st.over.lim source · line 1198 · raw

@bud:Maybe<&2, Nat> -> @+nn:Nat -> Bool

past the budget bud, if any, with nn bytes in the window

def st.over.at source · line 1207 · raw

@nn:Nat -> @bud:Maybe<&2, Nat> -> Bool

past the budget, nn the bytes in the window. The window is tested first: a reader with an empty window is within any budget.

def st.over source · line 1217 · raw

@st:St -> @bud:Maybe<&2, Nat> -> Bool

the bytes a reader has emitted past the budget. Stored bytes count once a Huffman block starts after them, and at the end: a stored block's bytes are read from the input one for one, so they cannot expand.

def st.cap.at source · line 1223 · raw

@zz:Bool -> @st:St -> St

a reader past the budget fails

def st.cap source · line 1231 · raw

@+st:St -> @+bud:Maybe<&2, Nat> -> St

the reader, failed once it is past the budget

def inflate.upto.fin source · line 1235 · raw

@zz:Bool -> @out:List<&2, U32> -> Maybe<&2, List<&2, U32>>

the output, when it fits the budget

def inflate.out source · line 1243 · raw

@bud:Maybe<&2, Nat> -> @+out:List<&2, U32> -> Maybe<&2, List<&2, U32>>

a finished reader's answer: its output, measured against the budget if any

def inflate.drive source · line 1252 · raw

@xs:List<&2, U32> -> @st:St -> @+bud:Maybe<&2, Nat> -> Maybe<&2, List<&2, U32>>

walk the bytes until the reader finishes or the bytes run out, capping the reader after each byte when there is a budget

def inflate source · line 1264 · raw

@xs:List<&2, U32> -> Maybe<&2, List<&2, U32>>

inflate a zlib wrapper around DEFLATE, as PNG compression method 0

def inflate.upto source · line 1276 · raw

@+lim:Nat -> @xs:List<&2, U32> -> Maybe<&2, List<&2, U32>>

inflate with an output budget of lim bytes: the output of inflate when it has at most lim bytes, and none otherwise (laws/InflateUptoKeepsBudget.bend proves what it answers inflate answers; laws/InflateUptoReadsStoredBlocks.bend that it answers the stored streams ezimg writes). It stops at the first input byte after which the bytes a Huffman block has emitted pass lim: past the budget by at most what one input byte decodes to (eight 258-byte back-references), so a hostile stream cannot expand far beyond it. Stored bytes, which cannot expand, are counted at the next Huffman block and at the end. Nat is native at runtime: the budget may exceed 2^32.