// A pure-JS zlib INFLATE that reports the bytes it consumed — the one thing the standard // DecompressionStream cannot say, and the one thing a packfile reader must know (each object's // zlib stream is followed by the next object's header with no length in between). Stored, // fixed-Huffman and dynamic-Huffman blocks (RFC 1951) inside a zlib wrapper (RFC 1950: a 2-byte // header, an Adler-32 trailer). No dependency; runs on Bun and workerd alike. const LENGTH_BASE = [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]; const LENGTH_EXTRA = [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]; const DIST_BASE = [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]; const DIST_EXTRA = [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]; const CL_ORDER = [16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15]; type Huffman = { counts: Uint16Array; symbols: Uint16Array }; function buildHuffman(lengths: Uint8Array | number[], n: number): Huffman { const counts = new Uint16Array(16); const symbols = new Uint16Array(n); for (let i = 0; i < n; i++) counts[lengths[i]!]!++; counts[0] = 0; const offs = new Uint16Array(16); for (let i = 1; i < 16; i++) offs[i] = offs[i - 1]! + counts[i - 1]!; for (let i = 0; i < n; i++) if (lengths[i]) symbols[offs[lengths[i]!]!++] = i; return { counts, symbols }; } class BitReader { pos: number; bitBuf = 0; bitCount = 0; constructor(readonly input: Uint8Array, offset: number) { this.pos = offset; } bit(): number { if (this.bitCount === 0) { if (this.pos >= this.input.length) throw new Error('inflate: unexpected end of input'); this.bitBuf = this.input[this.pos++]!; this.bitCount = 8; } const b = this.bitBuf & 1; this.bitBuf >>>= 1; this.bitCount--; return b; } bits(n: number): number { let v = 0; for (let i = 0; i < n; i++) v |= this.bit() << i; return v; } symbol(h: Huffman): number { let code = 0, first = 0, index = 0; for (let len = 1; len < 16; len++) { code |= this.bit(); const count = h.counts[len]!; if (code - count < first) return h.symbols[index + (code - first)]!; index += count; first += count; first <<= 1; code <<= 1; } throw new Error('inflate: bad huffman code'); } alignByte(): void { this.bitBuf = 0; this.bitCount = 0; } } class Out { buf = new Uint8Array(1 << 16); len = 0; push(b: number): void { if (this.len === this.buf.length) { const n = new Uint8Array(this.buf.length * 2); n.set(this.buf); this.buf = n; } this.buf[this.len++] = b; } copy(dist: number, length: number): void { if (dist > this.len) throw new Error('inflate: distance too far back'); for (let i = 0; i < length; i++) this.push(this.buf[this.len - dist]!); } // A right-sized copy: a view would pin the whole (>= 64 KiB, doubling) scratch buffer behind // every inflated object, which is what a 105k-object push cost the github twin (~7 GB). bytes(): Uint8Array { return this.buf.slice(0, this.len); } } let FIXED: { lit: Huffman; dist: Huffman } | undefined; function fixedTables(): { lit: Huffman; dist: Huffman } { if (FIXED) return FIXED; const l = new Uint8Array(288); for (let i = 0; i < 144; i++) l[i] = 8; for (let i = 144; i < 256; i++) l[i] = 9; for (let i = 256; i < 280; i++) l[i] = 7; for (let i = 280; i < 288; i++) l[i] = 8; const d = new Uint8Array(30).fill(5); FIXED = { lit: buildHuffman(l, 288), dist: buildHuffman(d, 30) }; return FIXED; } function inflateBlockData(r: BitReader, out: Out, lit: Huffman, dist: Huffman): void { for (;;) { const sym = r.symbol(lit); if (sym < 256) out.push(sym); else if (sym === 256) return; else { const li = sym - 257; if (li >= 29) throw new Error('inflate: bad length symbol'); const length = LENGTH_BASE[li]! + r.bits(LENGTH_EXTRA[li]!); const di = r.symbol(dist); if (di >= 30) throw new Error('inflate: bad distance symbol'); out.copy(DIST_BASE[di]! + r.bits(DIST_EXTRA[di]!), length); } } } function inflateDynamic(r: BitReader, out: Out): void { const hlit = r.bits(5) + 257, hdist = r.bits(5) + 1, hclen = r.bits(4) + 4; const cl = new Uint8Array(19); for (let i = 0; i < hclen; i++) cl[CL_ORDER[i]!] = r.bits(3); const clh = buildHuffman(cl, 19); const lengths = new Uint8Array(hlit + hdist); for (let i = 0; i < hlit + hdist;) { const sym = r.symbol(clh); if (sym < 16) lengths[i++] = sym; else if (sym === 16) { if (i === 0) throw new Error('inflate: repeat with no previous length'); const prev = lengths[i - 1]!; for (let n = 3 + r.bits(2); n > 0; n--) lengths[i++] = prev; } else if (sym === 17) { for (let n = 3 + r.bits(3); n > 0; n--) lengths[i++] = 0; } else { for (let n = 11 + r.bits(7); n > 0; n--) lengths[i++] = 0; } } inflateBlockData(r, out, buildHuffman(lengths.subarray(0, hlit), hlit), buildHuffman(lengths.subarray(hlit), hdist)); } /** Raw DEFLATE (RFC 1951) starting at `offset`: the bytes out and the input consumed. */ export function inflateRaw(input: Uint8Array, offset = 0): { out: Uint8Array; consumed: number } { const r = new BitReader(input, offset); const out = new Out(); let final = 0; do { final = r.bit(); const type = r.bits(2); if (type === 0) { r.alignByte(); const len = input[r.pos]! | (input[r.pos + 1]! << 8); const nlen = input[r.pos + 2]! | (input[r.pos + 3]! << 8); if ((len ^ 0xffff) !== nlen) throw new Error('inflate: stored block length mismatch'); r.pos += 4; for (let i = 0; i < len; i++) out.push(input[r.pos++]!); } else if (type === 1) { const t = fixedTables(); inflateBlockData(r, out, t.lit, t.dist); } else if (type === 2) inflateDynamic(r, out); else throw new Error('inflate: reserved block type'); } while (!final); return { out: out.bytes(), consumed: r.pos - offset }; } export function adler32(bytes: Uint8Array): number { let a = 1, b = 0; for (let i = 0; i < bytes.length; i++) { a = (a + bytes[i]!) % 65521; b = (b + a) % 65521; } return ((b << 16) | a) >>> 0; } /** zlib (RFC 1950) starting at `offset`: header checked, Adler-32 verified, consumed includes both. */ export function inflateZlib(input: Uint8Array, offset = 0): { out: Uint8Array; consumed: number } { const cmf = input[offset]!, flg = input[offset + 1]!; if ((cmf & 0x0f) !== 8 || ((cmf << 8) | flg) % 31 !== 0) throw new Error('inflate: not a zlib stream'); if (flg & 0x20) throw new Error('inflate: preset dictionary is not supported'); const { out, consumed } = inflateRaw(input, offset + 2); const t = offset + 2 + consumed; const expected = ((input[t]! << 24) | (input[t + 1]! << 16) | (input[t + 2]! << 8) | input[t + 3]!) >>> 0; if (expected !== adler32(out)) throw new Error('inflate: adler32 mismatch'); return { out, consumed: consumed + 6 }; }