1
0
Fork 0
mirror of https://github.com/codedread/bitjs synced 2025-10-03 17:49:16 +02:00

Add logging as a parameter (used in unzip.js). Add a TODO for unrar.js to use a ByteStream for the file headers

This commit is contained in:
codedread 2018-01-29 22:06:54 -08:00
parent 950dc65b56
commit b04e638c93
4 changed files with 47 additions and 22 deletions

View file

@ -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;
} }
} }

View file

@ -7,6 +7,10 @@
* Copyright(c) 2011 antimatter15 * 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). // This file expects to be invoked as a Worker (see onmessage below).
importScripts('../io/bitstream.js'); importScripts('../io/bitstream.js');
importScripts('../io/bytebuffer.js'); importScripts('../io/bytebuffer.js');
@ -24,6 +28,7 @@ const UnarchiveState = {
let unarchiveState = UnarchiveState.NOT_STARTED; let unarchiveState = UnarchiveState.NOT_STARTED;
let bitstream = null; let bitstream = null;
let allLocalFiles = null; let allLocalFiles = null;
let logToConsole = false;
// Progress variables. // Progress variables.
let currentFilename = ""; let currentFilename = "";
@ -1378,6 +1383,7 @@ function unrar() {
// event.data.bytes has all subsequent ArrayBuffers. // event.data.bytes has all subsequent ArrayBuffers.
onmessage = function(event) { onmessage = function(event) {
const bytes = event.data.file || event.data.bytes; 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. // This is the very first time we have been called. Initialize the bytestream.
if (!bitstream) { if (!bitstream) {

View file

@ -25,6 +25,7 @@ const UnarchiveState = {
let unarchiveState = UnarchiveState.NOT_STARTED; let unarchiveState = UnarchiveState.NOT_STARTED;
let bytestream = null; let bytestream = null;
let allLocalFiles = null; let allLocalFiles = null;
let logToConsole = false;
// Progress variables. // Progress variables.
let currentFilename = ""; let currentFilename = "";
@ -160,6 +161,7 @@ const untar = function() {
// event.data.bytes has all subsequent ArrayBuffers. // event.data.bytes has all subsequent ArrayBuffers.
onmessage = function(event) { onmessage = function(event) {
const bytes = event.data.file || event.data.bytes; 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. // This is the very first time we have been called. Initialize the bytestream.
if (!bytestream) { if (!bytestream) {

View file

@ -29,6 +29,7 @@ const UnarchiveState = {
let unarchiveState = UnarchiveState.NOT_STARTED; let unarchiveState = UnarchiveState.NOT_STARTED;
let bytestream = null; let bytestream = null;
let allLocalFiles = null; let allLocalFiles = null;
let logToConsole = false;
// Progress variables. // Progress variables.
let currentFilename = ""; let currentFilename = "";
@ -118,6 +119,7 @@ class ZipLocalFile {
} }
// Now that we have all the bytes for this file, we can print out some information. // Now that we have all the bytes for this file, we can print out some information.
if (logToConsole) {
info("Zip Local File Header:"); info("Zip Local File Header:");
info(" version=" + this.version); info(" version=" + this.version);
info(" general purpose=" + this.generalPurpose); info(" general purpose=" + this.generalPurpose);
@ -131,18 +133,23 @@ class ZipLocalFile {
info(" extra field length=" + this.extraFieldLength); info(" extra field length=" + this.extraFieldLength);
info(" filename = '" + this.filename + "'"); info(" filename = '" + this.filename + "'");
} }
}
// determine what kind of compressed data we have and decompress // determine what kind of compressed data we have and decompress
unzip() { unzip() {
// Zip Version 1.0, no compression (store only) // Zip Version 1.0, no compression (store only)
if (this.compressionMethod == 0 ) { if (this.compressionMethod == 0 ) {
if (logToConsole) {
info("ZIP v"+this.version+", store only: " + this.filename + " (" + this.compressedSize + " bytes)"); info("ZIP v"+this.version+", store only: " + this.filename + " (" + this.compressedSize + " bytes)");
}
currentBytesUnarchivedInFile = this.compressedSize; currentBytesUnarchivedInFile = this.compressedSize;
currentBytesUnarchived += this.compressedSize; currentBytesUnarchived += this.compressedSize;
} }
// version == 20, compression method == 8 (DEFLATE) // version == 20, compression method == 8 (DEFLATE)
else if (this.compressionMethod == 8) { else if (this.compressionMethod == 8) {
if (logToConsole) {
info("ZIP v2.0, DEFLATE: " + this.filename + " (" + this.compressedSize + " bytes)"); info("ZIP v2.0, DEFLATE: " + this.filename + " (" + this.compressedSize + " bytes)");
}
this.fileData = inflate(this.fileData, this.uncompressedSize); this.fileData = inflate(this.fileData, this.uncompressedSize);
} }
else { else {
@ -415,7 +422,6 @@ function inflate(compressedData, numDecompressedBytes) {
compressedData.byteOffset, compressedData.byteOffset,
compressedData.byteLength); compressedData.byteLength);
const buffer = new bitjs.io.ByteBuffer(numDecompressedBytes); const buffer = new bitjs.io.ByteBuffer(numDecompressedBytes);
let numBlocks = 0;
let blockSize = 0; let blockSize = 0;
// block format: http://tools.ietf.org/html/rfc1951#page-9 // block format: http://tools.ietf.org/html/rfc1951#page-9
@ -424,7 +430,6 @@ function inflate(compressedData, numDecompressedBytes) {
bFinal = bstream.readBits(1); bFinal = bstream.readBits(1);
let bType = bstream.readBits(2); let bType = bstream.readBits(2);
blockSize = 0; blockSize = 0;
++numBlocks;
// no compression // no compression
if (bType == 0) { if (bType == 0) {
// skip remaining bits in this byte // skip remaining bits in this byte
@ -551,7 +556,9 @@ function unzip() {
// archive extra data record // archive extra data record
if (bstream.peekNumber(4) == zArchiveExtraDataSignature) { if (bstream.peekNumber(4) == zArchiveExtraDataSignature) {
if (logToConsole) {
info(" Found an Archive Extra Data Signature"); info(" Found an Archive Extra Data Signature");
}
// skipping this record for now // skipping this record for now
bstream.readNumber(4); bstream.readNumber(4);
@ -562,7 +569,9 @@ function unzip() {
// central directory structure // central directory structure
// TODO: handle the rest of the structures (Zip64 stuff) // TODO: handle the rest of the structures (Zip64 stuff)
if (bytestream.peekNumber(4) == zCentralFileHeaderSignature) { if (bytestream.peekNumber(4) == zCentralFileHeaderSignature) {
if (logToConsole) {
info(" Found a Central File Header"); info(" Found a Central File Header");
}
// read all file headers // read all file headers
while (bstream.peekNumber(4) == zCentralFileHeaderSignature) { while (bstream.peekNumber(4) == zCentralFileHeaderSignature) {
@ -592,7 +601,9 @@ function unzip() {
// digital signature // digital signature
if (bstream.peekNumber(4) == zDigitalSignatureSignature) { if (bstream.peekNumber(4) == zDigitalSignatureSignature) {
if (logToConsole) {
info(" Found a Digital Signature"); info(" Found a Digital Signature");
}
bstream.readNumber(4); bstream.readNumber(4);
const sizeOfSignature = bstream.readNumber(2); const sizeOfSignature = bstream.readNumber(2);
@ -608,6 +619,7 @@ function unzip() {
// event.data.bytes has all subsequent ArrayBuffers. // event.data.bytes has all subsequent ArrayBuffers.
onmessage = function(event) { onmessage = function(event) {
const bytes = event.data.file || event.data.bytes; 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. // This is the very first time we have been called. Initialize the bytestream.
if (!bytestream) { if (!bytestream) {