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:
parent
950dc65b56
commit
b04e638c93
4 changed files with 47 additions and 22 deletions
|
@ -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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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) {
|
||||||
|
|
|
@ -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) {
|
||||||
|
|
|
@ -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,31 +119,37 @@ 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.
|
||||||
info("Zip Local File Header:");
|
if (logToConsole) {
|
||||||
info(" version=" + this.version);
|
info("Zip Local File Header:");
|
||||||
info(" general purpose=" + this.generalPurpose);
|
info(" version=" + this.version);
|
||||||
info(" compression method=" + this.compressionMethod);
|
info(" general purpose=" + this.generalPurpose);
|
||||||
info(" last mod file time=" + this.lastModFileTime);
|
info(" compression method=" + this.compressionMethod);
|
||||||
info(" last mod file date=" + this.lastModFileDate);
|
info(" last mod file time=" + this.lastModFileTime);
|
||||||
info(" crc32=" + this.crc32);
|
info(" last mod file date=" + this.lastModFileDate);
|
||||||
info(" compressed size=" + this.compressedSize);
|
info(" crc32=" + this.crc32);
|
||||||
info(" uncompressed size=" + this.uncompressedSize);
|
info(" compressed size=" + this.compressedSize);
|
||||||
info(" file name length=" + this.fileNameLength);
|
info(" uncompressed size=" + this.uncompressedSize);
|
||||||
info(" extra field length=" + this.extraFieldLength);
|
info(" file name length=" + this.fileNameLength);
|
||||||
info(" filename = '" + this.filename + "'");
|
info(" extra field length=" + this.extraFieldLength);
|
||||||
|
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 ) {
|
||||||
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;
|
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) {
|
||||||
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);
|
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
|
||||||
|
@ -436,11 +441,11 @@ function inflate(compressedData, numDecompressedBytes) {
|
||||||
blockSize = len;
|
blockSize = len;
|
||||||
}
|
}
|
||||||
// fixed Huffman codes
|
// fixed Huffman codes
|
||||||
else if(bType == 1) {
|
else if (bType == 1) {
|
||||||
blockSize = inflateBlockData(bstream, getFixedLiteralTable(), getFixedDistanceTable(), buffer);
|
blockSize = inflateBlockData(bstream, getFixedLiteralTable(), getFixedDistanceTable(), buffer);
|
||||||
}
|
}
|
||||||
// dynamic Huffman codes
|
// dynamic Huffman codes
|
||||||
else if(bType == 2) {
|
else if (bType == 2) {
|
||||||
const numLiteralLengthCodes = bstream.readBits(5) + 257;
|
const numLiteralLengthCodes = bstream.readBits(5) + 257;
|
||||||
const numDistanceCodes = bstream.readBits(5) + 1;
|
const numDistanceCodes = bstream.readBits(5) + 1;
|
||||||
const numCodeLengthCodes = bstream.readBits(4) + 4;
|
const numCodeLengthCodes = bstream.readBits(4) + 4;
|
||||||
|
@ -551,7 +556,9 @@ function unzip() {
|
||||||
|
|
||||||
// archive extra data record
|
// archive extra data record
|
||||||
if (bstream.peekNumber(4) == zArchiveExtraDataSignature) {
|
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
|
// 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) {
|
||||||
info(" Found a Central File Header");
|
if (logToConsole) {
|
||||||
|
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) {
|
||||||
info(" Found a Digital Signature");
|
if (logToConsole) {
|
||||||
|
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) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue