Code cosmetics unrar

pull/591/head
OzzieIsaacs 6 years ago
parent d2ea10e90e
commit 7dac87fa5d

@ -160,7 +160,7 @@ var RarVolumeHeader = function(bstream) {
// read in filename // read in filename
this.filename = bstream.readBytes(this.nameSize); this.filename = bstream.readBytes(this.nameSize);
var _s = "" var _s = "";
for (var _i = 0; _i < this.filename.length ; _i++) { for (var _i = 0; _i < this.filename.length ; _i++) {
_s += String.fromCharCode(this.filename[_i]); _s += String.fromCharCode(this.filename[_i]);
} }
@ -313,7 +313,7 @@ function RarReadTables(bstream) {
//console.log(DecodeLen, DecodePos, DecodeNum); //console.log(DecodeLen, DecodePos, DecodeNum);
for (i = 0; i < TableSize;) { for (i = 0; i < TableSize;) {
var N; var N;
var num = RarDecodeNumber(bstream, BD); var num = rarDecodeNumber(bstream, BD);
if (num < 16) { if (num < 16) {
Table[i] = (num + UnpOldTable[i]) & 0xf; Table[i] = (num + UnpOldTable[i]) & 0xf;
i++; i++;
@ -345,7 +345,7 @@ function RarReadTables(bstream) {
} }
function RarDecodeNumber(bstream, dec) { function rarDecodeNumber(bstream, dec) {
var DecodeLen = dec.DecodeLen, DecodePos = dec.DecodePos, DecodeNum = dec.DecodeNum; var DecodeLen = dec.DecodeLen, DecodePos = dec.DecodePos, DecodeNum = dec.DecodeNum;
var bitField = bstream.getBits() & 0xfffe; var bitField = bstream.getBits() & 0xfffe;
//some sort of rolled out binary search //some sort of rolled out binary search
@ -365,7 +365,7 @@ function RarDecodeNumber(bstream, dec) {
((bitField < DecodeLen[13]) ? 13 : 14) ((bitField < DecodeLen[13]) ? 13 : 14)
: 15)); : 15));
bstream.readBits(bits); bstream.readBits(bits);
var N = DecodePos[bits] + ((bitField - DecodeLen[bits -1]) >>> (16 - bits)); var N = DecodePos[bits] + ((bitField - DecodeLen[bits - 1]) >>> (16 - bits));
return DecodeNum[N]; return DecodeNum[N];
} }
@ -373,46 +373,56 @@ function RarDecodeNumber(bstream, dec) {
function rarMakeDecodeTables(BitLength, offset, dec, size) { function rarMakeDecodeTables(BitLength, offset, dec, size) {
var DecodeLen = dec.DecodeLen, DecodePos = dec.DecodePos, DecodeNum = dec.DecodeNum; var DecodeLen = dec.DecodeLen, DecodePos = dec.DecodePos, DecodeNum = dec.DecodeNum;
var LenCount = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], var LenCount = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
TmpPos = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], TmpPos = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
N = 0, M = 0; N = 0, M = 0;
for (var i = DecodeNum.length; i--;) DecodeNum[i] = 0; var i;
for (var i = 0; i < size; i++) { for (i = DecodeNum.length; i--;) DecodeNum[i] = 0;
for (i = 0; i < size; i++) {
LenCount[BitLength[i + offset] & 0xF]++; LenCount[BitLength[i + offset] & 0xF]++;
} }
LenCount[0] = 0; LenCount[0] = 0;
TmpPos[0] = 0; TmpPos[0] = 0;
DecodePos[0] = 0; DecodePos[0] = 0;
DecodeLen[0] = 0; DecodeLen[0] = 0;
for (var I = 1; I < 16; ++I) { var I;
for (I = 1; I < 16; ++I) {
N = 2 * (N + LenCount[I]); N = 2 * (N + LenCount[I]);
M = (N << (15-I)); M = (N << (15 - I));
if (M > 0xFFFF) if (M > 0xFFFF){
M = 0xFFFF; M = 0xFFFF;
}
DecodeLen[I] = M; DecodeLen[I] = M;
DecodePos[I] = DecodePos[I-1] + LenCount[I-1]; DecodePos[I] = DecodePos[I - 1] + LenCount[I - 1];
TmpPos[I] = DecodePos[I]; TmpPos[I] = DecodePos[I];
} }
for (I = 0; I < size; ++I) { for (I = 0; I < size; ++I) {
if (BitLength[I + offset] != 0) { if (BitLength[I + offset] !== 0) {
DecodeNum[ TmpPos[ BitLength[offset + I] & 0xF ]++] = I; DecodeNum[ TmpPos[ BitLength[offset + I] & 0xF ]++] = I;
} }
} }
} }
// TODO: implement // TODO: implement
function Unpack15(bstream, Solid) { function Unpack15() { //bstream, Solid) {
info("ERROR! RAR 1.5 compression not supported"); info("ERROR! RAR 1.5 compression not supported");
} }
function Unpack20(bstream, Solid) { var lowDistRepCount = 0, prevLowDist = 0;
var rOldDist = [0, 0, 0, 0];
var lastDist = 0;
var lastLength = 0;
function Unpack20(bstream) { //, Solid) {
var destUnpSize = rBuffer.data.length; var destUnpSize = rBuffer.data.length;
var oldDistPtr = 0; var oldDistPtr = 0;
rarReadTables20(bstream);
RarReadTables20(bstream);
while (destUnpSize > rBuffer.ptr) { while (destUnpSize > rBuffer.ptr) {
var num = RarDecodeNumber(bstream, LD); var num = rarDecodeNumber(bstream, LD);
var Bits;
if (num < 256) { if (num < 256) {
rBuffer.insertByte(num); rBuffer.insertByte(num);
continue; continue;
@ -422,35 +432,35 @@ function Unpack20(bstream, Solid) {
if ((Bits = rLBits[num]) > 0) { if ((Bits = rLBits[num]) > 0) {
Length += bstream.readBits(Bits); Length += bstream.readBits(Bits);
} }
var DistNumber = RarDecodeNumber(bstream, DD); var DistNumber = rarDecodeNumber(bstream, DD);
var Distance = rDDecode[DistNumber] + 1; var Distance = rDDecode[DistNumber] + 1;
if ((Bits = rDBits[DistNumber]) > 0) { if ((Bits = rDBits[DistNumber]) > 0) {
Distance += bstream.readBits(Bits); Distance += bstream.readBits(Bits);
} }
if (Distance >= 0x2000) { if (Distance >= 0x2000) {
Length++; Length++;
if(Distance >= 0x40000) Length++; if (Distance >= 0x40000) Length++;
} }
lastLength = Length; lastLength = Length;
lastDist = rOldDist[oldDistPtr++ & 3] = Distance; lastDist = rOldDist[oldDistPtr++ & 3] = Distance;
RarCopyString(Length, Distance); rarCopyString(Length, Distance);
continue; continue;
} }
if (num === 269) { if (num === 269) {
RarReadTables20(bstream); rarReadTables20(bstream);
RarUpdateProgress() rarUpdateProgress();
continue; continue;
} }
if (num === 256) { if (num === 256) {
lastDist = rOldDist[oldDistPtr++ & 3] = lastDist; lastDist = rOldDist[oldDistPtr++ & 3] = lastDist;
RarCopyString(lastLength, lastDist); rarCopyString(lastLength, lastDist);
continue; continue;
} }
if (num < 261) { if (num < 261) {
var Distance = rOldDist[(oldDistPtr - (num - 256)) & 3]; var Distance = rOldDist[(oldDistPtr - (num - 256)) & 3];
var LengthNumber = RarDecodeNumber(bstream, RD); var LengthNumber = rarDecodeNumber(bstream, RD);
var Length = rLDecode[LengthNumber] +2; var Length = rLDecode[LengthNumber] +2;
if ((Bits = rLBits[LengthNumber]) > 0) { if ((Bits = rLBits[LengthNumber]) > 0) {
Length += bstream.readBits(Bits); Length += bstream.readBits(Bits);
@ -464,7 +474,7 @@ function Unpack20(bstream, Solid) {
} }
lastLength = Length; lastLength = Length;
lastDist = rOldDist[oldDistPtr++ & 3] = Distance; lastDist = rOldDist[oldDistPtr++ & 3] = Distance;
RarCopyString(Length, Distance); rarCopyString(Length, Distance);
continue; continue;
} }
if (num < 270) { if (num < 270) {
@ -474,14 +484,14 @@ function Unpack20(bstream, Solid) {
} }
lastLength = 2; lastLength = 2;
lastDist = rOldDist[oldDistPtr++ & 3] = Distance; lastDist = rOldDist[oldDistPtr++ & 3] = Distance;
RarCopyString(2, Distance); rarCopyString(2, Distance);
continue; continue;
} }
} }
RarUpdateProgress() rarUpdateProgress()
} }
function RarUpdateProgress() { function rarUpdateProgress() {
var change = rBuffer.ptr - currentBytesUnarchivedInFile; var change = rBuffer.ptr - currentBytesUnarchivedInFile;
currentBytesUnarchivedInFile = rBuffer.ptr; currentBytesUnarchivedInFile = rBuffer.ptr;
currentBytesUnarchived += change; currentBytesUnarchived += change;
@ -497,7 +507,7 @@ var rNC20 = 298,
var UnpOldTable20 = new Array(rMC20 * 4); var UnpOldTable20 = new Array(rMC20 * 4);
function RarReadTables20(bstream) { function rarReadTables20(bstream) {
var BitLength = new Array(rBC20); var BitLength = new Array(rBC20);
var Table = new Array(rMC20 * 4); var Table = new Array(rMC20 * 4);
var TableSize, N, I; var TableSize, N, I;
@ -510,7 +520,7 @@ function RarReadTables20(bstream) {
rarMakeDecodeTables(BitLength, 0, BD, rBC20); rarMakeDecodeTables(BitLength, 0, BD, rBC20);
I = 0; I = 0;
while (I < TableSize) { while (I < TableSize) {
var num = RarDecodeNumber(bstream, BD); var num = rarDecodeNumber(bstream, BD);
if (num < 16) { if (num < 16) {
Table[I] = num + UnpOldTable20[I] & 0xf; Table[I] = num + UnpOldTable20[I] & 0xf;
I++; I++;
@ -537,12 +547,6 @@ function RarReadTables20(bstream) {
for (var i = UnpOldTable20.length; i--;) UnpOldTable20[i] = Table[i]; for (var i = UnpOldTable20.length; i--;) UnpOldTable20[i] = Table[i];
} }
var lowDistRepCount = 0, prevLowDist = 0;
var rOldDist = [0,0,0,0];
var lastDist;
var lastLength;
function Unpack29(bstream, Solid) { function Unpack29(bstream, Solid) {
// lazy initialize rDDecode and rDBits // lazy initialize rDDecode and rDBits
@ -562,7 +566,7 @@ function Unpack29(bstream, Solid) {
var Bits; var Bits;
//tablesRead = false; //tablesRead = false;
rOldDist = [0,0,0,0] rOldDist = [0, 0, 0, 0]
lastDist = 0; lastDist = 0;
lastLength = 0; lastLength = 0;
@ -573,7 +577,7 @@ function Unpack29(bstream, Solid) {
RarReadTables(bstream); RarReadTables(bstream);
while (true) { while (true) {
var num = RarDecodeNumber(bstream, LD); var num = rarDecodeNumber(bstream, LD);
if (num < 256) { if (num < 256) {
rBuffer.insertByte(num); rBuffer.insertByte(num);
@ -584,7 +588,7 @@ function Unpack29(bstream, Solid) {
if ((Bits = rLBits[num]) > 0) { if ((Bits = rLBits[num]) > 0) {
Length += bstream.readBits(Bits); Length += bstream.readBits(Bits);
} }
var DistNumber = RarDecodeNumber(bstream, DD); var DistNumber = rarDecodeNumber(bstream, DD);
var Distance = DDecode[DistNumber]+1; var Distance = DDecode[DistNumber]+1;
if ((Bits = DBits[DistNumber]) > 0) { if ((Bits = DBits[DistNumber]) > 0) {
if (DistNumber > 9) { if (DistNumber > 9) {
@ -597,7 +601,7 @@ function Unpack29(bstream, Solid) {
lowDistRepCount--; lowDistRepCount--;
Distance += prevLowDist; Distance += prevLowDist;
} else { } else {
var LowDist = RarDecodeNumber(bstream, LDD); var LowDist = rarDecodeNumber(bstream, LDD);
if (LowDist === 16) { if (LowDist === 16) {
lowDistRepCount = rLowDistRepCount - 1; lowDistRepCount = rLowDistRepCount - 1;
Distance += prevLowDist; Distance += prevLowDist;
@ -618,7 +622,7 @@ function Unpack29(bstream, Solid) {
} }
RarInsertOldDist(Distance); RarInsertOldDist(Distance);
RarInsertLastMatch(Length, Distance); RarInsertLastMatch(Length, Distance);
RarCopyString(Length, Distance); rarCopyString(Length, Distance);
continue; continue;
} }
if (num === 256) { if (num === 256) {
@ -632,7 +636,7 @@ function Unpack29(bstream, Solid) {
} }
if (num === 258) { if (num === 258) {
if (lastLength != 0) { if (lastLength != 0) {
RarCopyString(lastLength, lastDist); rarCopyString(lastLength, lastDist);
} }
continue; continue;
} }
@ -645,13 +649,13 @@ function Unpack29(bstream, Solid) {
} }
rOldDist[0] = Distance; rOldDist[0] = Distance;
var LengthNumber = RarDecodeNumber(bstream, RD); var LengthNumber = rarDecodeNumber(bstream, RD);
var Length = rLDecode[LengthNumber] + 2; var Length = rLDecode[LengthNumber] + 2;
if ((Bits = rLBits[LengthNumber]) > 0) { if ((Bits = rLBits[LengthNumber]) > 0) {
Length += bstream.readBits(Bits); Length += bstream.readBits(Bits);
} }
RarInsertLastMatch(Length, Distance); RarInsertLastMatch(Length, Distance);
RarCopyString(Length, Distance); rarCopyString(Length, Distance);
continue; continue;
} }
if (num < 272) { if (num < 272) {
@ -661,16 +665,16 @@ function Unpack29(bstream, Solid) {
} }
RarInsertOldDist(Distance); RarInsertOldDist(Distance);
RarInsertLastMatch(2, Distance); RarInsertLastMatch(2, Distance);
RarCopyString(2, Distance); rarCopyString(2, Distance);
continue; continue;
} }
} }
RarUpdateProgress() rarUpdateProgress()
} }
function RarReadEndOfBlock(bstream) { function RarReadEndOfBlock(bstream) {
RarUpdateProgress() rarUpdateProgress()
var NewTable = false, NewFile = false; var NewTable = false, NewFile = false;
if (bstream.readBits(1)) { if (bstream.readBits(1)) {
@ -719,7 +723,7 @@ function RarInsertOldDist(distance) {
} }
//this is the real function, the other one is for debugging //this is the real function, the other one is for debugging
function RarCopyString(length, distance) { function rarCopyString(length, distance) {
var destPtr = rBuffer.ptr - distance; var destPtr = rBuffer.ptr - distance;
if(destPtr < 0){ if(destPtr < 0){
var l = rOldBuffers.length; var l = rOldBuffers.length;
@ -751,11 +755,11 @@ function unpack(v) {
switch(Ver) { switch(Ver) {
case 15: // rar 1.5 compression case 15: // rar 1.5 compression
Unpack15(bstream, Solid); Unpack15(); //(bstream, Solid);
break; break;
case 20: // rar 2.x compression case 20: // rar 2.x compression
case 26: // files larger than 2GB case 26: // files larger than 2GB
Unpack20(bstream, Solid); Unpack20(bstream); //, Solid);
break; break;
case 29: // rar 3.x compression case 29: // rar 3.x compression
case 36: // alternative hash case 36: // alternative hash

@ -520,7 +520,7 @@ function inflate(compressedData, numDecompressedBytes) {
if (bType === 0) { if (bType === 0) {
// skip remaining bits in this byte // skip remaining bits in this byte
while (bstream.bitPtr !== 0) bstream.readBits(1); while (bstream.bitPtr !== 0) bstream.readBits(1);
var len = bstream.readBits(16), var len = bstream.readBits(16);
// nlen = bstream.readBits(16); // nlen = bstream.readBits(16);
// TODO: check if nlen is the ones-complement of len? // TODO: check if nlen is the ones-complement of len?

Loading…
Cancel
Save