// 从 https://github.com/101arrowz/fflate v0.7.4 复制过来并修改，github commit 0f439ed3293b1da1f439fbbc5125b1097b75d3ac
// 删掉了 worker, async, stream 相关的实现和接口，主要保证 unzipSync 能够使用
// 增加了 decoder 参数以支持文件名使用别的编码方式解码，如 gb18030, shift-jis
import { encode } from "./utils.js";
// DEFLATE is a complex format; to read this code, you should probably check the RFC first:
// https://tools.ietf.org/html/rfc1951
// You may also wish to take a look at the guide I made about this program:
// https://gist.github.com/101arrowz/253f31eb5abc3d9275ab943003ffecad
// Some of the following code is similar to that of UZIP.js:
// https://github.com/photopea/UZIP.js
// However, the vast majority of the codebase has diverged from UZIP.js to increase performance and reduce bundle size.
// Sometimes 0 will appear where -1 would be more appropriate. This is because using a uint
// is better for memory in most engines (I *think*).
// fixed length extra bits
const fleb = Buffer.from([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, /* unused */ 0, 0, /* impossible */ 0]);
// fixed distance extra bits
// see fleb note
const fdeb = Buffer.from([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, /* unused */ 0, 0]);
// code length index map
const clim = Buffer.from([16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15]);
// get base, reverse index map from extra bits
function freb(eb, start) {
    const b = new Uint16Array(31);
    for (let i = 0; i < 31; ++i)
        b[i] = start += 1 << eb[i - 1];
    // numbers here are at max 18 bits
    const r = new Uint32Array(b[30]);
    for (let i = 1; i < 30; ++i)
        for (let j = b[i]; j < b[i + 1]; ++j)
            r[j] = ((j - b[i]) << 5) | i;
    return [b, r];
}
const [fl, revfl] = freb(fleb, 2);
(fl[28] = 258), (revfl[258] = 28);
const [fd, revfd] = freb(fdeb, 0);
// map of value to reverse (assuming 16 bits)
const rev = new Uint16Array(32768);
for (let i = 0; i < 32768; ++i) {
    // reverse table algorithm from SO
    let x = ((i & 0xaaaa) >>> 1) | ((i & 0x5555) << 1);
    x = ((x & 0xcccc) >>> 2) | ((x & 0x3333) << 2);
    x = ((x & 0xf0f0) >>> 4) | ((x & 0x0f0f) << 4);
    rev[i] = (((x & 0xff00) >>> 8) | ((x & 0x00ff) << 8)) >>> 1;
}
// create huffman tree from u8 "map": index -> code length for code index
// mb (max bits) must be at most 15
// TODO: optimize/split up?
function hMap(cd, mb, r) {
    const s = cd.length;
    // index
    let i = 0;
    // u16 "map": index -> # of codes with bit length = index
    const l = new Uint16Array(mb);
    // length of cd must be 288 (total # of codes)
    for (; i < s; ++i)
        if (cd[i])
            ++l[cd[i] - 1];
    // u16 "map": index -> minimum code for bit length = index
    const le = new Uint16Array(mb);
    for (i = 0; i < mb; ++i)
        le[i] = (le[i - 1] + l[i - 1]) << 1;
    let co;
    if (r) {
        // u16 "map": index -> number of actual bits, symbol for code
        co = new Uint16Array(1 << mb);
        // bits to remove for reverser
        const rvb = 15 - mb;
        for (i = 0; i < s; ++i)
            // ignore 0 lengths
            if (cd[i]) {
                // num encoding both symbol and bits read
                const sv = (i << 4) | cd[i];
                // free bits
                const r = mb - cd[i];
                // start value
                let v = le[cd[i] - 1]++ << r;
                // m is end value
                for (const m = v | ((1 << r) - 1); v <= m; ++v)
                    // every 16 bit value starting with the code yields the same result
                    co[rev[v] >>> rvb] = sv;
            }
    }
    else {
        co = new Uint16Array(s);
        for (i = 0; i < s; ++i)
            if (cd[i])
                co[i] = rev[le[cd[i] - 1]++] >>> (15 - cd[i]);
    }
    return co;
}
// fixed length tree
const flt = new Uint8Array(288);
for (let i = 0; i < 144; ++i)
    flt[i] = 8;
for (let i = 144; i < 256; ++i)
    flt[i] = 9;
for (let i = 256; i < 280; ++i)
    flt[i] = 7;
for (let i = 280; i < 288; ++i)
    flt[i] = 8;
// fixed distance tree
const fdt = new Uint8Array(32);
for (let i = 0; i < 32; ++i)
    fdt[i] = 5;
// fixed length map
const flm = hMap(flt, 9, 0), flrm = hMap(flt, 9, 1);
// fixed distance map
const fdm = hMap(fdt, 5, 0), fdrm = hMap(fdt, 5, 1);
// find max of array
function max(a) {
    let m = a[0];
    for (let i = 1; i < a.length; ++i)
        if (a[i] > m)
            m = a[i];
    return m;
}
// read d, starting at bit p and mask with m
function bits(d, p, m) {
    const o = (p / 8) | 0;
    return ((d[o] | (d[o + 1] << 8)) >> (p & 7)) & m;
}
// read d, starting at bit p continuing for at least 16 bits
function bits16(d, p) {
    const o = (p / 8) | 0;
    return (d[o] | (d[o + 1] << 8) | (d[o + 2] << 16)) >> (p & 7);
}
// get end of byte
const shft = (p) => ((p + 7) / 8) | 0;
// typed array slice - allows garbage collector to free original reference,
// while being more compatible than .slice
function slc(v, s, e) {
    if (s == null || s < 0)
        s = 0;
    if (e == null || e > v.length)
        e = v.length;
    // can't use .constructor in case user-supplied
    const n = new (v.BYTES_PER_ELEMENT === 2 ? Uint16Array : v.BYTES_PER_ELEMENT === 4 ? Uint32Array : Uint8Array)(e - s);
    n.set(v.subarray(s, e));
    return n;
}
/** Codes for errors generated within this library */
export const FlateErrorCode = {
    UnexpectedEOF: 0,
    InvalidBlockType: 1,
    InvalidLengthLiteral: 2,
    InvalidDistance: 3,
    StreamFinished: 4,
    NoStreamHandler: 5,
    InvalidHeader: 6,
    NoCallback: 7,
    InvalidUTF8: 8,
    ExtraFieldTooLong: 9,
    InvalidDate: 10,
    FilenameTooLong: 11,
    StreamFinishing: 12,
    InvalidZipData: 13,
    UnknownCompressionMethod: 14
};
// error codes
const ec = [
    'unexpected EOF',
    'invalid block type',
    'invalid length/literal',
    'invalid distance',
    'stream finished',
    'no stream handler', // determined by compression function
    ,
    'no callback',
    'invalid UTF-8 data',
    'extra field too long',
    'date not in range 1980-2099',
    'filename too long',
    'stream finishing',
    'invalid zip data'
    // determined by unknown compression method
];
function err(ind, msg, nt) {
    const e = new Error(msg || ec[ind]);
    e.code = ind;
    if (Error.captureStackTrace)
        Error.captureStackTrace(e, err);
    if (!nt)
        throw e;
    return e;
}
// expands raw DEFLATE data
function inflt(dat, buf, st) {
    // source length
    const sl = dat.length;
    if (!sl || (st && st.f && !st.l))
        return buf || new Uint8Array(0);
    // have to estimate size
    const noBuf = !buf || st;
    // no state
    const noSt = !st || st.i;
    if (!st)
        st = {};
    // Assumes roughly 33% compression ratio average
    if (!buf)
        buf = new Uint8Array(sl * 3);
    // ensure buffer can fit at least l elements
    function cbuf(l) {
        let bl = buf.length;
        // need to increase size to fit
        if (l > bl) {
            // Double or set to necessary, whichever is greater
            const nbuf = new Uint8Array(Math.max(bl * 2, l));
            nbuf.set(buf);
            buf = nbuf;
        }
    }
    //  last chunk         bitpos           bytes
    let final = st.f || 0, pos = st.p || 0, bt = st.b || 0, lm = st.l, dm = st.d, lbt = st.m, dbt = st.n;
    // total bits
    const tbts = sl * 8;
    do {
        if (!lm) {
            // BFINAL - this is only 1 when last chunk is next
            final = bits(dat, pos, 1);
            // type: 0 = no compression, 1 = fixed huffman, 2 = dynamic huffman
            const type = bits(dat, pos + 1, 3);
            pos += 3;
            if (!type) {
                // go to end of byte boundary
                const s = shft(pos) + 4, l = dat[s - 4] | (dat[s - 3] << 8), t = s + l;
                if (t > sl) {
                    if (noSt)
                        err(0);
                    break;
                }
                // ensure size
                if (noBuf)
                    cbuf(bt + l);
                // Copy over uncompressed data
                buf.set(dat.subarray(s, t), bt);
                (st.b = bt += l), (st.p = pos = t * 8), (st.f = final);
                continue;
            }
            else if (type == 1)
                (lm = flrm), (dm = fdrm), (lbt = 9), (dbt = 5);
            else if (type == 2) {
                //  literal                            lengths
                const hLit = bits(dat, pos, 31) + 257, hcLen = bits(dat, pos + 10, 15) + 4;
                const tl = hLit + bits(dat, pos + 5, 31) + 1;
                pos += 14;
                // length+distance tree
                const ldt = new Uint8Array(tl);
                // code length tree
                const clt = new Uint8Array(19);
                for (let i = 0; i < hcLen; ++i)
                    // use index map to get real code
                    clt[clim[i]] = bits(dat, pos + i * 3, 7);
                pos += hcLen * 3;
                // code lengths bits
                const clb = max(clt), clbmsk = (1 << clb) - 1;
                // code lengths map
                const clm = hMap(clt, clb, 1);
                for (let i = 0; i < tl;) {
                    const r = clm[bits(dat, pos, clbmsk)];
                    // bits read
                    pos += r & 15;
                    // symbol
                    const s = r >>> 4;
                    // code length to copy
                    if (s < 16)
                        ldt[i++] = s;
                    else {
                        //  copy   count
                        let c = 0, n = 0;
                        if (s == 16)
                            (n = 3 + bits(dat, pos, 3)), (pos += 2), (c = ldt[i - 1]);
                        else if (s == 17)
                            (n = 3 + bits(dat, pos, 7)), (pos += 3);
                        else if (s == 18)
                            (n = 11 + bits(dat, pos, 127)), (pos += 7);
                        while (n--)
                            ldt[i++] = c;
                    }
                }
                //    length tree                 distance tree
                const lt = ldt.subarray(0, hLit), dt = ldt.subarray(hLit);
                // max length bits
                lbt = max(lt);
                // max dist bits
                dbt = max(dt);
                lm = hMap(lt, lbt, 1);
                dm = hMap(dt, dbt, 1);
            }
            else
                err(1);
            if (pos > tbts) {
                if (noSt)
                    err(0);
                break;
            }
        }
        // Make sure the buffer can hold this + the largest possible addition
        // Maximum chunk size (practically, theoretically infinite) is 2^17;
        if (noBuf)
            cbuf(bt + 131072);
        const lms = (1 << lbt) - 1, dms = (1 << dbt) - 1;
        let lpos = pos;
        for (;; lpos = pos) {
            // bits read, code
            const c = lm[bits16(dat, pos) & lms], sym = c >>> 4;
            pos += c & 15;
            if (pos > tbts) {
                if (noSt)
                    err(0);
                break;
            }
            if (!c)
                err(2);
            if (sym < 256)
                buf[bt++] = sym;
            else if (sym == 256) {
                (lpos = pos), (lm = null);
                break;
            }
            else {
                let add = sym - 254;
                // no extra bits needed if less
                if (sym > 264) {
                    // index
                    const i = sym - 257, b = fleb[i];
                    add = bits(dat, pos, (1 << b) - 1) + fl[i];
                    pos += b;
                }
                // dist
                const d = dm[bits16(dat, pos) & dms], dsym = d >>> 4;
                if (!d)
                    err(3);
                pos += d & 15;
                let dt = fd[dsym];
                if (dsym > 3) {
                    const b = fdeb[dsym];
                    (dt += bits16(dat, pos) & ((1 << b) - 1)), (pos += b);
                }
                if (pos > tbts) {
                    if (noSt)
                        err(0);
                    break;
                }
                if (noBuf)
                    cbuf(bt + 131072);
                const end = bt + add;
                for (; bt < end; bt += 4) {
                    buf[bt] = buf[bt - dt];
                    buf[bt + 1] = buf[bt + 1 - dt];
                    buf[bt + 2] = buf[bt + 2 - dt];
                    buf[bt + 3] = buf[bt + 3 - dt];
                }
                bt = end;
            }
        }
        (st.l = lm), (st.p = lpos), (st.b = bt), (st.f = final);
        if (lm)
            (final = 1), (st.m = lbt), (st.d = dm), (st.n = dbt);
    } while (!final);
    return bt == buf.length ? buf : slc(buf, 0, bt);
}
// starting at p, write the minimum number of bits that can hold v to d
function wbits(d, p, v) {
    v <<= p & 7;
    const o = (p / 8) | 0;
    d[o] |= v;
    d[o + 1] |= v >>> 8;
}
// starting at p, write the minimum number of bits (>8) that can hold v to d
function wbits16(d, p, v) {
    v <<= p & 7;
    const o = (p / 8) | 0;
    d[o] |= v;
    d[o + 1] |= v >>> 8;
    d[o + 2] |= v >>> 16;
}
// creates code lengths from a frequency table
function hTree(d, mb) {
    // Need extra info to make a tree
    const t = [];
    for (let i = 0; i < d.length; ++i)
        if (d[i])
            t.push({ s: i, f: d[i] });
    const s = t.length;
    const t2 = t.slice();
    if (!s)
        return [et, 0];
    if (s == 1) {
        const v = new Uint8Array(t[0].s + 1);
        v[t[0].s] = 1;
        return [v, 1];
    }
    t.sort((a, b) => a.f - b.f);
    // after i2 reaches last ind, will be stopped
    // freq must be greater than largest possible number of symbols
    t.push({ s: -1, f: 25001 });
    let l = t[0], r = t[1], i0 = 0, i1 = 1, i2 = 2;
    t[0] = { s: -1, f: l.f + r.f, l, r };
    // efficient algorithm from UZIP.js
    // i0 is lookbehind, i2 is lookahead - after processing two low-freq
    // symbols that combined have high freq, will start processing i2 (high-freq,
    // non-composite) symbols instead
    // see https://reddit.com/r/photopea/comments/ikekht/uzipjs_questions/
    while (i1 != s - 1) {
        l = t[t[i0].f < t[i2].f ? i0++ : i2++];
        r = t[i0 != i1 && t[i0].f < t[i2].f ? i0++ : i2++];
        t[i1++] = { s: -1, f: l.f + r.f, l, r };
    }
    let maxSym = t2[0].s;
    for (let i = 1; i < s; ++i)
        if (t2[i].s > maxSym)
            maxSym = t2[i].s;
    // code lengths
    const tr = new Uint16Array(maxSym + 1);
    // max bits in tree
    let mbt = ln(t[i1 - 1], tr, 0);
    if (mbt > mb) {
        // more algorithms from UZIP.js
        // TODO: find out how this code works (debt)
        //  ind    debt
        let i = 0, dt = 0;
        //    left            cost
        const lft = mbt - mb, cst = 1 << lft;
        t2.sort((a, b) => tr[b.s] - tr[a.s] || a.f - b.f);
        for (; i < s; ++i) {
            const i2 = t2[i].s;
            if (tr[i2] > mb) {
                dt += cst - (1 << (mbt - tr[i2]));
                tr[i2] = mb;
            }
            else
                break;
        }
        dt >>>= lft;
        while (dt > 0) {
            const i2 = t2[i].s;
            if (tr[i2] < mb)
                dt -= 1 << (mb - tr[i2]++ - 1);
            else
                ++i;
        }
        for (; i >= 0 && dt; --i) {
            const i2 = t2[i].s;
            if (tr[i2] == mb) {
                --tr[i2];
                ++dt;
            }
        }
        mbt = mb;
    }
    return [new Uint8Array(tr), mbt];
}
// get the max length and assign length codes
function ln(n, l, d) {
    return n.s == -1 ? Math.max(ln(n.l, l, d + 1), ln(n.r, l, d + 1)) : (l[n.s] = d);
}
// length codes generation
function lc(c) {
    let s = c.length;
    // Note that the semicolon was intentional
    while (s && !c[--s])
        ;
    const cl = new Uint16Array(++s);
    //  ind      num         streak
    let cli = 0, cln = c[0], cls = 1;
    function w(v) {
        cl[cli++] = v;
    }
    for (let i = 1; i <= s; ++i)
        if (c[i] == cln && i != s)
            ++cls;
        else {
            if (!cln && cls > 2) {
                for (; cls > 138; cls -= 138)
                    w(32754);
                if (cls > 2) {
                    w(cls > 10 ? ((cls - 11) << 5) | 28690 : ((cls - 3) << 5) | 12305);
                    cls = 0;
                }
            }
            else if (cls > 3) {
                w(cln), --cls;
                for (; cls > 6; cls -= 6)
                    w(8304);
                if (cls > 2)
                    w(((cls - 3) << 5) | 8208), (cls = 0);
            }
            while (cls--)
                w(cln);
            cls = 1;
            cln = c[i];
        }
    return [cl.subarray(0, cli), s];
}
// calculate the length of output from tree, code lengths
function clen(cf, cl) {
    let l = 0;
    for (let i = 0; i < cl.length; ++i)
        l += cf[i] * cl[i];
    return l;
}
// writes a fixed block
// returns the new bit pos
function wfblk(out, pos, dat) {
    // no need to write 00 as type: TypedArray defaults to 0
    const s = dat.length;
    const o = shft(pos + 2);
    out[o] = s & 255;
    out[o + 1] = s >>> 8;
    out[o + 2] = out[o] ^ 255;
    out[o + 3] = out[o + 1] ^ 255;
    for (let i = 0; i < s; ++i)
        out[o + i + 4] = dat[i];
    return (o + 4 + s) * 8;
}
// writes a block
function wblk(dat, out, final, syms, lf, df, eb, li, bs, bl, p) {
    wbits(out, p++, final);
    ++lf[256];
    const [dlt, mlb] = hTree(lf, 15);
    const [ddt, mdb] = hTree(df, 15);
    const [lclt, nlc] = lc(dlt);
    const [lcdt, ndc] = lc(ddt);
    const lcfreq = new Uint16Array(19);
    for (let i = 0; i < lclt.length; ++i)
        lcfreq[lclt[i] & 31]++;
    for (let i = 0; i < lcdt.length; ++i)
        lcfreq[lcdt[i] & 31]++;
    const [lct, mlcb] = hTree(lcfreq, 7);
    let nlcc = 19;
    for (; nlcc > 4 && !lct[clim[nlcc - 1]]; --nlcc)
        ;
    const flen = (bl + 5) << 3;
    const ftlen = clen(lf, flt) + clen(df, fdt) + eb;
    const dtlen = clen(lf, dlt) + clen(df, ddt) + eb + 14 + 3 * nlcc + clen(lcfreq, lct) + (2 * lcfreq[16] + 3 * lcfreq[17] + 7 * lcfreq[18]);
    if (flen <= ftlen && flen <= dtlen)
        return wfblk(out, p, dat.subarray(bs, bs + bl));
    let lm, ll, dm, dl;
    wbits(out, p, 1 + (dtlen < ftlen)), (p += 2);
    if (dtlen < ftlen) {
        (lm = hMap(dlt, mlb, 0)), (ll = dlt), (dm = hMap(ddt, mdb, 0)), (dl = ddt);
        const llm = hMap(lct, mlcb, 0);
        wbits(out, p, nlc - 257);
        wbits(out, p + 5, ndc - 1);
        wbits(out, p + 10, nlcc - 4);
        p += 14;
        for (let i = 0; i < nlcc; ++i)
            wbits(out, p + 3 * i, lct[clim[i]]);
        p += 3 * nlcc;
        const lcts = [lclt, lcdt];
        for (let it = 0; it < 2; ++it) {
            const clct = lcts[it];
            for (let i = 0; i < clct.length; ++i) {
                const len = clct[i] & 31;
                wbits(out, p, llm[len]), (p += lct[len]);
                if (len > 15)
                    wbits(out, p, (clct[i] >>> 5) & 127), (p += clct[i] >>> 12);
            }
        }
    }
    else
        (lm = flm), (ll = flt), (dm = fdm), (dl = fdt);
    for (let i = 0; i < li; ++i)
        if (syms[i] > 255) {
            const len = (syms[i] >>> 18) & 31;
            wbits16(out, p, lm[len + 257]), (p += ll[len + 257]);
            if (len > 7)
                wbits(out, p, (syms[i] >>> 23) & 31), (p += fleb[len]);
            const dst = syms[i] & 31;
            wbits16(out, p, dm[dst]), (p += dl[dst]);
            if (dst > 3)
                wbits16(out, p, (syms[i] >>> 5) & 8191), (p += fdeb[dst]);
        }
        else
            wbits16(out, p, lm[syms[i]]), (p += ll[syms[i]]);
    wbits16(out, p, lm[256]);
    return p + ll[256];
}
// deflate options (nice << 13) | chain
const deo = new Uint32Array([65540, 131080, 131088, 131104, 262176, 1048704, 1048832, 2114560, 2117632]);
// empty
const et = new Uint8Array(0);
// compresses data into a raw DEFLATE buffer
function dflt(dat, lvl, plvl, pre, post, lst) {
    const s = dat.length;
    const o = new Uint8Array(pre + s + 5 * (1 + Math.ceil(s / 7000)) + post);
    // writing to this writes to the output buffer
    const w = o.subarray(pre, o.length - post);
    let pos = 0;
    if (!lvl || s < 8)
        for (let i = 0; i <= s; i += 65535) {
            // end
            const e = i + 65535;
            if (e >= s)
                // write final block
                w[pos >> 3] = lst;
            pos = wfblk(w, pos + 1, dat.subarray(i, e));
        }
    else {
        const opt = deo[lvl - 1];
        const n = opt >>> 13, c = opt & 8191;
        const msk = (1 << plvl) - 1;
        //    prev 2-byte val map    curr 2-byte val map
        const prev = new Uint16Array(32768), head = new Uint16Array(msk + 1);
        const bs1 = Math.ceil(plvl / 3), bs2 = 2 * bs1;
        const hsh = (i) => (dat[i] ^ (dat[i + 1] << bs1) ^ (dat[i + 2] << bs2)) & msk;
        // 24576 is an arbitrary number of maximum symbols per block
        // 424 buffer for last block
        const syms = new Uint32Array(25000);
        // length/literal freq   distance freq
        const lf = new Uint16Array(288), df = new Uint16Array(32);
        //  l/lcnt  exbits  index  l/lind  waitdx  bitpos
        let lc = 0, eb = 0, i = 0, li = 0, wi = 0, bs = 0;
        for (; i < s; ++i) {
            // hash value
            // deopt when i > s - 3 - at end, deopt acceptable
            const hv = hsh(i);
            // index mod 32768    previous index mod
            let imod = i & 32767, pimod = head[hv];
            prev[imod] = pimod;
            head[hv] = imod;
            // We always should modify head and prev, but only add symbols if
            // this data is not yet processed ("wait" for wait index)
            if (wi <= i) {
                // bytes remaining
                const rem = s - i;
                if ((lc > 7000 || li > 24576) && rem > 423) {
                    pos = wblk(dat, w, 0, syms, lf, df, eb, li, bs, i - bs, pos);
                    (li = lc = eb = 0), (bs = i);
                    for (let j = 0; j < 286; ++j)
                        lf[j] = 0;
                    for (let j = 0; j < 30; ++j)
                        df[j] = 0;
                }
                //  len    dist   chain
                let l = 2, d = 0, ch = c, dif = (imod - pimod) & 32767;
                if (rem > 2 && hv == hsh(i - dif)) {
                    const maxn = Math.min(n, rem) - 1;
                    const maxd = Math.min(32767, i);
                    // max possible length
                    // not capped at dif because decompressors implement "rolling" index population
                    const ml = Math.min(258, rem);
                    while (dif <= maxd && --ch && imod != pimod) {
                        if (dat[i + l] == dat[i + l - dif]) {
                            let nl = 0;
                            for (; nl < ml && dat[i + nl] == dat[i + nl - dif]; ++nl)
                                ;
                            if (nl > l) {
                                (l = nl), (d = dif);
                                // break out early when we reach "nice" (we are satisfied enough)
                                if (nl > maxn)
                                    break;
                                // now, find the rarest 2-byte sequence within this
                                // length of literals and search for that instead.
                                // Much faster than just using the start
                                const mmd = Math.min(dif, nl - 2);
                                let md = 0;
                                for (let j = 0; j < mmd; ++j) {
                                    const ti = (i - dif + j + 32768) & 32767;
                                    const pti = prev[ti];
                                    const cd = (ti - pti + 32768) & 32767;
                                    if (cd > md)
                                        (md = cd), (pimod = ti);
                                }
                            }
                        }
                        // check the previous match
                        (imod = pimod), (pimod = prev[imod]);
                        dif += (imod - pimod + 32768) & 32767;
                    }
                }
                // d will be nonzero only when a match was found
                if (d) {
                    // store both dist and len data in one Uint32
                    // Make sure this is recognized as a len/dist with 28th bit (2^28)
                    syms[li++] = 268435456 | (revfl[l] << 18) | revfd[d];
                    const lin = revfl[l] & 31, din = revfd[d] & 31;
                    eb += fleb[lin] + fdeb[din];
                    ++lf[257 + lin];
                    ++df[din];
                    wi = i + l;
                    ++lc;
                }
                else {
                    syms[li++] = dat[i];
                    ++lf[dat[i]];
                }
            }
        }
        pos = wblk(dat, w, lst, syms, lf, df, eb, li, bs, i - bs, pos);
        // this is the easiest way to avoid needing to maintain state
        if (!lst && pos & 7)
            pos = wfblk(w, pos + 1, et);
    }
    return slc(o, 0, pre + shft(pos) + post);
}
// CRC32 table
const crct = (() => {
    const t = new Int32Array(256);
    for (let i = 0; i < 256; ++i) {
        let c = i, k = 9;
        while (--k)
            c = (c & 1 && -306674912) ^ (c >>> 1);
        t[i] = c;
    }
    return t;
})();
// CRC32
function crc() {
    let c = -1;
    return {
        p(d) {
            // closures have awful performance
            let cr = c;
            for (let i = 0; i < d.length; ++i)
                cr = crct[(cr & 255) ^ d[i]] ^ (cr >>> 8);
            c = cr;
        },
        d() {
            return ~c;
        }
    };
}
// Alder32
function adler() {
    let a = 1, b = 0;
    return {
        p(d) {
            // closures have awful performance
            let n = a, m = b;
            const l = d.length | 0;
            for (let i = 0; i != l;) {
                const e = Math.min(i + 2655, l);
                for (; i < e; ++i)
                    m += n += d[i];
                (n = (n & 65535) + 15 * (n >> 16)), (m = (m & 65535) + 15 * (m >> 16));
            }
            (a = n), (b = m);
        },
        d() {
            (a %= 65521), (b %= 65521);
            return ((a & 255) << 24) | ((a >>> 8) << 16) | ((b & 255) << 8) | (b >>> 8);
        }
    };
}
// deflate with opts
const dopt = (dat, opt, pre, post, st) => dflt(dat, opt.level == null ? 6 : opt.level, opt.mem == null ? Math.ceil(Math.max(8, Math.min(13, Math.log(dat.length))) * 1.5) : 12 + opt.mem, pre, post, !st);
// Walmart object spread
function mrg(a, b) {
    const o = {};
    for (const k in a)
        o[k] = a[k];
    for (const k in b)
        o[k] = b[k];
    return o;
}
// read 2 bytes
const b2 = (d, b) => d[b] | (d[b + 1] << 8);
// read 4 bytes
const b4 = (d, b) => (d[b] | (d[b + 1] << 8) | (d[b + 2] << 16) | (d[b + 3] << 24)) >>> 0;
const b8 = (d, b) => b4(d, b) + b4(d, b + 4) * 4294967296;
// write bytes
function wbytes(d, b, v) {
    for (; v; ++b)
        (d[b] = v), (v >>>= 8);
}
// gzip header
function gzh(c, o) {
    const fn = o.filename;
    (c[0] = 31), (c[1] = 139), (c[2] = 8), (c[8] = o.level < 2 ? 4 : o.level == 9 ? 2 : 0), (c[9] = 3); // assume Unix
    if (o.mtime != 0)
        wbytes(c, 4, Math.floor(new Date(o.mtime || Date.now()) / 1000));
    if (fn) {
        c[3] = 8;
        for (let i = 0; i <= fn.length; ++i)
            c[i + 10] = fn.charCodeAt(i);
    }
}
// gzip footer: -8 to -4 = CRC, -4 to -0 is length
// gzip start
function gzs(d) {
    if (d[0] != 31 || d[1] != 139 || d[2] != 8)
        err(6, 'invalid gzip data');
    const flg = d[3];
    let st = 10;
    if (flg & 4)
        st += d[10] | ((d[11] << 8) + 2);
    for (let zs = ((flg >> 3) & 1) + ((flg >> 4) & 1); zs > 0; zs -= !d[st++])
        ;
    return st + (flg & 2);
}
// gzip length
function gzl(d) {
    const l = d.length;
    return (d[l - 4] | (d[l - 3] << 8) | (d[l - 2] << 16) | (d[l - 1] << 24)) >>> 0;
}
// gzip header length
const gzhl = (o) => 10 + ((o.filename && o.filename.length + 1) || 0);
// zlib header
function zlh(c, o) {
    const lv = o.level, fl = lv == 0 ? 0 : lv < 6 ? 1 : lv == 9 ? 3 : 2;
    (c[0] = 120), (c[1] = (fl << 6) | (fl ? 32 - 2 * fl : 1));
}
// zlib valid
function zlv(d) {
    if ((d[0] & 15) != 8 || d[0] >>> 4 > 7 || ((d[0] << 8) | d[1]) % 31)
        err(6, 'invalid zlib data');
    if (d[1] & 32)
        err(6, 'invalid zlib data: preset dictionaries not supported');
}
// zlib footer: -4 to -0 is Adler32
/** Streaming DEFLATE compression */
export class Deflate {
    constructor(opts, cb) {
        if (!cb && typeof opts == 'function')
            (cb = opts), (opts = {});
        this.ondata = cb;
        this.o = opts || {};
    }
    o;
    d;
    /** The handler to call whenever data is available */
    ondata;
    p(c, f) {
        this.ondata(dopt(c, this.o, 0, 0, !f), f);
    }
    /**
        Pushes a chunk to be deflated
        @param chunk The chunk to push
        @param final Whether this is the last chunk
    */
    push(chunk, final) {
        if (!this.ondata)
            err(5);
        if (this.d)
            err(4);
        this.d = final;
        this.p(chunk, final || false);
    }
}
/**
    Expands DEFLATE data with no wrapper
    @param data The data to decompress
    @param out Where to write the data. Saves memory if you know the decompressed size and provide an output buffer of that length.
    @returns The decompressed version of the data
*/
export function inflateSync(data, out) {
    return inflt(data, out);
}
// before you yell at me for not just using extends, my reason is that TS inheritance is hard to workerize.
/** Streaming GZIP compression */
export class Gzip {
    c = crc();
    l = 0;
    v = 1;
    o;
    /** The handler to call whenever data is available */
    ondata;
    constructor(opts, cb) {
        Deflate.call(this, opts, cb);
    }
    /**
        Pushes a chunk to be GZIPped
        @param chunk The chunk to push
        @param final Whether this is the last chunk
    */
    push(chunk, final) {
        Deflate.prototype.push.call(this, chunk, final);
    }
    p(c, f) {
        this.c.p(c);
        this.l += c.length;
        const raw = dopt(c, this.o, this.v && gzhl(this.o), f && 8, !f);
        if (this.v)
            gzh(raw, this.o), (this.v = 0);
        if (f)
            wbytes(raw, raw.length - 8, this.c.d()), wbytes(raw, raw.length - 4, this.l);
        this.ondata(raw, f);
    }
}
/**
    Compresses data with GZIP
    @param data The data to compress
    @param opts The compression options
    @returns The gzipped version of the data
*/
export function gzipSync(data, opts) {
    if (!opts)
        opts = {};
    const c = crc(), l = data.length;
    c.p(data);
    const d = dopt(data, opts, gzhl(opts), 8), s = d.length;
    return gzh(d, opts), wbytes(d, s - 8, c.d()), wbytes(d, s - 4, l), d;
}
/**
    Expands GZIP data
    @param data The data to decompress
    @param out Where to write the data. GZIP already encodes the output size, so providing this doesn't save memory.
    @returns The decompressed version of the data
*/
export function gunzipSync(data, out) {
    return inflt(data.subarray(gzs(data), -8), out || new Uint8Array(gzl(data)));
}
/** Streaming Zlib compression */
export class Zlib {
    c = adler();
    v = 1;
    o;
    /** The handler to call whenever data is available */
    ondata;
    constructor(opts, cb) {
        Deflate.call(this, opts, cb);
    }
    /**
        Pushes a chunk to be zlibbed
        @param chunk The chunk to push
        @param final Whether this is the last chunk
    */
    push(chunk, final) {
        Deflate.prototype.push.call(this, chunk, final);
    }
    p(c, f) {
        this.c.p(c);
        const raw = dopt(c, this.o, this.v && 2, f && 4, !f);
        if (this.v)
            zlh(raw, this.o), (this.v = 0);
        if (f)
            wbytes(raw, raw.length - 4, this.c.d());
        this.ondata(raw, f);
    }
}
/**
    Compress data with Zlib
    @param data The data to compress
    @param opts The compression options
    @returns The zlib-compressed version of the data
*/
export function zlibSync(data, opts) {
    if (!opts)
        opts = {};
    const a = adler();
    a.p(data);
    const d = dopt(data, opts, 2, 4);
    return zlh(d, opts), wbytes(d, d.length - 4, a.d()), d;
}
/**
    Expands Zlib data
    @param data The data to decompress
    @param out Where to write the data. Saves memory if you know the decompressed size and provide an output buffer of that length.
    @returns The decompressed version of the data
*/
export function unzlibSync(data, out) {
    return inflt((zlv(data), data.subarray(2, -4)), out);
}
/**
    Expands compressed GZIP, Zlib, or raw DEFLATE data, automatically detecting the format
    @param data The data to decompress
    @param out Where to write the data. Saves memory if you know the decompressed size and provide an output buffer of that length.
    @returns The decompressed version of the data
*/
export function decompressSync(data, out) {
    return data[0] == 31 && data[1] == 139 && data[2] == 8
        ? gunzipSync(data, out)
        : (data[0] & 15) != 8 || data[0] >> 4 > 7 || ((data[0] << 8) | data[1]) % 31
            ? inflateSync(data, out)
            : unzlibSync(data, out);
}
// flatten a directory structure
function fltn(d, p, t, o) {
    for (const k in d) {
        let val = d[k], n = p + k, op = o;
        if (Array.isArray(val))
            (op = mrg(o, val[1])), (val = val[0]);
        if (val instanceof Uint8Array)
            t[n] = [val, op];
        else {
            t[(n += '/')] = [new Uint8Array(0), op];
            fltn(val, n, t, o);
        }
    }
}
// text decoder
const td = new TextDecoder();
// text decoder stream
let tds = 0;
try {
    td.decode(et, { stream: true });
    tds = 1;
}
catch (e) { }
// decode UTF8
function dutf8(d) {
    for (let r = '', i = 0;;) {
        let c = d[i++];
        const eb = (c > 127) + (c > 223) + (c > 239);
        if (i + eb > d.length)
            return [r, slc(d, i - 1)];
        if (!eb)
            r += String.fromCharCode(c);
        else if (eb == 3)
            (c = (((c & 15) << 18) | ((d[i++] & 63) << 12) | ((d[i++] & 63) << 6) | (d[i++] & 63)) - 65536),
                (r += String.fromCharCode(55296 | (c >> 10), 56320 | (c & 1023)));
        else if (eb & 1)
            r += String.fromCharCode(((c & 31) << 6) | (d[i++] & 63));
        else
            r += String.fromCharCode(((c & 15) << 12) | ((d[i++] & 63) << 6) | (d[i++] & 63));
    }
}
/** Streaming UTF-8 decoding */
export class DecodeUTF8 {
    p;
    t;
    /**
        Creates a UTF-8 decoding stream
        @param cb The callback to call whenever data is decoded
    */
    constructor(cb) {
        this.ondata = cb;
        if (tds)
            this.t = new TextDecoder();
        else
            this.p = et;
    }
    /**
        Pushes a chunk to be decoded from UTF-8 binary
        @param chunk The chunk to push
        @param final Whether this is the last chunk
    */
    push(chunk, final) {
        if (!this.ondata)
            err(5);
        final = !!final;
        if (this.t) {
            this.ondata(this.t.decode(chunk, { stream: true }), final);
            if (final) {
                if (this.t.decode().length)
                    err(8);
                this.t = null;
            }
            return;
        }
        if (!this.p)
            err(4);
        const dat = new Uint8Array(this.p.length + chunk.length);
        dat.set(this.p);
        dat.set(chunk, this.p.length);
        const [ch, np] = dutf8(dat);
        if (final) {
            if (np.length)
                err(8);
            this.p = null;
        }
        else
            this.p = np;
        this.ondata(ch, final);
    }
    /** The handler to call whenever data is available */
    ondata;
}
/** Streaming UTF-8 encoding */
export class EncodeUTF8 {
    d;
    /**
        Creates a UTF-8 decoding stream
        @param cb The callback to call whenever data is encoded
    */
    constructor(cb) {
        this.ondata = cb;
    }
    /**
        Pushes a chunk to be encoded to UTF-8
        @param chunk The string data to push
        @param final Whether this is the last chunk
    */
    push(chunk, final) {
        if (!this.ondata)
            err(5);
        if (this.d)
            err(4);
        this.ondata(strToU8(chunk), (this.d = final || false));
    }
    /** The handler to call whenever data is available */
    ondata;
}
/**
    Converts a string into a Uint8Array for use with compression/decompression methods
    @param str The string to encode
    @param latin1 Whether or not to interpret the data as Latin-1. This should
                  not need to be true unless decoding a binary string.
    @returns The string encoded in UTF-8/Latin-1 binary
*/
export function strToU8(str, latin1) {
    if (latin1) {
        const ar = new Uint8Array(str.length);
        for (let i = 0; i < str.length; ++i)
            ar[i] = str.charCodeAt(i);
        return ar;
    }
    return encode(str);
}
/**
    Converts a Uint8Array to a string
    @param dat The data to decode to string
    @param latin1 Whether or not to interpret the data as Latin-1. This should
                  not need to be true unless encoding to binary string.
    @returns The original UTF-8/Latin-1 string
*/
export function strFromU8(dat, decoder) {
    return decoder(dat);
}
// deflate bit flag
const dbf = (l) => (l == 1 ? 3 : l < 6 ? 2 : l == 9 ? 1 : 0);
// skip local zip header
const slzh = (d, b) => b + 30 + b2(d, b + 26) + b2(d, b + 28);
// read zip header
function zh(d, b, z, decoder) {
    const fnl = b2(d, b + 28), fn = strFromU8(d.subarray(b + 46, b + 46 + fnl), decoder), 
    // fn = strFromU8(d.subarray(b + 46, b + 46 + fnl), !(b2(d, b + 8) & 2048), decoder),
    es = b + 46 + fnl, bs = b4(d, b + 20);
    const [sc, su, off] = z && bs == 4294967295 ? z64e(d, es) : [bs, b4(d, b + 24), b4(d, b + 42)];
    return [b2(d, b + 10), sc, su, fn, es + b2(d, b + 30) + b2(d, b + 32), off];
}
// read zip64 extra field
function z64e(d, b) {
    for (; b2(d, b) != 1; b += 4 + b2(d, b + 2))
        ;
    return [b8(d, b + 12), b8(d, b + 4), b8(d, b + 20)];
}
/**
    Synchronously decompresses a ZIP archive. Prefer using `unzip` for better
    performance with more than one file.
    @param data The raw compressed ZIP file
    @param opts The ZIP extraction options
    @returns The decompressed files
*/
export function unzipSync(data, opts) {
    const files = {};
    let e = data.length - 22;
    for (; b4(data, e) != 0x6054b50; --e)
        if (!e || data.length - e > 65558)
            err(13);
    let c = b2(data, e + 8);
    if (!c)
        return {};
    let o = b4(data, e + 16);
    let z = o == 4294967295 || c == 65535;
    if (z) {
        let ze = b4(data, e - 12);
        z = b4(data, ze) == 0x6064b50;
        if (z) {
            c = b4(data, ze + 32);
            o = b4(data, ze + 48);
        }
    }
    const fltr = opts && opts.filter;
    for (let i = 0; i < c; ++i) {
        const [c, sc, su, fn, no, off] = zh(data, o, z, opts.decoder), b = slzh(data, off);
        o = no;
        if (!fltr ||
            fltr({
                name: fn,
                size: sc,
                originalSize: su,
                compression: c
            }))
            if (!c)
                files[fn] = slc(data, b, b + sc);
            else if (c == 8)
                files[fn] = inflateSync(data.subarray(b, b + sc), new Uint8Array(su));
            else
                err(14, 'unknown compression type ' + c);
    }
    return files;
}
//# sourceMappingURL=fflate.js.map