2017-09-17 10:36:32 +00:00
|
|
|
/**
|
|
|
|
* unrar.js
|
|
|
|
*
|
2019-05-09 23:16:03 +00:00
|
|
|
* Licensed under the MIT License
|
|
|
|
*
|
2017-09-17 10:36:32 +00:00
|
|
|
* Copyright(c) 2011 Google Inc.
|
|
|
|
* Copyright(c) 2011 antimatter15
|
|
|
|
*
|
|
|
|
* Reference Documentation:
|
|
|
|
*
|
|
|
|
* http://kthoom.googlecode.com/hg/docs/unrar.html
|
|
|
|
*/
|
2017-10-08 08:41:43 +00:00
|
|
|
/* global bitjs, importScripts */
|
2017-09-17 10:36:32 +00:00
|
|
|
|
|
|
|
// This file expects to be invoked as a Worker (see onmessage below).
|
2019-05-09 23:16:03 +00:00
|
|
|
importScripts('../io/bitstream.js');
|
|
|
|
importScripts('../io/bytebuffer.js');
|
|
|
|
importScripts('archive.js');
|
|
|
|
importScripts('rarvm.js');
|
2017-09-17 10:36:32 +00:00
|
|
|
|
|
|
|
// Progress variables.
|
|
|
|
var currentFilename = "";
|
|
|
|
var currentFileNumber = 0;
|
|
|
|
var currentBytesUnarchivedInFile = 0;
|
|
|
|
var currentBytesUnarchived = 0;
|
|
|
|
var totalUncompressedBytesInArchive = 0;
|
|
|
|
var totalFilesInArchive = 0;
|
|
|
|
|
|
|
|
// Helper functions.
|
|
|
|
var info = function(str) {
|
2017-09-17 12:51:17 +00:00
|
|
|
postMessage(new bitjs.archive.UnarchiveInfoEvent(str));
|
2017-09-17 10:36:32 +00:00
|
|
|
};
|
|
|
|
var err = function(str) {
|
2017-09-17 12:51:17 +00:00
|
|
|
postMessage(new bitjs.archive.UnarchiveErrorEvent(str));
|
2017-09-17 10:36:32 +00:00
|
|
|
};
|
|
|
|
var postProgress = function() {
|
2017-09-17 12:51:17 +00:00
|
|
|
postMessage(new bitjs.archive.UnarchiveProgressEvent(
|
|
|
|
currentFilename,
|
|
|
|
currentFileNumber,
|
|
|
|
currentBytesUnarchivedInFile,
|
|
|
|
currentBytesUnarchived,
|
|
|
|
totalUncompressedBytesInArchive,
|
|
|
|
totalFilesInArchive));
|
2017-09-17 10:36:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// shows a byte value as its hex representation
|
|
|
|
var nibble = "0123456789ABCDEF";
|
|
|
|
var byteValueToHexString = function(num) {
|
2018-08-12 07:29:57 +00:00
|
|
|
return nibble[num >> 4] + nibble[num & 0xF];
|
2017-09-17 10:36:32 +00:00
|
|
|
};
|
|
|
|
var twoByteValueToHexString = function(num) {
|
2018-08-12 07:29:57 +00:00
|
|
|
return nibble[(num >> 12) & 0xF] + nibble[(num >> 8) & 0xF] + nibble[(num >> 4) & 0xF] + nibble[num & 0xF];
|
2017-09-17 10:36:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Volume Types
|
2017-10-08 08:41:43 +00:00
|
|
|
// MARK_HEAD = 0x72;
|
|
|
|
var MAIN_HEAD = 0x73,
|
2017-09-17 12:51:17 +00:00
|
|
|
FILE_HEAD = 0x74,
|
2017-10-08 08:41:43 +00:00
|
|
|
// COMM_HEAD = 0x75,
|
|
|
|
// AV_HEAD = 0x76,
|
|
|
|
// SUB_HEAD = 0x77,
|
|
|
|
// PROTECT_HEAD = 0x78,
|
|
|
|
// SIGN_HEAD = 0x79,
|
|
|
|
// NEWSUB_HEAD = 0x7a,
|
2019-05-13 19:28:06 +00:00
|
|
|
ENDARC_HEAD = 0x7b;
|
2017-09-17 10:36:32 +00:00
|
|
|
|
2019-05-09 23:16:03 +00:00
|
|
|
// ============================================================================================== //
|
2017-09-17 10:36:32 +00:00
|
|
|
|
2019-05-09 23:16:03 +00:00
|
|
|
/**
|
2019-05-13 05:53:25 +00:00
|
|
|
* @param {bitjs.io.BitStream} bstream
|
|
|
|
* @constructor
|
2019-05-09 23:16:03 +00:00
|
|
|
*/
|
2019-05-13 05:53:25 +00:00
|
|
|
var RarVolumeHeader = function(bstream) {
|
2017-09-17 12:51:17 +00:00
|
|
|
var headPos = bstream.bytePtr;
|
|
|
|
// byte 1,2
|
2017-10-08 08:41:43 +00:00
|
|
|
info("Rar Volume Header @" + bstream.bytePtr);
|
2017-09-17 12:51:17 +00:00
|
|
|
|
|
|
|
this.crc = bstream.readBits(16);
|
|
|
|
info(" crc=" + this.crc);
|
2017-09-17 10:36:32 +00:00
|
|
|
|
2017-09-17 12:51:17 +00:00
|
|
|
// byte 3
|
|
|
|
this.headType = bstream.readBits(8);
|
|
|
|
info(" headType=" + this.headType);
|
2017-09-17 10:36:32 +00:00
|
|
|
|
2017-09-17 12:51:17 +00:00
|
|
|
// Get flags
|
|
|
|
// bytes 4,5
|
|
|
|
this.flags = {};
|
|
|
|
this.flags.value = bstream.peekBits(16);
|
2019-01-14 19:27:53 +00:00
|
|
|
|
2017-09-17 12:51:17 +00:00
|
|
|
info(" flags=" + twoByteValueToHexString(this.flags.value));
|
|
|
|
switch (this.headType) {
|
2017-09-18 16:59:08 +00:00
|
|
|
case MAIN_HEAD:
|
|
|
|
this.flags.MHD_VOLUME = !!bstream.readBits(1);
|
|
|
|
this.flags.MHD_COMMENT = !!bstream.readBits(1);
|
|
|
|
this.flags.MHD_LOCK = !!bstream.readBits(1);
|
|
|
|
this.flags.MHD_SOLID = !!bstream.readBits(1);
|
|
|
|
this.flags.MHD_PACK_COMMENT = !!bstream.readBits(1);
|
|
|
|
this.flags.MHD_NEWNUMBERING = this.flags.MHD_PACK_COMMENT;
|
|
|
|
this.flags.MHD_AV = !!bstream.readBits(1);
|
|
|
|
this.flags.MHD_PROTECT = !!bstream.readBits(1);
|
|
|
|
this.flags.MHD_PASSWORD = !!bstream.readBits(1);
|
|
|
|
this.flags.MHD_FIRSTVOLUME = !!bstream.readBits(1);
|
|
|
|
this.flags.MHD_ENCRYPTVER = !!bstream.readBits(1);
|
|
|
|
bstream.readBits(6); // unused
|
|
|
|
break;
|
|
|
|
case FILE_HEAD:
|
|
|
|
this.flags.LHD_SPLIT_BEFORE = !!bstream.readBits(1); // 0x0001
|
|
|
|
this.flags.LHD_SPLIT_AFTER = !!bstream.readBits(1); // 0x0002
|
|
|
|
this.flags.LHD_PASSWORD = !!bstream.readBits(1); // 0x0004
|
|
|
|
this.flags.LHD_COMMENT = !!bstream.readBits(1); // 0x0008
|
|
|
|
this.flags.LHD_SOLID = !!bstream.readBits(1); // 0x0010
|
|
|
|
bstream.readBits(3); // unused
|
|
|
|
this.flags.LHD_LARGE = !!bstream.readBits(1); // 0x0100
|
|
|
|
this.flags.LHD_UNICODE = !!bstream.readBits(1); // 0x0200
|
|
|
|
this.flags.LHD_SALT = !!bstream.readBits(1); // 0x0400
|
|
|
|
this.flags.LHD_VERSION = !!bstream.readBits(1); // 0x0800
|
|
|
|
this.flags.LHD_EXTTIME = !!bstream.readBits(1); // 0x1000
|
|
|
|
this.flags.LHD_EXTFLAGS = !!bstream.readBits(1); // 0x2000
|
|
|
|
bstream.readBits(2); // unused
|
|
|
|
info(" LHD_SPLIT_BEFORE = " + this.flags.LHD_SPLIT_BEFORE);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
bstream.readBits(16);
|
|
|
|
}
|
2019-01-14 19:27:53 +00:00
|
|
|
|
2017-09-18 16:59:08 +00:00
|
|
|
// byte 6,7
|
|
|
|
this.headSize = bstream.readBits(16);
|
|
|
|
info(" headSize=" + this.headSize);
|
|
|
|
switch (this.headType) {
|
|
|
|
case MAIN_HEAD:
|
|
|
|
this.highPosAv = bstream.readBits(16);
|
|
|
|
this.posAv = bstream.readBits(32);
|
|
|
|
if (this.flags.MHD_ENCRYPTVER) {
|
|
|
|
this.encryptVer = bstream.readBits(8);
|
|
|
|
}
|
|
|
|
info("Found MAIN_HEAD with highPosAv=" + this.highPosAv + ", posAv=" + this.posAv);
|
|
|
|
break;
|
|
|
|
case FILE_HEAD:
|
|
|
|
this.packSize = bstream.readBits(32);
|
|
|
|
this.unpackedSize = bstream.readBits(32);
|
|
|
|
this.hostOS = bstream.readBits(8);
|
|
|
|
this.fileCRC = bstream.readBits(32);
|
|
|
|
this.fileTime = bstream.readBits(32);
|
|
|
|
this.unpVer = bstream.readBits(8);
|
|
|
|
this.method = bstream.readBits(8);
|
|
|
|
this.nameSize = bstream.readBits(16);
|
|
|
|
this.fileAttr = bstream.readBits(32);
|
|
|
|
|
|
|
|
if (this.flags.LHD_LARGE) {
|
|
|
|
info("Warning: Reading in LHD_LARGE 64-bit size values");
|
|
|
|
this.HighPackSize = bstream.readBits(32);
|
|
|
|
this.HighUnpSize = bstream.readBits(32);
|
|
|
|
} else {
|
|
|
|
this.HighPackSize = 0;
|
|
|
|
this.HighUnpSize = 0;
|
2018-08-12 07:29:57 +00:00
|
|
|
if (this.unpackedSize === 0xffffffff) {
|
2017-10-08 08:41:43 +00:00
|
|
|
this.HighUnpSize = 0x7fffffff;
|
2017-09-18 16:59:08 +00:00
|
|
|
this.unpackedSize = 0xffffffff;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.fullPackSize = 0;
|
|
|
|
this.fullUnpackSize = 0;
|
|
|
|
this.fullPackSize |= this.HighPackSize;
|
|
|
|
this.fullPackSize <<= 32;
|
|
|
|
this.fullPackSize |= this.packSize;
|
|
|
|
|
|
|
|
// read in filename
|
|
|
|
|
|
|
|
this.filename = bstream.readBytes(this.nameSize);
|
2018-08-18 16:38:28 +00:00
|
|
|
var _s = "";
|
2018-08-12 16:21:57 +00:00
|
|
|
for (var _i = 0; _i < this.filename.length ; _i++) {
|
2017-09-18 16:59:08 +00:00
|
|
|
_s += String.fromCharCode(this.filename[_i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.filename = _s;
|
|
|
|
|
|
|
|
if (this.flags.LHD_SALT) {
|
|
|
|
info("Warning: Reading in 64-bit salt value");
|
|
|
|
this.salt = bstream.readBits(64); // 8 bytes
|
2017-09-17 12:51:17 +00:00
|
|
|
}
|
2017-09-18 16:59:08 +00:00
|
|
|
|
|
|
|
if (this.flags.LHD_EXTTIME) {
|
|
|
|
// 16-bit flags
|
|
|
|
var extTimeFlags = bstream.readBits(16);
|
|
|
|
|
|
|
|
// this is adapted straight out of arcread.cpp, Archive::ReadHeader()
|
|
|
|
for (var I = 0; I < 4; ++I) {
|
2017-10-08 08:41:43 +00:00
|
|
|
var rmode = extTimeFlags >> ((3 - I) * 4);
|
2018-08-12 07:29:57 +00:00
|
|
|
if ((rmode & 8) === 0) {
|
2017-09-18 16:59:08 +00:00
|
|
|
continue;
|
2018-08-12 07:29:57 +00:00
|
|
|
}
|
|
|
|
if (I !== 0) {
|
2017-09-18 16:59:08 +00:00
|
|
|
bstream.readBits(16);
|
2017-10-08 08:41:43 +00:00
|
|
|
}
|
|
|
|
var count = (rmode & 3);
|
|
|
|
for (var J = 0; J < count; ++J) {
|
|
|
|
bstream.readBits(8);
|
|
|
|
}
|
2017-09-18 16:59:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.flags.LHD_COMMENT) {
|
|
|
|
info("Found a LHD_COMMENT");
|
2017-09-17 12:51:17 +00:00
|
|
|
}
|
2017-09-18 16:59:08 +00:00
|
|
|
|
|
|
|
|
2019-05-13 19:28:06 +00:00
|
|
|
while (headPos + this.headSize > bstream.bytePtr) {
|
|
|
|
bstream.readBits(1);
|
|
|
|
}
|
2017-09-18 16:59:08 +00:00
|
|
|
|
2019-05-31 06:43:51 +00:00
|
|
|
// If Info line is commented in firefox fails if server on same computer than browser with error "expected expression, got default"
|
|
|
|
//info("Found FILE_HEAD with packSize=" + this.packSize + ", unpackedSize= " + this.unpackedSize + ", hostOS=" + this.hostOS + ", unpVer=" + this.unpVer + ", method=" + this.method + ", filename=" + this.filename);
|
2017-09-18 16:59:08 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
info("Found a header of type 0x" + byteValueToHexString(this.headType));
|
|
|
|
// skip the rest of the header bytes (for now)
|
2019-05-13 19:28:06 +00:00
|
|
|
bstream.readBytes(this.headSize - 7);
|
2017-09-18 16:59:08 +00:00
|
|
|
break;
|
2017-09-17 12:51:17 +00:00
|
|
|
}
|
2017-09-17 10:36:32 +00:00
|
|
|
};
|
|
|
|
|
2018-08-12 16:21:57 +00:00
|
|
|
//var BLOCK_LZ = 0;
|
2017-09-17 10:36:32 +00:00
|
|
|
|
2019-01-14 19:27:53 +00:00
|
|
|
var rLDecode = [0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224],
|
2019-05-13 19:28:06 +00:00
|
|
|
rLBits = [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5],
|
2017-10-08 08:41:43 +00:00
|
|
|
rDBitLengthCounts = [4, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 14, 0, 12],
|
|
|
|
rSDDecode = [0, 4, 8, 16, 32, 64, 128, 192],
|
2019-05-13 19:28:06 +00:00
|
|
|
rSDBits = [2, 2, 3, 4, 5, 6, 6, 6];
|
2019-01-14 19:27:53 +00:00
|
|
|
|
2017-09-17 10:36:32 +00:00
|
|
|
var rDDecode = [0, 1, 2, 3, 4, 6, 8, 12, 16, 24, 32,
|
2017-10-08 08:41:43 +00:00
|
|
|
48, 64, 96, 128, 192, 256, 384, 512, 768, 1024, 1536, 2048, 3072,
|
|
|
|
4096, 6144, 8192, 12288, 16384, 24576, 32768, 49152, 65536, 98304,
|
|
|
|
131072, 196608, 262144, 327680, 393216, 458752, 524288, 589824,
|
2019-05-13 19:28:06 +00:00
|
|
|
655360, 720896, 786432, 851968, 917504, 983040
|
|
|
|
];
|
2017-09-17 10:36:32 +00:00
|
|
|
|
|
|
|
var rDBits = [0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5,
|
2017-10-08 08:41:43 +00:00
|
|
|
5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14,
|
2019-05-13 19:28:06 +00:00
|
|
|
15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16
|
|
|
|
];
|
2017-09-17 10:36:32 +00:00
|
|
|
|
2018-08-12 16:21:57 +00:00
|
|
|
var rLowDistRepCount = 16;
|
2017-09-17 10:36:32 +00:00
|
|
|
|
|
|
|
var rNC = 299,
|
2017-09-17 12:51:17 +00:00
|
|
|
rDC = 60,
|
|
|
|
rLDC = 17,
|
|
|
|
rRC = 28,
|
|
|
|
rBC = 20,
|
2018-08-12 16:21:57 +00:00
|
|
|
rHuffTableSize = (rNC + rDC + rRC + rLDC);
|
2017-09-17 10:36:32 +00:00
|
|
|
|
2018-08-12 07:29:57 +00:00
|
|
|
//var UnpBlockType = BLOCK_LZ;
|
2018-08-12 16:21:57 +00:00
|
|
|
var UnpOldTable = new Array(rHuffTableSize);
|
2017-09-17 10:36:32 +00:00
|
|
|
|
|
|
|
var BD = { //bitdecode
|
2017-10-08 08:41:43 +00:00
|
|
|
DecodeLen: new Array(16),
|
|
|
|
DecodePos: new Array(16),
|
|
|
|
DecodeNum: new Array(rBC)
|
2017-09-17 10:36:32 +00:00
|
|
|
};
|
|
|
|
var LD = { //litdecode
|
2017-10-08 08:41:43 +00:00
|
|
|
DecodeLen: new Array(16),
|
|
|
|
DecodePos: new Array(16),
|
|
|
|
DecodeNum: new Array(rNC)
|
2017-09-17 10:36:32 +00:00
|
|
|
};
|
|
|
|
var DD = { //distdecode
|
2017-10-08 08:41:43 +00:00
|
|
|
DecodeLen: new Array(16),
|
|
|
|
DecodePos: new Array(16),
|
|
|
|
DecodeNum: new Array(rDC)
|
2017-09-17 10:36:32 +00:00
|
|
|
};
|
|
|
|
var LDD = { //low dist decode
|
2017-10-08 08:41:43 +00:00
|
|
|
DecodeLen: new Array(16),
|
|
|
|
DecodePos: new Array(16),
|
|
|
|
DecodeNum: new Array(rLDC)
|
2017-09-17 10:36:32 +00:00
|
|
|
};
|
|
|
|
var RD = { //rep decode
|
2017-10-08 08:41:43 +00:00
|
|
|
DecodeLen: new Array(16),
|
|
|
|
DecodePos: new Array(16),
|
|
|
|
DecodeNum: new Array(rRC)
|
2017-09-17 10:36:32 +00:00
|
|
|
};
|
|
|
|
|
2019-05-09 23:16:03 +00:00
|
|
|
/**
|
|
|
|
* @type {Array<bitjs.io.ByteBuffer>}
|
|
|
|
*/
|
2019-05-13 05:53:25 +00:00
|
|
|
var rOldBuffers = [];
|
2019-05-09 23:16:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The current buffer we are unpacking to.
|
|
|
|
* @type {bitjs.io.ByteBuffer}
|
|
|
|
*/
|
2017-09-17 10:36:32 +00:00
|
|
|
var rBuffer;
|
|
|
|
|
2019-05-09 23:16:03 +00:00
|
|
|
/**
|
|
|
|
* The buffer of the final bytes after filtering (only used in Unpack29).
|
|
|
|
* @type {bitjs.io.ByteBuffer}
|
|
|
|
*/
|
2019-05-13 05:53:25 +00:00
|
|
|
var wBuffer;
|
2019-05-09 23:16:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* In unpack.cpp, UnpPtr keeps track of what bytes have been unpacked
|
|
|
|
* into the Window buffer and WrPtr keeps track of what bytes have been
|
|
|
|
* actually written to disk after the unpacking and optional filtering
|
|
|
|
* has been done.
|
|
|
|
*
|
|
|
|
* In our case, rBuffer is the buffer for the unpacked bytes and wBuffer is
|
|
|
|
* the final output bytes.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read in Huffman tables for RAR
|
|
|
|
* @param {bitjs.io.BitStream} bstream
|
|
|
|
*/
|
2019-01-08 06:58:22 +00:00
|
|
|
function rarReadTables(bstream) {
|
2019-05-13 19:28:06 +00:00
|
|
|
var BitLength = new Array(rBC);
|
2019-05-31 06:43:51 +00:00
|
|
|
var Table = new Array(rHuffTableSize);
|
2018-08-12 16:21:57 +00:00
|
|
|
var i;
|
2017-09-17 12:51:17 +00:00
|
|
|
// before we start anything we need to get byte-aligned
|
2019-05-13 19:28:06 +00:00
|
|
|
bstream.readBits((8 - bstream.bitPtr) & 0x7);
|
2019-01-14 19:27:53 +00:00
|
|
|
|
2017-09-17 12:51:17 +00:00
|
|
|
if (bstream.readBits(1)) {
|
|
|
|
info("Error! PPM not implemented yet");
|
|
|
|
return;
|
|
|
|
}
|
2019-01-14 19:27:53 +00:00
|
|
|
|
2017-09-17 12:51:17 +00:00
|
|
|
if (!bstream.readBits(1)) { //discard old table
|
2019-05-31 06:43:51 +00:00
|
|
|
for (i = UnpOldTable.length; i--;) {
|
2019-05-13 19:28:06 +00:00
|
|
|
UnpOldTable[i] = 0;
|
|
|
|
}
|
2017-09-17 10:36:32 +00:00
|
|
|
}
|
2017-09-17 12:51:17 +00:00
|
|
|
|
|
|
|
// read in bit lengths
|
|
|
|
for (var I = 0; I < rBC; ++I) {
|
|
|
|
var Length = bstream.readBits(4);
|
2018-08-12 07:29:57 +00:00
|
|
|
if (Length === 15) {
|
|
|
|
var ZeroCount = bstream.readBits(4);
|
|
|
|
if (ZeroCount === 0) {
|
2017-09-17 12:51:17 +00:00
|
|
|
BitLength[I] = 15;
|
2018-08-12 07:29:57 +00:00
|
|
|
} else {
|
2017-09-17 12:51:17 +00:00
|
|
|
ZeroCount += 2;
|
2018-08-12 07:29:57 +00:00
|
|
|
while (ZeroCount-- > 0 && I < rBC) {
|
2017-09-17 12:51:17 +00:00
|
|
|
BitLength[I++] = 0;
|
2018-08-12 07:29:57 +00:00
|
|
|
}
|
2017-09-17 12:51:17 +00:00
|
|
|
--I;
|
|
|
|
}
|
2018-08-12 07:29:57 +00:00
|
|
|
} else {
|
2017-09-17 12:51:17 +00:00
|
|
|
BitLength[I] = Length;
|
|
|
|
}
|
2017-09-17 10:36:32 +00:00
|
|
|
}
|
|
|
|
|
2017-09-17 12:51:17 +00:00
|
|
|
// now all 20 bit lengths are obtained, we construct the Huffman Table:
|
|
|
|
|
2018-08-12 16:21:57 +00:00
|
|
|
rarMakeDecodeTables(BitLength, 0, BD, rBC);
|
2019-01-14 19:27:53 +00:00
|
|
|
|
2018-08-12 16:21:57 +00:00
|
|
|
var TableSize = rHuffTableSize;
|
2017-09-17 12:51:17 +00:00
|
|
|
//console.log(DecodeLen, DecodePos, DecodeNum);
|
2018-08-12 07:29:57 +00:00
|
|
|
for (i = 0; i < TableSize;) {
|
2018-08-12 16:21:57 +00:00
|
|
|
var N;
|
2018-08-18 16:38:28 +00:00
|
|
|
var num = rarDecodeNumber(bstream, BD);
|
2017-09-17 12:51:17 +00:00
|
|
|
if (num < 16) {
|
|
|
|
Table[i] = (num + UnpOldTable[i]) & 0xf;
|
|
|
|
i++;
|
2018-08-12 07:29:57 +00:00
|
|
|
} else if (num < 18) {
|
2018-08-12 16:21:57 +00:00
|
|
|
N = (num === 16) ? (bstream.readBits(3) + 3) : (bstream.readBits(7) + 11);
|
2017-09-17 12:51:17 +00:00
|
|
|
|
|
|
|
while (N-- > 0 && i < TableSize) {
|
|
|
|
Table[i] = Table[i - 1];
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
} else {
|
2018-08-12 16:21:57 +00:00
|
|
|
N = (num === 18) ? (bstream.readBits(3) + 3) : (bstream.readBits(7) + 11);
|
2017-09-17 10:36:32 +00:00
|
|
|
|
2017-09-17 12:51:17 +00:00
|
|
|
while (N-- > 0 && i < TableSize) {
|
|
|
|
Table[i++] = 0;
|
|
|
|
}
|
|
|
|
}
|
2017-09-17 10:36:32 +00:00
|
|
|
}
|
2019-01-14 19:27:53 +00:00
|
|
|
|
2018-08-12 16:21:57 +00:00
|
|
|
rarMakeDecodeTables(Table, 0, LD, rNC);
|
|
|
|
rarMakeDecodeTables(Table, rNC, DD, rDC);
|
|
|
|
rarMakeDecodeTables(Table, rNC + rDC, LDD, rLDC);
|
|
|
|
rarMakeDecodeTables(Table, rNC + rDC + rLDC, RD, rRC);
|
2019-01-14 19:27:53 +00:00
|
|
|
|
2018-08-12 07:29:57 +00:00
|
|
|
for (i = UnpOldTable.length; i--;) {
|
2017-09-17 12:51:17 +00:00
|
|
|
UnpOldTable[i] = Table[i];
|
|
|
|
}
|
|
|
|
return true;
|
2017-09-17 10:36:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-18 16:38:28 +00:00
|
|
|
function rarDecodeNumber(bstream, dec) {
|
2019-05-13 19:28:06 +00:00
|
|
|
var DecodeLen = dec.DecodeLen,
|
|
|
|
DecodePos = dec.DecodePos,
|
|
|
|
DecodeNum = dec.DecodeNum;
|
2017-09-17 12:51:17 +00:00
|
|
|
var bitField = bstream.getBits() & 0xfffe;
|
|
|
|
//some sort of rolled out binary search
|
2018-08-12 16:21:57 +00:00
|
|
|
var bits = ((bitField < DecodeLen[8]) ?
|
2018-08-12 07:29:57 +00:00
|
|
|
((bitField < DecodeLen[4]) ?
|
|
|
|
((bitField < DecodeLen[2]) ?
|
2019-05-13 19:28:06 +00:00
|
|
|
((bitField < DecodeLen[1]) ? 1 : 2) :
|
|
|
|
((bitField < DecodeLen[3]) ? 3 : 4)) :
|
|
|
|
(bitField < DecodeLen[6]) ?
|
|
|
|
((bitField < DecodeLen[5]) ? 5 : 6) :
|
|
|
|
((bitField < DecodeLen[7]) ? 7 : 8)) :
|
|
|
|
((bitField < DecodeLen[12]) ?
|
2018-08-12 07:29:57 +00:00
|
|
|
((bitField < DecodeLen[10]) ?
|
2019-05-13 19:28:06 +00:00
|
|
|
((bitField < DecodeLen[9]) ? 9 : 10) :
|
|
|
|
((bitField < DecodeLen[11]) ? 11 : 12)) :
|
|
|
|
(bitField < DecodeLen[14]) ?
|
|
|
|
((bitField < DecodeLen[13]) ? 13 : 14) :
|
|
|
|
15));
|
2017-09-17 12:51:17 +00:00
|
|
|
bstream.readBits(bits);
|
2018-08-18 16:38:28 +00:00
|
|
|
var N = DecodePos[bits] + ((bitField - DecodeLen[bits - 1]) >>> (16 - bits));
|
2019-01-14 19:27:53 +00:00
|
|
|
|
2017-09-17 12:51:17 +00:00
|
|
|
return DecodeNum[N];
|
2017-09-17 10:36:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-12 16:21:57 +00:00
|
|
|
function rarMakeDecodeTables(BitLength, offset, dec, size) {
|
2019-05-13 19:28:06 +00:00
|
|
|
var DecodeLen = dec.DecodeLen;
|
|
|
|
var DecodePos = dec.DecodePos;
|
|
|
|
var DecodeNum = dec.DecodeNum;
|
|
|
|
var LenCount = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
|
|
|
|
var TmpPos = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
|
|
|
|
var N = 0;
|
|
|
|
var M = 0;
|
2018-08-18 16:38:28 +00:00
|
|
|
var i;
|
2019-05-31 06:43:51 +00:00
|
|
|
for (i = DecodeNum.length; i--;) {
|
2019-05-13 19:28:06 +00:00
|
|
|
DecodeNum[i] = 0;
|
2019-05-09 23:16:03 +00:00
|
|
|
}
|
2018-08-18 16:38:28 +00:00
|
|
|
for (i = 0; i < size; i++) {
|
2017-09-17 12:51:17 +00:00
|
|
|
LenCount[BitLength[i + offset] & 0xF]++;
|
|
|
|
}
|
|
|
|
LenCount[0] = 0;
|
|
|
|
TmpPos[0] = 0;
|
|
|
|
DecodePos[0] = 0;
|
|
|
|
DecodeLen[0] = 0;
|
2018-08-18 16:38:28 +00:00
|
|
|
|
|
|
|
var I;
|
|
|
|
for (I = 1; I < 16; ++I) {
|
2018-08-12 16:21:57 +00:00
|
|
|
N = 2 * (N + LenCount[I]);
|
2018-08-18 16:38:28 +00:00
|
|
|
M = (N << (15 - I));
|
2018-08-19 08:14:20 +00:00
|
|
|
if (M > 0xFFFF) {
|
2017-09-17 12:51:17 +00:00
|
|
|
M = 0xFFFF;
|
2018-08-18 16:38:28 +00:00
|
|
|
}
|
2017-09-17 12:51:17 +00:00
|
|
|
DecodeLen[I] = M;
|
2018-08-18 16:38:28 +00:00
|
|
|
DecodePos[I] = DecodePos[I - 1] + LenCount[I - 1];
|
2017-09-17 12:51:17 +00:00
|
|
|
TmpPos[I] = DecodePos[I];
|
|
|
|
}
|
2018-08-12 16:21:57 +00:00
|
|
|
for (I = 0; I < size; ++I) {
|
2019-05-13 19:28:06 +00:00
|
|
|
if (BitLength[I + offset] != 0) {
|
|
|
|
DecodeNum[TmpPos[BitLength[offset + I] & 0xF]++] = I;
|
2018-08-12 16:21:57 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-09 23:16:03 +00:00
|
|
|
|
2017-09-17 10:36:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: implement
|
2019-05-09 23:16:03 +00:00
|
|
|
/**
|
|
|
|
* @param {bitjs.io.BitStream} bstream
|
|
|
|
* @param {boolean} Solid
|
|
|
|
*/
|
2019-03-16 16:28:21 +00:00
|
|
|
function unpack15() { //bstream, Solid) {
|
2017-09-17 12:51:17 +00:00
|
|
|
info("ERROR! RAR 1.5 compression not supported");
|
2017-09-17 10:36:32 +00:00
|
|
|
}
|
|
|
|
|
2019-05-09 23:16:03 +00:00
|
|
|
/**
|
|
|
|
* Unpacks the bit stream into rBuffer using the Unpack20 algorithm.
|
|
|
|
* @param {bitjs.io.BitStream} bstream
|
|
|
|
* @param {boolean} Solid
|
|
|
|
*/
|
2019-03-16 16:28:21 +00:00
|
|
|
function unpack20(bstream) { //, Solid) {
|
2017-09-17 12:51:17 +00:00
|
|
|
var destUnpSize = rBuffer.data.length;
|
|
|
|
var oldDistPtr = 0;
|
2018-08-19 08:14:20 +00:00
|
|
|
var Length;
|
|
|
|
var Distance;
|
2018-08-18 16:38:28 +00:00
|
|
|
rarReadTables20(bstream);
|
2017-09-17 12:51:17 +00:00
|
|
|
while (destUnpSize > rBuffer.ptr) {
|
2018-08-18 16:38:28 +00:00
|
|
|
var num = rarDecodeNumber(bstream, LD);
|
|
|
|
var Bits;
|
2017-09-17 12:51:17 +00:00
|
|
|
if (num < 256) {
|
|
|
|
rBuffer.insertByte(num);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (num > 269) {
|
2018-08-19 08:14:20 +00:00
|
|
|
Length = rLDecode[num -= 270] + 3;
|
2017-09-17 12:51:17 +00:00
|
|
|
if ((Bits = rLBits[num]) > 0) {
|
|
|
|
Length += bstream.readBits(Bits);
|
|
|
|
}
|
2018-08-18 16:38:28 +00:00
|
|
|
var DistNumber = rarDecodeNumber(bstream, DD);
|
2018-08-19 08:14:20 +00:00
|
|
|
Distance = rDDecode[DistNumber] + 1;
|
2017-09-17 12:51:17 +00:00
|
|
|
if ((Bits = rDBits[DistNumber]) > 0) {
|
|
|
|
Distance += bstream.readBits(Bits);
|
|
|
|
}
|
|
|
|
if (Distance >= 0x2000) {
|
|
|
|
Length++;
|
2019-05-13 19:28:06 +00:00
|
|
|
if (Distance >= 0x40000) {
|
|
|
|
Length++;
|
|
|
|
}
|
2017-09-17 12:51:17 +00:00
|
|
|
}
|
|
|
|
lastLength = Length;
|
|
|
|
lastDist = rOldDist[oldDistPtr++ & 3] = Distance;
|
2018-08-18 16:38:28 +00:00
|
|
|
rarCopyString(Length, Distance);
|
2017-09-17 12:51:17 +00:00
|
|
|
continue;
|
|
|
|
}
|
2018-08-12 07:29:57 +00:00
|
|
|
if (num === 269) {
|
2018-08-18 16:38:28 +00:00
|
|
|
rarReadTables20(bstream);
|
|
|
|
rarUpdateProgress();
|
2017-09-17 12:51:17 +00:00
|
|
|
continue;
|
|
|
|
}
|
2018-08-12 07:29:57 +00:00
|
|
|
if (num === 256) {
|
2017-09-17 12:51:17 +00:00
|
|
|
lastDist = rOldDist[oldDistPtr++ & 3] = lastDist;
|
2018-08-18 16:38:28 +00:00
|
|
|
rarCopyString(lastLength, lastDist);
|
2017-09-17 12:51:17 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (num < 261) {
|
2018-08-19 08:14:20 +00:00
|
|
|
Distance = rOldDist[(oldDistPtr - (num - 256)) & 3];
|
2018-08-18 16:38:28 +00:00
|
|
|
var LengthNumber = rarDecodeNumber(bstream, RD);
|
2018-08-19 08:14:20 +00:00
|
|
|
Length = rLDecode[LengthNumber] + 2;
|
2017-09-17 12:51:17 +00:00
|
|
|
if ((Bits = rLBits[LengthNumber]) > 0) {
|
|
|
|
Length += bstream.readBits(Bits);
|
|
|
|
}
|
|
|
|
if (Distance >= 0x101) {
|
|
|
|
Length++;
|
|
|
|
if (Distance >= 0x2000) {
|
2019-05-13 19:28:06 +00:00
|
|
|
Length++
|
|
|
|
if (Distance >= 0x40000) {
|
|
|
|
Length++;
|
|
|
|
}
|
2017-09-17 12:51:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
lastLength = Length;
|
|
|
|
lastDist = rOldDist[oldDistPtr++ & 3] = Distance;
|
2018-08-18 16:38:28 +00:00
|
|
|
rarCopyString(Length, Distance);
|
2017-09-17 12:51:17 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (num < 270) {
|
2019-01-08 06:58:22 +00:00
|
|
|
Distance = rSDDecode[num -= 261] + 1;
|
2017-09-17 12:51:17 +00:00
|
|
|
if ((Bits = rSDBits[num]) > 0) {
|
|
|
|
Distance += bstream.readBits(Bits);
|
|
|
|
}
|
|
|
|
lastLength = 2;
|
|
|
|
lastDist = rOldDist[oldDistPtr++ & 3] = Distance;
|
2018-08-18 16:38:28 +00:00
|
|
|
rarCopyString(2, Distance);
|
2017-09-17 12:51:17 +00:00
|
|
|
continue;
|
2017-09-17 10:36:32 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-19 08:14:20 +00:00
|
|
|
rarUpdateProgress();
|
2017-09-17 10:36:32 +00:00
|
|
|
}
|
|
|
|
|
2018-08-18 16:38:28 +00:00
|
|
|
function rarUpdateProgress() {
|
2017-09-17 12:51:17 +00:00
|
|
|
var change = rBuffer.ptr - currentBytesUnarchivedInFile;
|
|
|
|
currentBytesUnarchivedInFile = rBuffer.ptr;
|
|
|
|
currentBytesUnarchived += change;
|
|
|
|
postProgress();
|
2017-09-17 10:36:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var rNC20 = 298,
|
|
|
|
rDC20 = 48,
|
|
|
|
rRC20 = 28,
|
|
|
|
rBC20 = 19,
|
|
|
|
rMC20 = 257;
|
|
|
|
|
|
|
|
var UnpOldTable20 = new Array(rMC20 * 4);
|
|
|
|
|
2018-08-18 16:38:28 +00:00
|
|
|
function rarReadTables20(bstream) {
|
2017-09-17 12:51:17 +00:00
|
|
|
var BitLength = new Array(rBC20);
|
|
|
|
var Table = new Array(rMC20 * 4);
|
|
|
|
var TableSize, N, I;
|
2019-01-08 06:58:22 +00:00
|
|
|
var i;
|
2018-08-19 08:14:20 +00:00
|
|
|
bstream.readBits(1);
|
|
|
|
if (!bstream.readBits(1)) {
|
2019-05-31 06:43:51 +00:00
|
|
|
for (i = UnpOldTable20.length; i--;) {
|
2019-05-13 19:28:06 +00:00
|
|
|
UnpOldTable20[i] = 0;
|
|
|
|
}
|
2018-08-19 08:14:20 +00:00
|
|
|
}
|
2017-09-17 12:51:17 +00:00
|
|
|
TableSize = rNC20 + rDC20 + rRC20;
|
2018-08-19 08:14:20 +00:00
|
|
|
for (I = 0; I < rBC20; I++) {
|
|
|
|
BitLength[I] = bstream.readBits(4);
|
|
|
|
}
|
2018-08-12 16:21:57 +00:00
|
|
|
rarMakeDecodeTables(BitLength, 0, BD, rBC20);
|
2017-09-17 12:51:17 +00:00
|
|
|
I = 0;
|
|
|
|
while (I < TableSize) {
|
2018-08-18 16:38:28 +00:00
|
|
|
var num = rarDecodeNumber(bstream, BD);
|
2017-09-17 12:51:17 +00:00
|
|
|
if (num < 16) {
|
2018-08-19 08:14:20 +00:00
|
|
|
Table[I] = num + UnpOldTable20[I] & 0xf;
|
|
|
|
I++;
|
|
|
|
} else if (num === 16) {
|
2017-09-17 12:51:17 +00:00
|
|
|
N = bstream.readBits(2) + 3;
|
|
|
|
while (N-- > 0 && I < TableSize) {
|
|
|
|
Table[I] = Table[I - 1];
|
|
|
|
I++;
|
|
|
|
}
|
|
|
|
} else {
|
2018-08-12 07:29:57 +00:00
|
|
|
if (num === 17) {
|
2017-09-17 12:51:17 +00:00
|
|
|
N = bstream.readBits(3) + 3;
|
|
|
|
} else {
|
|
|
|
N = bstream.readBits(7) + 11;
|
|
|
|
}
|
|
|
|
while (N-- > 0 && I < TableSize) {
|
|
|
|
Table[I++] = 0;
|
|
|
|
}
|
|
|
|
}
|
2017-09-17 10:36:32 +00:00
|
|
|
}
|
2018-08-12 16:21:57 +00:00
|
|
|
rarMakeDecodeTables(Table, 0, LD, rNC20);
|
|
|
|
rarMakeDecodeTables(Table, rNC20, DD, rDC20);
|
|
|
|
rarMakeDecodeTables(Table, rNC20 + rDC20, RD, rRC20);
|
2019-05-31 06:43:51 +00:00
|
|
|
for (i = UnpOldTable20.length; i--;) {
|
2019-05-13 19:28:06 +00:00
|
|
|
UnpOldTable20[i] = Table[i];
|
2019-05-09 23:16:03 +00:00
|
|
|
}
|
2017-09-17 10:36:32 +00:00
|
|
|
}
|
|
|
|
|
2019-05-13 05:53:25 +00:00
|
|
|
var lowDistRepCount = 0;
|
|
|
|
var prevLowDist = 0;
|
2019-05-09 23:16:03 +00:00
|
|
|
|
2019-05-13 19:28:06 +00:00
|
|
|
var rOldDist = [0, 0, 0, 0];
|
2019-05-13 05:53:25 +00:00
|
|
|
var lastDist;
|
|
|
|
var lastLength;
|
2019-05-09 23:16:03 +00:00
|
|
|
|
|
|
|
// ============================================================================================== //
|
|
|
|
|
|
|
|
// Unpack code specific to RarVM
|
2019-05-13 05:53:25 +00:00
|
|
|
var VM = new RarVM();
|
2019-05-09 23:16:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Filters code, one entry per filter.
|
|
|
|
* @type {Array<UnpackFilter>}
|
|
|
|
*/
|
2019-05-13 05:53:25 +00:00
|
|
|
var Filters = [];
|
2019-05-09 23:16:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Filters stack, several entrances of same filter are possible.
|
|
|
|
* @type {Array<UnpackFilter>}
|
|
|
|
*/
|
2019-05-13 05:53:25 +00:00
|
|
|
var PrgStack = [];
|
2019-05-09 23:16:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Lengths of preceding blocks, one length per filter. Used to reduce
|
|
|
|
* size required to write block length if lengths are repeating.
|
|
|
|
* @type {Array<number>}
|
|
|
|
*/
|
2019-05-13 05:53:25 +00:00
|
|
|
var OldFilterLengths = [];
|
2019-05-09 23:16:03 +00:00
|
|
|
|
2019-05-13 05:53:25 +00:00
|
|
|
var LastFilter = 0;
|
2019-05-09 23:16:03 +00:00
|
|
|
|
|
|
|
function InitFilters() {
|
2019-05-13 19:28:06 +00:00
|
|
|
OldFilterLengths = [];
|
|
|
|
LastFilter = 0;
|
|
|
|
Filters = [];
|
|
|
|
PrgStack = [];
|
2019-05-09 23:16:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {number} firstByte The first byte (flags).
|
|
|
|
* @param {Uint8Array} vmCode An array of bytes.
|
|
|
|
*/
|
2019-05-31 06:43:51 +00:00
|
|
|
function rarAddVMCode(firstByte, vmCode) {
|
2019-05-13 19:28:06 +00:00
|
|
|
VM.init();
|
|
|
|
var bstream = new bitjs.io.BitStream(vmCode.buffer, true /* rtl */ );
|
|
|
|
|
|
|
|
var filtPos;
|
|
|
|
if (firstByte & 0x80) {
|
|
|
|
filtPos = RarVM.readData(bstream);
|
|
|
|
if (filtPos == 0) {
|
|
|
|
InitFilters();
|
|
|
|
} else {
|
|
|
|
filtPos--;
|
|
|
|
}
|
2019-05-09 23:16:03 +00:00
|
|
|
} else {
|
2019-05-13 19:28:06 +00:00
|
|
|
filtPos = LastFilter;
|
2019-05-09 23:16:03 +00:00
|
|
|
}
|
2019-05-13 19:28:06 +00:00
|
|
|
|
|
|
|
if (filtPos > Filters.length || filtPos > OldFilterLengths.length) {
|
|
|
|
return false;
|
2019-05-09 23:16:03 +00:00
|
|
|
}
|
|
|
|
|
2019-05-13 19:28:06 +00:00
|
|
|
LastFilter = filtPos;
|
|
|
|
var newFilter = (filtPos == Filters.length);
|
|
|
|
|
|
|
|
// new filter for PrgStack
|
|
|
|
var stackFilter = new UnpackFilter();
|
|
|
|
var filter = null;
|
|
|
|
// new filter code, never used before since VM reset
|
|
|
|
if (newFilter) {
|
|
|
|
// too many different filters, corrupt archive
|
|
|
|
if (filtPos > 1024) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
filter = new UnpackFilter();
|
|
|
|
Filters.push(filter);
|
|
|
|
stackFilter.ParentFilter = (Filters.length - 1);
|
|
|
|
OldFilterLengths.push(0); // OldFilterLengths.Add(1)
|
|
|
|
filter.ExecCount = 0;
|
|
|
|
} else { // filter was used in the past
|
|
|
|
filter = Filters[filtPos];
|
|
|
|
stackFilter.ParentFilter = filtPos;
|
|
|
|
filter.ExecCount++;
|
2019-05-09 23:16:03 +00:00
|
|
|
}
|
2019-05-13 19:28:06 +00:00
|
|
|
|
|
|
|
var emptyCount = 0;
|
|
|
|
for (var i = 0; i < PrgStack.length; ++i) {
|
|
|
|
PrgStack[i - emptyCount] = PrgStack[i];
|
|
|
|
|
|
|
|
if (PrgStack[i] == null) {
|
|
|
|
emptyCount++;
|
|
|
|
}
|
|
|
|
if (emptyCount > 0) {
|
|
|
|
PrgStack[i] = null;
|
|
|
|
}
|
2019-05-09 23:16:03 +00:00
|
|
|
}
|
2019-05-13 19:28:06 +00:00
|
|
|
|
|
|
|
if (emptyCount == 0) {
|
|
|
|
PrgStack.push(null); //PrgStack.Add(1);
|
|
|
|
emptyCount = 1;
|
2019-05-09 23:16:03 +00:00
|
|
|
}
|
|
|
|
|
2019-05-13 19:28:06 +00:00
|
|
|
var stackPos = PrgStack.length - emptyCount;
|
|
|
|
PrgStack[stackPos] = stackFilter;
|
|
|
|
stackFilter.ExecCount = filter.ExecCount;
|
|
|
|
|
|
|
|
var blockStart = RarVM.readData(bstream);
|
|
|
|
if (firstByte & 0x40) {
|
|
|
|
blockStart += 258;
|
2019-05-09 23:16:03 +00:00
|
|
|
}
|
2019-05-13 19:28:06 +00:00
|
|
|
stackFilter.BlockStart = (blockStart + rBuffer.ptr) & MAXWINMASK;
|
|
|
|
|
|
|
|
if (firstByte & 0x20) {
|
|
|
|
stackFilter.BlockLength = RarVM.readData(bstream);
|
|
|
|
} else {
|
|
|
|
stackFilter.BlockLength = filtPos < OldFilterLengths.length ?
|
|
|
|
OldFilterLengths[filtPos] :
|
|
|
|
0;
|
2019-05-09 23:16:03 +00:00
|
|
|
}
|
2019-05-13 19:28:06 +00:00
|
|
|
stackFilter.NextWindow = (wBuffer.ptr != rBuffer.ptr) &&
|
|
|
|
(((wBuffer.ptr - rBuffer.ptr) & MAXWINMASK) <= blockStart);
|
|
|
|
|
|
|
|
OldFilterLengths[filtPos] = stackFilter.BlockLength;
|
|
|
|
|
|
|
|
for (var i = 0; i < 7; ++i) {
|
|
|
|
stackFilter.Prg.InitR[i] = 0;
|
2019-05-09 23:16:03 +00:00
|
|
|
}
|
2019-05-13 19:28:06 +00:00
|
|
|
stackFilter.Prg.InitR[3] = VM_GLOBALMEMADDR;
|
|
|
|
stackFilter.Prg.InitR[4] = stackFilter.BlockLength;
|
|
|
|
stackFilter.Prg.InitR[5] = stackFilter.ExecCount;
|
|
|
|
|
|
|
|
// set registers to optional parameters if any
|
|
|
|
if (firstByte & 0x10) {
|
|
|
|
var initMask = bstream.readBits(7);
|
|
|
|
for (var i = 0; i < 7; ++i) {
|
|
|
|
if (initMask & (1 << i)) {
|
|
|
|
stackFilter.Prg.InitR[i] = RarVM.readData(bstream);
|
|
|
|
}
|
|
|
|
}
|
2019-05-09 23:16:03 +00:00
|
|
|
}
|
|
|
|
|
2019-05-13 19:28:06 +00:00
|
|
|
if (newFilter) {
|
|
|
|
var vmCodeSize = RarVM.readData(bstream);
|
|
|
|
if (vmCodeSize >= 0x10000 || vmCodeSize == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
var vmCode = new Uint8Array(vmCodeSize);
|
|
|
|
for (var i = 0; i < vmCodeSize; ++i) {
|
|
|
|
//if (Inp.Overflow(3))
|
|
|
|
// return(false);
|
|
|
|
vmCode[i] = bstream.readBits(8);
|
|
|
|
}
|
|
|
|
VM.prepare(vmCode, filter.Prg);
|
|
|
|
}
|
|
|
|
stackFilter.Prg.Cmd = filter.Prg.Cmd;
|
|
|
|
stackFilter.Prg.AltCmd = filter.Prg.Cmd;
|
|
|
|
|
|
|
|
var staticDataSize = filter.Prg.StaticData.length;
|
|
|
|
if (staticDataSize > 0 && staticDataSize < VM_GLOBALMEMSIZE) {
|
|
|
|
// read statically defined data contained in DB commands
|
|
|
|
for (var i = 0; i < staticDataSize; ++i) {
|
|
|
|
stackFilter.Prg.StaticData[i] = filter.Prg.StaticData[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stackFilter.Prg.GlobalData.length < VM_FIXEDGLOBALSIZE) {
|
|
|
|
stackFilter.Prg.GlobalData = new Uint8Array(VM_FIXEDGLOBALSIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
var globalData = stackFilter.Prg.GlobalData;
|
|
|
|
for (var i = 0; i < 7; ++i) {
|
|
|
|
VM.setLowEndianValue(globalData, stackFilter.Prg.InitR[i], i * 4);
|
|
|
|
}
|
2019-05-09 23:16:03 +00:00
|
|
|
|
2019-05-13 19:28:06 +00:00
|
|
|
VM.setLowEndianValue(globalData, stackFilter.BlockLength, 0x1c);
|
|
|
|
VM.setLowEndianValue(globalData, 0, 0x20);
|
|
|
|
VM.setLowEndianValue(globalData, stackFilter.ExecCount, 0x2c);
|
|
|
|
for (var i = 0; i < 16; ++i) {
|
|
|
|
globalData[0x30 + i] = 0;
|
2019-05-09 23:16:03 +00:00
|
|
|
}
|
2019-05-13 19:28:06 +00:00
|
|
|
|
|
|
|
// put data block passed as parameter if any
|
|
|
|
if (firstByte & 8) {
|
|
|
|
//if (Inp.Overflow(3))
|
|
|
|
// return(false);
|
|
|
|
var dataSize = RarVM.readData(bstream);
|
|
|
|
if (dataSize > (VM_GLOBALMEMSIZE - VM_FIXEDGLOBALSIZE)) {
|
|
|
|
return (false);
|
|
|
|
}
|
2017-09-17 10:36:32 +00:00
|
|
|
|
2019-05-13 19:28:06 +00:00
|
|
|
var curSize = stackFilter.Prg.GlobalData.length;
|
|
|
|
if (curSize < dataSize + VM_FIXEDGLOBALSIZE) {
|
|
|
|
// Resize global data and update the stackFilter and local variable.
|
|
|
|
var numBytesToAdd = dataSize + VM_FIXEDGLOBALSIZE - curSize;
|
|
|
|
var newGlobalData = new Uint8Array(globalData.length + numBytesToAdd);
|
|
|
|
newGlobalData.set(globalData);
|
|
|
|
|
|
|
|
stackFilter.Prg.GlobalData = newGlobalData;
|
|
|
|
globalData = newGlobalData;
|
|
|
|
}
|
|
|
|
//byte *GlobalData=&StackFilter->Prg.GlobalData[VM_FIXEDGLOBALSIZE];
|
|
|
|
for (var i = 0; i < dataSize; ++i) {
|
|
|
|
//if (Inp.Overflow(3))
|
|
|
|
// return(false);
|
|
|
|
globalData[VM_FIXEDGLOBALSIZE + i] = bstream.readBits(8);
|
|
|
|
}
|
2019-05-09 23:16:03 +00:00
|
|
|
}
|
|
|
|
|
2019-05-13 19:28:06 +00:00
|
|
|
return true;
|
2017-09-17 10:36:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-09 23:16:03 +00:00
|
|
|
/**
|
|
|
|
* @param {!bitjs.io.BitStream} bstream
|
|
|
|
*/
|
2019-05-31 06:43:51 +00:00
|
|
|
function rarReadVMCode(bstream) {
|
2019-05-13 19:28:06 +00:00
|
|
|
var firstByte = bstream.readBits(8);
|
|
|
|
var length = (firstByte & 7) + 1;
|
|
|
|
if (length == 7) {
|
|
|
|
length = bstream.readBits(8) + 7;
|
|
|
|
} else if (length == 8) {
|
|
|
|
length = bstream.readBits(16);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read all bytes of VM code into an array.
|
|
|
|
var vmCode = new Uint8Array(length);
|
|
|
|
for (var i = 0; i < length; i++) {
|
|
|
|
// Do something here with checking readbuf.
|
|
|
|
vmCode[i] = bstream.readBits(8);
|
|
|
|
}
|
|
|
|
return RarAddVMCode(firstByte, vmCode);
|
2019-05-09 23:16:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unpacks the bit stream into rBuffer using the Unpack29 algorithm.
|
|
|
|
* @param {bitjs.io.BitStream} bstream
|
|
|
|
* @param {boolean} Solid
|
|
|
|
*/
|
2019-03-16 16:28:21 +00:00
|
|
|
function unpack29(bstream) {
|
2017-09-17 12:51:17 +00:00
|
|
|
// lazy initialize rDDecode and rDBits
|
2017-09-17 10:36:32 +00:00
|
|
|
|
2017-09-17 12:51:17 +00:00
|
|
|
var DDecode = new Array(rDC);
|
|
|
|
var DBits = new Array(rDC);
|
2019-01-08 06:58:22 +00:00
|
|
|
var Distance = 0;
|
|
|
|
var Length = 0;
|
2018-08-19 08:14:20 +00:00
|
|
|
var Dist = 0, BitLength = 0, Slot = 0;
|
|
|
|
var I;
|
2019-01-08 06:58:22 +00:00
|
|
|
for (I = 0; I < rDBitLengthCounts.length; I++, BitLength++) {
|
|
|
|
for (var J = 0; J < rDBitLengthCounts[I]; J++, Slot++, Dist += (1 << BitLength)) {
|
|
|
|
DDecode[Slot] = Dist;
|
|
|
|
DBits[Slot] = BitLength;
|
2017-09-17 12:51:17 +00:00
|
|
|
}
|
2017-09-17 10:36:32 +00:00
|
|
|
}
|
2019-01-14 19:27:53 +00:00
|
|
|
|
2017-09-17 12:51:17 +00:00
|
|
|
var Bits;
|
|
|
|
//tablesRead = false;
|
2017-09-17 10:36:32 +00:00
|
|
|
|
2019-01-08 06:58:22 +00:00
|
|
|
rOldDist = [0, 0, 0, 0];
|
2017-09-17 10:36:32 +00:00
|
|
|
|
2017-09-17 12:51:17 +00:00
|
|
|
lastDist = 0;
|
|
|
|
lastLength = 0;
|
2018-08-19 08:14:20 +00:00
|
|
|
var i;
|
2019-05-31 06:43:51 +00:00
|
|
|
for (i = UnpOldTable.length; i--;) {
|
2019-05-13 19:28:06 +00:00
|
|
|
UnpOldTable[i] = 0;
|
2019-05-09 23:16:03 +00:00
|
|
|
}
|
2019-01-14 19:27:53 +00:00
|
|
|
|
2017-09-17 12:51:17 +00:00
|
|
|
// read in Huffman tables
|
2019-01-08 06:58:22 +00:00
|
|
|
rarReadTables(bstream);
|
2019-01-14 19:27:53 +00:00
|
|
|
|
2017-09-17 12:51:17 +00:00
|
|
|
while (true) {
|
2018-08-18 16:38:28 +00:00
|
|
|
var num = rarDecodeNumber(bstream, LD);
|
2019-01-14 19:27:53 +00:00
|
|
|
|
2017-09-17 12:51:17 +00:00
|
|
|
if (num < 256) {
|
|
|
|
rBuffer.insertByte(num);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (num >= 271) {
|
2019-01-08 06:58:22 +00:00
|
|
|
Length = rLDecode[num -= 271] + 3;
|
2017-09-17 12:51:17 +00:00
|
|
|
if ((Bits = rLBits[num]) > 0) {
|
|
|
|
Length += bstream.readBits(Bits);
|
2017-09-17 10:36:32 +00:00
|
|
|
}
|
2018-08-18 16:38:28 +00:00
|
|
|
var DistNumber = rarDecodeNumber(bstream, DD);
|
2019-01-08 06:58:22 +00:00
|
|
|
Distance = DDecode[DistNumber] + 1;
|
2017-09-17 12:51:17 +00:00
|
|
|
if ((Bits = DBits[DistNumber]) > 0) {
|
|
|
|
if (DistNumber > 9) {
|
|
|
|
if (Bits > 4) {
|
|
|
|
Distance += ((bstream.getBits() >>> (20 - Bits)) << 4);
|
|
|
|
bstream.readBits(Bits - 4);
|
|
|
|
//todo: check this
|
|
|
|
}
|
|
|
|
if (lowDistRepCount > 0) {
|
|
|
|
lowDistRepCount--;
|
|
|
|
Distance += prevLowDist;
|
|
|
|
} else {
|
2018-08-18 16:38:28 +00:00
|
|
|
var LowDist = rarDecodeNumber(bstream, LDD);
|
2018-08-12 07:29:57 +00:00
|
|
|
if (LowDist === 16) {
|
2018-08-12 16:21:57 +00:00
|
|
|
lowDistRepCount = rLowDistRepCount - 1;
|
2017-09-17 12:51:17 +00:00
|
|
|
Distance += prevLowDist;
|
|
|
|
} else {
|
|
|
|
Distance += LowDist;
|
|
|
|
prevLowDist = LowDist;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Distance += bstream.readBits(Bits);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (Distance >= 0x2000) {
|
|
|
|
Length++;
|
|
|
|
if (Distance >= 0x40000) {
|
|
|
|
Length++;
|
|
|
|
}
|
|
|
|
}
|
2019-01-08 06:58:22 +00:00
|
|
|
rarInsertOldDist(Distance);
|
|
|
|
rarInsertLastMatch(Length, Distance);
|
2018-08-18 16:38:28 +00:00
|
|
|
rarCopyString(Length, Distance);
|
2017-09-17 12:51:17 +00:00
|
|
|
continue;
|
2017-09-17 10:36:32 +00:00
|
|
|
}
|
2018-08-12 07:29:57 +00:00
|
|
|
if (num === 256) {
|
2019-05-31 06:43:51 +00:00
|
|
|
if (!rarReadEndOfBlock(bstream)) {
|
2019-05-13 19:28:06 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-09-17 12:51:17 +00:00
|
|
|
continue;
|
|
|
|
}
|
2018-08-12 07:29:57 +00:00
|
|
|
if (num === 257) {
|
2019-05-31 06:43:51 +00:00
|
|
|
if (!rarReadVMCode(bstream)) {
|
2019-05-13 19:28:06 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-01-08 06:58:22 +00:00
|
|
|
continue;
|
2017-09-17 12:51:17 +00:00
|
|
|
}
|
2018-08-12 07:29:57 +00:00
|
|
|
if (num === 258) {
|
2019-03-24 14:58:43 +00:00
|
|
|
if (lastLength !== 0) {
|
2018-08-18 16:38:28 +00:00
|
|
|
rarCopyString(lastLength, lastDist);
|
2017-09-17 12:51:17 +00:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (num < 263) {
|
|
|
|
var DistNum = num - 259;
|
2019-01-08 06:58:22 +00:00
|
|
|
Distance = rOldDist[DistNum];
|
2017-09-17 12:51:17 +00:00
|
|
|
|
2019-03-16 16:28:21 +00:00
|
|
|
for (var I2 = DistNum; I2 > 0; I2--) {
|
|
|
|
rOldDist[I2] = rOldDist[I2 - 1];
|
2017-09-17 12:51:17 +00:00
|
|
|
}
|
|
|
|
rOldDist[0] = Distance;
|
|
|
|
|
2018-08-18 16:38:28 +00:00
|
|
|
var LengthNumber = rarDecodeNumber(bstream, RD);
|
2019-01-08 06:58:22 +00:00
|
|
|
Length = rLDecode[LengthNumber] + 2;
|
2017-09-17 12:51:17 +00:00
|
|
|
if ((Bits = rLBits[LengthNumber]) > 0) {
|
|
|
|
Length += bstream.readBits(Bits);
|
|
|
|
}
|
2019-01-08 06:58:22 +00:00
|
|
|
rarInsertLastMatch(Length, Distance);
|
2018-08-18 16:38:28 +00:00
|
|
|
rarCopyString(Length, Distance);
|
2017-09-17 12:51:17 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (num < 272) {
|
2019-01-08 06:58:22 +00:00
|
|
|
Distance = rSDDecode[num -= 263] + 1;
|
2017-09-17 12:51:17 +00:00
|
|
|
if ((Bits = rSDBits[num]) > 0) {
|
|
|
|
Distance += bstream.readBits(Bits);
|
|
|
|
}
|
2019-01-08 06:58:22 +00:00
|
|
|
rarInsertOldDist(Distance);
|
|
|
|
rarInsertLastMatch(2, Distance);
|
2018-08-18 16:38:28 +00:00
|
|
|
rarCopyString(2, Distance);
|
2017-09-17 12:51:17 +00:00
|
|
|
continue;
|
2017-09-17 10:36:32 +00:00
|
|
|
}
|
2019-05-13 19:28:06 +00:00
|
|
|
} // while (true)
|
2019-01-14 19:27:53 +00:00
|
|
|
rarUpdateProgress();
|
2019-05-31 06:43:51 +00:00
|
|
|
rarWriteBuf();
|
2017-09-17 10:36:32 +00:00
|
|
|
}
|
|
|
|
|
2019-05-13 19:28:06 +00:00
|
|
|
/**
|
|
|
|
* Does stuff to the current byte buffer (rBuffer) based on
|
|
|
|
* the filters loaded into the RarVM and writes out to wBuffer.
|
|
|
|
*/
|
2019-05-31 06:43:51 +00:00
|
|
|
function rarWriteBuf() {
|
2019-05-13 19:28:06 +00:00
|
|
|
var writeSize = (rBuffer.ptr & MAXWINMASK);
|
2019-05-09 23:16:03 +00:00
|
|
|
|
2019-05-13 19:28:06 +00:00
|
|
|
for (var i = 0; i < PrgStack.length; ++i) {
|
|
|
|
var flt = PrgStack[i];
|
|
|
|
if (flt == null) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-05-09 23:16:03 +00:00
|
|
|
|
2019-05-13 19:28:06 +00:00
|
|
|
if (flt.NextWindow) {
|
|
|
|
flt.NextWindow = false;
|
|
|
|
continue;
|
|
|
|
}
|
2019-05-09 23:16:03 +00:00
|
|
|
|
2019-05-13 19:28:06 +00:00
|
|
|
var blockStart = flt.BlockStart;
|
|
|
|
var blockLength = flt.BlockLength;
|
2019-05-09 23:16:03 +00:00
|
|
|
|
2019-05-13 19:28:06 +00:00
|
|
|
// WrittenBorder = wBuffer.ptr
|
|
|
|
if (((blockStart - wBuffer.ptr) & MAXWINMASK) < writeSize) {
|
|
|
|
if (wBuffer.ptr != blockStart) {
|
|
|
|
// Copy blockStart bytes from rBuffer into wBuffer.
|
2019-05-31 06:43:51 +00:00
|
|
|
rarWriteArea(wBuffer.ptr, blockStart);
|
2019-05-13 19:28:06 +00:00
|
|
|
writeSize = (rBuffer.ptr - wBuffer.ptr) & MAXWINMASK;
|
2019-05-09 23:16:03 +00:00
|
|
|
}
|
2019-05-13 19:28:06 +00:00
|
|
|
if (blockLength <= writeSize) {
|
|
|
|
var blockEnd = (blockStart + blockLength) & MAXWINMASK;
|
|
|
|
if (blockStart < blockEnd || blockEnd == 0) {
|
|
|
|
VM.setMemory(0, rBuffer.data.subarray(blockStart, blockStart + blockLength), blockLength);
|
|
|
|
} else {
|
|
|
|
var firstPartLength = MAXWINSIZE - blockStart;
|
|
|
|
VM.setMemory(0, rBuffer.data.subarray(blockStart, blockStart + firstPartLength), firstPartLength);
|
|
|
|
VM.setMemory(firstPartLength, rBuffer.data, blockEnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
var parentPrg = Filters[flt.ParentFilter].Prg;
|
|
|
|
var prg = flt.Prg;
|
|
|
|
|
|
|
|
if (parentPrg.GlobalData.length > VM_FIXEDGLOBALSIZE) {
|
|
|
|
// Copy global data from previous script execution if any.
|
|
|
|
prg.GlobalData = new Uint8Array(parentPrg.GlobalData);
|
|
|
|
}
|
|
|
|
|
2019-05-31 06:43:51 +00:00
|
|
|
rarExecuteCode(prg);
|
2019-05-13 19:28:06 +00:00
|
|
|
|
|
|
|
if (prg.GlobalData.length > VM_FIXEDGLOBALSIZE) {
|
|
|
|
// Save global data for next script execution.
|
|
|
|
var globalDataLen = prg.GlobalData.length;
|
|
|
|
if (parentPrg.GlobalData.length < globalDataLen) {
|
|
|
|
parentPrg.GlobalData = new Uint8Array(globalDataLen);
|
|
|
|
}
|
|
|
|
parentPrg.GlobalData.set(
|
|
|
|
this.mem_.subarray(VM_FIXEDGLOBALSIZE, VM_FIXEDGLOBALSIZE + globalDataLen),
|
|
|
|
VM_FIXEDGLOBALSIZE);
|
|
|
|
} else {
|
|
|
|
parentPrg.GlobalData = new Uint8Array(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
var filteredData = prg.FilteredData;
|
|
|
|
|
|
|
|
PrgStack[i] = null;
|
|
|
|
while (i + 1 < PrgStack.length) {
|
|
|
|
var nextFilter = PrgStack[i + 1];
|
|
|
|
if (nextFilter == null || nextFilter.BlockStart != blockStart ||
|
|
|
|
nextFilter.BlockLength != filteredData.length || nextFilter.NextWindow) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply several filters to same data block.
|
|
|
|
|
|
|
|
VM.setMemory(0, filteredData, filteredData.length);
|
|
|
|
|
|
|
|
var parentPrg = Filters[nextFilter.ParentFilter].Prg;
|
|
|
|
var nextPrg = nextFilter.Prg;
|
|
|
|
|
|
|
|
var globalDataLen = parentPrg.GlobalData.length;
|
|
|
|
if (globalDataLen > VM_FIXEDGLOBALSIZE) {
|
|
|
|
// Copy global data from previous script execution if any.
|
|
|
|
nextPrg.GlobalData = new Uint8Array(globalDataLen);
|
|
|
|
nextPrg.GlobalData.set(parentPrg.GlobalData.subarray(VM_FIXEDGLOBALSIZE, VM_FIXEDGLOBALSIZE + globalDataLen), VM_FIXEDGLOBALSIZE);
|
|
|
|
}
|
2019-01-14 19:27:53 +00:00
|
|
|
|
2019-05-31 06:43:51 +00:00
|
|
|
rarExecuteCode(nextPrg);
|
2019-05-13 19:28:06 +00:00
|
|
|
|
|
|
|
if (nextPrg.GlobalData.length > VM_GLOBALMEMSIZE) {
|
|
|
|
// Save global data for next script execution.
|
|
|
|
var globalDataLen = nextPrg.GlobalData.length;
|
|
|
|
if (parentPrg.GlobalData.length < globalDataLen) {
|
|
|
|
parentPrg.GlobalData = new Uint8Array(globalDataLen);
|
|
|
|
}
|
|
|
|
parentPrg.GlobalData.set(
|
|
|
|
this.mem_.subarray(VM_FIXEDGLOBALSIZE, VM_FIXEDGLOBALSIZE + globalDataLen),
|
|
|
|
VM_FIXEDGLOBALSIZE);
|
|
|
|
} else {
|
|
|
|
parentPrg.GlobalData = new Uint8Array(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
filteredData = nextPrg.FilteredData;
|
|
|
|
i++;
|
|
|
|
PrgStack[i] = null;
|
|
|
|
} // while (i + 1 < PrgStack.length)
|
|
|
|
|
|
|
|
for (var j = 0; j < filteredData.length; ++j) {
|
|
|
|
wBuffer.insertByte(filteredData[j]);
|
|
|
|
}
|
|
|
|
writeSize = (rBuffer.ptr - wBuffer.ptr) & MAXWINMASK;
|
|
|
|
} // if (blockLength <= writeSize)
|
|
|
|
else {
|
|
|
|
for (var j = i; j < PrgStack.length; ++j) {
|
|
|
|
var flt = PrgStack[j];
|
|
|
|
if (flt != null && flt.NextWindow) {
|
|
|
|
flt.NextWindow = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//WrPtr=WrittenBorder;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} // if (((blockStart - wBuffer.ptr) & MAXWINMASK) < writeSize)
|
|
|
|
} // for (var i = 0; i < PrgStack.length; ++i)
|
2019-05-09 23:16:03 +00:00
|
|
|
|
2019-05-13 19:28:06 +00:00
|
|
|
// Write any remaining bytes from rBuffer to wBuffer;
|
2019-05-31 06:43:51 +00:00
|
|
|
rarWriteArea(wBuffer.ptr, rBuffer.ptr);
|
2019-05-09 23:16:03 +00:00
|
|
|
|
2019-05-13 19:28:06 +00:00
|
|
|
// Now that the filtered buffer has been written, swap it back to rBuffer.
|
|
|
|
rBuffer = wBuffer;
|
2019-05-09 23:16:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy bytes from rBuffer to wBuffer.
|
|
|
|
* @param {number} startPtr The starting point to copy from rBuffer.
|
|
|
|
* @param {number} endPtr The ending point to copy from rBuffer.
|
|
|
|
*/
|
2019-05-31 06:43:51 +00:00
|
|
|
function rarWriteArea(startPtr, endPtr) {
|
2019-05-13 19:28:06 +00:00
|
|
|
if (endPtr < startPtr) {
|
|
|
|
console.error('endPtr < startPtr, endPtr=' + endPtr + ', startPtr=' + startPtr);
|
2019-05-31 06:43:51 +00:00
|
|
|
// rarWriteData(startPtr, -(int)StartPtr & MAXWINMASK);
|
2019-05-13 19:28:06 +00:00
|
|
|
// RarWriteData(0, endPtr);
|
|
|
|
return;
|
|
|
|
} else if (startPtr < endPtr) {
|
2019-05-31 06:43:51 +00:00
|
|
|
rarWriteData(startPtr, endPtr - startPtr);
|
2019-05-13 19:28:06 +00:00
|
|
|
}
|
2019-05-09 23:16:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Writes bytes into wBuffer from rBuffer.
|
|
|
|
* @param {number} offset The starting point to copy bytes from rBuffer.
|
|
|
|
* @param {number} numBytes The number of bytes to copy.
|
|
|
|
*/
|
2019-05-31 06:43:51 +00:00
|
|
|
function rarWriteData(offset, numBytes) {
|
2019-05-13 19:28:06 +00:00
|
|
|
if (wBuffer.ptr >= rBuffer.data.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var leftToWrite = rBuffer.data.length - wBuffer.ptr;
|
|
|
|
if (numBytes > leftToWrite) {
|
|
|
|
numBytes = leftToWrite;
|
|
|
|
}
|
|
|
|
for (var i = 0; i < numBytes; ++i) {
|
|
|
|
wBuffer.insertByte(rBuffer.data[offset + i]);
|
|
|
|
}
|
2019-05-09 23:16:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {VM_PreparedProgram} prg
|
|
|
|
*/
|
2019-05-31 06:43:51 +00:00
|
|
|
function rarExecuteCode(prg) {
|
2019-05-13 19:28:06 +00:00
|
|
|
if (prg.GlobalData.length > 0) {
|
|
|
|
var writtenFileSize = wBuffer.ptr;
|
|
|
|
prg.InitR[6] = writtenFileSize;
|
|
|
|
VM.setLowEndianValue(prg.GlobalData, writtenFileSize, 0x24);
|
|
|
|
VM.setLowEndianValue(prg.GlobalData, (writtenFileSize >>> 32) >> 0, 0x28);
|
|
|
|
VM.execute(prg);
|
|
|
|
}
|
2019-05-09 23:16:03 +00:00
|
|
|
}
|
2019-01-14 19:27:53 +00:00
|
|
|
|
2019-05-31 06:43:51 +00:00
|
|
|
function rarReadEndOfBlock(bstream) {
|
2019-01-08 06:58:22 +00:00
|
|
|
rarUpdateProgress();
|
2017-09-17 12:51:17 +00:00
|
|
|
|
2019-05-13 19:28:06 +00:00
|
|
|
var NewTable = false,
|
|
|
|
NewFile = false;
|
2017-09-17 12:51:17 +00:00
|
|
|
if (bstream.readBits(1)) {
|
|
|
|
NewTable = true;
|
|
|
|
} else {
|
|
|
|
NewFile = true;
|
|
|
|
NewTable = !!bstream.readBits(1);
|
|
|
|
}
|
|
|
|
//tablesRead = !NewTable;
|
2019-03-24 14:58:43 +00:00
|
|
|
return !(NewFile || (NewTable && !rarReadTables(bstream)));
|
2017-09-17 10:36:32 +00:00
|
|
|
}
|
|
|
|
|
2019-01-08 06:58:22 +00:00
|
|
|
function rarInsertLastMatch(length, distance) {
|
2017-09-17 12:51:17 +00:00
|
|
|
lastDist = distance;
|
|
|
|
lastLength = length;
|
2017-09-17 10:36:32 +00:00
|
|
|
}
|
|
|
|
|
2019-01-08 06:58:22 +00:00
|
|
|
function rarInsertOldDist(distance) {
|
2019-01-14 19:27:53 +00:00
|
|
|
rOldDist.splice(3, 1);
|
|
|
|
rOldDist.splice(0, 0, distance);
|
2017-09-17 10:36:32 +00:00
|
|
|
}
|
|
|
|
|
2019-05-09 23:16:03 +00:00
|
|
|
/**
|
|
|
|
* Copies len bytes from distance bytes ago in the buffer to the end of the
|
|
|
|
* current byte buffer.
|
|
|
|
* @param {number} length How many bytes to copy.
|
|
|
|
* @param {number} distance How far back in the buffer from the current write
|
|
|
|
* pointer to start copying from.
|
|
|
|
*/
|
2018-08-18 16:38:28 +00:00
|
|
|
function rarCopyString(length, distance) {
|
2019-05-13 19:28:06 +00:00
|
|
|
var srcPtr = rBuffer.ptr - distance;
|
|
|
|
if (srcPtr < 0) {
|
2017-09-17 12:51:17 +00:00
|
|
|
var l = rOldBuffers.length;
|
2019-05-13 19:28:06 +00:00
|
|
|
while (srcPtr < 0) {
|
|
|
|
srcPtr = rOldBuffers[--l].data.length + srcPtr;
|
|
|
|
}
|
|
|
|
// TODO: lets hope that it never needs to read beyond file boundaries
|
2019-05-31 06:43:51 +00:00
|
|
|
while (length--) {
|
2019-05-13 19:28:06 +00:00
|
|
|
rBuffer.insertByte(rOldBuffers[l].data[srcPtr++]);
|
2017-09-17 12:51:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (length > distance) {
|
2019-05-31 06:43:51 +00:00
|
|
|
while (length--) {
|
2019-05-13 19:28:06 +00:00
|
|
|
rBuffer.insertByte(rBuffer.data[srcPtr++]);
|
|
|
|
}
|
2017-09-17 12:51:17 +00:00
|
|
|
} else {
|
2019-05-31 06:43:51 +00:00
|
|
|
rBuffer.insertBytes(rBuffer.data.subarray(srcPtr, srcPtr + length));
|
2017-09-17 10:36:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-09 23:16:03 +00:00
|
|
|
/**
|
|
|
|
* @param {RarLocalFile} v
|
|
|
|
*/
|
2017-09-17 10:36:32 +00:00
|
|
|
function unpack(v) {
|
2019-01-14 19:27:53 +00:00
|
|
|
// TODO: implement what happens when unpVer is < 15
|
2019-05-13 19:28:06 +00:00
|
|
|
var Ver = v.header.unpVer <= 15 ? 15 : v.header.unpVer;
|
|
|
|
var Solid = v.header.LHD_SOLID;
|
|
|
|
var bstream = new bitjs.io.BitStream(v.fileData.buffer, true /* rtl */ , v.fileData.byteOffset, v.fileData.byteLength);
|
2019-01-14 19:27:53 +00:00
|
|
|
|
2017-09-17 12:51:17 +00:00
|
|
|
rBuffer = new bitjs.io.ByteBuffer(v.header.unpackedSize);
|
2017-09-17 10:36:32 +00:00
|
|
|
|
2019-01-14 19:27:53 +00:00
|
|
|
info("Unpacking " + v.filename + " RAR v" + Ver);
|
|
|
|
|
2019-03-16 16:28:21 +00:00
|
|
|
switch (Ver) {
|
2017-09-17 12:51:17 +00:00
|
|
|
case 15: // rar 1.5 compression
|
2019-03-16 16:28:21 +00:00
|
|
|
unpack15(); //(bstream, Solid);
|
2017-09-17 12:51:17 +00:00
|
|
|
break;
|
|
|
|
case 20: // rar 2.x compression
|
|
|
|
case 26: // files larger than 2GB
|
2019-03-16 16:28:21 +00:00
|
|
|
unpack20(bstream); //, Solid);
|
2017-09-17 12:51:17 +00:00
|
|
|
break;
|
|
|
|
case 29: // rar 3.x compression
|
|
|
|
case 36: // alternative hash
|
2019-05-13 19:28:06 +00:00
|
|
|
wBuffer = new bitjs.io.ByteBuffer(rBuffer.data.length);
|
2019-03-16 16:28:21 +00:00
|
|
|
unpack29(bstream);
|
2017-09-17 12:51:17 +00:00
|
|
|
break;
|
|
|
|
} // switch(method)
|
2019-01-14 19:27:53 +00:00
|
|
|
|
2017-09-17 12:51:17 +00:00
|
|
|
rOldBuffers.push(rBuffer);
|
2019-05-13 19:28:06 +00:00
|
|
|
// TODO: clear these old buffers when there's over 4MB of history
|
2017-09-17 12:51:17 +00:00
|
|
|
return rBuffer.data;
|
2017-09-17 10:36:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// bstream is a bit stream
|
|
|
|
var RarLocalFile = function(bstream) {
|
2017-09-17 12:51:17 +00:00
|
|
|
this.header = new RarVolumeHeader(bstream);
|
|
|
|
this.filename = this.header.filename;
|
2019-01-14 19:27:53 +00:00
|
|
|
|
2019-03-24 14:58:43 +00:00
|
|
|
if (this.header.headType !== FILE_HEAD && this.header.headType !== ENDARC_HEAD) {
|
2017-09-17 12:51:17 +00:00
|
|
|
this.isValid = false;
|
|
|
|
info("Error! RAR Volume did not include a FILE_HEAD header ");
|
2018-08-12 16:21:57 +00:00
|
|
|
} else {
|
2017-09-17 12:51:17 +00:00
|
|
|
// read in the compressed data
|
|
|
|
this.fileData = null;
|
|
|
|
if (this.header.packSize > 0) {
|
|
|
|
this.fileData = bstream.readBytes(this.header.packSize);
|
|
|
|
this.isValid = true;
|
|
|
|
}
|
2017-09-17 10:36:32 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
RarLocalFile.prototype.unrar = function() {
|
2017-09-17 12:51:17 +00:00
|
|
|
if (!this.header.flags.LHD_SPLIT_BEFORE) {
|
|
|
|
// unstore file
|
2018-08-12 07:29:57 +00:00
|
|
|
if (this.header.method === 0x30) {
|
|
|
|
info("Unstore " + this.filename);
|
2017-09-17 12:51:17 +00:00
|
|
|
this.isValid = true;
|
2019-01-14 19:27:53 +00:00
|
|
|
|
2017-09-17 12:51:17 +00:00
|
|
|
currentBytesUnarchivedInFile += this.fileData.length;
|
|
|
|
currentBytesUnarchived += this.fileData.length;
|
|
|
|
|
|
|
|
// Create a new buffer and copy it over.
|
|
|
|
var len = this.header.packSize;
|
|
|
|
var newBuffer = new bitjs.io.ByteBuffer(len);
|
|
|
|
newBuffer.insertBytes(this.fileData);
|
|
|
|
this.fileData = newBuffer.data;
|
|
|
|
} else {
|
|
|
|
this.isValid = true;
|
|
|
|
this.fileData = unpack(this);
|
|
|
|
}
|
2017-09-17 10:36:32 +00:00
|
|
|
}
|
2019-01-14 19:27:53 +00:00
|
|
|
};
|
2017-09-17 10:36:32 +00:00
|
|
|
|
|
|
|
var unrar = function(arrayBuffer) {
|
2017-09-17 12:51:17 +00:00
|
|
|
currentFilename = "";
|
|
|
|
currentFileNumber = 0;
|
|
|
|
currentBytesUnarchivedInFile = 0;
|
|
|
|
currentBytesUnarchived = 0;
|
|
|
|
totalUncompressedBytesInArchive = 0;
|
|
|
|
totalFilesInArchive = 0;
|
|
|
|
|
|
|
|
postMessage(new bitjs.archive.UnarchiveStartEvent());
|
|
|
|
var bstream = new bitjs.io.BitStream(arrayBuffer, false /* rtl */);
|
2017-10-08 08:41:43 +00:00
|
|
|
|
2017-09-17 12:51:17 +00:00
|
|
|
var header = new RarVolumeHeader(bstream);
|
2018-08-12 07:29:57 +00:00
|
|
|
if (header.crc === 0x6152 &&
|
|
|
|
header.headType === 0x72 &&
|
|
|
|
header.flags.value === 0x1A21 &&
|
2019-01-14 19:27:53 +00:00
|
|
|
header.headSize === 7) {
|
2017-09-17 12:51:17 +00:00
|
|
|
info("Found RAR signature");
|
|
|
|
|
|
|
|
var mhead = new RarVolumeHeader(bstream);
|
2019-03-24 14:58:43 +00:00
|
|
|
if (mhead.headType !== MAIN_HEAD) {
|
2017-09-17 12:51:17 +00:00
|
|
|
info("Error! RAR did not include a MAIN_HEAD header");
|
|
|
|
} else {
|
2019-01-14 19:27:53 +00:00
|
|
|
var localFiles = [];
|
|
|
|
var localFile = null;
|
2017-09-17 12:51:17 +00:00
|
|
|
do {
|
|
|
|
try {
|
|
|
|
localFile = new RarLocalFile(bstream);
|
|
|
|
info("RAR localFile isValid=" + localFile.isValid + ", volume packSize=" + localFile.header.packSize);
|
|
|
|
if (localFile && localFile.isValid && localFile.header.packSize > 0) {
|
|
|
|
totalUncompressedBytesInArchive += localFile.header.unpackedSize;
|
|
|
|
localFiles.push(localFile);
|
2018-08-12 07:29:57 +00:00
|
|
|
} else if (localFile.header.packSize === 0 && localFile.header.unpackedSize === 0) {
|
2019-01-14 19:27:53 +00:00
|
|
|
localFile.isValid = true;
|
2017-09-17 12:51:17 +00:00
|
|
|
}
|
2019-01-14 19:27:53 +00:00
|
|
|
} catch (err) {
|
2017-09-17 12:51:17 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
//info("bstream" + bstream.bytePtr+"/"+bstream.bytes.length);
|
2019-05-13 19:28:06 +00:00
|
|
|
} while (localFile.isValid);
|
2017-09-17 12:51:17 +00:00
|
|
|
totalFilesInArchive = localFiles.length;
|
|
|
|
|
|
|
|
// now we have all information but things are unpacked
|
|
|
|
// TODO: unpack
|
2019-01-14 19:27:53 +00:00
|
|
|
localFiles = localFiles.sort(function(a, b) {
|
2017-09-17 12:51:17 +00:00
|
|
|
var aname = a.filename.toLowerCase();
|
|
|
|
var bname = b.filename.toLowerCase();
|
|
|
|
return aname > bname ? 1 : -1;
|
|
|
|
});
|
|
|
|
|
2019-03-16 16:28:21 +00:00
|
|
|
info(localFiles.map(function(a) {
|
|
|
|
return a.filename;
|
|
|
|
}).join(", "));
|
2017-09-17 12:51:17 +00:00
|
|
|
for (var i = 0; i < localFiles.length; ++i) {
|
|
|
|
var localfile = localFiles[i];
|
|
|
|
|
|
|
|
// update progress
|
|
|
|
currentFilename = localfile.header.filename;
|
|
|
|
currentBytesUnarchivedInFile = 0;
|
|
|
|
|
|
|
|
// actually do the unzipping
|
|
|
|
localfile.unrar();
|
|
|
|
|
|
|
|
if (localfile.isValid) {
|
|
|
|
postMessage(new bitjs.archive.UnarchiveExtractEvent(localfile));
|
|
|
|
postProgress();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
postProgress();
|
|
|
|
}
|
2018-08-19 08:14:20 +00:00
|
|
|
} else {
|
2017-09-17 12:51:17 +00:00
|
|
|
err("Invalid RAR file");
|
2017-09-17 10:36:32 +00:00
|
|
|
}
|
2017-09-17 12:51:17 +00:00
|
|
|
postMessage(new bitjs.archive.UnarchiveFinishEvent());
|
2017-09-17 10:36:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// event.data.file has the ArrayBuffer.
|
|
|
|
onmessage = function(event) {
|
2017-09-17 12:51:17 +00:00
|
|
|
var ab = event.data.file;
|
|
|
|
unrar(ab, true);
|
2017-09-17 10:36:32 +00:00
|
|
|
};
|