UNPKG

node-apk

Version:

A library to parse Android application manifest and signature

98 lines (96 loc) 4.34 kB
"use strict"; /*Copyright (c) 2019 XdevL. All rights reserved. This work is licensed under the terms of the MIT license. For a copy, see <https://opensource.org/licenses/MIT>.*/ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ZipEntry = void 0; var zlib_1 = __importDefault(require("zlib")); var source_1 = __importDefault(require("./source")); var Signature; (function (Signature) { Signature[Signature["CENTRAL_HEADER"] = 33639248] = "CENTRAL_HEADER"; Signature[Signature["LOCAL_HEADER"] = 67324752] = "LOCAL_HEADER"; })(Signature || (Signature = {})); var Algorithm; (function (Algorithm) { Algorithm[Algorithm["DEFLATE"] = 8] = "DEFLATE"; Algorithm[Algorithm["NONE"] = 0] = "NONE"; })(Algorithm || (Algorithm = {})); var ZipEntry = /** @class */ (function () { function ZipEntry(buffer, source, signature) { if (signature === void 0) { signature = Signature.CENTRAL_HEADER; } this.buffer = buffer; this.signature = source.readUInt(); if (this.signature !== signature) { throw Error("Invalid file header signature found: 0x".concat(this.signature.toString(16))); } this.versionMadeBy = this.signature === Signature.CENTRAL_HEADER ? source.readUShort() : 0; this.extractVersion = source.readUShort(); this.flags = source.readUShort(); this.compressionMethod = source.readUShort(); this.time = source.readUShort(); this.date = source.readUShort(); this.crc32 = source.readUInt(); this.compressedSize = source.readUInt(); this.unCompressedSize = source.readUInt(); this.nameLength = source.readUShort(); this.extraLength = source.readUShort(); this.commentLength = this.signature === Signature.CENTRAL_HEADER ? source.readUShort() : 0; this.diskNumber = this.signature === Signature.CENTRAL_HEADER ? source.readUShort() : 0; this.internalAttributes = this.signature === Signature.CENTRAL_HEADER ? source.readUShort() : 0; this.externalAttributes = this.signature === Signature.CENTRAL_HEADER ? source.readUInt() : 0; this.offset = this.signature === Signature.CENTRAL_HEADER ? source.readUInt() : 0; this.name = source.readUtf8String(this.nameLength); source.getCursorAndMove(this.extraLength + this.commentLength); } ZipEntry.lookup = function (loader, key) { return ZipEntry.index(loader).then(function (map) { var entry = map.get(key); if (entry) { return entry; } else { throw Error("Entry not found: " + key); } }); }; ZipEntry.index = function (loader) { return loader().then(function (buffer) { var index = buffer.lastIndexOf("504B0506", undefined, "hex"); if (index < 0) { throw Error("End of central directory not found"); } var source = new source_1.default(buffer.slice(index + 10)); var count = source.readUShort(); var size = source.readUInt(); var offset = source.readUInt(); var directory = new source_1.default(buffer.slice(offset, offset + size)); var map = new Map(); for (var i = 0; i < count; ++i) { var entry = new ZipEntry(buffer, directory); map.set(entry.name, entry); } return map; }); }; ZipEntry.prototype.stream = function () { var source = new source_1.default(this.buffer); source.moveAt(this.offset); var local = new ZipEntry(this.buffer, source, Signature.LOCAL_HEADER); var stream = source.stream(this.compressedSize); if (local.compressionMethod === Algorithm.DEFLATE) { return stream.pipe(zlib_1.default.createInflateRaw()); } else if (local.compressionMethod === Algorithm.NONE) { return stream; } else { throw Error("Unsupported compression method: 0x".concat(this.compressionMethod.toString(16))); } }; return ZipEntry; }()); exports.ZipEntry = ZipEntry;