|
@@ -45,19 +45,19 @@ function deflateChunk(ctx: DeflateContext, count: number) {
|
|
|
|
|
|
const end = Math.min(i + count, dlen);
|
|
|
|
|
|
- for(; i < end; i++) {
|
|
|
+ for (; i < end; i++) {
|
|
|
c = nc;
|
|
|
|
|
|
- if(i + 1 < dlen - 2) {
|
|
|
+ if (i + 1 < dlen - 2) {
|
|
|
nc = _hash(data, i + 1);
|
|
|
const ii = ((i + 1) & 0x7fff);
|
|
|
prev[ii] = strt[nc];
|
|
|
strt[nc] = ii;
|
|
|
}
|
|
|
|
|
|
- if(cvrd <= i) {
|
|
|
- if((li > 14000 || lc > 26697) && (dlen - i) > 100) {
|
|
|
- if(cvrd < i) {
|
|
|
+ if (cvrd <= i) {
|
|
|
+ if ((li > 14000 || lc > 26697) && (dlen - i) > 100) {
|
|
|
+ if (cvrd < i) {
|
|
|
lits[li] = i - cvrd;
|
|
|
li += 2;
|
|
|
cvrd = i;
|
|
@@ -68,11 +68,11 @@ function deflateChunk(ctx: DeflateContext, count: number) {
|
|
|
}
|
|
|
|
|
|
let mch = 0;
|
|
|
- if(i < dlen - 2) {
|
|
|
+ if (i < dlen - 2) {
|
|
|
mch = _bestMatch(data, i, prev, c, Math.min(opt[2], dlen - i), opt[3]);
|
|
|
}
|
|
|
|
|
|
- if(mch !== 0) {
|
|
|
+ if (mch !== 0) {
|
|
|
const len = mch >>> 16, dst = mch & 0xffff;
|
|
|
const lgi = _goodIndex(len, U.of0); U.lhst[257 + lgi]++;
|
|
|
const dgi = _goodIndex(dst, U.df0); U.dhst[dgi]++; ebits += U.exb[lgi] + U.dxb[dgi];
|
|
@@ -120,10 +120,10 @@ export async function _deflateRaw(runtime: RuntimeContext, data: Uint8Array, out
|
|
|
const ctx = DeflateContext(data, out, opos, lvl);
|
|
|
const { dlen } = ctx;
|
|
|
|
|
|
- if(lvl === 0) {
|
|
|
+ if (lvl === 0) {
|
|
|
let { i, pos } = ctx;
|
|
|
|
|
|
- while(i < dlen) {
|
|
|
+ while (i < dlen) {
|
|
|
const len = Math.min(0xffff, dlen - i);
|
|
|
_putsE(out, pos, (i + len === dlen ? 1 : 0));
|
|
|
pos = _copyExact(data, i, len, out, pos + 8);
|
|
@@ -132,7 +132,7 @@ export async function _deflateRaw(runtime: RuntimeContext, data: Uint8Array, out
|
|
|
return pos >>> 3;
|
|
|
}
|
|
|
|
|
|
- if(dlen > 2) {
|
|
|
+ if (dlen > 2) {
|
|
|
ctx.nc = _hash(data, 0);
|
|
|
ctx.strt[ctx.nc] = 0;
|
|
|
}
|
|
@@ -147,15 +147,15 @@ export async function _deflateRaw(runtime: RuntimeContext, data: Uint8Array, out
|
|
|
let { li, cvrd, pos } = ctx;
|
|
|
const { i, lits, bs, ebits } = ctx;
|
|
|
|
|
|
- if(bs !== i || data.length === 0) {
|
|
|
- if(cvrd < i) {
|
|
|
+ if (bs !== i || data.length === 0) {
|
|
|
+ if (cvrd < i) {
|
|
|
lits[li] = i - cvrd;
|
|
|
li += 2;
|
|
|
cvrd = i;
|
|
|
}
|
|
|
pos = _writeBlock(1, lits, li, ebits, data, bs, i - bs, out, pos);
|
|
|
}
|
|
|
- while((pos & 7) !== 0) pos++;
|
|
|
+ while ((pos & 7) !== 0) pos++;
|
|
|
return pos >>> 3;
|
|
|
}
|
|
|
|
|
@@ -163,21 +163,21 @@ function _bestMatch(data: Uint8Array, i: number, prev: Uint16Array, c: number, n
|
|
|
let ci = (i & 0x7fff), pi = prev[ci];
|
|
|
// console.log("----", i);
|
|
|
let dif = ((ci - pi + (1 << 15)) & 0x7fff);
|
|
|
- if(pi === ci || c !== _hash(data, i - dif)) return 0;
|
|
|
+ if (pi === ci || c !== _hash(data, i - dif)) return 0;
|
|
|
let tl = 0, td = 0; // top length, top distance
|
|
|
const dlim = Math.min(0x7fff, i);
|
|
|
- while(dif <= dlim && --chain !== 0 && pi !== ci /* && c==UZIP.F._hash(data,i-dif)*/) {
|
|
|
- if(tl === 0 || (data[i + tl] === data[i + tl - dif])) {
|
|
|
+ while (dif <= dlim && --chain !== 0 && pi !== ci /* && c==UZIP.F._hash(data,i-dif)*/) {
|
|
|
+ if (tl === 0 || (data[i + tl] === data[i + tl - dif])) {
|
|
|
let cl = _howLong(data, i, dif);
|
|
|
- if(cl > tl) {
|
|
|
- tl = cl; td = dif; if(tl >= nice) break; //*
|
|
|
- if(dif + 2 < cl) cl = dif + 2;
|
|
|
+ if (cl > tl) {
|
|
|
+ tl = cl; td = dif; if (tl >= nice) break; //*
|
|
|
+ if (dif + 2 < cl) cl = dif + 2;
|
|
|
let maxd = 0; // pi does not point to the start of the word
|
|
|
- for(let j = 0; j < cl - 2; j++) {
|
|
|
+ for (let j = 0; j < cl - 2; j++) {
|
|
|
const ei = (i - dif + j + (1 << 15)) & 0x7fff;
|
|
|
const li = prev[ei];
|
|
|
const curd = (ei - li + (1 << 15)) & 0x7fff;
|
|
|
- if(curd > maxd) { maxd = curd; pi = ei; }
|
|
|
+ if (curd > maxd) { maxd = curd; pi = ei; }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -189,11 +189,11 @@ function _bestMatch(data: Uint8Array, i: number, prev: Uint16Array, c: number, n
|
|
|
}
|
|
|
|
|
|
function _howLong(data: Uint8Array, i: number, dif: number) {
|
|
|
- if(data[i] !== data[i - dif] || data[i + 1] !== data[i + 1 - dif] || data[i + 2] !== data[i + 2 - dif]) return 0;
|
|
|
+ if (data[i] !== data[i - dif] || data[i + 1] !== data[i + 1 - dif] || data[i + 2] !== data[i + 2 - dif]) return 0;
|
|
|
const oi = i, l = Math.min(data.length, i + 258);
|
|
|
i += 3;
|
|
|
// while(i+4<l && data[i]==data[i-dif] && data[i+1]==data[i+1-dif] && data[i+2]==data[i+2-dif] && data[i+3]==data[i+3-dif]) i+=4;
|
|
|
- while(i < l && data[i] === data[i - dif]) i++;
|
|
|
+ while (i < l && data[i] === data[i - dif]) i++;
|
|
|
return i - oi;
|
|
|
}
|
|
|
|
|
@@ -217,9 +217,9 @@ function _writeBlock(BFINAL: number, lits: Uint32Array, li: number, ebits: numbe
|
|
|
let dynSize = ebits + contSize(U.ltree, U.lhst) + contSize(U.dtree, U.dhst);
|
|
|
dynSize += 14 + 3 * numh + contSize(U.itree, U.ihst) + (U.ihst[16] * 2 + U.ihst[17] * 3 + U.ihst[18] * 7);
|
|
|
|
|
|
- for(let j = 0; j < 286; j++) U.lhst[j] = 0;
|
|
|
- for(let j = 0; j < 30; j++) U.dhst[j] = 0;
|
|
|
- for(let j = 0; j < 19; j++) U.ihst[j] = 0;
|
|
|
+ for (let j = 0; j < 286; j++) U.lhst[j] = 0;
|
|
|
+ for (let j = 0; j < 30; j++) U.dhst[j] = 0;
|
|
|
+ for (let j = 0; j < 19; j++) U.ihst[j] = 0;
|
|
|
|
|
|
const BTYPE = (cstSize < fxdSize && cstSize < dynSize) ? 0 : (fxdSize < dynSize ? 1 : 2);
|
|
|
_putsF(out, pos, BFINAL);
|
|
@@ -227,14 +227,14 @@ function _writeBlock(BFINAL: number, lits: Uint32Array, li: number, ebits: numbe
|
|
|
pos += 3;
|
|
|
|
|
|
// let opos = pos;
|
|
|
- if(BTYPE === 0) {
|
|
|
- while((pos & 7) !== 0) pos++;
|
|
|
+ if (BTYPE === 0) {
|
|
|
+ while ((pos & 7) !== 0) pos++;
|
|
|
pos = _copyExact(data, o0, l0, out, pos);
|
|
|
} else {
|
|
|
let ltree: number[], dtree: number[];
|
|
|
- if(BTYPE === 1) {
|
|
|
+ if (BTYPE === 1) {
|
|
|
ltree = U.fltree; dtree = U.fdtree;
|
|
|
- } else if(BTYPE === 2) {
|
|
|
+ } else if (BTYPE === 2) {
|
|
|
makeCodes(U.ltree, ML); revCodes(U.ltree, ML);
|
|
|
makeCodes(U.dtree, MD); revCodes(U.dtree, MD);
|
|
|
makeCodes(U.itree, MH); revCodes(U.itree, MH);
|
|
@@ -245,7 +245,7 @@ function _writeBlock(BFINAL: number, lits: Uint32Array, li: number, ebits: numbe
|
|
|
_putsE(out, pos, numd - 1); pos += 5; // 30
|
|
|
_putsE(out, pos, numh - 4); pos += 4; // 19
|
|
|
|
|
|
- for(let i = 0; i < numh; i++) _putsE(out, pos + i * 3, U.itree[(U.ordr[i] << 1) + 1]);
|
|
|
+ for (let i = 0; i < numh; i++) _putsE(out, pos + i * 3, U.itree[(U.ordr[i] << 1) + 1]);
|
|
|
pos += 3 * numh;
|
|
|
pos = _codeTiny(lset, U.itree, out, pos);
|
|
|
pos = _codeTiny(dset, U.itree, out, pos);
|
|
@@ -254,11 +254,11 @@ function _writeBlock(BFINAL: number, lits: Uint32Array, li: number, ebits: numbe
|
|
|
}
|
|
|
|
|
|
let off = o0;
|
|
|
- for(let si = 0; si < li; si += 2) {
|
|
|
+ for (let si = 0; si < li; si += 2) {
|
|
|
const qb = lits[si], len = (qb >>> 23), end = off + (qb & ((1 << 23) - 1));
|
|
|
- while(off < end) pos = _writeLit(data[off++], ltree, out, pos);
|
|
|
+ while (off < end) pos = _writeLit(data[off++], ltree, out, pos);
|
|
|
|
|
|
- if(len !== 0) {
|
|
|
+ if (len !== 0) {
|
|
|
const qc = lits[si + 1], dst = (qc >> 16), lgi = (qc >> 8) & 255, dgi = (qc & 255);
|
|
|
pos = _writeLit(257 + lgi, ltree, out, pos);
|
|
|
_putsE(out, pos, len - U.of0[lgi]); pos += U.exb[lgi];
|
|
@@ -298,26 +298,26 @@ function getTrees() {
|
|
|
const numl = _lenCodes(U.ltree, lset);
|
|
|
const dset: number[] = [];
|
|
|
const numd = _lenCodes(U.dtree, dset);
|
|
|
- for(let i = 0; i < lset.length; i += 2) U.ihst[lset[i]]++;
|
|
|
- for(let i = 0; i < dset.length; i += 2) U.ihst[dset[i]]++;
|
|
|
+ for (let i = 0; i < lset.length; i += 2) U.ihst[lset[i]]++;
|
|
|
+ for (let i = 0; i < dset.length; i += 2) U.ihst[dset[i]]++;
|
|
|
const MH = _hufTree(U.ihst, U.itree, 7);
|
|
|
let numh = 19;
|
|
|
- while(numh > 4 && U.itree[(U.ordr[numh - 1] << 1) + 1] === 0) numh--;
|
|
|
+ while (numh > 4 && U.itree[(U.ordr[numh - 1] << 1) + 1] === 0) numh--;
|
|
|
return [ML, MD, MH, numl, numd, numh, lset, dset] as const;
|
|
|
}
|
|
|
|
|
|
function contSize(tree: number[], hst: NumberArray) {
|
|
|
let s = 0;
|
|
|
- for(let i = 0; i < hst.length; i++) s += hst[i] * tree[(i << 1) + 1];
|
|
|
+ for (let i = 0; i < hst.length; i++) s += hst[i] * tree[(i << 1) + 1];
|
|
|
return s;
|
|
|
}
|
|
|
|
|
|
function _codeTiny(set: number[], tree: number[], out: Uint8Array, pos: number) {
|
|
|
- for(let i = 0; i < set.length; i += 2) {
|
|
|
+ for (let i = 0; i < set.length; i += 2) {
|
|
|
const l = set[i], rst = set[i + 1]; // console.log(l, pos, tree[(l<<1)+1]);
|
|
|
pos = _writeLit(l, tree, out, pos);
|
|
|
const rsl = l === 16 ? 2 : (l === 17 ? 3 : 7);
|
|
|
- if(l > 15) {
|
|
|
+ if (l > 15) {
|
|
|
_putsE(out, pos, rst);
|
|
|
pos += rsl;
|
|
|
}
|
|
@@ -328,19 +328,19 @@ function _codeTiny(set: number[], tree: number[], out: Uint8Array, pos: number)
|
|
|
|
|
|
function _lenCodes(tree: number[], set: number[]) {
|
|
|
let len = tree.length;
|
|
|
- while(len !== 2 && tree[len - 1] === 0) len -= 2; // when no distances, keep one code with length 0
|
|
|
- for(let i = 0; i < len; i += 2) {
|
|
|
+ while (len !== 2 && tree[len - 1] === 0) len -= 2; // when no distances, keep one code with length 0
|
|
|
+ for (let i = 0; i < len; i += 2) {
|
|
|
const l = tree[i + 1], nxt = (i + 3 < len ? tree[i + 3] : -1), nnxt = (i + 5 < len ? tree[i + 5] : -1), prv = (i === 0 ? -1 : tree[i - 1]);
|
|
|
- if(l === 0 && nxt === l && nnxt === l) {
|
|
|
+ if (l === 0 && nxt === l && nnxt === l) {
|
|
|
let lz = i + 5;
|
|
|
- while(lz + 2 < len && tree[lz + 2] === l) lz += 2;
|
|
|
+ while (lz + 2 < len && tree[lz + 2] === l) lz += 2;
|
|
|
const zc = Math.min((lz + 1 - i) >>> 1, 138);
|
|
|
- if(zc < 11) set.push(17, zc - 3);
|
|
|
+ if (zc < 11) set.push(17, zc - 3);
|
|
|
else set.push(18, zc - 11);
|
|
|
i += zc * 2 - 2;
|
|
|
- } else if(l === prv && nxt === l && nnxt === l) {
|
|
|
+ } else if (l === prv && nxt === l && nnxt === l) {
|
|
|
let lz = i + 5;
|
|
|
- while(lz + 2 < len && tree[lz + 2] === l) lz += 2;
|
|
|
+ while (lz + 2 < len && tree[lz + 2] === l) lz += 2;
|
|
|
const zc = Math.min((lz + 1 - i) >>> 1, 6);
|
|
|
set.push(16, zc - 3);
|
|
|
i += zc * 2 - 2;
|
|
@@ -353,11 +353,11 @@ function _lenCodes(tree: number[], set: number[]) {
|
|
|
|
|
|
function _goodIndex(v: number, arr: number[]) {
|
|
|
let i = 0;
|
|
|
- if(arr[i | 16] <= v) i |= 16;
|
|
|
- if(arr[i | 8] <= v) i |= 8;
|
|
|
- if(arr[i | 4] <= v) i |= 4;
|
|
|
- if(arr[i | 2] <= v) i |= 2;
|
|
|
- if(arr[i | 1] <= v) i |= 1;
|
|
|
+ if (arr[i | 16] <= v) i |= 16;
|
|
|
+ if (arr[i | 8] <= v) i |= 8;
|
|
|
+ if (arr[i | 4] <= v) i |= 4;
|
|
|
+ if (arr[i | 2] <= v) i |= 2;
|
|
|
+ if (arr[i | 1] <= v) i |= 1;
|
|
|
return i;
|
|
|
}
|
|
|
|