rar5: porting old algos

This commit is contained in:
coderb
2017-12-20 19:04:41 -05:00
parent c42dc646ae
commit 3c29122dfe
3 changed files with 78 additions and 89 deletions

View File

@@ -1,15 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static SharpCompress.Compressors.Rar.UnpackV2017.PackDef;
using static SharpCompress.Compressors.Rar.UnpackV2017.Unpack.Unpack15Local;
namespace SharpCompress.Compressors.Rar.UnpackV2017
{
internal partial class Unpack
{
#if !RarV2017_RAR5ONLY
#if true || !RarV2017_RAR5ONLY
const int STARTL1 =2;
static uint[] DecL1={0x8000,0xa000,0xc000,0xd000,0xe000,0xea00,
@@ -129,13 +124,14 @@ uint GetShortLen1(uint pos) { return ((pos)==1 ? (uint)(this.Buf60+3):ShortLen1[
//#define GetShortLen2(pos) ((pos)==3 ? Buf60+3:ShortLen2[pos])
uint GetShortLen2(uint pos) { return ((pos)==3 ? (uint)(this.Buf60+3):ShortLen2[pos]); }
static uint[] ShortLen1={1,3,4,4,5,6,7,8,8,4,4,5,6,6,4,0};
static uint[] ShortXor1={0,0xa0,0xd0,0xe0,0xf0,0xf8,0xfc,0xfe,
internal static class Unpack15Local {
public static readonly uint[] ShortLen1={1,3,4,4,5,6,7,8,8,4,4,5,6,6,4,0};
public static readonly uint[] ShortXor1={0,0xa0,0xd0,0xe0,0xf0,0xf8,0xfc,0xfe,
0xff,0xc0,0x80,0x90,0x98,0x9c,0xb0};
static uint[] ShortLen2={2,3,3,3,4,4,5,6,6,4,4,5,6,6,4,0};
static uint[] ShortXor2={0,0x40,0x60,0xa0,0xd0,0xe0,0xf0,0xf8,
public static readonly uint[] ShortLen2={2,3,3,3,4,4,5,6,6,4,4,5,6,6,4,0};
public static readonly uint[] ShortXor2={0,0x40,0x60,0xa0,0xd0,0xe0,0xf0,0xf8,
0xfc,0xc0,0x80,0x90,0x98,0x9c,0xb0};
}
void ShortLZ()
{
@@ -166,14 +162,14 @@ void ShortLZ()
if (AvrLn1<37)
{
for (Length=0;;Length++)
if (((BitField^ShortXor1[Length]) & (~(0xff>>GetShortLen1(Length))))==0)
if (((BitField^ShortXor1[Length]) & (~(0xff>>(int)GetShortLen1(Length))))==0)
break;
Inp.faddbits(GetShortLen1(Length));
}
else
{
for (Length=0;;Length++)
if (((BitField^ShortXor2[Length]) & (~(0xff>>GetShortLen2(Length))))==0)
if (((BitField^ShortXor2[Length]) & (~(0xff>>(int)GetShortLen2(Length))))==0)
break;
Inp.faddbits(GetShortLen2(Length));
}
@@ -224,13 +220,13 @@ void ShortLZ()
AvrLn1 += Length;
AvrLn1 -= AvrLn1 >> 4;
DistancePlace=DecodeNum(Inp.fgetbits(),STARTHF2,DecHf2,PosHf2) & 0xff;
DistancePlace=(int)(DecodeNum(Inp.fgetbits(),STARTHF2,DecHf2,PosHf2) & 0xff);
Distance=ChSetA[DistancePlace];
if (--DistancePlace != -1)
{
LastDistance=ChSetA[DistancePlace];
ChSetA[DistancePlace+1]=LastDistance;
ChSetA[DistancePlace]=Distance;
ChSetA[DistancePlace+1]=(ushort)LastDistance;
ChSetA[DistancePlace]=(ushort)Distance;
}
Length+=2;
OldDist[OldDistPtr++] = ++Distance;
@@ -271,7 +267,7 @@ void LongLZ()
}
else
{
for (Length=0;((BitField<<Length)&0x8000)==0;Length++)
for (Length=0;((BitField<<(int)Length)&0x8000)==0;Length++)
;
Inp.faddbits(Length+1);
}
@@ -290,18 +286,18 @@ void LongLZ()
AvrPlcB += DistancePlace;
AvrPlcB -= AvrPlcB >> 8;
while (1)
while (true)
{
Distance = ChSetB[DistancePlace & 0xff];
NewDistancePlace = NToPlB[Distance++ & 0xff]++;
if (!(Distance & 0xff))
if ((Distance & 0xff) != 0)
CorrHuff(ChSetB,NToPlB);
else
break;
}
ChSetB[DistancePlace & 0xff]=ChSetB[NewDistancePlace];
ChSetB[NewDistancePlace]=Distance;
ChSetB[NewDistancePlace]=(ushort)Distance;
Distance=((Distance & 0xff00) | (Inp.fgetbits() >> 8)) >> 1;
Inp.faddbits(7);
@@ -343,18 +339,18 @@ void HuffDecode()
uint BitField=Inp.fgetbits();
if (AvrPlc > 0x75ff)
BytePlace=DecodeNum(BitField,STARTHF4,DecHf4,PosHf4);
BytePlace=(int)DecodeNum(BitField,STARTHF4,DecHf4,PosHf4);
else
if (AvrPlc > 0x5dff)
BytePlace=DecodeNum(BitField,STARTHF3,DecHf3,PosHf3);
BytePlace=(int)DecodeNum(BitField,STARTHF3,DecHf3,PosHf3);
else
if (AvrPlc > 0x35ff)
BytePlace=DecodeNum(BitField,STARTHF2,DecHf2,PosHf2);
BytePlace=(int)DecodeNum(BitField,STARTHF2,DecHf2,PosHf2);
else
if (AvrPlc > 0x0dff)
BytePlace=DecodeNum(BitField,STARTHF1,DecHf1,PosHf1);
BytePlace=(int)DecodeNum(BitField,STARTHF1,DecHf1,PosHf1);
else
BytePlace=DecodeNum(BitField,STARTHF0,DecHf0,PosHf0);
BytePlace=(int)DecodeNum(BitField,STARTHF0,DecHf0,PosHf0);
BytePlace&=0xff;
if (StMode != 0)
{
@@ -384,7 +380,7 @@ void HuffDecode()
else
if (NumHuf++ >= 16 && FlagsCnt==0)
StMode=1;
AvrPlc += BytePlace;
AvrPlc += (uint)BytePlace;
AvrPlc -= AvrPlc >> 8;
Nhfb+=16;
if (Nhfb > 0xff)
@@ -396,7 +392,7 @@ void HuffDecode()
Window[UnpPtr++]=(byte)(ChSet[BytePlace]>>8);
--DestUnpSize;
while (1)
while (true)
{
CurByte=ChSet[BytePlace];
NewBytePlace=NToPl[CurByte++ & 0xff]++;
@@ -407,7 +403,7 @@ void HuffDecode()
}
ChSet[BytePlace]=ChSet[NewBytePlace];
ChSet[NewBytePlace]=CurByte;
ChSet[NewBytePlace]=(ushort)CurByte;
}
@@ -421,7 +417,7 @@ void GetFlagsBuf()
// While normally we do not use the last item to code the flags byte here,
// we need to check for value 256 when unpacking in case we unpack
// a corrupt archive.
if (FlagsPlace>=sizeof(ChSetC)/sizeof(ChSetC[0]))
if (FlagsPlace>=ChSetC.Length)
return;
while (true)
@@ -435,7 +431,7 @@ void GetFlagsBuf()
}
ChSetC[FlagsPlace]=ChSetC[NewFlagsPlace];
ChSetC[NewFlagsPlace]=Flags;
ChSetC[NewFlagsPlace]=(ushort)Flags;
}
#endif
@@ -482,7 +478,6 @@ void CorrHuff(ushort[] CharSet,byte[] NumToPlace)
NumToPlace[I]=(byte)((7-I)*32);
}
#if !RarV2017_RAR5ONLY
void CopyString15(uint Distance,uint Length)
{
@@ -495,15 +490,14 @@ void CopyString15(uint Distance,uint Length)
}
uint DecodeNum(uint Num,uint StartPos,uint *DecTab,uint *PosTab)
uint DecodeNum(uint Num,uint StartPos,uint[] DecTab,uint[] PosTab)
{
int I;
for (Num&=0xfff0,I=0;DecTab[I]<=Num;I++)
StartPos++;
Inp.faddbits(StartPos);
return(((Num-(I ? DecTab[I-1]:0))>>(16-StartPos))+PosTab[StartPos]);
return(((Num-(I != 0 ? DecTab[I-1]:0))>>(int)(16-StartPos))+PosTab[StartPos]);
}
#endif
}
}

View File

@@ -10,19 +10,15 @@ using size_t = System.UInt64;
using int64 = System.Int64;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static SharpCompress.Compressors.Rar.UnpackV2017.PackDef;
//using static SharpCompress.Compressors.Rar.UnpackV2017.Unpack.Unpack20Local;
using static SharpCompress.Compressors.Rar.UnpackV2017.Unpack.Unpack20Local;
namespace SharpCompress.Compressors.Rar.UnpackV2017
{
internal partial class Unpack
{
#if !RarV2017_RAR5ONLY
#if true || !RarV2017_RAR5ONLY
void CopyString20(uint Length,uint Distance)
{
LastDist=OldDist[OldDistPtr++ & 3]=Distance;
@@ -33,12 +29,12 @@ void CopyString20(uint Length,uint Distance)
internal static class Unpack20Local {
public static byte[] LDecode={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};
public static byte[] LBits= {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};
public static uint[] DDecode={0,1,2,3,4,6,8,12,16,24,32,48,64,96,128,192,256,384,512,768,1024,1536,2048,3072,4096,6144,8192,12288,16384,24576,32768U,49152U,65536,98304,131072,196608,262144,327680,393216,458752,524288,589824,655360,720896,786432,851968,917504,983040};
public static byte[] DBits= {0,0,0,0,1,1,2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16};
public static byte[] SDDecode={0,4,8,16,32,64,128,192};
public static byte[] SDBits= {2,2,3, 4, 5, 6, 6, 6};
public static readonly byte[] LDecode={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};
public static readonly byte[] LBits= {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};
public static readonly uint[] DDecode={0,1,2,3,4,6,8,12,16,24,32,48,64,96,128,192,256,384,512,768,1024,1536,2048,3072,4096,6144,8192,12288,16384,24576,32768U,49152U,65536,98304,131072,196608,262144,327680,393216,458752,524288,589824,655360,720896,786432,851968,917504,983040};
public static readonly byte[] DBits= {0,0,0,0,1,1,2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16};
public static readonly byte[] SDDecode={0,4,8,16,32,64,128,192};
public static readonly byte[] SDBits= {2,2,3, 4, 5, 6, 6, 6};
}
void Unpack20(bool Solid)
{
@@ -95,10 +91,10 @@ void Unpack20(bool Solid)
}
if (Number>269)
{
uint Length=LDecode[Number-=270]+3;
uint Length=(uint)(LDecode[Number-=270]+3);
if ((Bits=LBits[Number])>0)
{
Length+=Inp.getbits()>>(16-Bits);
Length+=Inp.getbits()>>(int)(16-Bits);
Inp.addbits(Bits);
}
@@ -106,7 +102,7 @@ void Unpack20(bool Solid)
uint Distance=DDecode[DistNumber]+1;
if ((Bits=DBits[DistNumber])>0)
{
Distance+=Inp.getbits()>>(16-Bits);
Distance+=Inp.getbits()>>(int)(16-Bits);
Inp.addbits(Bits);
}
@@ -135,10 +131,10 @@ void Unpack20(bool Solid)
{
uint Distance=OldDist[(OldDistPtr-(Number-256)) & 3];
uint LengthNumber=DecodeNumber(Inp,BlockTables.RD);
uint Length=LDecode[LengthNumber]+2;
uint Length=(uint)(LDecode[LengthNumber]+2);
if ((Bits=LBits[LengthNumber])>0)
{
Length+=Inp.getbits()>>(16-Bits);
Length+=Inp.getbits()>>(int)(16-Bits);
Inp.addbits(Bits);
}
if (Distance>=0x101)
@@ -156,10 +152,10 @@ void Unpack20(bool Solid)
}
if (Number<270)
{
uint Distance=SDDecode[Number-=261]+1;
uint Distance=(uint)(SDDecode[Number-=261]+1);
if ((Bits=SDBits[Number])>0)
{
Distance+=Inp.getbits()>>(16-Bits);
Distance+=Inp.getbits()>>(int)(16-Bits);
Inp.addbits(Bits);
}
CopyString20(2,Distance);
@@ -177,20 +173,20 @@ void UnpWriteBuf20()
UnpSomeRead=true;
if (UnpPtr<WrPtr)
{
UnpIO->UnpWrite(&Window[WrPtr],-(int)WrPtr & MaxWinMask);
UnpIO->UnpWrite(Window,UnpPtr);
UnpIO_UnpWrite(Window, WrPtr,(uint)(-(int)this.WrPtr & this.MaxWinMask));
UnpIO_UnpWrite(Window,0,UnpPtr);
UnpAllBuf=true;
}
else
UnpIO->UnpWrite(&Window[WrPtr],UnpPtr-WrPtr);
UnpIO_UnpWrite(Window,WrPtr,UnpPtr-WrPtr);
WrPtr=UnpPtr;
}
bool ReadTables20()
{
byte BitLength[BC20];
byte Table[MC20*4];
byte[] BitLength = new byte[BC20];
byte[] Table = new byte[MC20*4];
if (Inp.InAddr>ReadTop-25)
if (!UnpReadBuf())
return false;
@@ -218,7 +214,7 @@ bool ReadTables20()
BitLength[I]=(byte)(Inp.getbits() >> 12);
Inp.addbits(4);
}
MakeDecodeTables(BitLength,BlockTables.BD,BC20);
MakeDecodeTables(BitLength,0,BlockTables.BD,BC20);
for (uint I=0;I<TableSize;)
{
if (Inp.InAddr>ReadTop-5)
@@ -227,7 +223,7 @@ bool ReadTables20()
uint Number=DecodeNumber(Inp,BlockTables.BD);
if (Number<16)
{
Table[I]=(Number+UnpOldTable20[I]) & 0xf;
Table[I]=(byte)((Number+this.UnpOldTable20[I]) & 0xf);
I++;
}
else
@@ -266,14 +262,15 @@ bool ReadTables20()
return true;
if (UnpAudioBlock)
for (uint I=0;I<UnpChannels;I++)
MakeDecodeTables(&Table[I*MC20],&MD[I],MC20);
MakeDecodeTables(Table,(int)(I*MC20),MD[I],MC20);
else
{
MakeDecodeTables(&Table[0],BlockTables.LD,NC20);
MakeDecodeTables(&Table[NC20],BlockTables.DD,DC20);
MakeDecodeTables(&Table[NC20+DC20],BlockTables.RD,RC20);
MakeDecodeTables(Table,0,BlockTables.LD,NC20);
MakeDecodeTables(Table,(int)NC20,BlockTables.DD,DC20);
MakeDecodeTables(Table,(int)(NC20+DC20),BlockTables.RD,RC20);
}
memcpy(UnpOldTable20,Table,sizeof(UnpOldTable20));
//x memcpy(UnpOldTable20,Table,sizeof(UnpOldTable20));
Array.Copy(Table,0,UnpOldTable20,0,UnpOldTable20.Length);
return true;
}
@@ -311,7 +308,6 @@ void UnpInitData20(bool Solid)
}
}
#if !RarV2017_RAR5ONLY
byte DecodeAudio(int Delta)
{
@@ -324,27 +320,27 @@ byte DecodeAudio(int Delta)
int PCh=8*V.LastChar+V.K1*V.D1+V.K2*V.D2+V.K3*V.D3+V.K4*V.D4+V.K5*UnpChannelDelta;
PCh=(PCh>>3) & 0xFF;
uint Ch=PCh-Delta;
uint Ch=(uint)(PCh-Delta);
int D=(sbyte)Delta;
// Left shift of negative value is undefined behavior in C++,
// so we cast it to unsigned to follow the standard.
D=(uint)D<<3;
D=(int)((uint)D<<3);
V.Dif[0]+=Math.Abs(D);
V.Dif[1]+=Math.Abs(D-V.D1);
V.Dif[2]+=Math.Abs(D+V.D1);
V.Dif[3]+=Math.Abs(D-V.D2);
V.Dif[4]+=Math.Abs(D+V.D2);
V.Dif[5]+=Math.Abs(D-V.D3);
V.Dif[6]+=Math.Abs(D+V.D3);
V.Dif[7]+=Math.Abs(D-V.D4);
V.Dif[8]+=Math.Abs(D+V.D4);
V.Dif[9]+=Math.Abs(D-UnpChannelDelta);
V.Dif[10]+=Math.Abs(D+UnpChannelDelta);
V.Dif[0]+=(uint)Math.Abs(D);
V.Dif[1]+=(uint)Math.Abs(D-V.D1);
V.Dif[2]+=(uint)Math.Abs(D+V.D1);
V.Dif[3]+=(uint)Math.Abs(D-V.D2);
V.Dif[4]+=(uint)Math.Abs(D+V.D2);
V.Dif[5]+=(uint)Math.Abs(D-V.D3);
V.Dif[6]+=(uint)Math.Abs(D+V.D3);
V.Dif[7]+=(uint)Math.Abs(D-V.D4);
V.Dif[8]+=(uint)Math.Abs(D+V.D4);
V.Dif[9]+=(uint)Math.Abs(D-UnpChannelDelta);
V.Dif[10]+=(uint)Math.Abs(D+UnpChannelDelta);
UnpChannelDelta=V.LastDelta=(sbyte)(Ch-V.LastChar);
V.LastChar=Ch;
V.LastChar=(int)Ch;
if ((V.ByteCount & 0x1F)==0)
{
@@ -405,7 +401,6 @@ byte DecodeAudio(int Delta)
}
return (byte)Ch;
}
#endif
}
}

View File

@@ -35,13 +35,13 @@ int SafePPMDecodeChar()
}
internal static class Unpack30Local {
public static byte[] LDecode={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};
public static byte[] LBits= {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};
public static int[] DDecode = new int[DC];
public static byte[] DBits = new byte[DC];
public static int[] DBitLengthCounts= {4,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,14,0,12};
public static byte[] SDDecode={0,4,8,16,32,64,128,192};
public static byte[] SDBits= {2,2,3, 4, 5, 6, 6, 6};
public static readonly byte[] LDecode={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};
public static readonly byte[] LBits= {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};
public static readonly int[] DDecode = new int[DC];
public static readonly byte[] DBits = new byte[DC];
public static readonly int[] DBitLengthCounts= {4,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,14,0,12};
public static readonly byte[] SDDecode={0,4,8,16,32,64,128,192};
public static readonly byte[] SDBits= {2,2,3, 4, 5, 6, 6, 6};
}
void Unpack29(bool Solid)
{