From 3e8f52689e77678d8a72d6fdffd97e1edaee9710 Mon Sep 17 00:00:00 2001 From: coderb Date: Wed, 20 Dec 2017 16:01:55 -0500 Subject: [PATCH] unrar5: unpack wip --- .../FragmentedWindow.unpack50frag_cpp.cs | 4 +-- .../Compressors/Rar/UnpackV2017/Unpack.cs | 9 +++-- .../Rar/UnpackV2017/Unpack.unpack15_cpp.cs | 34 +++++++++---------- .../Rar/UnpackV2017/Unpack.unpack20_cpp.cs | 18 +++++----- .../Rar/UnpackV2017/Unpack.unpack30_cpp.cs | 12 +++---- .../Rar/UnpackV2017/Unpack.unpack50_cpp.cs | 4 +-- .../Rar/UnpackV2017/Unpack.unpack_cpp.cs | 11 +++--- .../UnpackV2017/Unpack.unpackinline_cpp.cs | 4 +-- .../Compressors/Rar/UnpackV2017/unpack_hpp.cs | 6 ++-- 9 files changed, 45 insertions(+), 57 deletions(-) diff --git a/src/SharpCompress/Compressors/Rar/UnpackV2017/FragmentedWindow.unpack50frag_cpp.cs b/src/SharpCompress/Compressors/Rar/UnpackV2017/FragmentedWindow.unpack50frag_cpp.cs index 8d4dab7a..b0bc6da7 100644 --- a/src/SharpCompress/Compressors/Rar/UnpackV2017/FragmentedWindow.unpack50frag_cpp.cs +++ b/src/SharpCompress/Compressors/Rar/UnpackV2017/FragmentedWindow.unpack50frag_cpp.cs @@ -1,5 +1,4 @@ -#if RARWIP -#if !Rar2017_64bit +#if !Rar2017_64bit using nint = System.Int32; using nuint = System.UInt32; using size_t = System.UInt32; @@ -158,4 +157,3 @@ public size_t GetBlockSize(size_t StartPos,size_t RequiredSize) } } -#endif \ No newline at end of file diff --git a/src/SharpCompress/Compressors/Rar/UnpackV2017/Unpack.cs b/src/SharpCompress/Compressors/Rar/UnpackV2017/Unpack.cs index 6fbf86c0..e40ec2ec 100644 --- a/src/SharpCompress/Compressors/Rar/UnpackV2017/Unpack.cs +++ b/src/SharpCompress/Compressors/Rar/UnpackV2017/Unpack.cs @@ -1,5 +1,4 @@ -#if RARWIP -#if !Rar2017_64bit +#if !Rar2017_64bit using nint = System.Int32; using nuint = System.UInt32; using size_t = System.UInt32; @@ -43,7 +42,8 @@ namespace SharpCompress.Compressors.Rar.UnpackV2017 // as of 12/2017 .NET limits array indexing to using a signed integer // MaxWinSize causes unpack to use a fragmented window when the file // window size exceeds MaxWinSize - MaxWinSize = ((uint)int.MaxValue) + 1; + // uggh, that's not how this variable is used, it's the size of the currently allocated window buffer + //x MaxWinSize = ((uint)int.MaxValue) + 1; // may be long.MaxValue which could indicate unknown size (not present in header) DestUnpSize = fileHeader.UncompressedSize; @@ -63,7 +63,7 @@ namespace SharpCompress.Compressors.Rar.UnpackV2017 { UnstoreFile(); } else { - DoUnpack(this.fileHeader.CompressionMethod, this.fileHeader.IsSolid); + DoUnpack(this.fileHeader.CompressionAlgorithm, this.fileHeader.IsSolid); } } @@ -106,4 +106,3 @@ namespace SharpCompress.Compressors.Rar.UnpackV2017 } } -#endif \ No newline at end of file diff --git a/src/SharpCompress/Compressors/Rar/UnpackV2017/Unpack.unpack15_cpp.cs b/src/SharpCompress/Compressors/Rar/UnpackV2017/Unpack.unpack15_cpp.cs index 917116ef..9889e8e8 100644 --- a/src/SharpCompress/Compressors/Rar/UnpackV2017/Unpack.unpack15_cpp.cs +++ b/src/SharpCompress/Compressors/Rar/UnpackV2017/Unpack.unpack15_cpp.cs @@ -1,6 +1,4 @@ -#if RARWIP -#if !RarV2017_RAR5ONLY -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -11,6 +9,7 @@ namespace SharpCompress.Compressors.Rar.UnpackV2017 { internal partial class Unpack { +#if !RarV2017_RAR5ONLY const int STARTL1 =2; static uint[] DecL1={0x8000,0xa000,0xc000,0xd000,0xe000,0xea00, @@ -439,7 +438,7 @@ void GetFlagsBuf() ChSetC[NewFlagsPlace]=Flags; } - +#endif void UnpInitData15(bool Solid) { if (!Solid) @@ -457,33 +456,33 @@ void UnpInitData15(bool Solid) ReadTop=0; } - void InitHuff() { for (uint I=0;I<256;I++) { - ChSet[I]=ChSetB[I]=I<<8; - ChSetA[I]=I; - ChSetC[I]=((~I+1) & 0xff)<<8; + ChSet[I]=ChSetB[I]=(ushort)(I<<8); + ChSetA[I]=(ushort)I; + ChSetC[I]=(ushort)(((~I+1) & 0xff)<<8); } - memset(NToPl,0,sizeof(NToPl)); - memset(NToPlB,0,sizeof(NToPlB)); - memset(NToPlC,0,sizeof(NToPlC)); + Utility.Memset(NToPl,0,NToPl.Length); + Utility.Memset(NToPlB,0,NToPlB.Length); + Utility.Memset(NToPlC,0,NToPlC.Length); CorrHuff(ChSetB,NToPlB); } -void CorrHuff(ushort *CharSet,byte *NumToPlace) +void CorrHuff(ushort[] CharSet,byte[] NumToPlace) { int I,J; for (I=7;I>=0;I--) - for (J=0;J<32;J++,CharSet++) - *CharSet=(*CharSet & ~0xff) | I; - memset(NumToPlace,0,sizeof(NToPl)); + for (J=0;J<32;J++) + CharSet[J]=(ushort)((CharSet[J] & ~0xff) | I); + Utility.Memset(NumToPlace,0,NToPl.Length); for (I=6;I>=0;I--) - NumToPlace[I]=(7-I)*32; + NumToPlace[I]=(byte)((7-I)*32); } +#if !RarV2017_RAR5ONLY void CopyString15(uint Distance,uint Length) { @@ -504,8 +503,7 @@ uint DecodeNum(uint Num,uint StartPos,uint *DecTab,uint *PosTab) Inp.faddbits(StartPos); return(((Num-(I ? DecTab[I-1]:0))>>(16-StartPos))+PosTab[StartPos]); } +#endif } } -#endif -#endif \ No newline at end of file diff --git a/src/SharpCompress/Compressors/Rar/UnpackV2017/Unpack.unpack20_cpp.cs b/src/SharpCompress/Compressors/Rar/UnpackV2017/Unpack.unpack20_cpp.cs index 4744849b..40c5f055 100644 --- a/src/SharpCompress/Compressors/Rar/UnpackV2017/Unpack.unpack20_cpp.cs +++ b/src/SharpCompress/Compressors/Rar/UnpackV2017/Unpack.unpack20_cpp.cs @@ -1,6 +1,4 @@ -#if RARWIP -#if !RarV2017_RAR5ONLY -#if !Rar2017_64bit +#if !Rar2017_64bit using nint = System.Int32; using nuint = System.UInt32; using size_t = System.UInt32; @@ -17,13 +15,14 @@ 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 void CopyString20(uint Length,uint Distance) { LastDist=OldDist[OldDistPtr++ & 3]=Distance; @@ -292,6 +291,7 @@ void ReadLastTables() ReadTables20(); } +#endif void UnpInitData20(bool Solid) { @@ -303,12 +303,15 @@ void UnpInitData20(bool Solid) UnpCurChannel=0; UnpChannels=1; - memset(AudV,0,sizeof(AudV)); + //memset(AudV,0,sizeof(AudV)); + AudV = new AudioVariables[4]; Utility.Memset(UnpOldTable20, 0, UnpOldTable20.Length); - memset(MD,0,sizeof(MD)); + //memset(MD,0,sizeof(MD)); + MD = new DecodeTable[4]; } } +#if !RarV2017_RAR5ONLY byte DecodeAudio(int Delta) { @@ -402,8 +405,7 @@ byte DecodeAudio(int Delta) } return (byte)Ch; } +#endif } } -#endif -#endif \ No newline at end of file diff --git a/src/SharpCompress/Compressors/Rar/UnpackV2017/Unpack.unpack30_cpp.cs b/src/SharpCompress/Compressors/Rar/UnpackV2017/Unpack.unpack30_cpp.cs index f440d0d5..4b63e61b 100644 --- a/src/SharpCompress/Compressors/Rar/UnpackV2017/Unpack.unpack30_cpp.cs +++ b/src/SharpCompress/Compressors/Rar/UnpackV2017/Unpack.unpack30_cpp.cs @@ -1,6 +1,4 @@ -#if RARWIP -#if !RarV2017_RAR5ONLY -#if !Rar2017_64bit +#if !Rar2017_64bit using nint = System.Int32; using nuint = System.UInt32; using size_t = System.UInt32; @@ -18,12 +16,14 @@ using System.Text; using System.Threading.Tasks; using static SharpCompress.Compressors.Rar.UnpackV2017.PackDef; using static SharpCompress.Compressors.Rar.UnpackV2017.UnpackGlobal; -using static SharpCompress.Compressors.Rar.UnpackV2017.Unpack.Unpack30Local; +//using static SharpCompress.Compressors.Rar.UnpackV2017.Unpack.Unpack30Local; namespace SharpCompress.Compressors.Rar.UnpackV2017 { internal partial class Unpack { + +#if !RarV2017_RAR5ONLY // We use it instead of direct PPM.DecodeChar call to be sure that // we reset PPM structures in case of corrupt data. It is important, // because these structures can be invalid after PPM.DecodeChar returned -1. @@ -761,6 +761,7 @@ bool ReadTables30() return true; } +#endif void UnpInitData30(bool Solid) { @@ -794,7 +795,6 @@ void InitFilters30(bool Solid) PrgStack.Clear(); } + } } -#endif -#endif \ No newline at end of file diff --git a/src/SharpCompress/Compressors/Rar/UnpackV2017/Unpack.unpack50_cpp.cs b/src/SharpCompress/Compressors/Rar/UnpackV2017/Unpack.unpack50_cpp.cs index d39dbb98..8e5b7b85 100644 --- a/src/SharpCompress/Compressors/Rar/UnpackV2017/Unpack.unpack50_cpp.cs +++ b/src/SharpCompress/Compressors/Rar/UnpackV2017/Unpack.unpack50_cpp.cs @@ -1,5 +1,4 @@ -#if RARWIP -#if !Rar2017_64bit +#if !Rar2017_64bit using nint = System.Int32; using nuint = System.UInt32; using size_t = System.UInt32; @@ -724,4 +723,3 @@ void InitFilters() } } -#endif \ No newline at end of file diff --git a/src/SharpCompress/Compressors/Rar/UnpackV2017/Unpack.unpack_cpp.cs b/src/SharpCompress/Compressors/Rar/UnpackV2017/Unpack.unpack_cpp.cs index 502763ad..4126bf34 100644 --- a/src/SharpCompress/Compressors/Rar/UnpackV2017/Unpack.unpack_cpp.cs +++ b/src/SharpCompress/Compressors/Rar/UnpackV2017/Unpack.unpack_cpp.cs @@ -1,5 +1,4 @@ -#if RARWIP -#if !Rar2017_64bit +#if !Rar2017_64bit using nint = System.Int32; using nuint = System.UInt32; using size_t = System.UInt32; @@ -44,13 +43,11 @@ public Unpack(/* ComprDataIO *DataIO */) // It prevents crash if first DoUnpack call is later made with wrong // (true) 'Solid' value. UnpInitData(false); -#if !RarV2017_RAR5ONLY #if !RarV2017_SFX_MODULE // RAR 1.5 decompression initialization UnpInitData15(false); InitHuff(); #endif -#endif } // later: may need Dispose() if we support thread pool @@ -191,6 +188,9 @@ void DoUnpack(uint Method,bool Solid) #endif Unpack5(Solid); break; +#if !Rar2017_NOSTRICT + default: throw new InvalidFormatException("unknown compression method " + Method); +#endif } } @@ -222,12 +222,10 @@ void UnpInitData(bool Solid) //memset(&BlockHeader,0,sizeof(BlockHeader)); BlockHeader = new UnpackBlockHeader(); BlockHeader.BlockSize=-1; // '-1' means not defined yet. -#if !RarV2017_RAR5ONLY #if !RarV2017_SFX_MODULE UnpInitData20(Solid); #endif UnpInitData30(Solid); -#endif UnpInitData50(Solid); } @@ -379,4 +377,3 @@ void MakeDecodeTables(byte[] LengthTable, int offset, DecodeTable Dec,uint Size) } } -#endif \ No newline at end of file diff --git a/src/SharpCompress/Compressors/Rar/UnpackV2017/Unpack.unpackinline_cpp.cs b/src/SharpCompress/Compressors/Rar/UnpackV2017/Unpack.unpackinline_cpp.cs index 0f839584..cd2fa8f0 100644 --- a/src/SharpCompress/Compressors/Rar/UnpackV2017/Unpack.unpackinline_cpp.cs +++ b/src/SharpCompress/Compressors/Rar/UnpackV2017/Unpack.unpackinline_cpp.cs @@ -1,5 +1,4 @@ -#if RARWIP -#if !Rar2017_64bit +#if !Rar2017_64bit using nint = System.Int32; using nuint = System.UInt32; using size_t = System.UInt32; @@ -175,4 +174,3 @@ uint SlotToLength(BitInput Inp,uint Slot) } } -#endif \ No newline at end of file diff --git a/src/SharpCompress/Compressors/Rar/UnpackV2017/unpack_hpp.cs b/src/SharpCompress/Compressors/Rar/UnpackV2017/unpack_hpp.cs index 3e238e24..afea642c 100644 --- a/src/SharpCompress/Compressors/Rar/UnpackV2017/unpack_hpp.cs +++ b/src/SharpCompress/Compressors/Rar/UnpackV2017/unpack_hpp.cs @@ -1,5 +1,4 @@ -#if RARWIP -#if !Rar2017_64bit +#if !Rar2017_64bit using nint = System.Int32; using nuint = System.UInt32; using size_t = System.UInt32; @@ -341,7 +340,7 @@ internal partial class Unpack /***************************** Unpack v 2.0 *********************************/ //void Unpack20(bool Solid); - readonly DecodeTable[] MD = new DecodeTable[4]; // Decode multimedia data, up to 4 channels. + DecodeTable[] MD = new DecodeTable[4]; // Decode multimedia data, up to 4 channels. readonly byte[] UnpOldTable20 = new byte[MC20*4]; bool UnpAudioBlock; @@ -442,4 +441,3 @@ internal partial class Unpack } } -#endif \ No newline at end of file