~/bend-docscommunity

zlib.bend source

zlib.bend on the hub · documented module

# DEFLATE, gzip, and zlib encoding and decoding (RFC 1951, 1952, 1950) over Bytes. Source: https://github.com/paymog/bend-kit/tree/main/zlibimport Baseimport bend-kit-bytes@0.3.3.3/bytes.bend as Bytesimport bend-kit-bits@0.4.0.2/bits.bend as Bitsimport ./encode.bend as Encodeimport bend-kit-hash@0.3.2.4/hash.bend as Hashdef bits(+n: U32) -> Nat:  U32.to_nat(n)# Bit reader. bad: a read ran past the end, so the input was cut short.type Br is Type:  Br{r: Bits.Reader, bad: Bool}def br.got(+bad: Bool, rm: Bits.Reader & Maybe<&2, U32>) -> Br & U32:  (r, m) = rm  match m:    case None{}:      (Br{r, True{}}, 0)    case Some{v}:      (Br{r, bad}, v)def br.take(b: Br, +n: U32) -> Br & U32:  Br{r, +bad} = b  br.got(bad, Bits.Reader.lsb(r, n))# No code: 9999, never a symbol.def br.sym.got(+bad: Bool, r: Bits.Huff & Bits.Reader & Maybe<&2, U32>) -> Bits.Huff & Br & U32:  (h, rd, m) = r  match m:    case None{}:      (h, Br{rd, True{}}, 9999)    case Some{v}:      (h, Br{rd, bad}, v)def br.sym(h: Bits.Huff, b: Br) -> Bits.Huff & Br & U32:  Br{r, +bad} = b  br.sym.got(bad, Bits.Huff.decode(h, r))# RFC 1951 §3.2.4: a stored block starts on a byte boundary.def br.align(b: Br) -> Br:  Br{r, bad} = b  Br{Bits.Reader.align(r), bad}def nth(xs: List<&2, U32>, +i: U32) -> U32:  match xs:    case Nil{}:      0    case Con{h, t}:      Bool.pick(U32, U32.is_zero(i), h, nth(t, (i - 1 : U32)))def rep(k: Nat, +v: U32, acc: List<&2, U32>) -> List<&2, U32>:  match k:    case 0n:      acc    case 1n+p:      rep(p, v, Con{v, acc})def take.n(k: Nat, xs: List<&2, U32>) -> List<&2, U32>:  match k:    case 0n:      Nil{}    case 1n+p:      match xs:        case Nil{}:          Nil{}        case Con{h, t}:          Con{h, take.n(p, t)}def drop.n(k: Nat, xs: List<&2, U32>) -> List<&2, U32>:  match k:    case 0n:      xs    case 1n+p:      match xs:        case Nil{}:          Nil{}        case Con{h, t}:          drop.n(p, t)# RFC 1951 §3.2.5def len.base() -> List<&2, U32>:  [3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258]def len.extra() -> List<&2, U32>:  [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0]def dist.base() -> List<&2, U32>:  [1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577]def dist.extra() -> List<&2, U32>:  [0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13]# RFC 1951 §3.2.6def fixed.lit() -> Maybe<&1, Bits.Huff>:  Bits.Huff.from_lens(False{}, rep(144n, 8, rep(112n, 9, rep(24n, 7, rep(8n, 8, Nil{})))))def fixed.dist() -> Maybe<&1, Bits.Huff>:  Bits.Huff.from_lens(False{}, rep(30n, 5, Nil{}))# Output: 2^dep words of packed bytes, n of them written. Matches copy from the output itself.type Out is Type:  Out{buf: Array<U32>, dep: U32, n: U32}def out.fits(+dep: U32, +want: U32) -> Bool:  U32.is_le(want, U32.shln(4, bits(dep)))def out.dep(f: Nat, fits: Bool, +dep: U32, +want: U32) -> U32:  match f:    case 0n:      dep    case 1n+p:      match fits:        case True{}:          dep        case False{}:          +d = (dep + 1 : U32)          out.dep(p, out.fits(d, want), d, want)def out.grow(fits: Bool, o: Out, +want: U32) -> Out:  match fits:    case True{}:      o    case False{}:      Out{buf, +dep, +n} = o      +d = out.dep(28n, False{}, dep, want)      Out{Bytes.dst(Bytes.copy(n, buf, Array.new(U32, bits(d), 0), 0, 0)), d, n}# The buffer doubles until k more bytes fit, so output is O(total), and it ends as the# fewest 2^dep words that hold n, as Bytes expects.def out.room(o: Out, +k: U32) -> Out:  Out{buf, +dep, +n} = o  +want = (n + k : U32)  out.grow(out.fits(dep, want), Out{buf, dep, n}, want)def out.put(o: Out, +c: U32) -> Out:  Out{buf, dep, +n} = o  Out{Bytes.poke(buf, n, c), dep, (n + 1 : U32)}def out.size(o: Out) -> Out & U32:  Out{buf, dep, +n} = o  (Out{buf, dep, n}, n)def out.bytes(o: Out) -> Bytes.Bytes:  Out{buf, dep, n} = o  Bytes.Bytes{n, buf}type Mo is Type:  MoHead{}  MoCodes{lit: Bits.Huff, dist: Bits.Huff}  MoDone{}  MoBad{}type Is is Type:  Is{br: Br, o: Out, mode: Mo, last: Bool}def inf.next(last: Bool) -> Mo:  match last:    case True{}:      MoDone{}    case False{}:      MoHead{}def inf.bad(b: Br, o: Out) -> Is:  Is{b, o, MoBad{}, True{}}# Stored block (§3.2.4): LEN, then NLEN (its complement), then LEN raw bytes.def sb.put(bv: Br & U32, o: Out) -> Br & Out:  (b, +c) = bv  (b, out.put(o, c))def sb.go(k: Nat, st: Br & Out) -> Br & Out:  match k:    case 0n:      st    case 1n+f:      (b, o) = st      sb.go(f, sb.put(br.take(b, 8), o))def sb.done(st: Br & Out, +last: Bool) -> Is:  (b, o) = st  Is{b, o, inf.next(last), last}def sb.ok(ok: Bool, b: Br, +len: U32, o: Out, +last: Bool) -> Is:  match ok:    case False{}:      inf.bad(b, o)    case True{}:      sb.done(sb.go(bits(len), (b, out.room(o, len))), last)def sb.nlen(bv: Br & U32, +len: U32, o: Out, +last: Bool) -> Is:  (b, +nlen) = bv  sb.ok(U32.is_eq(U32.xor(len, 65535), nlen), b, len, o, last)def sb.len(bv: Br & U32, o: Out, +last: Bool) -> Is:  (b, +len) = bv  sb.nlen(br.take(b, 16), len, o, last)def inf.stored(b: Br, o: Out, +last: Bool) -> Is:  sb.len(br.take(br.align(b), 16), o, last)# 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.slot() -> List<&2, U32>:  [3, 17, 15, 13, 11, 9, 7, 5, 4, 6, 8, 10, 12, 14, 16, 18, 0, 1, 2]def cl.place(xs: List<&2, U32>, +vals: List<&2, U32>) -> List<&2, U32>:  match xs:    case Nil{}:      Nil{}    case Con{+i, t}:      Con{nth(vals, i), cl.place(t, vals)}def cl.push(bv: Br & U32, acc: List<&2, U32>) -> Br & List<&2, U32>:  (b, v) = bv  (b, Con{v, acc})def cl.one(st: Br & List<&2, U32>) -> Br & List<&2, U32>:  (b, acc) = st  cl.push(br.take(b, 3), acc)def cl.read(k: Nat, st: Br & List<&2, U32>) -> Br & List<&2, U32>:  match k:    case 0n:      st    case 1n+f:      cl.read(f, cl.one(st))# The code-length code, lengths so far (reversed), how many, the last one (for code 16), and whether it went wrong.type Cd is Type:  Cd{h: Bits.Huff, br: Br, acc: List<&2, U32>, n: U32, prev: U32, bad: Bool}def cd.fill(over: Bool, h: Bits.Huff, b: Br, acc: List<&2, U32>, +n: U32, +v: U32, +cnt: U32) -> Cd:  match over:    case True{}:      Cd{h, b, acc, n, v, True{}}    case False{}:      Cd{h, b, rep(bits(cnt), v, acc), (n + cnt : U32), v, False{}}def cd.rep(h: Bits.Huff, bv: Br & U32, +v: U32, +base: U32, acc: List<&2, U32>, +n: U32, +total: U32) -> Cd:  (b, +e) = bv  +cnt = (base + e : U32)  cd.fill(U32.is_lt(total, (n + cnt : U32)), h, b, acc, n, v, cnt)def cd.prev(ok: Bool, h: Bits.Huff, b: Br, acc: List<&2, U32>, +n: U32, +prev: U32, +total: U32) -> Cd:  match ok:    case False{}:      Cd{h, b, acc, n, prev, True{}}    case True{}:      cd.rep(h, br.take(b, 2), prev, 3, acc, n, total)def cd.z18(ok: Bool, h: Bits.Huff, b: Br, acc: List<&2, U32>, +n: U32, +total: U32) -> Cd:  match ok:    case True{}:      cd.rep(h, br.take(b, 7), 0, 11, acc, n, total)    case False{}:      Cd{h, b, acc, n, 0, True{}}def cd.z17(ok: Bool, h: Bits.Huff, b: Br, acc: List<&2, U32>, +n: U32, +s: U32, +total: U32) -> Cd:  match ok:    case True{}:      cd.rep(h, br.take(b, 3), 0, 3, acc, n, total)    case False{}:      cd.z18(U32.is_eq(s, 18), h, b, acc, n, total)def cd.c16(ok: Bool, h: Bits.Huff, b: Br, acc: List<&2, U32>, +n: U32, +prev: U32, +s: U32, +total: U32) -> Cd:  match ok:    case True{}:      cd.prev(U32.is_lt(0, n), h, b, acc, n, prev, total)    case False{}:      cd.z17(U32.is_eq(s, 17), h, b, acc, n, s, total)def cd.lit(ok: Bool, h: Bits.Huff, b: Br, acc: List<&2, U32>, +n: U32, +prev: U32, +s: U32, +total: U32) -> Cd:  match ok:    case True{}:      Cd{h, b, Con{s, acc}, (n + 1 : U32), s, False{}}    case False{}:      cd.c16(U32.is_eq(s, 16), h, b, acc, n, prev, s, total)# A + binder in a nested tuple does not check (bendlang/bend#1077), so a helper takes it.def cd.sym.at(h: Bits.Huff, b: Br, +s: U32, acc: List<&2, U32>, +n: U32, +prev: U32, +total: U32) -> Cd:  cd.lit(U32.is_lt(s, 16), h, b, acc, n, prev, s, total)def cd.sym(r: Bits.Huff & Br & U32, acc: List<&2, U32>, +n: U32, +prev: U32, +total: U32) -> Cd:  (h, b, s) = r  cd.sym.at(h, b, s, acc, n, prev, total)def cd.step(stop: Bool, h: Bits.Huff, b: Br, acc: List<&2, U32>, +n: U32, +prev: U32, +bad: Bool, +total: U32) -> Cd:  match stop:    case True{}:      Cd{h, b, acc, n, prev, bad}    case False{}:      cd.sym(br.sym(h, b), acc, n, prev, total)# Each step adds at least one length, so total steps suffice.def cd.go(k: Nat, +total: U32, st: Cd) -> Cd:  match k:    case 0n:      st    case 1n+f:      Cd{h, b, acc, +n, +prev, +bad} = st      cd.go(f, total, cd.step(Bool.or(bad, U32.is_le(total, n)), h, b, acc, n, prev, bad, total))def dyn.dist(lit: Bits.Huff, m: Maybe<&1, Bits.Huff>) -> Mo:  match m:    case None{}:      MoBad{}    case Some{dist}:      MoCodes{lit, dist}def dyn.mo(l: Maybe<&1, Bits.Huff>, d: Maybe<&1, Bits.Huff>) -> Mo:  match l:    case None{}:      MoBad{}    case Some{lit}:      dyn.dist(lit, d)def dyn.tables(bad: Bool, b: Br, +lens: List<&2, U32>, +hlit: U32) -> Br & Mo:  match bad:    case True{}:      (b, MoBad{})    case False{}:      (b, dyn.mo(Bits.Huff.from_lens(False{}, take.n(bits(hlit), lens)), Bits.Huff.from_lens(False{}, drop.n(bits(hlit), lens))))def dyn.done(st: Cd, +hlit: U32, +total: U32) -> Br & Mo:  Cd{h, b, acc, +n, prev, +bad} = st  dyn.tables(Bool.or(bad, Bool.not(U32.is_eq(n, total))), b, List.reverse(&2, U32, acc), hlit)def dyn.cl(m: Maybe<&1, Bits.Huff>, b: Br, +hlit: U32, +total: U32) -> Br & Mo:  match m:    case None{}:      (b, MoBad{})    case Some{h}:      dyn.done(cd.go(bits(total), total, Cd{h, b, Nil{}, 0, 0, False{}}), hlit, total)def dyn.lens(st: Br & List<&2, U32>, +hlit: U32, +hdist: U32) -> Br & Mo:  (b, acc) = st  dyn.cl(Bits.Huff.from_lens(False{}, cl.place(cl.slot(), List.reverse(&2, U32, acc))), b, hlit, (hlit + hdist : U32))def dyn.clen(bv: Br & U32, +hlit: U32, +hdist: U32) -> Br & Mo:  (b, +hclen) = bv  dyn.lens(cl.read(bits((hclen + 4 : U32)), (b, Nil{})), hlit, hdist)def dyn.hdist(bv: Br & U32, +hlit: U32) -> Br & Mo:  (b, +hd) = bv  dyn.clen(br.take(b, 4), hlit, (hd + 1 : U32))def dyn.hlit(bv: Br & U32) -> Br & Mo:  (b, +hl) = bv  dyn.hdist(br.take(b, 5), (hl + 257 : U32))def dyn.read(b: Br) -> Br & Mo:  dyn.hlit(br.take(b, 5))# Matches keep the byte loop when source and destination overlap.def cp.of(+dep: U32, +n: U32, r: Array<U32> & U32) -> Out:  (a, +c) = r  Out{Bytes.poke(a, n, c), dep, (n + 1 : U32)}def cp.one(+d: U32, o: Out) -> Out:  Out{buf, +dep, +n} = o  cp.of(dep, n, Bytes.peek(buf, (n - d : U32)))def cp.go(k: Nat, +d: U32, o: Out) -> Out:  match k:    case 0n:      o    case 1n+f:      cp.go(f, d, cp.one(d, o))def cp.word(+n: U32, r: Array<U32> & U32) -> Array<U32>:  (a, v) = r  Array.set(U32, a, n, v)def cp.words(k: Nat, a: Array<U32>, +s: U32, +n: U32) -> Array<U32>:  match k:    case 0n:      a    case 1n+f:      cp.words(f, cp.word(n, Array.get(U32, a, s)), (s + 1 : U32), (n + 1 : U32))def cp.shift(k: Nat, +prev: U32, r: Array<U32> & U32, +s: U32, +n: U32, +lo: Nat, +hi: Nat) -> Array<U32>:  match k:    case 0n:      (a, w) = r      a    case 1n+f:      (a, +w) = r      a = Array.set(U32, a, n, (U32.shrn(prev, lo) .|. U32.shln(w, hi) : U32))      cp.shift(f, w, Array.get(U32, a, (s + 1 : U32)), (s + 1 : U32), (n + 1 : U32), lo, hi)def cp.shift.start(+k: U32, +s: U32, +n: U32, +lo: U32, r: Array<U32> & U32) -> Array<U32>:  (a, prev) = r  cp.shift(bits(k), prev, Array.get(U32, a, (s + 1 : U32)), (s + 1 : U32), n, bits(lo), bits((32 - lo : U32)))def cp.pick(aligned: Bool, +k: U32, a: Array<U32>, +s: U32, +n: U32) -> Array<U32>:  match aligned:    case True{}:      cp.words(bits(k), a, (s >> 2n : U32), (n >> 2n : U32))    case False{}:      cp.shift.start(k, (s >> 2n : U32), (n >> 2n : U32), ((s .&. 3 : U32) * 8 : U32), Array.get(U32, a, (s >> 2n : U32)))def cp.packed(+len: U32, +d: U32, o: Out) -> Out:  Out{a, dep, +n} = o  +s = (n - d : U32)  +whole = (len .&. 4294967292 : U32)  cp.go(bits((len .&. 3 : U32)), d, Out{cp.pick(U32.is_eq((s .&. 3 : U32), 0), (len >> 2n : U32), a, s, n), dep, (n + whole : U32)})def cp.start(+len: U32, +d: U32, o: Out) -> Out:  Out{a, dep, +n} = o  +prefix = U32.min(len, ((4 - (n .&. 3) : U32) .&. 3 : U32))  cp.packed((len - prefix : U32), d, cp.go(bits(prefix), d, Out{a, dep, n}))def cp.match(nonoverlap: Bool, +len: U32, +d: U32, o: Out) -> Out:  match nonoverlap:    case False{}:      cp.go(bits(len), d, o)    case True{}:      cp.start(len, d, o)# RFC 1951 §3.2.5: a distance may not reach before the first byte out.def inf.far(ok: Bool, lit: Bits.Huff, dist: Bits.Huff, b: Br, +len: U32, +d: U32, o: Out, +last: Bool) -> Is:  match ok:    case False{}:      inf.bad(b, o)    case True{}:      Is{b, cp.match(U32.is_le(len, d), len, d, out.room(o, len)), MoCodes{lit, dist}, last}def inf.dn(lit: Bits.Huff, dist: Bits.Huff, b: Br, +len: U32, +d: U32, r: Out & U32, +last: Bool) -> Is:  (o, +n) = r  inf.far(U32.is_le(d, n), lit, dist, b, len, d, o, last)def inf.dx(bv: Br & U32, +db: U32, +len: U32, lit: Bits.Huff, dist: Bits.Huff, o: Out, +last: Bool) -> Is:  (b, +e) = bv  inf.dn(lit, dist, b, len, (db + e : U32), out.size(o), last)def inf.ds(ok: Bool, lit: Bits.Huff, dist: Bits.Huff, b: Br, +ds: U32, +len: U32, o: Out, +last: Bool) -> Is:  match ok:    case False{}:      inf.bad(b, o)    case True{}:      inf.dx(br.take(b, nth(dist.extra(), ds)), nth(dist.base(), ds), len, lit, dist, o, last)# A + binder in a nested tuple does not check (bendlang/bend#1077), so a helper takes it.def inf.d.at(dist: Bits.Huff, b: Br, +ds: U32, +len: U32, lit: Bits.Huff, o: Out, +last: Bool) -> Is:  inf.ds(U32.is_lt(ds, 30), lit, dist, b, ds, len, o, last)def inf.d(r: Bits.Huff & Br & U32, +len: U32, lit: Bits.Huff, o: Out, +last: Bool) -> Is:  (dist, b, ds) = r  inf.d.at(dist, b, ds, len, lit, o, last)def inf.lx(bv: Br & U32, +lb: U32, lit: Bits.Huff, dist: Bits.Huff, o: Out, +last: Bool) -> Is:  (b, +e) = bv  inf.d(br.sym(dist, b), (lb + e : U32), lit, o, last)def inf.ls(ok: Bool, lit: Bits.Huff, dist: Bits.Huff, b: Br, +s: U32, o: Out, +last: Bool) -> Is:  match ok:    case False{}:      inf.bad(b, o)    case True{}:      +i = (s - 257 : U32)      inf.lx(br.take(b, nth(len.extra(), i)), nth(len.base(), i), lit, dist, o, last)def inf.end(eob: Bool, lit: Bits.Huff, dist: Bits.Huff, b: Br, +s: U32, o: Out, +last: Bool) -> Is:  match eob:    case True{}:      Is{b, o, inf.next(last), last}    case False{}:      inf.ls(U32.is_le(s, 285), lit, dist, b, s, o, last)def inf.lit(ok: Bool, lit: Bits.Huff, dist: Bits.Huff, b: Br, +s: U32, o: Out, +last: Bool) -> Is:  match ok:    case True{}:      Is{b, out.put(out.room(o, 1), s), MoCodes{lit, dist}, last}    case False{}:      inf.end(U32.is_eq(s, 256), lit, dist, b, s, o, last)# A + binder in a nested tuple does not check (bendlang/bend#1077), so a helper takes it.def inf.sym.at(lit: Bits.Huff, b: Br, +s: U32, dist: Bits.Huff, o: Out, +last: Bool) -> Is:  inf.lit(U32.is_lt(s, 256), lit, dist, b, s, o, last)def inf.sym(r: Bits.Huff & Br & U32, dist: Bits.Huff, o: Out, +last: Bool) -> Is:  (lit, b, s) = r  inf.sym.at(lit, b, s, dist, o, last)def inf.dyn(bm: Br & Mo, o: Out, +last: Bool) -> Is:  (b, m) = bm  Is{b, o, m, last}def inf.t2(is2: Bool, b: Br, o: Out, +last: Bool) -> Is:  match is2:    case True{}:      inf.dyn(dyn.read(b), o, last)    case False{}:      inf.bad(b, o)def inf.t1(is1: Bool, b: Br, o: Out, +last: Bool, +t: U32) -> Is:  match is1:    case True{}:      Is{b, o, dyn.mo(fixed.lit(), fixed.dist()), last}    case False{}:      inf.t2(U32.is_eq(t, 2), b, o, last)def inf.t0(is0: Bool, b: Br, o: Out, +last: Bool, +t: U32) -> Is:  match is0:    case True{}:      inf.stored(b, o, last)    case False{}:      inf.t1(U32.is_eq(t, 1), b, o, last, t)# §3.2.3: BFINAL, then BTYPE.def inf.head(bv: Br & U32, o: Out) -> Is:  (b, +v) = bv  +t = U32.shr(v)  inf.t0(U32.is_zero(t), b, o, U32.is_eq(U32.and(v, 1), 1), t)# Every step reads at least one bit or fails, so 8 steps per input byte always finish a valid stream.def inf.go(k: Nat, st: Is) -> Is:  match k:    case 0n:      st    case 1n+f:      Is{b, o, mode, +last} = st      match mode:        case MoHead{}:          inf.go(f, inf.head(br.take(b, 3), o))        case MoCodes{lit, dist}:          inf.go(f, inf.sym(br.sym(lit, b), dist, o, last))        case MoDone{}:          Is{b, o, MoDone{}, last}        case MoBad{}:          Is{b, o, MoBad{}, last}# out: the bytes; src: the input back; at: the first byte after the stream.type Inflated is Type:  Inflated{out: Bytes.Bytes, src: Bytes.Bytes, at: U32}def inf.done(r: Bytes.Bytes & Nat, o: Out) -> Maybe<&1, Inflated>:  (src, +pos) = r  Some{Inflated{out.bytes(o), src, Bits.pos.bytes(pos)}}def inf.fin(b: Br, o: Out) -> Maybe<&1, Inflated>:  Br{r, bad} = b  match bad:    case True{}:      None{}    case False{}:      inf.done(Bits.Reader.finish(r), o)def inf.result(st: Is) -> Maybe<&1, Inflated>:  Is{b, o, mode, last} = st  match mode:    case MoDone{}:      inf.fin(b, o)    case MoHead{}:      None{}    case MoCodes{l, d}:      None{}    case MoBad{}:      None{}def inf.start(ok: Bool, +len: U32, buf: Array<U32>, +at: U32) -> Maybe<&1, Inflated>:  match ok:    case False{}:      None{}    case True{}:      +fuel = Nat.add(Nat.mul(bits((len - at : U32)), 8n), 8n)      inf.result(inf.go(fuel, Is{Br{Bits.Reader.at(len, buf, Nat.mul(bits(at), 8n)), False{}}, Out{Array.new(U32, 0n, 0), 0, 0}, MoHead{}, False{}}))# A raw DEFLATE stream that starts at byte at.def inflate.at(b: Bytes.Bytes, +at: U32) -> Maybe<&1, Inflated>:  Bytes.Bytes{+len, buf} = b  inf.start(U32.is_le(at, len), len, buf, at)def inflate.out(m: Maybe<&1, Inflated>) -> Maybe<&1, Bytes.Bytes>:  match m:    case None{}:      None{}    case Some{Inflated{out, src, at}}:      Some{out}# Raw DEFLATE. None: malformed or cut short.def inflate(b: Bytes.Bytes) -> Maybe<&1, Bytes.Bytes>:  inflate.out(inflate.at(b, 0))def holds(m: Maybe<&2, U32>, +v: U32) -> Bool:  match m:    case None{}:      False{}    case Some{+x}:      U32.is_eq(x, v)def keep(ok: Bool, b: Bytes.Bytes) -> Maybe<&1, Bytes.Bytes>:  match ok:    case True{}:      Some{b}    case False{}:      None{}def flag(+f: U32, +bit: U32) -> Bool:  Bool.not(U32.is_zero(U32.and(f, bit)))# RFC 1952 §2.3: after the fixed 10 bytes, optional FEXTRA, FNAME, FCOMMENT, FHCRC.# Each step takes the buffer and the offset so far; None once a field runs past the end.def gz.xlen(+off: U32, r: Bytes.Bytes & Maybe<&2, U32>) -> Bytes.Bytes & Maybe<&2, U32>:  (b, m) = r  match m:    case None{}:      (b, None{})    case Some{+x}:      (b, Some{(off + 2 + x : U32)})def gz.fextra.at(m: Maybe<&2, U32>, b: Bytes.Bytes) -> Bytes.Bytes & Maybe<&2, U32>:  match m:    case None{}:      (b, None{})    case Some{+off}:      gz.xlen(off, Bytes.get.u16le(b, off))def gz.fextra(on: Bool, r: Bytes.Bytes & Maybe<&2, U32>) -> Bytes.Bytes & Maybe<&2, U32>:  match on:    case False{}:      r    case True{}:      (b, m) = r      gz.fextra.at(m, b)def gz.zero(r: Bytes.Bytes & Maybe<&2, U32>) -> Bytes.Bytes & Maybe<&2, U32>:  (b, m) = r  match m:    case None{}:      (b, None{})    case Some{+i}:      (b, Some{(i + 1 : U32)})def gz.fstr.at(m: Maybe<&2, U32>, b: Bytes.Bytes) -> Bytes.Bytes & Maybe<&2, U32>:  match m:    case None{}:      (b, None{})    case Some{+off}:      gz.zero(Bytes.find.from(b, SCon{Chr{0}, SNil{}}, off))# A zero-terminated FNAME or FCOMMENT.def gz.fstr(on: Bool, r: Bytes.Bytes & Maybe<&2, U32>) -> Bytes.Bytes & Maybe<&2, U32>:  match on:    case False{}:      r    case True{}:      (b, m) = r      gz.fstr.at(m, b)def gz.fhcrc.at(m: Maybe<&2, U32>, b: Bytes.Bytes) -> Bytes.Bytes & Maybe<&2, U32>:  match m:    case None{}:      (b, None{})    case Some{+off}:      (b, Some{(off + 2 : U32)})def gz.fhcrc(on: Bool, r: Bytes.Bytes & Maybe<&2, U32>) -> Bytes.Bytes & Maybe<&2, U32>:  match on:    case False{}:      r    case True{}:      (b, m) = r      gz.fhcrc.at(m, b)# One member's output, the input back, and the first byte after its trailer.type Member is Type:  Member{out: Bytes.Bytes, src: Bytes.Bytes, next: U32}def gz.keep(ok: Bool, out: Bytes.Bytes, src: Bytes.Bytes, +next: U32) -> Maybe<&1, Member>:  match ok:    case True{}:      Some{Member{out, src, next}}    case False{}:      None{}def gz.isize(+ok: Bool, +n: U32, +next: U32, out: Bytes.Bytes, r: Bytes.Bytes & Maybe<&2, U32>) -> Maybe<&1, Member>:  (src, z) = r  gz.keep(Bool.and(ok, holds(z, n)), out, src, next)def gz.crc(r: Bytes.Bytes & U32, +n: U32, +at: U32, c: Bytes.Bytes & Maybe<&2, U32>) -> Maybe<&1, Member>:  (out, +crc) = r  (src, m) = c  gz.isize(holds(m, crc), n, (at + 8 : U32), out, Bytes.get.u32le(src, (at + 4 : U32)))def gz.trail(m: Maybe<&1, Inflated>) -> Maybe<&1, Member>:  match m:    case None{}:      None{}    case Some{Inflated{out, src, +at}}:      Bytes.Bytes{+n, buf} = out      gz.crc(Hash.crc32(Bytes.Bytes{n, buf}), n, at, Bytes.get.u32le(src, at))def gz.body(r: Bytes.Bytes & Maybe<&2, U32>) -> Maybe<&1, Member>:  (b, m) = r  match m:    case None{}:      None{}    case Some{+off}:      gz.trail(inflate.at(b, off))def gz.flags(+f: U32, +p: U32, b: Bytes.Bytes) -> Maybe<&1, Member>:  gz.body(gz.fhcrc(flag(f, 2), gz.fstr(flag(f, 16), gz.fstr(flag(f, 8), gz.fextra(flag(f, 4), (b, Some{(p + 10 : U32)}))))))def gz.magic(ok: Bool, +f: U32, +p: U32, b: Bytes.Bytes) -> Maybe<&1, Member>:  match ok:    case True{}:      gz.flags(f, p, b)    case False{}:      None{}# ID1 31, ID2 139, CM 8 (DEFLATE), then FLG.def gz.head(+p: U32, r: Bytes.Bytes & Maybe<&2, U32>) -> Maybe<&1, Member>:  (b, m) = r  match m:    case None{}:      None{}    case Some{+w}:      gz.magic(U32.is_eq(U32.and(w, 16777215), 559903), U32.shrn(w, 24n), p, b)# The member that starts at byte p.def gz.member(b: Bytes.Bytes, +p: U32) -> Maybe<&1, Member>:  gz.head(p, Bytes.get.u32le(b, p))# RFC 1952 §2.2: members follow each other until the input ends.type Gz is Type:  GzMore{acc: Bytes.Bytes, src: Bytes.Bytes, at: U32}  GzDone{out: Bytes.Bytes}  GzBad{}def gz.more(more: Bool, acc: Bytes.Bytes, src: Bytes.Bytes, +at: U32) -> Gz:  match more:    case True{}:      GzMore{acc, src, at}    case False{}:      GzDone{acc}def gz.step(m: Maybe<&1, Member>, acc: Bytes.Bytes) -> Gz:  match m:    case None{}:      GzBad{}    case Some{Member{out, src, +next}}:      Bytes.Bytes{+len, buf} = src      gz.more(U32.is_lt(next, len), Bytes.append(acc, out), Bytes.Bytes{len, buf}, next)# Each member takes at least 18 bytes, so one step per byte suffices.def gz.go(k: Nat, st: Gz) -> Maybe<&1, Bytes.Bytes>:  match k:    case 0n:      None{}    case 1n+f:      match st:        case GzMore{acc, src, +at}:          gz.go(f, gz.step(gz.member(src, at), acc))        case GzDone{out}:          Some{out}        case GzBad{}:          None{}# Every gzip member, concatenated; each CRC-32 and ISIZE must match. None: malformed, cut short, corrupt, or junk after a member.def gunzip(b: Bytes.Bytes) -> Maybe<&1, Bytes.Bytes>:  Bytes.Bytes{+len, buf} = b  gz.go(Nat.add(bits(len), 1n), GzMore{Bytes.Bytes{0, Array.new(U32, 0n, 0)}, Bytes.Bytes{len, buf}, 0})# RFC 1950: CMF and FLG, then DEFLATE, then Adler-32, big-endian.def zl.sum(r: Bytes.Bytes & U32, c: Bytes.Bytes & Maybe<&2, U32>) -> Maybe<&1, Bytes.Bytes>:  (out, +a) = r  (src, m) = c  keep(holds(m, a), out)def zl.trail(m: Maybe<&1, Inflated>) -> Maybe<&1, Bytes.Bytes>:  match m:    case None{}:      None{}    case Some{Inflated{out, src, +at}}:      zl.sum(Hash.adler32(out), Bytes.get.u32be(src, at))def zl.ok(+cmf: U32, +flg: U32) -> Bool:  Bool.and(Bool.and(U32.is_eq(U32.and(cmf, 15), 8), U32.is_le(U32.shrn(cmf, 4n), 7)), Bool.and(U32.is_zero(U32.mod((cmf * 256 + flg : U32), 31)), Bool.not(flag(flg, 32))))def zl.go(ok: Bool, b: Bytes.Bytes) -> Maybe<&1, Bytes.Bytes>:  match ok:    case True{}:      zl.trail(inflate.at(b, 2))    case False{}:      None{}def zl.head(r: Bytes.Bytes & Maybe<&2, U32>) -> Maybe<&1, Bytes.Bytes>:  (b, m) = r  match m:    case None{}:      None{}    case Some{+h}:      zl.go(zl.ok(U32.shrn(h, 8n), U32.and(h, 255)), b)def unzlib(b: Bytes.Bytes) -> Maybe<&1, Bytes.Bytes>:  zl.head(Bytes.get.u16be(b, 0))# DEFLATE encoding: lazy LZ77 over hash chains, then bounded Huffman blocks.def bw.code(w: Bits.Writer, cl: U32 & U32) -> Bits.Writer:  (+c, +l) = cl  Bits.Writer.lsb(w, l, Bits.rev(bits(l), c, 0))# RFC 1951 §3.2.6: the fixed code of literal/length symbol s, and its length.def fx.hi(+s: U32, lo: Bool) -> U32 & U32:  match lo:    case True{}:      ((s - 256 : U32), 7)    case False{}:      ((s - 88 : U32), 8)def fx.mid(+s: U32, lo: Bool) -> U32 & U32:  match lo:    case True{}:      ((s + 256 : U32), 9)    case False{}:      fx.hi(s, U32.is_lt(s, 280))def fx.low(+s: U32, lo: Bool) -> U32 & U32:  match lo:    case True{}:      ((s + 48 : U32), 8)    case False{}:      fx.mid(s, U32.is_lt(s, 256))def fx.lit(+s: U32) -> U32 & U32:  fx.low(s, U32.is_lt(s, 144))# RFC 1951 symbol index, base value, and extra-bit count.type Cx is Data:  Cx{i: U32, base: U32, extra: U32}def cx.length.group(+v: U32, +extra: Nat) -> Cx:  +top = U32.shrn((v - 3 : U32), extra)  Cx{(4 * U32.from_nat(extra) + top : U32), (3 + U32.shln(top, extra) : U32), U32.from_nat(extra)}def cx.length.small(small: Bool, +v: U32) -> Cx:  match small:    case True{}:      Cx{(v - 3 : U32), v, 0}    case False{}:      cx.length.group(v, Nat.sub(U32.log2((v - 3 : U32)), 2n))def cx.length(+v: U32) -> Cx:  match v:    case 258:      Cx{28, 258, 0}    case _:      cx.length.small(U32.is_le(v, 10), v)def cx.distance.group(+v: U32, +extra: Nat) -> Cx:  +top = U32.shrn((v - 1 : U32), extra)  Cx{(2 * U32.from_nat(extra) + top : U32), (1 + U32.shln(top, extra) : U32), U32.from_nat(extra)}def cx.distance.small(small: Bool, +v: U32) -> Cx:  match small:    case True{}:      Cx{(v - 1 : U32), v, 0}    case False{}:      cx.distance.group(v, Nat.sub(U32.log2((v - 1 : U32)), 1n))def cx.distance(+v: U32) -> Cx:  cx.distance.small(U32.is_le(v, 4), v)def em.lit(w: Bits.Writer, +c: U32) -> Bits.Writer:  bw.code(w, fx.lit(c))# Match finder: the input bytes, packed, the newest position + 1 per hash, and per position the previous one with its hash (0: none).type Lz is Type:  Lz{src: Array<U32>, head: Array<U32>, prev: Array<U32>}def lz.at.of(r: Array<U32> & U32, head: Array<U32>, prev: Array<U32>) -> Lz & U32:  (src, v) = r  (Lz{src, head, prev}, v)def lz.at(z: Lz, +i: U32) -> Lz & U32:  Lz{src, head, prev} = z  lz.at.of(Bytes.peek(src, i), head, prev)def lz.drop(r: Lz & U32) -> Lz:  (z, v) = r  zdef lz.h3(r: Lz & U32, +a: U32, +b: U32) -> Lz & U32:  (z, +c) = r  (z, U32.and(U32.xor(U32.xor(U32.shln(a, 10n), U32.shln(b, 5n)), c), 32767))def lz.h2(r: Lz & U32, +i: U32, +a: U32) -> Lz & U32:  (z, +b) = r  lz.h3(lz.at(z, (i + 2 : U32)), a, b)def lz.h1(r: Lz & U32, +i: U32) -> Lz & U32:  (z, +a) = r  lz.h2(lz.at(z, (i + 1 : U32)), i, a)def lz.hash(z: Lz, +i: U32) -> Lz & U32:  lz.h1(lz.at(z, i), i)def lz.link(r: Array<U32> & U32, +i: U32, +h: U32, src: Array<U32>, prev: Array<U32>) -> Lz & U32:  (head, +old) = r  (Lz{src, Array.set(U32, head, h, (i + 1 : U32)), Array.set(U32, prev, U32.and(i, 32767), old)}, old)def lz.ins.of(r: Lz & U32, +i: U32) -> Lz & U32:  (z, +h) = r  Lz{src, head, prev} = z  lz.link(Array.get(U32, head, h), i, h, src, prev)# Links position i into the chain for its next three bytes; answers the chain's previous head.def lz.insert(z: Lz, +i: U32) -> Lz & U32:  lz.ins.of(lz.hash(z, i), i)# Match length: bytes at a + k and b + k compared so far, and whether the last pair was equal.type Ml is Type:  Ml{z: Lz, a: U32, b: U32, k: U32, eq: Bool}def ml.cmp(r: Lz & U32, +a: U32, +b: U32, +k: U32, +x: U32) -> Ml:  (z, +y) = r  Ml{z, a, b, (k + 1 : U32), U32.is_eq(x, y)}def ml.read(r: Lz & U32, +a: U32, +b: U32, +k: U32) -> Ml:  (z, +x) = r  ml.cmp(lz.at(z, (b + k : U32)), a, b, k, x)def ml.len(+k: U32, eq: Bool) -> U32:  match eq:    case True{}:      k    case False{}:      (k - 1 : U32)# Counts equal bytes from a and from b, up to fuel.def ml.go(fuel: Nat, st: Ml) -> Lz & U32:  match fuel:    case 0n:      Ml{z, a, b, +k, eq} = st      (z, ml.len(k, eq))    case 1n+f:      Ml{z, +a, +b, +k, eq} = st      match eq:        case True{}:          ml.go(f, ml.read(lz.at(z, (a + k : U32)), a, b, k))        case False{}:          (z, (k - 1 : U32))# Chain walk at i: candidate c (position + 1), the longest match so far, and whether to go on.type Ch is Type:  Ch{z: Lz, i: U32, c: U32, max: U32, blen: U32, bdist: U32, go: Bool}# RFC 1951 §3.2.5: a distance is at most 32768.def ch.ok(+i: U32, +c: U32, +max: U32, +blen: U32) -> Bool:  Bool.and(Bool.and(U32.is_lt(0, c), U32.is_lt((i - c : U32), 32768)), U32.is_lt(blen, max))def ch.next(r: Array<U32> & U32, src: Array<U32>, head: Array<U32>, +i: U32, +max: U32, +blen: U32, +bdist: U32) -> Ch:  (prev, +c) = r  Ch{Lz{src, head, prev}, i, c, max, blen, bdist, ch.ok(i, c, max, blen)}def ch.best(r: Lz & U32, +i: U32, +p: U32, +max: U32, +blen: U32, +bdist: U32) -> Ch:  (z, +len) = r  Lz{src, head, prev} = z  +more = U32.is_lt(blen, len)  ch.next(Array.get(U32, prev, U32.and(p, 32767)), src, head, i, max, Bool.pick(U32, more, len, blen), Bool.pick(U32, more, (i - p : U32), bdist))def ch.go(fuel: Nat, st: Ch) -> Ch:  match fuel:    case 0n:      st    case 1n+f:      Ch{z, +i, +c, +max, +blen, +bdist, go} = st      match go:        case True{}:          +p = (c - 1 : U32)          ch.go(f, ch.best(ml.go(bits(max), Ml{z, i, p, 0, True{}}), i, p, max, blen, bdist))        case False{}:          Ch{z, i, c, max, blen, bdist, False{}}type Es is Type:  Es{z: Lz, tokens: List<&2, Encode.Token>, i: U32, n: U32, more: Bool, len: U32, dist: U32}def es.lit.of(r: Lz & U32, tokens: List<&2, Encode.Token>, +i: U32, +n: U32) -> Es:  (z, c) = r  +j = (i + 1 : U32)  Es{z, Con{Encode.Lit{c}, tokens}, j, n, U32.is_lt(j, n), 0, 0}def es.lit(z: Lz, tokens: List<&2, Encode.Token>, +i: U32, +n: U32) -> Es:  es.lit.of(lz.at(z, i), tokens, i, n)def ins.one(ok: Bool, z: Lz, +j: U32) -> Lz:  match ok:    case True{}:      lz.drop(lz.insert(z, j))    case False{}:      z# Positions inside a match join their chains too, when three bytes remain.def ins.go(k: Nat, z: Lz, +j: U32, +n: U32) -> Lz:  match k:    case 0n:      z    case 1n+f:      ins.go(f, ins.one(U32.is_le((j + 3 : U32), n), z, j), (j + 1 : U32), n)def es.copy(+len: U32, +d: U32, lc: Cx, dc: Cx) -> Encode.Token:  Cx{+ls, +lb, le} = lc  Cx{ds, +db, de} = dc  Encode.Copy{(ls + 257 : U32), le, (len - lb : U32), ds, de, (d - db : U32)}def es.emit(hit: Bool, z: Lz, tokens: List<&2, Encode.Token>, +i: U32, +n: U32, +blen: U32, +bdist: U32) -> Es:  match hit:    case True{}:      +j = (i + blen : U32)      token = es.copy(blen, bdist, cx.length(blen), cx.distance(bdist))      Es{ins.go(bits((blen - 1 : U32)), z, (i + 1 : U32), n), Con{token, tokens}, j, n, U32.is_lt(j, n), 0, 0}    case False{}:      es.lit(z, tokens, i, n)def es.remember(st: Es, len: U32, dist: U32) -> Es:  Es{z, tokens, i, n, more, oldlen, olddist} = st  Es{z, tokens, i, n, more, len, dist}def es.lazy.emit(keep: Bool, z: Lz, tokens: List<&2, Encode.Token>, +i: U32, +n: U32, blen: U32, bdist: U32, next: U32, dist: U32) -> Es:  match keep:    case True{}:      es.emit(True{}, z, tokens, i, n, blen, bdist)    case False{}:      es.remember(es.lit(z, tokens, i, n), next, dist)def es.lazy.pick(st: Ch, tokens: List<&2, Encode.Token>, +i: U32, +n: U32, +blen: U32, +bdist: U32) -> Es:  Ch{z, j, c, max, +next, d, go} = st  es.lazy.emit(U32.is_le(next, blen), z, tokens, i, n, blen, bdist, next, d)def es.lazy.chain(r: Array<U32> & U32, src: Array<U32>, prev: Array<U32>, tokens: List<&2, Encode.Token>, +i: U32, +n: U32, +blen: U32, +bdist: U32) -> Es:  (head, +c) = r  +j = (i + 1 : U32)  +room = (n - j : U32)  +max = Bool.pick(U32, U32.is_lt(room, 258), room, 258)  es.lazy.pick(ch.go(32n, Ch{Lz{src, head, prev}, j, c, max, 0, 0, ch.ok(j, c, max, 0)}), tokens, i, n, blen, bdist)def es.lazy.hash(r: Lz & U32, tokens: List<&2, Encode.Token>, +i: U32, +n: U32, +blen: U32, +bdist: U32) -> Es:  (z, h) = r  Lz{src, head, prev} = z  es.lazy.chain(Array.get(U32, head, h), src, prev, tokens, i, n, blen, bdist)def es.lazy(ok: Bool, z: Lz, tokens: List<&2, Encode.Token>, +i: U32, +n: U32, +blen: U32, +bdist: U32) -> Es:  match ok:    case True{}:      es.lazy.hash(lz.hash(z, (i + 1 : U32)), tokens, i, n, blen, bdist)    case False{}:      es.emit(U32.is_le(3, blen), z, tokens, i, n, blen, bdist)def es.pick(st: Ch, tokens: List<&2, Encode.Token>, +n: U32) -> Es:  Ch{z, +i, c, max, +blen, +bdist, go} = st  es.lazy(Bool.and(U32.is_le(3, blen), Bool.and(U32.is_lt(blen, 258), U32.is_le((i + 4 : U32), n))), z, tokens, i, n, blen, bdist)def es.chain(r: Lz & U32, tokens: List<&2, Encode.Token>, +i: U32, +n: U32) -> Es:  (z, +c) = r  +room = (n - i : U32)  +max = Bool.pick(U32, U32.is_lt(room, 258), room, 258)  es.pick(ch.go(32n, Ch{z, i, c, max, 0, 0, ch.ok(i, c, max, 0)}), tokens, n)# A deferred match was searched before this position joined its hash chain.def es.cached(hit: Bool, z: Lz, tokens: List<&2, Encode.Token>, +i: U32, +n: U32, len: U32, dist: U32) -> Es:  match hit:    case True{}:      es.pick(Ch{lz.drop(lz.insert(z, i)), i, 0, 0, len, dist, False{}}, tokens, n)    case False{}:      es.chain(lz.insert(z, i), tokens, i, n)def es.find(short: Bool, z: Lz, tokens: List<&2, Encode.Token>, +i: U32, +n: U32, +len: U32, dist: U32) -> Es:  match short:    case True{}:      es.lit(z, tokens, i, n)    case False{}:      es.cached(U32.is_le(3, len), z, tokens, i, n, len, dist)def es.go(fuel: Nat, st: Es) -> Es:  match fuel:    case 0n:      st    case 1n+f:      Es{z, tokens, +i, +n, more, len, dist} = st      match more:        case True{}:          es.go(f, es.find(U32.is_lt((n - i : U32), 3), z, tokens, i, n, len, dist))        case False{}:          Es{z, tokens, i, n, False{}, len, dist}def block.fixed(xs: List<&2, Encode.Token>, w: Bits.Writer) -> Bits.Writer:  match xs:    case Nil{}:      em.lit(w, 256)    case Con{Encode.Lit{s}, t}:      block.fixed(t, em.lit(w, s))    case Con{Encode.Copy{ls, +le, lv, +ds, +de, dv}, t}:      w = Bits.Writer.lsb(bw.code(w, fx.lit(ls)), le, lv)      block.fixed(t, Bits.Writer.lsb(Bits.Writer.lsb(w, 5, Bits.rev(5n, ds, 0)), de, dv))def block.dynamic(tables: Maybe<&1, Encode.Tables>, xs: List<&2, Encode.Token>, w: Bits.Writer, +last: U32) -> Bits.Writer:  match tables:    case None{}:      block.fixed(xs, Bits.Writer.lsb(w, 3, (2 + last : U32)))    case Some{t}:      Encode.dynamic(xs, w, last, t)def block.plan(plan: Encode.Plan, xs: List<&2, Encode.Token>, w: Bits.Writer, +last: U32) -> Bits.Writer:  match plan:    case Encode.Fixed{}:      block.fixed(xs, Bits.Writer.lsb(w, 3, (2 + last : U32)))    case Encode.Dynamic{+ll, +dl}:      block.dynamic(Encode.tables(ll, dl), xs, w, last)def block.emit(small: Bool, +xs: List<&2, Encode.Token>, w: Bits.Writer, +last: U32) -> Bits.Writer:  match small:    case True{}:      block.fixed(xs, Bits.Writer.lsb(w, 3, (2 + last : U32)))    case False{}:      block.plan(Encode.plan(xs), xs, w, last)type Blocks is Type:  Blocks{z: Lz, w: Bits.Writer, i: U32, n: U32, more: Bool}def block.out(st: Es, w: Bits.Writer, +start: U32, +n: U32) -> Blocks:  Es{z, tokens, +i, end, more, len, dist} = st  +last = U32.is_eq(i, n)  Blocks{z, block.emit(U32.is_le((i - start : U32), 128), List.reverse(&2, Encode.Token, tokens), w, Bool.pick(U32, last, 1, 0)), i, n, Bool.not(last)}def blocks.go(fuel: Nat, st: Blocks) -> Bits.Writer:  match fuel:    case 0n:      Blocks{z, w, i, n, more} = st      Bits.Writer.align(w)    case 1n+p:      Blocks{z, w, +i, +n, more} = st      match more:        case False{}:          Bits.Writer.align(w)        case True{}:          +size = Bool.pick(U32, U32.is_lt((n - i : U32), 32768), (n - i : U32), 32768)          +end = (i + size : U32)          blocks.go(p, block.out(es.go(bits(size), Es{z, Nil{}, i, end, U32.is_lt(i, end), 0, 0}), w, i, n))def deflate.into(w: Bits.Writer, b: Bytes.Bytes) -> Bits.Writer:  Bytes.Bytes{+n, src} = b  blocks.go(Nat.add(Nat.div(bits(n), 32768n), 1n), Blocks{Lz{src, Array.new(U32, 15n, 0), Array.new(U32, 15n, 0)}, w, 0, n, True{}})# Raw DEFLATE.def deflate(b: Bytes.Bytes) -> Bytes.Bytes:  Bits.Writer.finish(deflate.into(Bits.Writer.new(), b))def gz.put(r: Bytes.Bytes & U32) -> Bytes.Bytes:  (b, +crc) = r  Bytes.Bytes{+n, buf} = b  head = Bits.Writer.lsb(Bits.Writer.lsb(Bits.Writer.lsb(Bits.Writer.new(), 32, 559903), 32, 0), 16, 65280)  Bits.Writer.finish(Bits.Writer.lsb(Bits.Writer.lsb(deflate.into(head, Bytes.Bytes{n, buf}), 32, crc), 32, n))# One gzip member (RFC 1952): no flags, mtime 0, OS unknown.def gzip(b: Bytes.Bytes) -> Bytes.Bytes:  gz.put(Hash.crc32(b))def zl.put(r: Bytes.Bytes & U32) -> Bytes.Bytes:  (b, +a) = r  Bits.Writer.finish(Bits.Writer.lsb(deflate.into(Bits.Writer.lsb(Bits.Writer.new(), 16, 376), b), 32, Bytes.bswap(a)))# RFC 1950: CMF 0x78 (32 KiB window), FLG 0x01 (fastest), then DEFLATE, then Adler-32, big-endian.def zlib(b: Bytes.Bytes) -> Bytes.Bytes:  zl.put(Hash.adler32(b))