Code cosmetics

This commit is contained in:
OzzieIsaacs 2017-09-17 14:51:17 +02:00
parent 85068d75bf
commit 5b9b36f997
3 changed files with 1217 additions and 1231 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -22,147 +22,147 @@ var totalFilesInArchive = 0;
// Helper functions. // Helper functions.
var info = function(str) { var info = function(str) {
postMessage(new bitjs.archive.UnarchiveInfoEvent(str)); postMessage(new bitjs.archive.UnarchiveInfoEvent(str));
}; };
var err = function(str) { var err = function(str) {
postMessage(new bitjs.archive.UnarchiveErrorEvent(str)); postMessage(new bitjs.archive.UnarchiveErrorEvent(str));
}; };
var postProgress = function() { var postProgress = function() {
postMessage(new bitjs.archive.UnarchiveProgressEvent( postMessage(new bitjs.archive.UnarchiveProgressEvent(
currentFilename, currentFilename,
currentFileNumber, currentFileNumber,
currentBytesUnarchivedInFile, currentBytesUnarchivedInFile,
currentBytesUnarchived, currentBytesUnarchived,
totalUncompressedBytesInArchive, totalUncompressedBytesInArchive,
totalFilesInArchive)); totalFilesInArchive));
}; };
// Removes all characters from the first zero-byte in the string onwards. // Removes all characters from the first zero-byte in the string onwards.
var readCleanString = function(bstr, numBytes) { var readCleanString = function(bstr, numBytes) {
var str = bstr.readString(numBytes); var str = bstr.readString(numBytes);
var zIndex = str.indexOf(String.fromCharCode(0)); var zIndex = str.indexOf(String.fromCharCode(0));
return zIndex != -1 ? str.substr(0, zIndex) : str; return zIndex != -1 ? str.substr(0, zIndex) : str;
}; };
// takes a ByteStream and parses out the local file information // takes a ByteStream and parses out the local file information
var TarLocalFile = function(bstream) { var TarLocalFile = function(bstream) {
this.isValid = false; this.isValid = false;
// Read in the header block // Read in the header block
this.name = readCleanString(bstream, 100); this.name = readCleanString(bstream, 100);
this.mode = readCleanString(bstream, 8); this.mode = readCleanString(bstream, 8);
this.uid = readCleanString(bstream, 8); this.uid = readCleanString(bstream, 8);
this.gid = readCleanString(bstream, 8); this.gid = readCleanString(bstream, 8);
this.size = parseInt(readCleanString(bstream, 12), 8); this.size = parseInt(readCleanString(bstream, 12), 8);
this.mtime = readCleanString(bstream, 12); this.mtime = readCleanString(bstream, 12);
this.chksum = readCleanString(bstream, 8); this.chksum = readCleanString(bstream, 8);
this.typeflag = readCleanString(bstream, 1); this.typeflag = readCleanString(bstream, 1);
this.linkname = readCleanString(bstream, 100); this.linkname = readCleanString(bstream, 100);
this.maybeMagic = readCleanString(bstream, 6); this.maybeMagic = readCleanString(bstream, 6);
if (this.maybeMagic == "ustar") { if (this.maybeMagic == "ustar") {
this.version = readCleanString(bstream, 2); this.version = readCleanString(bstream, 2);
this.uname = readCleanString(bstream, 32); this.uname = readCleanString(bstream, 32);
this.gname = readCleanString(bstream, 32); this.gname = readCleanString(bstream, 32);
this.devmajor = readCleanString(bstream, 8); this.devmajor = readCleanString(bstream, 8);
this.devminor = readCleanString(bstream, 8); this.devminor = readCleanString(bstream, 8);
this.prefix = readCleanString(bstream, 155); this.prefix = readCleanString(bstream, 155);
if (this.prefix.length) { if (this.prefix.length) {
this.name = this.prefix + this.name; this.name = this.prefix + this.name;
} }
bstream.readBytes(12); // 512 - 500 bstream.readBytes(12); // 512 - 500
} else { } else {
bstream.readBytes(255); // 512 - 257 bstream.readBytes(255); // 512 - 257
} }
// Done header, now rest of blocks are the file contents. // Done header, now rest of blocks are the file contents.
this.filename = this.name; this.filename = this.name;
this.fileData = null; this.fileData = null;
info("Untarring file '" + this.filename + "'"); info("Untarring file '" + this.filename + "'");
info(" size = " + this.size); info(" size = " + this.size);
info(" typeflag = " + this.typeflag); info(" typeflag = " + this.typeflag);
// A regular file. // A regular file.
if (this.typeflag == 0) { if (this.typeflag == 0) {
info(" This is a regular file."); info(" This is a regular file.");
var sizeInBytes = parseInt(this.size); var sizeInBytes = parseInt(this.size);
this.fileData = new Uint8Array(bstream.bytes.buffer, bstream.ptr, this.size); this.fileData = new Uint8Array(bstream.bytes.buffer, bstream.ptr, this.size);
if (this.name.length > 0 && this.size > 0 && this.fileData && this.fileData.buffer) { if (this.name.length > 0 && this.size > 0 && this.fileData && this.fileData.buffer) {
this.isValid = true; this.isValid = true;
} }
bstream.readBytes(this.size); bstream.readBytes(this.size);
// Round up to 512-byte blocks. // Round up to 512-byte blocks.
var remaining = 512 - this.size % 512; var remaining = 512 - this.size % 512;
if (remaining > 0 && remaining < 512) { if (remaining > 0 && remaining < 512) {
bstream.readBytes(remaining); bstream.readBytes(remaining);
} }
} else if (this.typeflag == 5) { } else if (this.typeflag == 5) {
info(" This is a directory.") info(" This is a directory.")
} }
}; };
// Takes an ArrayBuffer of a tar file in // Takes an ArrayBuffer of a tar file in
// returns null on error // returns null on error
// returns an array of DecompressedFile objects on success // returns an array of DecompressedFile objects on success
var untar = function(arrayBuffer) { var untar = function(arrayBuffer) {
currentFilename = ""; currentFilename = "";
currentFileNumber = 0; currentFileNumber = 0;
currentBytesUnarchivedInFile = 0; currentBytesUnarchivedInFile = 0;
currentBytesUnarchived = 0; currentBytesUnarchived = 0;
totalUncompressedBytesInArchive = 0; totalUncompressedBytesInArchive = 0;
totalFilesInArchive = 0; totalFilesInArchive = 0;
postMessage(new bitjs.archive.UnarchiveStartEvent()); postMessage(new bitjs.archive.UnarchiveStartEvent());
var bstream = new bitjs.io.ByteStream(arrayBuffer); var bstream = new bitjs.io.ByteStream(arrayBuffer);
var localFiles = []; var localFiles = [];
// While we don't encounter an empty block, keep making TarLocalFiles. // While we don't encounter an empty block, keep making TarLocalFiles.
while (bstream.peekNumber(4) != 0) { while (bstream.peekNumber(4) != 0) {
var oneLocalFile = new TarLocalFile(bstream); var oneLocalFile = new TarLocalFile(bstream);
if (oneLocalFile && oneLocalFile.isValid) { if (oneLocalFile && oneLocalFile.isValid) {
localFiles.push(oneLocalFile); localFiles.push(oneLocalFile);
totalUncompressedBytesInArchive += oneLocalFile.size; totalUncompressedBytesInArchive += oneLocalFile.size;
} }
} }
totalFilesInArchive = localFiles.length; totalFilesInArchive = localFiles.length;
// got all local files, now sort them // got all local files, now sort them
localFiles.sort(function(a,b) { localFiles.sort(function(a,b) {
var aname = a.filename.toLowerCase(); var aname = a.filename.toLowerCase();
var bname = b.filename.toLowerCase(); var bname = b.filename.toLowerCase();
return aname > bname ? 1 : -1; return aname > bname ? 1 : -1;
}); });
// report # files and total length
if (localFiles.length > 0) {
postProgress();
}
// now do the shipping of each file
for (var i = 0; i < localFiles.length; ++i) {
var localfile = localFiles[i];
info("Sending file '" + localfile.filename + "' up");
// update progress
currentFilename = localfile.filename;
currentFileNumber = i;
currentBytesUnarchivedInFile = localfile.size;
currentBytesUnarchived += localfile.size;
postMessage(new bitjs.archive.UnarchiveExtractEvent(localfile));
postProgress();
}
// report # files and total length
if (localFiles.length > 0) {
postProgress(); postProgress();
}
// now do the shipping of each file postMessage(new bitjs.archive.UnarchiveFinishEvent());
for (var i = 0; i < localFiles.length; ++i) {
var localfile = localFiles[i];
info("Sending file '" + localfile.filename + "' up");
// update progress
currentFilename = localfile.filename;
currentFileNumber = i;
currentBytesUnarchivedInFile = localfile.size;
currentBytesUnarchived += localfile.size;
postMessage(new bitjs.archive.UnarchiveExtractEvent(localfile));
postProgress();
}
postProgress();
postMessage(new bitjs.archive.UnarchiveFinishEvent());
}; };
// event.data.file has the ArrayBuffer. // event.data.file has the ArrayBuffer.
onmessage = function(event) { onmessage = function(event) {
var ab = event.data.file; var ab = event.data.file;
untar(ab); untar(ab);
}; };