molstar
Version:
A comprehensive macromolecular library.
232 lines • 8.16 kB
JavaScript
/**
* Copyright (c) 2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Alexander Rose <alexander.rose@weirdbyte.de>
*
* ported from https://github.com/photopea/UZIP.js/blob/master/UZIP.js
* MIT License, Copyright (c) 2018 Photopea
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports._inflate = void 0;
var tslib_1 = require("tslib");
var util_1 = require("./util");
function InflateContext(data, buf) {
var noBuf = buf === undefined;
if (buf === undefined)
buf = new Uint8Array((data.length >>> 2) << 3);
return {
data: data,
buf: buf,
noBuf: noBuf,
BFINAL: 0,
off: 0,
pos: 0
};
}
function inflateBlocks(ctx, count) {
var data = ctx.data, noBuf = ctx.noBuf;
var buf = ctx.buf, BFINAL = ctx.BFINAL, off = ctx.off, pos = ctx.pos;
var iBlock = 0;
while (BFINAL === 0 && iBlock < count) {
var lmap = void 0, dmap = void 0;
var ML = 0, MD = 0;
BFINAL = _bitsF(data, pos, 1);
iBlock += 1;
var BTYPE = _bitsF(data, pos + 1, 2);
pos += 3;
if (BTYPE === 0) {
// uncompressed block
if ((pos & 7) !== 0)
pos += 8 - (pos & 7);
var p8 = (pos >>> 3) + 4;
var len = data[p8 - 4] | (data[p8 - 3] << 8);
if (noBuf)
buf = _check(buf, off + len);
buf.set(new Uint8Array(data.buffer, data.byteOffset + p8, len), off);
pos = ((p8 + len) << 3);
off += len;
continue;
}
// grow output buffer if not provided
if (noBuf)
buf = _check(buf, off + (1 << 17));
if (BTYPE === 1) {
// block compressed with fixed Huffman codes
lmap = util_1.U.flmap;
dmap = util_1.U.fdmap;
ML = (1 << 9) - 1;
MD = (1 << 5) - 1;
}
else if (BTYPE === 2) {
// block compressed with dynamic Huffman codes
var HLIT = _bitsE(data, pos, 5) + 257;
var HDIST = _bitsE(data, pos + 5, 5) + 1;
var HCLEN = _bitsE(data, pos + 10, 4) + 4;
pos += 14;
for (var i = 0; i < 38; i += 2) {
util_1.U.itree[i] = 0;
util_1.U.itree[i + 1] = 0;
}
var tl = 1;
for (var i = 0; i < HCLEN; i++) {
var l = _bitsE(data, pos + i * 3, 3);
util_1.U.itree[(util_1.U.ordr[i] << 1) + 1] = l;
if (l > tl)
tl = l;
}
pos += 3 * HCLEN;
(0, util_1.makeCodes)(util_1.U.itree, tl);
(0, util_1.codes2map)(util_1.U.itree, tl, util_1.U.imap);
lmap = util_1.U.lmap;
dmap = util_1.U.dmap;
pos = _decodeTiny(util_1.U.imap, (1 << tl) - 1, HLIT + HDIST, data, pos, util_1.U.ttree);
var mx0 = _copyOut(util_1.U.ttree, 0, HLIT, util_1.U.ltree);
ML = (1 << mx0) - 1;
var mx1 = _copyOut(util_1.U.ttree, HLIT, HDIST, util_1.U.dtree);
MD = (1 << mx1) - 1;
(0, util_1.makeCodes)(util_1.U.ltree, mx0);
(0, util_1.codes2map)(util_1.U.ltree, mx0, lmap);
(0, util_1.makeCodes)(util_1.U.dtree, mx1);
(0, util_1.codes2map)(util_1.U.dtree, mx1, dmap);
}
else {
throw new Error("unknown BTYPE " + BTYPE);
}
while (true) {
var code = lmap[_get17(data, pos) & ML];
pos += code & 15;
var lit = code >>> 4;
if ((lit >>> 8) === 0) {
buf[off++] = lit;
}
else if (lit === 256) {
break;
}
else {
var end = off + lit - 254;
if (lit > 264) {
var ebs = util_1.U.ldef[lit - 257];
end = off + (ebs >>> 3) + _bitsE(data, pos, ebs & 7);
pos += ebs & 7;
}
var dcode = dmap[_get17(data, pos) & MD];
pos += dcode & 15;
var dlit = dcode >>> 4;
var dbs = util_1.U.ddef[dlit];
var dst = (dbs >>> 4) + _bitsF(data, pos, dbs & 15);
pos += dbs & 15;
if (noBuf)
buf = _check(buf, off + (1 << 17));
while (off < end) {
buf[off] = buf[off++ - dst];
buf[off] = buf[off++ - dst];
buf[off] = buf[off++ - dst];
buf[off] = buf[off++ - dst];
}
off = end;
}
}
}
ctx.buf = buf;
ctx.BFINAL = BFINAL;
ctx.off = off;
ctx.pos = pos;
}
// https://tools.ietf.org/html/rfc1951
function _inflate(runtime, data, buf) {
return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
var ctx;
return (0, tslib_1.__generator)(this, function (_a) {
switch (_a.label) {
case 0:
if (data[0] === 3 && data[1] === 0)
return [2 /*return*/, (buf ? buf : new Uint8Array(0))];
ctx = InflateContext(data, buf);
_a.label = 1;
case 1:
if (!(ctx.BFINAL === 0)) return [3 /*break*/, 4];
if (!runtime.shouldUpdate) return [3 /*break*/, 3];
return [4 /*yield*/, runtime.update({ message: 'Inflating blocks...', current: ctx.pos, max: data.length })];
case 2:
_a.sent();
_a.label = 3;
case 3:
inflateBlocks(ctx, 100);
return [3 /*break*/, 1];
case 4: return [2 /*return*/, ctx.buf.length === ctx.off ? ctx.buf : ctx.buf.slice(0, ctx.off)];
}
});
});
}
exports._inflate = _inflate;
function _check(buf, len) {
var bl = buf.length;
if (len <= bl)
return buf;
var nbuf = new Uint8Array(Math.max(bl << 1, len));
nbuf.set(buf, 0);
return nbuf;
}
function _decodeTiny(lmap, LL, len, data, pos, tree) {
var i = 0;
while (i < len) {
var code = lmap[_get17(data, pos) & LL];
pos += code & 15;
var lit = code >>> 4;
if (lit <= 15) {
tree[i] = lit;
i++;
}
else {
var ll = 0, n = 0;
if (lit === 16) {
n = (3 + _bitsE(data, pos, 2));
pos += 2;
ll = tree[i - 1];
}
else if (lit === 17) {
n = (3 + _bitsE(data, pos, 3));
pos += 3;
}
else if (lit === 18) {
n = (11 + _bitsE(data, pos, 7));
pos += 7;
}
var ni = i + n;
while (i < ni) {
tree[i] = ll;
i++;
}
}
}
return pos;
}
function _copyOut(src, off, len, tree) {
var mx = 0, i = 0;
var tl = tree.length >>> 1;
while (i < len) {
var v = src[i + off];
tree[(i << 1)] = 0;
tree[(i << 1) + 1] = v;
if (v > mx)
mx = v;
i++;
}
while (i < tl) {
tree[(i << 1)] = 0;
tree[(i << 1) + 1] = 0;
i++;
}
return mx;
}
function _bitsE(dt, pos, length) {
return ((dt[pos >>> 3] | (dt[(pos >>> 3) + 1] << 8)) >>> (pos & 7)) & ((1 << length) - 1);
}
function _bitsF(dt, pos, length) {
return ((dt[pos >>> 3] | (dt[(pos >>> 3) + 1] << 8) | (dt[(pos >>> 3) + 2] << 16)) >>> (pos & 7)) & ((1 << length) - 1);
}
function _get17(dt, pos) {
return (dt[pos >>> 3] | (dt[(pos >>> 3) + 1] << 8) | (dt[(pos >>> 3) + 2] << 16)) >>> (pos & 7);
}
//# sourceMappingURL=inflate.js.map
;