From b04e638c93767618823de5049cacb78a2200b59a Mon Sep 17 00:00:00 2001 From: codedread Date: Mon, 29 Jan 2018 22:06:54 -0800 Subject: [PATCH] Add logging as a parameter (used in unzip.js). Add a TODO for unrar.js to use a ByteStream for the file headers --- archive/archive.js | 7 +++++- archive/unrar.js | 6 ++++++ archive/untar.js | 2 ++ archive/unzip.js | 54 ++++++++++++++++++++++++++++------------------ 4 files changed, 47 insertions(+), 22 deletions(-) diff --git a/archive/archive.js b/archive/archive.js index 9de9a42..e5746de 100644 --- a/archive/archive.js +++ b/archive/archive.js @@ -270,7 +270,12 @@ bitjs.archive.Unarchiver = class { } }; - this.worker_.postMessage({file: this.ab}); + const ab = this.ab; + this.worker_.postMessage({ + file: ab, + logToConsole: false, + }); + this.ab = null; } } diff --git a/archive/unrar.js b/archive/unrar.js index bfe4c16..f510385 100644 --- a/archive/unrar.js +++ b/archive/unrar.js @@ -7,6 +7,10 @@ * Copyright(c) 2011 antimatter15 */ +// TODO: Rewrite the RarLocalHeader parsing to use a ByteStream instead +// of a BitStream so that it throws properly when not enough bytes are +// present. + // This file expects to be invoked as a Worker (see onmessage below). importScripts('../io/bitstream.js'); importScripts('../io/bytebuffer.js'); @@ -24,6 +28,7 @@ const UnarchiveState = { let unarchiveState = UnarchiveState.NOT_STARTED; let bitstream = null; let allLocalFiles = null; +let logToConsole = false; // Progress variables. let currentFilename = ""; @@ -1378,6 +1383,7 @@ function unrar() { // event.data.bytes has all subsequent ArrayBuffers. onmessage = function(event) { const bytes = event.data.file || event.data.bytes; + logToConsole = !!event.data.logToConsole; // This is the very first time we have been called. Initialize the bytestream. if (!bitstream) { diff --git a/archive/untar.js b/archive/untar.js index 523476a..99b3d60 100644 --- a/archive/untar.js +++ b/archive/untar.js @@ -25,6 +25,7 @@ const UnarchiveState = { let unarchiveState = UnarchiveState.NOT_STARTED; let bytestream = null; let allLocalFiles = null; +let logToConsole = false; // Progress variables. let currentFilename = ""; @@ -160,6 +161,7 @@ const untar = function() { // event.data.bytes has all subsequent ArrayBuffers. onmessage = function(event) { const bytes = event.data.file || event.data.bytes; + logToConsole = !!event.data.logToConsole; // This is the very first time we have been called. Initialize the bytestream. if (!bytestream) { diff --git a/archive/unzip.js b/archive/unzip.js index 927f17b..6c966a0 100644 --- a/archive/unzip.js +++ b/archive/unzip.js @@ -29,6 +29,7 @@ const UnarchiveState = { let unarchiveState = UnarchiveState.NOT_STARTED; let bytestream = null; let allLocalFiles = null; +let logToConsole = false; // Progress variables. let currentFilename = ""; @@ -118,31 +119,37 @@ class ZipLocalFile { } // Now that we have all the bytes for this file, we can print out some information. - info("Zip Local File Header:"); - info(" version=" + this.version); - info(" general purpose=" + this.generalPurpose); - info(" compression method=" + this.compressionMethod); - info(" last mod file time=" + this.lastModFileTime); - info(" last mod file date=" + this.lastModFileDate); - info(" crc32=" + this.crc32); - info(" compressed size=" + this.compressedSize); - info(" uncompressed size=" + this.uncompressedSize); - info(" file name length=" + this.fileNameLength); - info(" extra field length=" + this.extraFieldLength); - info(" filename = '" + this.filename + "'"); + if (logToConsole) { + info("Zip Local File Header:"); + info(" version=" + this.version); + info(" general purpose=" + this.generalPurpose); + info(" compression method=" + this.compressionMethod); + info(" last mod file time=" + this.lastModFileTime); + info(" last mod file date=" + this.lastModFileDate); + info(" crc32=" + this.crc32); + info(" compressed size=" + this.compressedSize); + info(" uncompressed size=" + this.uncompressedSize); + info(" file name length=" + this.fileNameLength); + info(" extra field length=" + this.extraFieldLength); + info(" filename = '" + this.filename + "'"); + } } // determine what kind of compressed data we have and decompress unzip() { // Zip Version 1.0, no compression (store only) if (this.compressionMethod == 0 ) { - info("ZIP v"+this.version+", store only: " + this.filename + " (" + this.compressedSize + " bytes)"); + if (logToConsole) { + info("ZIP v"+this.version+", store only: " + this.filename + " (" + this.compressedSize + " bytes)"); + } currentBytesUnarchivedInFile = this.compressedSize; currentBytesUnarchived += this.compressedSize; } // version == 20, compression method == 8 (DEFLATE) else if (this.compressionMethod == 8) { - info("ZIP v2.0, DEFLATE: " + this.filename + " (" + this.compressedSize + " bytes)"); + if (logToConsole) { + info("ZIP v2.0, DEFLATE: " + this.filename + " (" + this.compressedSize + " bytes)"); + } this.fileData = inflate(this.fileData, this.uncompressedSize); } else { @@ -415,7 +422,6 @@ function inflate(compressedData, numDecompressedBytes) { compressedData.byteOffset, compressedData.byteLength); const buffer = new bitjs.io.ByteBuffer(numDecompressedBytes); - let numBlocks = 0; let blockSize = 0; // block format: http://tools.ietf.org/html/rfc1951#page-9 @@ -424,7 +430,6 @@ function inflate(compressedData, numDecompressedBytes) { bFinal = bstream.readBits(1); let bType = bstream.readBits(2); blockSize = 0; - ++numBlocks; // no compression if (bType == 0) { // skip remaining bits in this byte @@ -436,11 +441,11 @@ function inflate(compressedData, numDecompressedBytes) { blockSize = len; } // fixed Huffman codes - else if(bType == 1) { + else if (bType == 1) { blockSize = inflateBlockData(bstream, getFixedLiteralTable(), getFixedDistanceTable(), buffer); } // dynamic Huffman codes - else if(bType == 2) { + else if (bType == 2) { const numLiteralLengthCodes = bstream.readBits(5) + 257; const numDistanceCodes = bstream.readBits(5) + 1; const numCodeLengthCodes = bstream.readBits(4) + 4; @@ -551,7 +556,9 @@ function unzip() { // archive extra data record if (bstream.peekNumber(4) == zArchiveExtraDataSignature) { - info(" Found an Archive Extra Data Signature"); + if (logToConsole) { + info(" Found an Archive Extra Data Signature"); + } // skipping this record for now bstream.readNumber(4); @@ -562,7 +569,9 @@ function unzip() { // central directory structure // TODO: handle the rest of the structures (Zip64 stuff) if (bytestream.peekNumber(4) == zCentralFileHeaderSignature) { - info(" Found a Central File Header"); + if (logToConsole) { + info(" Found a Central File Header"); + } // read all file headers while (bstream.peekNumber(4) == zCentralFileHeaderSignature) { @@ -592,7 +601,9 @@ function unzip() { // digital signature if (bstream.peekNumber(4) == zDigitalSignatureSignature) { - info(" Found a Digital Signature"); + if (logToConsole) { + info(" Found a Digital Signature"); + } bstream.readNumber(4); const sizeOfSignature = bstream.readNumber(2); @@ -608,6 +619,7 @@ function unzip() { // event.data.bytes has all subsequent ArrayBuffers. onmessage = function(event) { const bytes = event.data.file || event.data.bytes; + logToConsole = !!event.data.logToConsole; // This is the very first time we have been called. Initialize the bytestream. if (!bytestream) {