2016-11-28 10:42:58 +00:00
|
|
|
/* globals Zlib, bzip2 */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compression operations.
|
|
|
|
*
|
|
|
|
* @author n1474335 [n1474335@gmail.com]
|
|
|
|
* @copyright Crown Copyright 2016
|
|
|
|
* @license Apache-2.0
|
|
|
|
*
|
|
|
|
* @namespace
|
|
|
|
*/
|
|
|
|
var Compress = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
COMPRESSION_TYPE: ["Dynamic Huffman Coding", "Fixed Huffman Coding", "None (Store)"],
|
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
INFLATE_BUFFER_TYPE: ["Adaptive", "Block"],
|
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
COMPRESSION_METHOD: ["Deflate", "None (Store)"],
|
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
OS: ["MSDOS", "Unix", "Macintosh"],
|
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
RAW_COMPRESSION_TYPE_LOOKUP: {
|
|
|
|
"Fixed Huffman Coding" : Zlib.RawDeflate.CompressionType.FIXED,
|
|
|
|
"Dynamic Huffman Coding" : Zlib.RawDeflate.CompressionType.DYNAMIC,
|
|
|
|
"None (Store)" : Zlib.RawDeflate.CompressionType.NONE,
|
|
|
|
},
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* Raw Deflate operation.
|
|
|
|
*
|
2017-01-31 18:24:56 +00:00
|
|
|
* @param {byteArray} input
|
2016-11-28 10:42:58 +00:00
|
|
|
* @param {Object[]} args
|
2017-01-31 18:24:56 +00:00
|
|
|
* @returns {byteArray}
|
2016-11-28 10:42:58 +00:00
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runRawDeflate: function(input, args) {
|
2016-11-28 10:42:58 +00:00
|
|
|
var deflate = new Zlib.RawDeflate(input, {
|
|
|
|
compressionType: Compress.RAW_COMPRESSION_TYPE_LOOKUP[args[0]]
|
|
|
|
});
|
|
|
|
return Array.prototype.slice.call(deflate.compress());
|
|
|
|
},
|
2017-02-09 15:09:33 +00:00
|
|
|
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
INFLATE_INDEX: 0,
|
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
INFLATE_BUFFER_SIZE: 0,
|
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
INFLATE_RESIZE: false,
|
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
INFLATE_VERIFY: false,
|
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
RAW_BUFFER_TYPE_LOOKUP: {
|
2017-01-31 18:24:56 +00:00
|
|
|
"Adaptive" : Zlib.RawInflate.BufferType.ADAPTIVE,
|
|
|
|
"Block" : Zlib.RawInflate.BufferType.BLOCK,
|
2016-11-28 10:42:58 +00:00
|
|
|
},
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* Raw Inflate operation.
|
|
|
|
*
|
2017-01-31 18:24:56 +00:00
|
|
|
* @param {byteArray} input
|
2016-11-28 10:42:58 +00:00
|
|
|
* @param {Object[]} args
|
2017-01-31 18:24:56 +00:00
|
|
|
* @returns {byteArray}
|
2016-11-28 10:42:58 +00:00
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runRawInflate: function(input, args) {
|
2016-11-28 10:42:58 +00:00
|
|
|
// Deal with character encoding issues
|
2017-01-31 18:24:56 +00:00
|
|
|
input = Utils.strToByteArray(Utils.byteArrayToUtf8(input));
|
2016-11-28 10:42:58 +00:00
|
|
|
var inflate = new Zlib.RawInflate(input, {
|
|
|
|
index: args[0],
|
|
|
|
bufferSize: args[1],
|
|
|
|
bufferType: Compress.RAW_BUFFER_TYPE_LOOKUP[args[2]],
|
|
|
|
resize: args[3],
|
|
|
|
verify: args[4]
|
|
|
|
}),
|
|
|
|
result = Array.prototype.slice.call(inflate.decompress());
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
// Raw Inflate somethimes messes up and returns nonsense like this:
|
|
|
|
// ]....]....]....]....]....]....]....]....]....]....]....]....]....]....]....]....]....]....]....]....]....]....]....]....]....]....]....]....]....]....]....]...
|
|
|
|
// e.g. Input data of [8b, 1d, dc, 44]
|
|
|
|
// Look for the first two square brackets:
|
2016-12-14 16:39:17 +00:00
|
|
|
if (result.length > 158 && result[0] === 93 && result[5] === 93) {
|
2016-11-28 10:42:58 +00:00
|
|
|
// If the first two square brackets are there, check that the others
|
|
|
|
// are also there. If they are, throw an error. If not, continue.
|
|
|
|
var valid = false;
|
|
|
|
for (var i = 0; i < 155; i += 5) {
|
2016-12-14 16:39:17 +00:00
|
|
|
if (result[i] !== 93) {
|
2016-11-28 10:42:58 +00:00
|
|
|
valid = true;
|
|
|
|
}
|
|
|
|
}
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
if (!valid) {
|
|
|
|
throw "Error: Unable to inflate data";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Trust me, this is the easiest way...
|
|
|
|
return result;
|
|
|
|
},
|
2017-02-09 15:09:33 +00:00
|
|
|
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
ZLIB_COMPRESSION_TYPE_LOOKUP: {
|
|
|
|
"Fixed Huffman Coding" : Zlib.Deflate.CompressionType.FIXED,
|
|
|
|
"Dynamic Huffman Coding" : Zlib.Deflate.CompressionType.DYNAMIC,
|
|
|
|
"None (Store)" : Zlib.Deflate.CompressionType.NONE,
|
|
|
|
},
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* Zlib Deflate operation.
|
|
|
|
*
|
2017-01-31 18:24:56 +00:00
|
|
|
* @param {byteArray} input
|
2016-11-28 10:42:58 +00:00
|
|
|
* @param {Object[]} args
|
2017-01-31 18:24:56 +00:00
|
|
|
* @returns {byteArray}
|
2016-11-28 10:42:58 +00:00
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runZlibDeflate: function(input, args) {
|
2016-11-28 10:42:58 +00:00
|
|
|
var deflate = new Zlib.Deflate(input, {
|
|
|
|
compressionType: Compress.ZLIB_COMPRESSION_TYPE_LOOKUP[args[0]]
|
|
|
|
});
|
|
|
|
return Array.prototype.slice.call(deflate.compress());
|
|
|
|
},
|
2017-02-09 15:09:33 +00:00
|
|
|
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
ZLIB_BUFFER_TYPE_LOOKUP: {
|
|
|
|
"Adaptive" : Zlib.Inflate.BufferType.ADAPTIVE,
|
|
|
|
"Block" : Zlib.Inflate.BufferType.BLOCK,
|
|
|
|
},
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* Zlib Inflate operation.
|
|
|
|
*
|
2017-01-31 18:24:56 +00:00
|
|
|
* @param {byteArray} input
|
2016-11-28 10:42:58 +00:00
|
|
|
* @param {Object[]} args
|
2017-01-31 18:24:56 +00:00
|
|
|
* @returns {byteArray}
|
2016-11-28 10:42:58 +00:00
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runZlibInflate: function(input, args) {
|
2016-11-28 10:42:58 +00:00
|
|
|
// Deal with character encoding issues
|
2017-01-31 18:24:56 +00:00
|
|
|
input = Utils.strToByteArray(Utils.byteArrayToUtf8(input));
|
2016-11-28 10:42:58 +00:00
|
|
|
var inflate = new Zlib.Inflate(input, {
|
2016-12-14 16:39:17 +00:00
|
|
|
index: args[0],
|
|
|
|
bufferSize: args[1],
|
|
|
|
bufferType: Compress.ZLIB_BUFFER_TYPE_LOOKUP[args[2]],
|
|
|
|
resize: args[3],
|
|
|
|
verify: args[4]
|
|
|
|
});
|
2016-11-28 10:42:58 +00:00
|
|
|
return Array.prototype.slice.call(inflate.decompress());
|
|
|
|
},
|
2017-02-09 15:09:33 +00:00
|
|
|
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
GZIP_CHECKSUM: false,
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* Gzip operation.
|
|
|
|
*
|
2017-01-31 18:24:56 +00:00
|
|
|
* @param {byteArray} input
|
2016-11-28 10:42:58 +00:00
|
|
|
* @param {Object[]} args
|
2017-01-31 18:24:56 +00:00
|
|
|
* @returns {byteArray}
|
2016-11-28 10:42:58 +00:00
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runGzip: function(input, args) {
|
2016-11-28 10:42:58 +00:00
|
|
|
var filename = args[1],
|
|
|
|
comment = args[2],
|
|
|
|
options = {
|
|
|
|
deflateOptions: {
|
|
|
|
compressionType: Compress.ZLIB_COMPRESSION_TYPE_LOOKUP[args[0]]
|
|
|
|
},
|
|
|
|
flags: {
|
|
|
|
fhcrc: args[3]
|
|
|
|
}
|
|
|
|
};
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
if (filename.length) {
|
|
|
|
options.flags.fname = true;
|
|
|
|
options.filename = filename;
|
|
|
|
}
|
|
|
|
if (comment.length) {
|
|
|
|
options.flags.fcommenct = true;
|
|
|
|
options.comment = comment;
|
|
|
|
}
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
var gzip = new Zlib.Gzip(input, options);
|
|
|
|
return Array.prototype.slice.call(gzip.compress());
|
|
|
|
},
|
2017-02-09 15:09:33 +00:00
|
|
|
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* Gunzip operation.
|
|
|
|
*
|
2017-01-31 18:24:56 +00:00
|
|
|
* @param {byteArray} input
|
2016-11-28 10:42:58 +00:00
|
|
|
* @param {Object[]} args
|
2017-01-31 18:24:56 +00:00
|
|
|
* @returns {byteArray}
|
2016-11-28 10:42:58 +00:00
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runGunzip: function(input, args) {
|
2016-11-28 10:42:58 +00:00
|
|
|
// Deal with character encoding issues
|
2017-01-31 18:24:56 +00:00
|
|
|
input = Utils.strToByteArray(Utils.byteArrayToUtf8(input));
|
2016-11-28 10:42:58 +00:00
|
|
|
var gunzip = new Zlib.Gunzip(input);
|
|
|
|
return Array.prototype.slice.call(gunzip.decompress());
|
|
|
|
},
|
2017-02-09 15:09:33 +00:00
|
|
|
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
PKZIP_FILENAME: "file.txt",
|
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
ZIP_COMPRESSION_METHOD_LOOKUP: {
|
|
|
|
"Deflate" : Zlib.Zip.CompressionMethod.DEFLATE,
|
|
|
|
"None (Store)" : Zlib.Zip.CompressionMethod.STORE
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
ZIP_OS_LOOKUP: {
|
|
|
|
"MSDOS" : Zlib.Zip.OperatingSystem.MSDOS,
|
|
|
|
"Unix" : Zlib.Zip.OperatingSystem.UNIX,
|
|
|
|
"Macintosh" : Zlib.Zip.OperatingSystem.MACINTOSH
|
|
|
|
},
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* Zip operation.
|
|
|
|
*
|
2017-01-31 18:24:56 +00:00
|
|
|
* @param {byteArray} input
|
2016-11-28 10:42:58 +00:00
|
|
|
* @param {Object[]} args
|
2017-01-31 18:24:56 +00:00
|
|
|
* @returns {byteArray}
|
2016-11-28 10:42:58 +00:00
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runPkzip: function(input, args) {
|
|
|
|
var password = Utils.strToByteArray(args[2]),
|
2016-11-28 10:42:58 +00:00
|
|
|
options = {
|
2017-01-31 18:24:56 +00:00
|
|
|
filename: Utils.strToByteArray(args[0]),
|
|
|
|
comment: Utils.strToByteArray(args[1]),
|
2016-11-28 10:42:58 +00:00
|
|
|
compressionMethod: Compress.ZIP_COMPRESSION_METHOD_LOOKUP[args[3]],
|
|
|
|
os: Compress.ZIP_OS_LOOKUP[args[4]],
|
|
|
|
deflateOption: {
|
|
|
|
compressionType: Compress.ZLIB_COMPRESSION_TYPE_LOOKUP[args[5]]
|
|
|
|
},
|
|
|
|
},
|
|
|
|
zip = new Zlib.Zip();
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
if (password.length)
|
|
|
|
zip.setPassword(password);
|
|
|
|
zip.addFile(input, options);
|
|
|
|
return Array.prototype.slice.call(zip.compress());
|
|
|
|
},
|
2017-02-09 15:09:33 +00:00
|
|
|
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
PKUNZIP_VERIFY: false,
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* Unzip operation.
|
|
|
|
*
|
2017-01-31 18:24:56 +00:00
|
|
|
* @param {byteArray} input
|
2016-11-28 10:42:58 +00:00
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runPkunzip: function(input, args) {
|
2016-11-28 10:42:58 +00:00
|
|
|
var options = {
|
2017-01-31 18:24:56 +00:00
|
|
|
password: Utils.strToByteArray(args[0]),
|
2016-11-28 10:42:58 +00:00
|
|
|
verify: args[1]
|
|
|
|
},
|
|
|
|
unzip = new Zlib.Unzip(input, options),
|
|
|
|
filenames = unzip.getFilenames(),
|
2017-02-09 12:00:36 -05:00
|
|
|
files = [];
|
|
|
|
|
|
|
|
filenames.forEach(function(fileName) {
|
|
|
|
var contents = unzip.decompress(fileName);
|
|
|
|
|
|
|
|
contents = Utils.byteArrayToUtf8(contents);
|
|
|
|
|
|
|
|
var file = {
|
|
|
|
fileName: fileName,
|
|
|
|
size: contents.length,
|
|
|
|
};
|
|
|
|
|
|
|
|
var isDir = contents.length === 0 && fileName.endsWith("/");
|
|
|
|
if (!isDir) {
|
|
|
|
file.contents = contents;
|
|
|
|
}
|
|
|
|
|
|
|
|
files.push(file);
|
|
|
|
});
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2017-02-09 12:00:36 -05:00
|
|
|
return Utils.displayFilesAsHTML(files);
|
2016-11-28 10:42:58 +00:00
|
|
|
},
|
2017-02-09 15:09:33 +00:00
|
|
|
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* Bzip2 Decompress operation.
|
|
|
|
*
|
2017-01-31 18:24:56 +00:00
|
|
|
* @param {byteArray} input
|
2016-11-28 10:42:58 +00:00
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runBzip2Decompress: function(input, args) {
|
2016-11-28 10:42:58 +00:00
|
|
|
var compressed = new Uint8Array(input),
|
2017-01-31 18:24:56 +00:00
|
|
|
bzip2Reader,
|
2016-11-28 10:42:58 +00:00
|
|
|
plain = "";
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2017-01-31 18:24:56 +00:00
|
|
|
bzip2Reader = bzip2.array(compressed);
|
|
|
|
plain = bzip2.simple(bzip2Reader);
|
2016-11-28 10:42:58 +00:00
|
|
|
return plain;
|
|
|
|
},
|
2017-02-08 00:05:52 -05:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
TAR_FILENAME: "file.txt",
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2017-02-09 13:04:46 -05:00
|
|
|
* Tar pack operation.
|
2017-02-08 00:05:52 -05:00
|
|
|
*
|
2017-02-08 12:51:54 -05:00
|
|
|
* @author tlwr [toby@toby.codes]
|
|
|
|
*
|
2017-02-08 00:05:52 -05:00
|
|
|
* @param {byteArray} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {byteArray}
|
|
|
|
*/
|
2017-02-08 12:51:18 -05:00
|
|
|
runTar: function(input, args) {
|
2017-02-08 23:35:54 -05:00
|
|
|
var Tarball = function() {
|
|
|
|
this.bytes = new Array(512);
|
|
|
|
this.position = 0;
|
|
|
|
};
|
|
|
|
|
2017-02-09 11:25:09 -05:00
|
|
|
Tarball.prototype.addEmptyBlock = function() {
|
|
|
|
var filler = new Array(512);
|
|
|
|
filler.fill(0);
|
|
|
|
this.bytes = this.bytes.concat(filler);
|
|
|
|
};
|
|
|
|
|
2017-02-08 23:35:54 -05:00
|
|
|
Tarball.prototype.writeBytes = function(bytes) {
|
|
|
|
var self = this;
|
|
|
|
|
2017-02-09 11:38:20 -05:00
|
|
|
if (this.position + bytes.length > this.bytes.length) {
|
2017-02-09 11:25:09 -05:00
|
|
|
this.addEmptyBlock();
|
2017-02-08 23:35:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Array.prototype.forEach.call(bytes, function(b, i) {
|
2017-02-09 11:38:20 -05:00
|
|
|
if (typeof b.charCodeAt !== "undefined") {
|
2017-02-08 23:35:54 -05:00
|
|
|
b = b.charCodeAt();
|
|
|
|
}
|
|
|
|
|
|
|
|
self.bytes[self.position] = b;
|
|
|
|
self.position += 1;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Tarball.prototype.writeEndBlocks = function() {
|
2017-02-09 11:25:09 -05:00
|
|
|
var numEmptyBlocks = 2;
|
2017-02-09 11:38:20 -05:00
|
|
|
for (var i = 0; i < numEmptyBlocks; i++) {
|
2017-02-09 11:25:09 -05:00
|
|
|
this.addEmptyBlock();
|
|
|
|
}
|
2017-02-08 23:35:54 -05:00
|
|
|
};
|
|
|
|
|
2017-02-09 13:07:46 -05:00
|
|
|
var fileSize = Utils.padLeft(input.length.toString(8), 11, "0");
|
2017-02-08 23:35:54 -05:00
|
|
|
var currentUnixTimestamp = Math.floor(Date.now() / 1000);
|
2017-02-09 13:07:46 -05:00
|
|
|
var lastModTime = Utils.padLeft(currentUnixTimestamp.toString(8), 11, "0");
|
2017-02-08 23:35:54 -05:00
|
|
|
|
|
|
|
var file = {
|
2017-02-09 13:07:46 -05:00
|
|
|
fileName: Utils.padBytesRight(args[0], 100),
|
|
|
|
fileMode: Utils.padBytesRight("0000664", 8),
|
|
|
|
ownerUID: Utils.padBytesRight("0", 8),
|
|
|
|
ownerGID: Utils.padBytesRight("0", 8),
|
|
|
|
size: Utils.padBytesRight(fileSize, 12),
|
|
|
|
lastModTime: Utils.padBytesRight(lastModTime, 12),
|
2017-02-08 23:35:54 -05:00
|
|
|
checksum: " ",
|
|
|
|
type: "0",
|
2017-02-09 13:07:46 -05:00
|
|
|
linkedFileName: Utils.padBytesRight("", 100),
|
|
|
|
USTARFormat: Utils.padBytesRight("ustar", 6),
|
2017-02-08 23:35:54 -05:00
|
|
|
version: "00",
|
2017-02-09 13:07:46 -05:00
|
|
|
ownerUserName: Utils.padBytesRight("", 32),
|
|
|
|
ownerGroupName: Utils.padBytesRight("", 32),
|
|
|
|
deviceMajor: Utils.padBytesRight("", 8),
|
|
|
|
deviceMinor: Utils.padBytesRight("", 8),
|
|
|
|
fileNamePrefix: Utils.padBytesRight("", 155),
|
2017-02-08 23:35:54 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
var checksum = 0;
|
2017-02-09 11:38:20 -05:00
|
|
|
for (var key in file) {
|
2017-02-08 23:35:54 -05:00
|
|
|
var bytes = file[key];
|
|
|
|
Array.prototype.forEach.call(bytes, function(b) {
|
2017-02-09 11:38:20 -05:00
|
|
|
if (typeof b.charCodeAt !== "undefined") {
|
2017-02-08 23:35:54 -05:00
|
|
|
checksum += b.charCodeAt();
|
|
|
|
} else {
|
|
|
|
checksum += b;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-02-09 13:07:46 -05:00
|
|
|
checksum = Utils.padBytesRight(Utils.padLeft(checksum.toString(8), 7, "0"), 8);
|
2017-02-08 23:35:54 -05:00
|
|
|
file.checksum = checksum;
|
|
|
|
|
|
|
|
var tarball = new Tarball();
|
|
|
|
tarball.writeBytes(file.fileName);
|
|
|
|
tarball.writeBytes(file.fileMode);
|
|
|
|
tarball.writeBytes(file.ownerUID);
|
|
|
|
tarball.writeBytes(file.ownerGID);
|
|
|
|
tarball.writeBytes(file.size);
|
|
|
|
tarball.writeBytes(file.lastModTime);
|
|
|
|
tarball.writeBytes(file.checksum);
|
|
|
|
tarball.writeBytes(file.type);
|
|
|
|
tarball.writeBytes(file.linkedFileName);
|
|
|
|
tarball.writeBytes(file.USTARFormat);
|
|
|
|
tarball.writeBytes(file.version);
|
|
|
|
tarball.writeBytes(file.ownerUserName);
|
|
|
|
tarball.writeBytes(file.ownerGroupName);
|
|
|
|
tarball.writeBytes(file.deviceMajor);
|
|
|
|
tarball.writeBytes(file.deviceMinor);
|
|
|
|
tarball.writeBytes(file.fileNamePrefix);
|
2017-02-09 13:07:46 -05:00
|
|
|
tarball.writeBytes(Utils.padBytesRight("", 12));
|
2017-02-08 23:35:54 -05:00
|
|
|
tarball.writeBytes(input);
|
|
|
|
tarball.writeEndBlocks();
|
|
|
|
|
|
|
|
return tarball.bytes;
|
2017-02-08 00:05:52 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Untar unpack operation.
|
|
|
|
*
|
2017-02-08 12:51:54 -05:00
|
|
|
* @author tlwr [toby@toby.codes]
|
|
|
|
*
|
2017-02-08 00:05:52 -05:00
|
|
|
* @param {byteArray} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {html}
|
|
|
|
*/
|
2017-02-08 12:51:18 -05:00
|
|
|
runUntar: function(input, args) {
|
2017-02-08 00:05:52 -05:00
|
|
|
var Stream = function(input) {
|
|
|
|
this.bytes = input;
|
|
|
|
this.position = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
Stream.prototype.readString = function(numBytes) {
|
|
|
|
var result = "";
|
2017-02-09 11:38:20 -05:00
|
|
|
for (var i = this.position; i < this.position + numBytes; i++) {
|
2017-02-08 00:05:52 -05:00
|
|
|
var currentByte = this.bytes[i];
|
2017-02-09 11:38:20 -05:00
|
|
|
if (currentByte === 0) break;
|
2017-02-08 00:05:52 -05:00
|
|
|
result += String.fromCharCode(currentByte);
|
|
|
|
}
|
|
|
|
this.position += numBytes;
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
|
|
|
Stream.prototype.readInt = function(numBytes, base) {
|
|
|
|
var string = this.readString(numBytes);
|
|
|
|
return parseInt(string, base);
|
|
|
|
};
|
|
|
|
|
|
|
|
Stream.prototype.hasMore = function() {
|
|
|
|
return this.position < this.bytes.length;
|
|
|
|
};
|
|
|
|
|
|
|
|
var stream = new Stream(input),
|
|
|
|
files = [];
|
|
|
|
|
2017-02-09 11:38:20 -05:00
|
|
|
while (stream.hasMore()) {
|
2017-02-08 00:05:52 -05:00
|
|
|
var dataPosition = stream.position + 512;
|
|
|
|
|
|
|
|
var file = {
|
|
|
|
fileName: stream.readString(100),
|
|
|
|
fileMode: stream.readString(8),
|
|
|
|
ownerUID: stream.readString(8),
|
|
|
|
ownerGID: stream.readString(8),
|
|
|
|
size: parseInt(stream.readString(12), 8), // Octal
|
|
|
|
lastModTime: new Date(1000 * stream.readInt(12, 8)), // Octal
|
|
|
|
checksum: stream.readString(8),
|
|
|
|
type: stream.readString(1),
|
|
|
|
linkedFileName: stream.readString(100),
|
|
|
|
USTARFormat: stream.readString(6).indexOf("ustar") >= 0,
|
|
|
|
};
|
|
|
|
|
2017-02-09 11:38:20 -05:00
|
|
|
if (file.USTARFormat) {
|
2017-02-08 00:05:52 -05:00
|
|
|
file.version = stream.readString(2);
|
|
|
|
file.ownerUserName = stream.readString(32);
|
|
|
|
file.ownerGroupName = stream.readString(32);
|
|
|
|
file.deviceMajor = stream.readString(8);
|
|
|
|
file.deviceMinor = stream.readString(8);
|
|
|
|
file.filenamePrefix = stream.readString(155);
|
|
|
|
}
|
|
|
|
|
|
|
|
stream.position = dataPosition;
|
|
|
|
|
2017-02-09 11:38:20 -05:00
|
|
|
if (file.type === "0") {
|
2017-02-08 00:05:52 -05:00
|
|
|
// File
|
|
|
|
files.push(file);
|
|
|
|
var endPosition = stream.position + file.size;
|
2017-02-09 11:38:20 -05:00
|
|
|
if (file.size % 512 !== 0) {
|
2017-02-08 00:05:52 -05:00
|
|
|
endPosition += 512 - (file.size % 512);
|
|
|
|
}
|
|
|
|
|
|
|
|
file.contents = "";
|
|
|
|
|
2017-02-09 11:38:20 -05:00
|
|
|
while (stream.position < endPosition) {
|
2017-02-08 00:05:52 -05:00
|
|
|
file.contents += stream.readString(512);
|
|
|
|
}
|
2017-02-09 11:38:20 -05:00
|
|
|
} else if (file.type === "5") {
|
2017-02-08 00:05:52 -05:00
|
|
|
// Directory
|
|
|
|
files.push(file);
|
|
|
|
} else {
|
|
|
|
// Symlink or empty bytes
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-08 12:51:18 -05:00
|
|
|
return Utils.displayFilesAsHTML(files);
|
2017-02-08 00:05:52 -05:00
|
|
|
},
|
2016-11-28 10:42:58 +00:00
|
|
|
};
|