UNPKG

molstar

Version:

A comprehensive macromolecular library.

376 lines 15.5 kB
"use strict"; /** * 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 * * - added `ungzip` */ Object.defineProperty(exports, "__esModule", { value: true }); exports.zip = exports.Zip = exports.deflate = exports.ungzip = exports.inflate = exports.inflateRaw = exports.unzip = void 0; var tslib_1 = require("tslib"); var bin_1 = require("./bin"); var checksum_1 = require("./checksum"); var inflate_1 = require("./inflate"); var deflate_1 = require("./deflate"); var mol_task_1 = require("../../mol-task"); function unzip(runtime, buf, onlyNames) { if (onlyNames === void 0) { onlyNames = false; } return (0, tslib_1.__awaiter)(this, void 0, void 0, function () { var out, data, eocd, o, cnu, coffs, i, csize, usize, nl, el, cl, roff; return (0, tslib_1.__generator)(this, function (_a) { switch (_a.label) { case 0: out = Object.create(null); data = new Uint8Array(buf); eocd = data.length - 4; while ((0, bin_1.readUint)(data, eocd) !== 0x06054b50) eocd--; o = eocd; o += 4; // sign = 0x06054b50 o += 4; // disks = 0; cnu = (0, bin_1.readUshort)(data, o); o += 2; // const cnt = readUshort(data, o); o += 2; // const csize = readUint(data, o); o += 4; coffs = (0, bin_1.readUint)(data, o); o += 4; o = coffs; i = 0; _a.label = 1; case 1: if (!(i < cnu)) return [3 /*break*/, 4]; // const sign = readUint(data, o); o += 4; o += 4; // versions; o += 4; // flag + compr o += 4; // time // const crc32 = readUint(data, o); o += 4; csize = (0, bin_1.readUint)(data, o); o += 4; usize = (0, bin_1.readUint)(data, o); o += 4; nl = (0, bin_1.readUshort)(data, o); el = (0, bin_1.readUshort)(data, o + 2); cl = (0, bin_1.readUshort)(data, o + 4); o += 6; // name, extra, comment o += 8; // disk, attribs roff = (0, bin_1.readUint)(data, o); o += 4; o += nl + el + cl; return [4 /*yield*/, _readLocal(runtime, data, roff, out, csize, usize, onlyNames)]; case 2: _a.sent(); _a.label = 3; case 3: i++; return [3 /*break*/, 1]; case 4: // console.log(out); return [2 /*return*/, out]; } }); }); } exports.unzip = unzip; function _readLocal(runtime, data, o, out, csize, usize, onlyNames) { return (0, tslib_1.__awaiter)(this, void 0, void 0, function () { var cmpr, nlen, elen, name, file, buf; return (0, tslib_1.__generator)(this, function (_a) { switch (_a.label) { case 0: // const sign = readUint(data, o); o += 4; // const ver = readUshort(data, o); o += 2; // const gpflg = readUshort(data, o); o += 2; cmpr = (0, bin_1.readUshort)(data, o); o += 2; // const time = readUint(data, o); o += 4; // const crc32 = readUint(data, o); o += 4; // var csize = rUi(data, o); o+=4; // var usize = rUi(data, o); o+=4; o += 8; nlen = (0, bin_1.readUshort)(data, o); o += 2; elen = (0, bin_1.readUshort)(data, o); o += 2; name = (0, bin_1.readUTF8)(data, o, nlen); o += nlen; // console.log(name); o += elen; if (onlyNames) { out[name] = { size: usize, csize: csize }; return [2 /*return*/]; } file = new Uint8Array(data.buffer, o); if (!(cmpr === 0)) return [3 /*break*/, 1]; out[name] = new Uint8Array(file.buffer.slice(o, o + csize)); return [3 /*break*/, 4]; case 1: if (!(cmpr === 8)) return [3 /*break*/, 3]; buf = new Uint8Array(usize); return [4 /*yield*/, inflateRaw(runtime, file, buf)]; case 2: _a.sent(); out[name] = buf; return [3 /*break*/, 4]; case 3: throw new Error("unknown compression method: " + cmpr); case 4: return [2 /*return*/]; } }); }); } function inflateRaw(runtime, file, buf) { return (0, tslib_1.__awaiter)(this, void 0, void 0, function () { return (0, tslib_1.__generator)(this, function (_a) { return [2 /*return*/, (0, inflate_1._inflate)(runtime, file, buf)]; }); }); } exports.inflateRaw = inflateRaw; function inflate(runtime, file, buf) { // const CMF = file[0] // const FLG = file[1] // const CM = (CMF&15) // const CINFO = (CMF>>>4); // console.log(CM, CINFO,CMF,FLG); return inflateRaw(runtime, new Uint8Array(file.buffer, file.byteOffset + 2, file.length - 6), buf); } exports.inflate = inflate; // https://tools.ietf.org/html/rfc1952 function ungzip(runtime, file, buf) { return (0, tslib_1.__awaiter)(this, void 0, void 0, function () { var flg, o, xlen, zero, zero, crc32, isize, blocks, inflated, crcValue; return (0, tslib_1.__generator)(this, function (_a) { switch (_a.label) { case 0: flg = file[3]; o = 10; if (flg & 4) { // FEXTRA xlen = (0, bin_1.readUshort)(file, o); // console.log('FEXTRA', xlen) o += xlen; } if (flg & 8) { // FNAME zero = o; while (file[zero] !== 0) ++zero; // const name = readUTF8(file, o, zero - o) // console.log('FNAME', name, zero - o) o = zero + 1; } if (flg & 16) { // FCOMMENT zero = o; while (file[zero] !== 0) ++zero; // const comment = readUTF8(file, o, zero - o) // console.log('FCOMMENT', comment) o = zero + 1; } if (flg & 1) { // FHCRC // const hcrc = readUshort(file, o) // console.log('FHCRC', hcrc) o += 2; } crc32 = (0, bin_1.toInt32)((0, bin_1.readUint)(file, file.length - 8)); isize = (0, bin_1.readUint)(file, file.length - 4); if (buf === undefined) buf = new Uint8Array(isize); blocks = new Uint8Array(file.buffer, file.byteOffset + o, file.length - o - 8); return [4 /*yield*/, inflateRaw(runtime, blocks, buf)]; case 1: inflated = _a.sent(); crcValue = (0, checksum_1.crc)(inflated, 0, inflated.length); if (crc32 !== crcValue) { console.error("ungzip: checksums don't match"); } return [2 /*return*/, inflated]; } }); }); } exports.ungzip = ungzip; function deflate(runtime, data, opts /* , buf, off*/) { return (0, tslib_1.__awaiter)(this, void 0, void 0, function () { var off, buf, crcValue; return (0, tslib_1.__generator)(this, function (_a) { switch (_a.label) { case 0: if (opts === undefined) opts = { level: 6 }; off = 0; buf = new Uint8Array(50 + Math.floor(data.length * 1.1)); buf[off] = 120; buf[off + 1] = 156; off += 2; return [4 /*yield*/, (0, deflate_1._deflateRaw)(runtime, data, buf, off, opts.level)]; case 1: off = _a.sent(); crcValue = (0, checksum_1.adler)(data, 0, data.length); buf[off + 0] = ((crcValue >>> 24) & 255); buf[off + 1] = ((crcValue >>> 16) & 255); buf[off + 2] = ((crcValue >>> 8) & 255); buf[off + 3] = ((crcValue >>> 0) & 255); return [2 /*return*/, new Uint8Array(buf.buffer, 0, off + 4)]; } }); }); } exports.deflate = deflate; function deflateRaw(runtime, data, opts) { return (0, tslib_1.__awaiter)(this, void 0, void 0, function () { var buf, off; return (0, tslib_1.__generator)(this, function (_a) { switch (_a.label) { case 0: if (opts === undefined) opts = { level: 6 }; buf = new Uint8Array(50 + Math.floor(data.length * 1.1)); return [4 /*yield*/, (0, deflate_1._deflateRaw)(runtime, data, buf, 0, opts.level)]; case 1: off = _a.sent(); return [2 /*return*/, new Uint8Array(buf.buffer, 0, off)]; } }); }); } function Zip(obj, noCmpr) { if (noCmpr === void 0) { noCmpr = false; } return mol_task_1.Task.create('Zip', function (ctx) { return zip(ctx, obj, noCmpr); }); } exports.Zip = Zip; function zip(runtime, obj, noCmpr) { if (noCmpr === void 0) { noCmpr = false; } return (0, tslib_1.__awaiter)(this, void 0, void 0, function () { var tot, zpd, _a, _b, _i, p, cpr, buf, crcValue, _c, _d, _e, p, data, o, fof, p, file, i, ioff, p, file, csize; var _f; return (0, tslib_1.__generator)(this, function (_g) { switch (_g.label) { case 0: tot = 0; zpd = {}; _a = []; for (_b in obj) _a.push(_b); _i = 0; _g.label = 1; case 1: if (!(_i < _a.length)) return [3 /*break*/, 6]; p = _a[_i]; cpr = !_noNeed(p) && !noCmpr, buf = obj[p]; crcValue = (0, checksum_1.crc)(buf, 0, buf.length); _c = zpd; _d = p; _f = { cpr: cpr, usize: buf.length, crc: crcValue }; if (!cpr) return [3 /*break*/, 3]; return [4 /*yield*/, deflateRaw(runtime, buf)]; case 2: _e = _g.sent(); return [3 /*break*/, 4]; case 3: _e = buf; _g.label = 4; case 4: _c[_d] = (_f.file = (_e), _f); _g.label = 5; case 5: _i++; return [3 /*break*/, 1]; case 6: for (p in zpd) tot += zpd[p].file.length + 30 + 46 + 2 * (0, bin_1.sizeUTF8)(p); tot += 22; data = new Uint8Array(tot); o = 0; fof = []; for (p in zpd) { file = zpd[p]; fof.push(o); o = _writeHeader(data, o, p, file, 0); } i = 0; ioff = o; for (p in zpd) { file = zpd[p]; fof.push(o); o = _writeHeader(data, o, p, file, 1, fof[i++]); } csize = o - ioff; (0, bin_1.writeUint)(data, o, 0x06054b50); o += 4; o += 4; // disks (0, bin_1.writeUshort)(data, o, i); o += 2; (0, bin_1.writeUshort)(data, o, i); o += 2; // number of c d records (0, bin_1.writeUint)(data, o, csize); o += 4; (0, bin_1.writeUint)(data, o, ioff); o += 4; o += 2; return [2 /*return*/, data.buffer]; } }); }); } exports.zip = zip; // no need to compress .PNG, .ZIP, .JPEG .... function _noNeed(fn) { var ext = fn.split('.').pop().toLowerCase(); return 'png,jpg,jpeg,zip'.indexOf(ext) !== -1; } function _writeHeader(data, o, p, obj, t, roff) { if (roff === void 0) { roff = 0; } var file = obj.file; (0, bin_1.writeUint)(data, o, t === 0 ? 0x04034b50 : 0x02014b50); o += 4; // sign if (t === 1) o += 2; // ver made by (0, bin_1.writeUshort)(data, o, 20); o += 2; // ver (0, bin_1.writeUshort)(data, o, 0); o += 2; // gflip (0, bin_1.writeUshort)(data, o, obj.cpr ? 8 : 0); o += 2; // cmpr (0, bin_1.writeUint)(data, o, 0); o += 4; // time (0, bin_1.writeUint)(data, o, obj.crc); o += 4; // crc32 (0, bin_1.writeUint)(data, o, file.length); o += 4; // csize (0, bin_1.writeUint)(data, o, obj.usize); o += 4; // usize (0, bin_1.writeUshort)(data, o, (0, bin_1.sizeUTF8)(p)); o += 2; // nlen (0, bin_1.writeUshort)(data, o, 0); o += 2; // elen if (t === 1) { o += 2; // comment length o += 2; // disk number o += 6; // attributes (0, bin_1.writeUint)(data, o, roff); o += 4; // usize } var nlen = (0, bin_1.writeUTF8)(data, o, p); o += nlen; if (t === 0) { data.set(file, o); o += file.length; } return o; } //# sourceMappingURL=zip.js.map