diff --git a/SabreTools.Library/DatFiles/DatFile.cs b/SabreTools.Library/DatFiles/DatFile.cs index 8caa617e..c7daac21 100644 --- a/SabreTools.Library/DatFiles/DatFile.cs +++ b/SabreTools.Library/DatFiles/DatFile.cs @@ -4064,7 +4064,7 @@ namespace SabreTools.Library.DatFiles foreach (BaseFile entry in entries) { DatItem datItem = Utilities.GetDatItem(entry); - usedInternally &= RebuildIndividualFile(datItem, file, outDir, date, inverse, outputFormat, + usedInternally |= RebuildIndividualFile(datItem, file, outDir, date, inverse, outputFormat, romba, updateDat, !isTorrentGzip /* isZip */, headerToCheckAgainst); } } @@ -4094,8 +4094,8 @@ namespace SabreTools.Library.DatFiles private bool RebuildIndividualFile(DatItem datItem, string file, string outDir, bool date, bool inverse, OutputFormat outputFormat, bool romba, bool updateDat, bool? isZip, string headerToCheckAgainst) { - // Set the output value - bool rebuilt = true; + // Set the initial output value + bool rebuilt = false; // If the DatItem is a Disk, force rebuilding to a folder except if TGZ if (datItem.ItemType == ItemType.Disk && outputFormat != OutputFormat.TorrentGzip) @@ -4152,14 +4152,12 @@ namespace SabreTools.Library.DatFiles try { File.Copy(file, outDir); - rebuilt &= true; + return true; } catch { - rebuilt = false; + return false; } - - return rebuilt; } // Get a generic stream for the file @@ -4237,14 +4235,12 @@ namespace SabreTools.Library.DatFiles try { File.Copy(file, outDir); - rebuilt &= true; + return true; } catch { - rebuilt = false; + return false; } - - return rebuilt; } // Get a generic stream for the file diff --git a/SabreTools.Library/Data/Constants.cs b/SabreTools.Library/Data/Constants.cs index 2a5762d7..a75f131f 100644 --- a/SabreTools.Library/Data/Constants.cs +++ b/SabreTools.Library/Data/Constants.cs @@ -19,7 +19,7 @@ namespace SabreTools.Library.Data /// /// The current toolset version to be used by all child applications /// - public readonly static string Version = "v1.0.0-" + Assembly.GetExecutingAssembly().GetLinkerTime().ToString("yyyy-MM-dd HH:mm:ss"); + public readonly static string Version = "v1.0.0-" + File.GetCreationTime(Assembly.GetExecutingAssembly().Location).ToString("yyyy-MM-dd HH:mm:ss"); public const int HeaderHeight = 3; #region 0-byte file constants diff --git a/SabreTools.Library/Data/Flags.cs b/SabreTools.Library/Data/Flags.cs index 9701e677..dba9adf5 100644 --- a/SabreTools.Library/Data/Flags.cs +++ b/SabreTools.Library/Data/Flags.cs @@ -231,18 +231,6 @@ namespace SabreTools.Library.Data LastAccessTimePresent = 1 << 3, } - /// - /// Zipfile special status - /// - /// https://github.com/gjefferyes/RomVault/blob/5a93500001f0d068f32cf77a048950717507f733/ROMVault2/SupportedFiles/ZipEnums.cs - [Flags] - public enum ZipStatus - { - None = 0x00, - TorrentZip = 1 << 0, - ExtraData = 1 << 1 - } - #endregion #region DatFile related diff --git a/SabreTools.Library/Data/Globals.cs b/SabreTools.Library/Data/Globals.cs index eabaef63..00245f65 100644 --- a/SabreTools.Library/Data/Globals.cs +++ b/SabreTools.Library/Data/Globals.cs @@ -21,9 +21,6 @@ namespace SabreTools.Library.Data private static Logger _logger = null; private static int _maxDegreeOfParallelism = System.Environment.ProcessorCount; - private static string _exeName = new Uri(Assembly.GetExecutingAssembly().GetName().CodeBase).LocalPath; - private static string _exeDir = Path.GetDirectoryName(_exeName); - private static string _args = string.Join(" ", Environment.GetCommandLineArgs()); #endregion @@ -41,11 +38,13 @@ namespace SabreTools.Library.Data } set { _logger = value; } } + public static int MaxThreads { get { return _maxDegreeOfParallelism; } set { _maxDegreeOfParallelism = value; } } + public static ParallelOptions ParallelOptions { get @@ -56,26 +55,20 @@ namespace SabreTools.Library.Data }; } } + public static string ExeName { - get - { - return _exeName; - } + get { return new Uri(Assembly.GetExecutingAssembly().GetName().CodeBase).LocalPath; } } + public static string ExeDir { - get - { - return _exeDir; - } + get { return Path.GetDirectoryName(ExeName); } } + public static string CommandLineArgs { - get - { - return _args; - } + get { return string.Join(" ", Environment.GetCommandLineArgs()); } } #endregion diff --git a/SabreTools.Library/External/Compress/File/File.cs b/SabreTools.Library/External/Compress/File/File.cs index 6cd580af..82a39660 100644 --- a/SabreTools.Library/External/Compress/File/File.cs +++ b/SabreTools.Library/External/Compress/File/File.cs @@ -171,7 +171,7 @@ namespace Compress.File - public void ZipFileAddDirectory() + public void ZipFileAddZeroLengthFile() { throw new NotImplementedException(); } @@ -211,4 +211,4 @@ namespace Compress.File } -} +} \ No newline at end of file diff --git a/SabreTools.Library/External/Compress/ICompress.cs b/SabreTools.Library/External/Compress/ICompress.cs index 5e5c147c..3b8fe765 100644 --- a/SabreTools.Library/External/Compress/ICompress.cs +++ b/SabreTools.Library/External/Compress/ICompress.cs @@ -15,7 +15,7 @@ namespace Compress ZipOpenType ZipOpen { get; } - ZipReturn ZipFileOpen(string newFilename, long timestamp =-1, bool readHeaders=true); + ZipReturn ZipFileOpen(string newFilename, long timestamp = -1, bool readHeaders = true); ZipReturn ZipFileOpen(Stream inStream); void ZipFileClose(); @@ -30,11 +30,11 @@ namespace Compress string ZipFilename { get; } long TimeStamp { get; } - void ZipFileAddDirectory(); + void ZipFileAddZeroLengthFile(); ZipReturn ZipFileCreate(string newFilename); ZipReturn ZipFileCloseWriteStream(byte[] crc32); void ZipFileCloseFailed(); } -} +} \ No newline at end of file diff --git a/SabreTools.Library/External/Compress/SevenZip/Common/ICoder.cs b/SabreTools.Library/External/Compress/SevenZip/Common/ICoder.cs index 2677e52f..022fbc62 100644 --- a/SabreTools.Library/External/Compress/SevenZip/Common/ICoder.cs +++ b/SabreTools.Library/External/Compress/SevenZip/Common/ICoder.cs @@ -60,8 +60,8 @@ namespace Compress.SevenZip.Common public interface ICoder2 { void Code(ISequentialInStream []inStreams, - const UInt64 []inSizes, - ISequentialOutStream []outStreams, + const UInt64 []inSizes, + ISequentialOutStream []outStreams, UInt64 []outSizes, ICodeProgress progress); }; @@ -149,4 +149,4 @@ namespace Compress.SevenZip.Common { void SetDecoderProperties(byte[] properties); } -} +} \ No newline at end of file diff --git a/SabreTools.Library/External/Compress/SevenZip/Compress/BZip2/BZip2Constants.cs b/SabreTools.Library/External/Compress/SevenZip/Compress/BZip2/BZip2Constants.cs index c945d571..b44c2d31 100644 --- a/SabreTools.Library/External/Compress/SevenZip/Compress/BZip2/BZip2Constants.cs +++ b/SabreTools.Library/External/Compress/SevenZip/Compress/BZip2/BZip2Constants.cs @@ -97,4 +97,4 @@ namespace Compress.SevenZip.Compress.BZip2 936, 638 }; } -} +} \ No newline at end of file diff --git a/SabreTools.Library/External/Compress/SevenZip/Compress/BZip2/CBZip2InputStream.cs b/SabreTools.Library/External/Compress/SevenZip/Compress/BZip2/CBZip2InputStream.cs index da697686..110f0b47 100644 --- a/SabreTools.Library/External/Compress/SevenZip/Compress/BZip2/CBZip2InputStream.cs +++ b/SabreTools.Library/External/Compress/SevenZip/Compress/BZip2/CBZip2InputStream.cs @@ -1130,4 +1130,4 @@ namespace Compress.SevenZip.Compress.BZip2 } } } -} +} \ No newline at end of file diff --git a/SabreTools.Library/External/Compress/SevenZip/Compress/BZip2/CBZip2OutputStream.cs b/SabreTools.Library/External/Compress/SevenZip/Compress/BZip2/CBZip2OutputStream.cs index 24b763d7..71a833b5 100644 --- a/SabreTools.Library/External/Compress/SevenZip/Compress/BZip2/CBZip2OutputStream.cs +++ b/SabreTools.Library/External/Compress/SevenZip/Compress/BZip2/CBZip2OutputStream.cs @@ -1982,4 +1982,4 @@ namespace Compress.SevenZip.Compress.BZip2 } } } -} +} \ No newline at end of file diff --git a/SabreTools.Library/External/Compress/SevenZip/Compress/BZip2/CRC.cs b/SabreTools.Library/External/Compress/SevenZip/Compress/BZip2/CRC.cs index e6457051..e99aa57a 100644 --- a/SabreTools.Library/External/Compress/SevenZip/Compress/BZip2/CRC.cs +++ b/SabreTools.Library/External/Compress/SevenZip/Compress/BZip2/CRC.cs @@ -135,4 +135,4 @@ namespace Compress.SevenZip.Compress.BZip2 internal int globalCrc; } -} +} \ No newline at end of file diff --git a/SabreTools.Library/External/Compress/SevenZip/Compress/LZ/LzBinTree.cs b/SabreTools.Library/External/Compress/SevenZip/Compress/LZ/LzBinTree.cs index 4ce018eb..5d4f579e 100644 --- a/SabreTools.Library/External/Compress/SevenZip/Compress/LZ/LzBinTree.cs +++ b/SabreTools.Library/External/Compress/SevenZip/Compress/LZ/LzBinTree.cs @@ -363,4 +363,4 @@ namespace Compress.SevenZip.Compress.LZ public void SetCutValue(UInt32 cutValue) { _cutValue = cutValue; } } -} +} \ No newline at end of file diff --git a/SabreTools.Library/External/Compress/SevenZip/Compress/LZ/LzInWindow.cs b/SabreTools.Library/External/Compress/SevenZip/Compress/LZ/LzInWindow.cs index 2fd6642a..322e27d3 100644 --- a/SabreTools.Library/External/Compress/SevenZip/Compress/LZ/LzInWindow.cs +++ b/SabreTools.Library/External/Compress/SevenZip/Compress/LZ/LzInWindow.cs @@ -145,4 +145,4 @@ namespace Compress.SevenZip.Compress.LZ } } } -} +} \ No newline at end of file diff --git a/SabreTools.Library/External/Compress/SevenZip/Compress/LZ/LzOutWindow.cs b/SabreTools.Library/External/Compress/SevenZip/Compress/LZ/LzOutWindow.cs index f5b4a9f8..03cd34c8 100644 --- a/SabreTools.Library/External/Compress/SevenZip/Compress/LZ/LzOutWindow.cs +++ b/SabreTools.Library/External/Compress/SevenZip/Compress/LZ/LzOutWindow.cs @@ -183,4 +183,4 @@ namespace Compress.SevenZip.Compress.LZ } } } -} +} \ No newline at end of file diff --git a/SabreTools.Library/External/Compress/SevenZip/Compress/LZMA/LzmaBase.cs b/SabreTools.Library/External/Compress/SevenZip/Compress/LZMA/LzmaBase.cs index a24fd1f5..15d3af58 100644 --- a/SabreTools.Library/External/Compress/SevenZip/Compress/LZMA/LzmaBase.cs +++ b/SabreTools.Library/External/Compress/SevenZip/Compress/LZMA/LzmaBase.cs @@ -71,4 +71,4 @@ namespace Compress.SevenZip.Compress.LZMA (1 << kNumHighLenBits); public const uint kMatchMaxLen = kMatchMinLen + kNumLenSymbols - 1; } -} +} \ No newline at end of file diff --git a/SabreTools.Library/External/Compress/SevenZip/Compress/LZMA/LzmaDecoder.cs b/SabreTools.Library/External/Compress/SevenZip/Compress/LZMA/LzmaDecoder.cs index 42705b2a..9e704ac4 100644 --- a/SabreTools.Library/External/Compress/SevenZip/Compress/LZMA/LzmaDecoder.cs +++ b/SabreTools.Library/External/Compress/SevenZip/Compress/LZMA/LzmaDecoder.cs @@ -387,7 +387,7 @@ namespace Compress.SevenZip.Compress.LZMA set { } } public override void Flush() { } - public override int Read(byte[] buffer, int offset, int count) + public override int Read(byte[] buffer, int offset, int count) { return 0; } @@ -401,4 +401,4 @@ namespace Compress.SevenZip.Compress.LZMA public override void SetLength(long value) {} */ } -} +} \ No newline at end of file diff --git a/SabreTools.Library/External/Compress/SevenZip/Compress/LZMA/LzmaEncoder.cs b/SabreTools.Library/External/Compress/SevenZip/Compress/LZMA/LzmaEncoder.cs index 61340432..dc7c98ed 100644 --- a/SabreTools.Library/External/Compress/SevenZip/Compress/LZMA/LzmaEncoder.cs +++ b/SabreTools.Library/External/Compress/SevenZip/Compress/LZMA/LzmaEncoder.cs @@ -876,7 +876,7 @@ namespace Compress.SevenZip.Compress.LZMA } } - UInt32 startLen = 2; // speed optimization + UInt32 startLen = 2; // speed optimization for (UInt32 repIndex = 0; repIndex < Base.kNumRepDistances; repIndex++) { @@ -1418,7 +1418,7 @@ namespace Compress.SevenZip.Compress.LZMA } - static string[] kMatchFinderIDs = + static string[] kMatchFinderIDs = { "BT2", "BT4", @@ -1544,4 +1544,4 @@ namespace Compress.SevenZip.Compress.LZMA } } -} +} \ No newline at end of file diff --git a/SabreTools.Library/External/Compress/SevenZip/Compress/LZMA/LzmaEncoderProperties.cs b/SabreTools.Library/External/Compress/SevenZip/Compress/LZMA/LzmaEncoderProperties.cs index db866ebf..9a51f9f0 100644 --- a/SabreTools.Library/External/Compress/SevenZip/Compress/LZMA/LzmaEncoderProperties.cs +++ b/SabreTools.Library/External/Compress/SevenZip/Compress/LZMA/LzmaEncoderProperties.cs @@ -54,4 +54,4 @@ namespace Compress.SevenZip.Compress.LZMA }; } } -} +} \ No newline at end of file diff --git a/SabreTools.Library/External/Compress/SevenZip/Compress/LZMA/LzmaStream.cs b/SabreTools.Library/External/Compress/SevenZip/Compress/LZMA/LzmaStream.cs index 564e162a..a6be2672 100644 --- a/SabreTools.Library/External/Compress/SevenZip/Compress/LZMA/LzmaStream.cs +++ b/SabreTools.Library/External/Compress/SevenZip/Compress/LZMA/LzmaStream.cs @@ -181,11 +181,11 @@ namespace Compress.SevenZip.Compress.LZMA toProcess = (int)availableBytes; outWindow.SetLimit(toProcess); - if(uncompressedChunk) + if (uncompressedChunk) { inputPosition += outWindow.CopyStream(inputStream, toProcess); } - else if(decoder.Code(dictionarySize, outWindow, rangeDecoder) + else if (decoder.Code(dictionarySize, outWindow, rangeDecoder) && outputSize < 0) { availableBytes = outWindow.AvailableBytes; @@ -281,10 +281,10 @@ namespace Compress.SevenZip.Compress.LZMA public override long Seek(long offset, SeekOrigin origin) { - if (origin!=SeekOrigin.Current) - throw new NotImplementedException(); + if (origin != SeekOrigin.Current) + throw new NotImplementedException(); - byte[] tmpBuff=new byte[1024]; + byte[] tmpBuff = new byte[1024]; long sizeToGo = offset; while (sizeToGo > 0) { @@ -292,7 +292,7 @@ namespace Compress.SevenZip.Compress.LZMA Read(tmpBuff, 0, sizenow); sizeToGo -= sizenow; } - + return offset; } @@ -315,4 +315,4 @@ namespace Compress.SevenZip.Compress.LZMA } } } -} +} \ No newline at end of file diff --git a/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/H/ModelPPM.cs b/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/H/ModelPPM.cs index 081a70d5..424709f1 100644 --- a/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/H/ModelPPM.cs +++ b/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/H/ModelPPM.cs @@ -883,7 +883,7 @@ namespace Compress.SevenZip.Compress.PPmd.H charMask[rs.Symbol] = 0; prevSuccess = 0; } - for (;;) + for (; ; ) { State s = tempState1.Initialize(Heap); int i; @@ -920,7 +920,7 @@ namespace Compress.SevenZip.Compress.PPmd.H { byte symbol; State ps = tempState2.Initialize(Heap); - for (hiCnt = 0, i = 0, ps.Address = minContext.ps[i]; (hiCnt += ps.Freq) <= count; i++, ps.Address = minContext.ps[i]); + for (hiCnt = 0, i = 0, ps.Address = minContext.ps[i]; (hiCnt += ps.Freq) <= count; i++, ps.Address = minContext.ps[i]) ; s.Address = ps.Address; decoder.Decode((uint)(hiCnt - s.Freq), (uint)s.Freq); see.update(); diff --git a/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/H/PPMContext.cs b/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/H/PPMContext.cs index 10cc6be4..21cb6af1 100644 --- a/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/H/PPMContext.cs +++ b/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/H/PPMContext.cs @@ -305,7 +305,7 @@ namespace Compress.SevenZip.Compress.PPmd.H // byte[] bytes = model.getSubAlloc().getHeap(); // int p1 = state1.Address; // int p2 = state2.Address; - // + // // for (int i = 0; i < StatePtr.size; i++) { // byte temp = bytes[p1+i]; // bytes[p1+i] = bytes[p2+i]; diff --git a/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/H/RangeCoder.cs b/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/H/RangeCoder.cs index da085256..a40813a6 100644 --- a/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/H/RangeCoder.cs +++ b/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/H/RangeCoder.cs @@ -73,7 +73,7 @@ namespace Compress.SevenZip.Compress.PPmd.H internal void AriDecNormalize() { - // while ((low ^ (low + range)) < TOP || range < BOT && ((range = -low & (BOT - 1)) != 0 ? true : true)) + // while ((low ^ (low + range)) < TOP || range < BOT && ((range = -low & (BOT - 1)) != 0 ? true : true)) // { // code = ((code << 8) | unpackRead.getChar()&0xff)&uintMask; // range = (range << 8)&uintMask; diff --git a/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/H/SubAllocator.cs b/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/H/SubAllocator.cs index 957b8205..e5a3af55 100644 --- a/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/H/SubAllocator.cs +++ b/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/H/SubAllocator.cs @@ -206,7 +206,7 @@ namespace Compress.SevenZip.Compress.PPmd.H // Bug fixed freeListPos = heapStart + allocSize; //UPGRADE_ISSUE: The following fragment of code could not be parsed and was not converted. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1156'" - //assert(realAllocSize - tempMemBlockPos == RarMemBlock.size): realAllocSize + //assert(realAllocSize - tempMemBlockPos == RarMemBlock.size): realAllocSize //+ + tempMemBlockPos + + RarMemBlock.size; // Init freeList diff --git a/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/I1/Allocator.cs b/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/I1/Allocator.cs index e85afa88..5a9b9d71 100644 --- a/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/I1/Allocator.cs +++ b/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/I1/Allocator.cs @@ -454,4 +454,4 @@ namespace Compress.SevenZip.Compress.PPmd.I1 #endregion } -} +} \ No newline at end of file diff --git a/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/I1/Coder.cs b/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/I1/Coder.cs index c243ae60..3f9db375 100644 --- a/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/I1/Coder.cs +++ b/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/I1/Coder.cs @@ -33,9 +33,9 @@ namespace Compress.SevenZip.Compress.PPmd.I1 public void RangeEncoderNormalize(Stream stream) { - while ((low ^ (low + range)) < RangeTop || range < RangeBottom && ((range = (uint) -low & (RangeBottom - 1)) != 0 || true)) + while ((low ^ (low + range)) < RangeTop || range < RangeBottom && ((range = (uint)-low & (RangeBottom - 1)) != 0 || true)) { - stream.WriteByte((byte) (low >> 24)); + stream.WriteByte((byte)(low >> 24)); range <<= 8; low <<= 8; } @@ -57,7 +57,7 @@ namespace Compress.SevenZip.Compress.PPmd.I1 { for (uint index = 0; index < 4; index++) { - stream.WriteByte((byte) (low >> 24)); + stream.WriteByte((byte)(low >> 24)); low <<= 8; } } @@ -68,14 +68,14 @@ namespace Compress.SevenZip.Compress.PPmd.I1 code = 0; range = uint.MaxValue; for (uint index = 0; index < 4; index++) - code = (code << 8) | (byte) stream.ReadByte(); + code = (code << 8) | (byte)stream.ReadByte(); } public void RangeDecoderNormalize(Stream stream) { - while ((low ^ (low + range)) < RangeTop || range < RangeBottom && ((range = (uint) -low & (RangeBottom - 1)) != 0 || true)) + while ((low ^ (low + range)) < RangeTop || range < RangeBottom && ((range = (uint)-low & (RangeBottom - 1)) != 0 || true)) { - code = (code << 8) | (byte) stream.ReadByte(); + code = (code << 8) | (byte)stream.ReadByte(); range <<= 8; low <<= 8; } @@ -97,4 +97,4 @@ namespace Compress.SevenZip.Compress.PPmd.I1 range *= HighCount - LowCount; } } -} +} \ No newline at end of file diff --git a/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/I1/MemoryNode.cs b/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/I1/MemoryNode.cs index bde225f8..923381b9 100644 --- a/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/I1/MemoryNode.cs +++ b/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/I1/MemoryNode.cs @@ -48,13 +48,13 @@ namespace Compress.SevenZip.Compress.PPmd.I1 /// public uint Stamp { - get { return ((uint) Memory[Address]) | ((uint) Memory[Address + 1]) << 8 | ((uint) Memory[Address + 2]) << 16 | ((uint) Memory[Address + 3]) << 24; } + get { return ((uint)Memory[Address]) | ((uint)Memory[Address + 1]) << 8 | ((uint)Memory[Address + 2]) << 16 | ((uint)Memory[Address + 3]) << 24; } set { - Memory[Address] = (byte) value; - Memory[Address + 1] = (byte) (value >> 8); - Memory[Address + 2] = (byte) (value >> 16); - Memory[Address + 3] = (byte) (value >> 24); + Memory[Address] = (byte)value; + Memory[Address + 1] = (byte)(value >> 8); + Memory[Address + 2] = (byte)(value >> 16); + Memory[Address + 3] = (byte)(value >> 24); } } @@ -63,13 +63,13 @@ namespace Compress.SevenZip.Compress.PPmd.I1 /// public MemoryNode Next { - get { return new MemoryNode(((uint) Memory[Address + 4]) | ((uint) Memory[Address + 5]) << 8 | ((uint) Memory[Address + 6]) << 16 | ((uint) Memory[Address + 7]) << 24, Memory); } + get { return new MemoryNode(((uint)Memory[Address + 4]) | ((uint)Memory[Address + 5]) << 8 | ((uint)Memory[Address + 6]) << 16 | ((uint)Memory[Address + 7]) << 24, Memory); } set { - Memory[Address + 4] = (byte) value.Address; - Memory[Address + 5] = (byte) (value.Address >> 8); - Memory[Address + 6] = (byte) (value.Address >> 16); - Memory[Address + 7] = (byte) (value.Address >> 24); + Memory[Address + 4] = (byte)value.Address; + Memory[Address + 5] = (byte)(value.Address >> 8); + Memory[Address + 6] = (byte)(value.Address >> 16); + Memory[Address + 7] = (byte)(value.Address >> 24); } } @@ -78,13 +78,13 @@ namespace Compress.SevenZip.Compress.PPmd.I1 /// public uint UnitCount { - get { return ((uint) Memory[Address + 8]) | ((uint) Memory[Address + 9]) << 8 | ((uint) Memory[Address + 10]) << 16 | ((uint) Memory[Address + 11]) << 24; } + get { return ((uint)Memory[Address + 8]) | ((uint)Memory[Address + 9]) << 8 | ((uint)Memory[Address + 10]) << 16 | ((uint)Memory[Address + 11]) << 24; } set { - Memory[Address + 8] = (byte) value; - Memory[Address + 9] = (byte) (value >> 8); - Memory[Address + 10] = (byte) (value >> 16); - Memory[Address + 11] = (byte) (value >> 24); + Memory[Address + 8] = (byte)value; + Memory[Address + 9] = (byte)(value >> 8); + Memory[Address + 10] = (byte)(value >> 16); + Memory[Address + 11] = (byte)(value >> 24); } } @@ -157,7 +157,7 @@ namespace Compress.SevenZip.Compress.PPmd.I1 /// public static MemoryNode operator +(MemoryNode memoryNode, int offset) { - memoryNode.Address = (uint) (memoryNode.Address + offset * Size); + memoryNode.Address = (uint)(memoryNode.Address + offset * Size); return memoryNode; } @@ -181,7 +181,7 @@ namespace Compress.SevenZip.Compress.PPmd.I1 /// public static MemoryNode operator -(MemoryNode memoryNode, int offset) { - memoryNode.Address = (uint) (memoryNode.Address - offset * Size); + memoryNode.Address = (uint)(memoryNode.Address - offset * Size); return memoryNode; } @@ -228,7 +228,7 @@ namespace Compress.SevenZip.Compress.PPmd.I1 { if (obj is MemoryNode) { - MemoryNode memoryNode = (MemoryNode) obj; + MemoryNode memoryNode = (MemoryNode)obj; return memoryNode.Address == Address; } return base.Equals(obj); @@ -243,4 +243,4 @@ namespace Compress.SevenZip.Compress.PPmd.I1 return Address.GetHashCode(); } } -} +} \ No newline at end of file diff --git a/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/I1/Model.cs b/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/I1/Model.cs index f464c626..5b531535 100644 --- a/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/I1/Model.cs +++ b/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/I1/Model.cs @@ -816,4 +816,4 @@ namespace Compress.SevenZip.Compress.PPmd.I1 #endregion } -} +} \ No newline at end of file diff --git a/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/I1/ModelRestorationMethod.cs b/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/I1/ModelRestorationMethod.cs index e7a7cdef..892dabf2 100644 --- a/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/I1/ModelRestorationMethod.cs +++ b/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/I1/ModelRestorationMethod.cs @@ -26,4 +26,4 @@ namespace Compress.SevenZip.Compress.PPmd.I1 /// Freeze = 2 } -} +} \ No newline at end of file diff --git a/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/I1/Pointer.cs b/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/I1/Pointer.cs index 2df401a0..330c8e99 100644 --- a/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/I1/Pointer.cs +++ b/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/I1/Pointer.cs @@ -45,18 +45,18 @@ namespace Compress.SevenZip.Compress.PPmd.I1 { get { - #if DEBUG +#if DEBUG if (Address == 0) throw new InvalidOperationException("The pointer being indexed is a null pointer."); - #endif +#endif return Memory[Address + offset]; } set { - #if DEBUG +#if DEBUG if (Address == 0) throw new InvalidOperationException("The pointer being indexed is a null pointer."); - #endif +#endif Memory[Address + offset] = value; } } @@ -99,11 +99,11 @@ namespace Compress.SevenZip.Compress.PPmd.I1 /// public static Pointer operator +(Pointer pointer, int offset) { - #if DEBUG +#if DEBUG if (pointer.Address == 0) throw new InvalidOperationException("The pointer is a null pointer."); - #endif - pointer.Address = (uint) (pointer.Address + offset); +#endif + pointer.Address = (uint)(pointer.Address + offset); return pointer; } @@ -115,10 +115,10 @@ namespace Compress.SevenZip.Compress.PPmd.I1 /// public static Pointer operator +(Pointer pointer, uint offset) { - #if DEBUG +#if DEBUG if (pointer.Address == 0) throw new InvalidOperationException("The pointer is a null pointer."); - #endif +#endif pointer.Address += offset; return pointer; } @@ -130,10 +130,10 @@ namespace Compress.SevenZip.Compress.PPmd.I1 /// public static Pointer operator ++(Pointer pointer) { - #if DEBUG +#if DEBUG if (pointer.Address == 0) throw new InvalidOperationException("The pointer being incremented is a null pointer."); - #endif +#endif pointer.Address++; return pointer; } @@ -146,11 +146,11 @@ namespace Compress.SevenZip.Compress.PPmd.I1 /// public static Pointer operator -(Pointer pointer, int offset) { - #if DEBUG +#if DEBUG if (pointer.Address == 0) throw new InvalidOperationException("The pointer is a null pointer."); - #endif - pointer.Address = (uint) (pointer.Address - offset); +#endif + pointer.Address = (uint)(pointer.Address - offset); return pointer; } @@ -162,10 +162,10 @@ namespace Compress.SevenZip.Compress.PPmd.I1 /// public static Pointer operator -(Pointer pointer, uint offset) { - #if DEBUG +#if DEBUG if (pointer.Address == 0) throw new InvalidOperationException("The pointer is a null pointer."); - #endif +#endif pointer.Address -= offset; return pointer; } @@ -177,10 +177,10 @@ namespace Compress.SevenZip.Compress.PPmd.I1 /// public static Pointer operator --(Pointer pointer) { - #if DEBUG +#if DEBUG if (pointer.Address == 0) throw new InvalidOperationException("The pointer being decremented is a null pointer."); - #endif +#endif pointer.Address--; return pointer; } @@ -193,12 +193,12 @@ namespace Compress.SevenZip.Compress.PPmd.I1 /// The number of bytes between the two pointers. public static uint operator -(Pointer pointer1, Pointer pointer2) { - #if DEBUG +#if DEBUG if (pointer1.Address == 0) throw new InvalidOperationException("The pointer to the left of the subtraction operator is a null pointer."); if (pointer2.Address == 0) throw new InvalidOperationException("The pointer to the right of the subtraction operator is a null pointer."); - #endif +#endif return pointer1.Address - pointer2.Address; } @@ -210,12 +210,12 @@ namespace Compress.SevenZip.Compress.PPmd.I1 /// public static bool operator <(Pointer pointer1, Pointer pointer2) { - #if DEBUG +#if DEBUG if (pointer1.Address == 0) throw new InvalidOperationException("The pointer to the left of the less than operator is a null pointer."); if (pointer2.Address == 0) throw new InvalidOperationException("The pointer to the right of the less than operator is a null pointer."); - #endif +#endif return pointer1.Address < pointer2.Address; } @@ -227,12 +227,12 @@ namespace Compress.SevenZip.Compress.PPmd.I1 /// public static bool operator <=(Pointer pointer1, Pointer pointer2) { - #if DEBUG +#if DEBUG if (pointer1.Address == 0) throw new InvalidOperationException("The pointer to the left of the less than or equal to operator is a null pointer."); if (pointer2.Address == 0) throw new InvalidOperationException("The pointer to the right of the less than or equal to operator is a null pointer."); - #endif +#endif return pointer1.Address <= pointer2.Address; } @@ -244,12 +244,12 @@ namespace Compress.SevenZip.Compress.PPmd.I1 /// public static bool operator >(Pointer pointer1, Pointer pointer2) { - #if DEBUG +#if DEBUG if (pointer1.Address == 0) throw new InvalidOperationException("The pointer to the left of the greater than operator is a null pointer."); if (pointer2.Address == 0) throw new InvalidOperationException("The pointer to the right of the greater than operator is a null pointer."); - #endif +#endif return pointer1.Address > pointer2.Address; } @@ -261,12 +261,12 @@ namespace Compress.SevenZip.Compress.PPmd.I1 /// public static bool operator >=(Pointer pointer1, Pointer pointer2) { - #if DEBUG +#if DEBUG if (pointer1.Address == 0) throw new InvalidOperationException("The pointer to the left of the greater than or equal to operator is a null pointer."); if (pointer2.Address == 0) throw new InvalidOperationException("The pointer to the right of the greater than or equal to operator is a null pointer."); - #endif +#endif return pointer1.Address >= pointer2.Address; } @@ -301,7 +301,7 @@ namespace Compress.SevenZip.Compress.PPmd.I1 { if (obj is Pointer) { - Pointer pointer = (Pointer) obj; + Pointer pointer = (Pointer)obj; return pointer.Address == Address; } return base.Equals(obj); @@ -316,4 +316,4 @@ namespace Compress.SevenZip.Compress.PPmd.I1 return Address.GetHashCode(); } } -} +} \ No newline at end of file diff --git a/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/I1/PpmContext.cs b/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/I1/PpmContext.cs index 81aad5e1..2164bf72 100644 --- a/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/I1/PpmContext.cs +++ b/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/I1/PpmContext.cs @@ -377,7 +377,7 @@ namespace Compress.SevenZip.Compress.PPmd.I1 Coder.LowCount = lowCount; lowCount += state.Frequency; Coder.HighCount = lowCount; - for (PpmState p1 = state; --index != 0; ) + for (PpmState p1 = state; --index != 0;) { do { @@ -784,4 +784,4 @@ namespace Compress.SevenZip.Compress.PPmd.I1 return context; } } -} +} \ No newline at end of file diff --git a/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/I1/PpmState.cs b/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/I1/PpmState.cs index 79b509f6..38001ef0 100644 --- a/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/I1/PpmState.cs +++ b/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/I1/PpmState.cs @@ -58,13 +58,13 @@ namespace Compress.SevenZip.Compress.PPmd.I1 /// public Model.PpmContext Successor { - get { return new Model.PpmContext(((uint) Memory[Address + 2]) | ((uint) Memory[Address + 3]) << 8 | ((uint) Memory[Address + 4]) << 16 | ((uint) Memory[Address + 5]) << 24, Memory); } + get { return new Model.PpmContext(((uint)Memory[Address + 2]) | ((uint)Memory[Address + 3]) << 8 | ((uint)Memory[Address + 4]) << 16 | ((uint)Memory[Address + 5]) << 24, Memory); } set { - Memory[Address + 2] = (byte) value.Address; - Memory[Address + 3] = (byte) (value.Address >> 8); - Memory[Address + 4] = (byte) (value.Address >> 16); - Memory[Address + 5] = (byte) (value.Address >> 24); + Memory[Address + 2] = (byte)value.Address; + Memory[Address + 3] = (byte)(value.Address >> 8); + Memory[Address + 4] = (byte)(value.Address >> 16); + Memory[Address + 5] = (byte)(value.Address >> 24); } } @@ -76,7 +76,7 @@ namespace Compress.SevenZip.Compress.PPmd.I1 /// public PpmState this[int offset] { - get { return new PpmState((uint) (Address + offset * Size), Memory); } + get { return new PpmState((uint)(Address + offset * Size), Memory); } } /// @@ -97,7 +97,7 @@ namespace Compress.SevenZip.Compress.PPmd.I1 /// public static PpmState operator +(PpmState state, int offset) { - state.Address = (uint) (state.Address + offset * Size); + state.Address = (uint)(state.Address + offset * Size); return state; } @@ -120,7 +120,7 @@ namespace Compress.SevenZip.Compress.PPmd.I1 /// public static PpmState operator -(PpmState state, int offset) { - state.Address = (uint) (state.Address - offset * Size); + state.Address = (uint)(state.Address - offset * Size); return state; } @@ -188,7 +188,7 @@ namespace Compress.SevenZip.Compress.PPmd.I1 { if (obj is PpmState) { - PpmState state = (PpmState) obj; + PpmState state = (PpmState)obj; return state.Address == Address; } return base.Equals(obj); @@ -203,4 +203,4 @@ namespace Compress.SevenZip.Compress.PPmd.I1 return Address.GetHashCode(); } } -} +} \ No newline at end of file diff --git a/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/I1/See2Context.cs b/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/I1/See2Context.cs index 970a0889..1650d856 100644 --- a/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/I1/See2Context.cs +++ b/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/I1/See2Context.cs @@ -32,15 +32,15 @@ namespace Compress.SevenZip.Compress.PPmd.I1 public void Initialize(uint initialValue) { Shift = PeriodBitCount - 4; - Summary = (ushort) (initialValue << Shift); + Summary = (ushort)(initialValue << Shift); Count = 7; } public uint Mean() { - uint value = (uint) (Summary >> Shift); - Summary = (ushort) (Summary - value); - return (uint) (value + ((value == 0) ? 1 : 0)); + uint value = (uint)(Summary >> Shift); + Summary = (ushort)(Summary - value); + return (uint)(value + ((value == 0) ? 1 : 0)); } public void Update() @@ -48,8 +48,8 @@ namespace Compress.SevenZip.Compress.PPmd.I1 if (Shift < PeriodBitCount && --Count == 0) { Summary += Summary; - Count = (byte) (3 << Shift++); + Count = (byte)(3 << Shift++); } } } -} +} \ No newline at end of file diff --git a/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/PpmdProperties.cs b/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/PpmdProperties.cs index 2e5adbfd..501331cd 100644 --- a/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/PpmdProperties.cs +++ b/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/PpmdProperties.cs @@ -78,4 +78,4 @@ namespace Compress.SevenZip.Compress.PPmd } } } -} +} \ No newline at end of file diff --git a/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/PpmdStream.cs b/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/PpmdStream.cs index f5db7540..b5c11980 100644 --- a/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/PpmdStream.cs +++ b/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/PpmdStream.cs @@ -151,4 +151,4 @@ namespace Compress.SevenZip.Compress.PPmd model.EncodeBlock(stream, new MemoryStream(buffer, offset, count), false); } } -} +} \ No newline at end of file diff --git a/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/Utility.cs b/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/Utility.cs index 934f4fbb..bda8e9f0 100644 --- a/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/Utility.cs +++ b/SabreTools.Library/External/Compress/SevenZip/Compress/PPmd/Utility.cs @@ -119,7 +119,7 @@ namespace Compress.SevenZip.Compress.PPmd } /// Read a int value from the byte array at the given position (Big Endian) - /// + /// /// /// the array to read from /// @@ -142,7 +142,7 @@ namespace Compress.SevenZip.Compress.PPmd /// Read a short value from the byte array at the given position (little /// Endian) - /// + /// /// /// the array to read from /// @@ -157,7 +157,7 @@ namespace Compress.SevenZip.Compress.PPmd /// Read an int value from the byte array at the given position (little /// Endian) - /// + /// /// /// the array to read from /// @@ -171,7 +171,7 @@ namespace Compress.SevenZip.Compress.PPmd } /// Write an int value into the byte array at the given position (Big endian) - /// + /// /// /// the array /// @@ -189,7 +189,7 @@ namespace Compress.SevenZip.Compress.PPmd /// Write a short value into the byte array at the given position (little /// endian) - /// + /// /// /// the array /// @@ -221,7 +221,7 @@ namespace Compress.SevenZip.Compress.PPmd /// Write an int value into the byte array at the given position (little /// endian) - /// + /// /// /// the array /// @@ -464,4 +464,4 @@ namespace Compress.SevenZip.Compress.PPmd } #endif } -} +} \ No newline at end of file diff --git a/SabreTools.Library/External/Compress/SevenZip/Compress/RangeCoder/RangeCoder.cs b/SabreTools.Library/External/Compress/SevenZip/Compress/RangeCoder/RangeCoder.cs index 10f2e09f..6c45c58a 100644 --- a/SabreTools.Library/External/Compress/SevenZip/Compress/RangeCoder/RangeCoder.cs +++ b/SabreTools.Library/External/Compress/SevenZip/Compress/RangeCoder/RangeCoder.cs @@ -244,4 +244,4 @@ namespace Compress.SevenZip.Compress.RangeCoder // ulong GetProcessedSize() {return Stream.GetProcessedSize(); } } -} +} \ No newline at end of file diff --git a/SabreTools.Library/External/Compress/SevenZip/Compress/RangeCoder/RangeCoderBit.cs b/SabreTools.Library/External/Compress/SevenZip/Compress/RangeCoder/RangeCoderBit.cs index 357a3e79..9ff1ac16 100644 --- a/SabreTools.Library/External/Compress/SevenZip/Compress/RangeCoder/RangeCoderBit.cs +++ b/SabreTools.Library/External/Compress/SevenZip/Compress/RangeCoder/RangeCoderBit.cs @@ -116,4 +116,4 @@ namespace Compress.SevenZip.Compress.RangeCoder } } } -} +} \ No newline at end of file diff --git a/SabreTools.Library/External/Compress/SevenZip/Compress/RangeCoder/RangeCoderBitTree.cs b/SabreTools.Library/External/Compress/SevenZip/Compress/RangeCoder/RangeCoderBitTree.cs index 06a814ef..9ad382b0 100644 --- a/SabreTools.Library/External/Compress/SevenZip/Compress/RangeCoder/RangeCoderBitTree.cs +++ b/SabreTools.Library/External/Compress/SevenZip/Compress/RangeCoder/RangeCoderBitTree.cs @@ -22,7 +22,7 @@ namespace Compress.SevenZip.Compress.RangeCoder public void Encode(Encoder rangeEncoder, UInt32 symbol) { UInt32 m = 1; - for (int bitIndex = NumBitLevels; bitIndex > 0; ) + for (int bitIndex = NumBitLevels; bitIndex > 0;) { bitIndex--; UInt32 bit = (symbol >> bitIndex) & 1; @@ -47,7 +47,7 @@ namespace Compress.SevenZip.Compress.RangeCoder { UInt32 price = 0; UInt32 m = 1; - for (int bitIndex = NumBitLevels; bitIndex > 0; ) + for (int bitIndex = NumBitLevels; bitIndex > 0;) { bitIndex--; UInt32 bit = (symbol >> bitIndex) & 1; @@ -154,4 +154,4 @@ namespace Compress.SevenZip.Compress.RangeCoder return symbol; } } -} +} \ No newline at end of file diff --git a/SabreTools.Library/External/Compress/SevenZip/Compress/ZSTD/ZstandardDictionary.cs b/SabreTools.Library/External/Compress/SevenZip/Compress/ZSTD/ZstandardDictionary.cs new file mode 100644 index 00000000..e2fb1c50 --- /dev/null +++ b/SabreTools.Library/External/Compress/SevenZip/Compress/ZSTD/ZstandardDictionary.cs @@ -0,0 +1,194 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices; +using Interop = Zstandard.Net.ZstandardInterop; + +namespace Zstandard.Net +{ + /// + /// A Zstandard dictionary improves the compression ratio and speed on small data dramatically. + /// + /// + /// A Zstandard dictionary is calculated with a high number of small sample data. + /// Please refer to the Zstandard documentation for more details. + /// + /// + public sealed class ZstandardDictionary : IDisposable + { + private byte[] dictionary; + private IntPtr ddict; + private Dictionary cdicts = new Dictionary(); + private object lockObject = new object(); + private bool isDisposed = false; + + /// + /// Initializes a new instance of the class. + /// + /// The dictionary raw data. + public ZstandardDictionary(byte[] dictionary) + { + this.dictionary = dictionary; + } + + /// + /// Initializes a new instance of the class. + /// + /// The dictionary path. + public ZstandardDictionary(string dictionaryPath) + { + this.dictionary = File.ReadAllBytes(dictionaryPath); + } + + /// + /// Initializes a new instance of the class. + /// + /// The dictionary stream. + public ZstandardDictionary(Stream dictionaryStream) + { + using (var memoryStream = new MemoryStream()) + { + dictionaryStream.CopyTo(memoryStream); + this.dictionary = memoryStream.ToArray(); + } + } + + /// + /// Finalizes an instance of the class. + /// + ~ZstandardDictionary() + { + this.Dispose(false); + } + + /// + /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. + /// + public void Dispose() + { + this.Dispose(true); + GC.SuppressFinalize(this); + } + + /// + /// Releases unmanaged resources. + /// + /// true to release both managed and unmanaged resources; false to release only unmanaged resources. + private void Dispose(bool dispose) + { + if (this.isDisposed == false) + { + this.isDisposed = true; + + if (this.ddict != IntPtr.Zero) + { + Interop.ZSTD_freeDDict(this.ddict); + this.ddict = IntPtr.Zero; + } + + foreach (var kv in this.cdicts.ToList()) + { + Interop.ZSTD_freeCDict(kv.Value); + this.cdicts.Remove(kv.Key); + } + } + } + + /// + /// Gets the compression dictionary for the specified compression level. + /// + /// The compression level. + /// + /// The IntPtr to the compression dictionary. + /// + /// ZstandardDictionary + internal IntPtr GetCompressionDictionary(int compressionLevel) + { + if (this.isDisposed) + { + throw new ObjectDisposedException(nameof(ZstandardDictionary)); + } + + lock (this.lockObject) + { + if (this.cdicts.TryGetValue(compressionLevel, out var cdict) == false) + { + this.cdicts[compressionLevel] = cdict = this.CreateCompressionDictionary(compressionLevel); + } + + return cdict; + } + } + + /// + /// Gets the decompression dictionary. + /// + /// + /// The IntPtr to the decompression dictionary. + /// + /// ZstandardDictionary + internal IntPtr GetDecompressionDictionary() + { + if (this.isDisposed) + { + throw new ObjectDisposedException(nameof(ZstandardDictionary)); + } + + lock (this.lockObject) + { + if (this.ddict == IntPtr.Zero) + { + this.ddict = this.CreateDecompressionDictionary(); + } + + return this.ddict; + } + } + + /// + /// Creates a new compression dictionary. + /// + /// The compression level. + /// + /// The IntPtr to the compression dictionary. + /// + private IntPtr CreateCompressionDictionary(int compressionLevel) + { + var alloc = GCHandle.Alloc(this.dictionary, GCHandleType.Pinned); + + try + { + var dictBuffer = Marshal.UnsafeAddrOfPinnedArrayElement(this.dictionary, 0); + var dictSize = new UIntPtr((uint)this.dictionary.Length); + return Interop.ZSTD_createCDict(dictBuffer, dictSize, compressionLevel); + } + finally + { + alloc.Free(); + } + } + + /// + /// Creates a new decompression dictionary. + /// + /// + /// The IntPtr to the decompression dictionary. + /// + private IntPtr CreateDecompressionDictionary() + { + var alloc = GCHandle.Alloc(this.dictionary, GCHandleType.Pinned); + + try + { + var dictBuffer = Marshal.UnsafeAddrOfPinnedArrayElement(this.dictionary, 0); + var dictSize = new UIntPtr((uint)this.dictionary.Length); + return Interop.ZSTD_createDDict(dictBuffer, dictSize); + } + finally + { + alloc.Free(); + } + } + } +} \ No newline at end of file diff --git a/SabreTools.Library/External/Compress/SevenZip/Compress/ZSTD/ZstandardInterop.cs b/SabreTools.Library/External/Compress/SevenZip/Compress/ZSTD/ZstandardInterop.cs new file mode 100644 index 00000000..03abe7a9 --- /dev/null +++ b/SabreTools.Library/External/Compress/SevenZip/Compress/ZSTD/ZstandardInterop.cs @@ -0,0 +1,143 @@ +using System; +using System.IO; +using System.Runtime.InteropServices; + +namespace Zstandard.Net +{ + internal static class ZstandardInterop + { + static ZstandardInterop() + { + if (Environment.OSVersion.Platform == PlatformID.Win32NT) + { + var root = Path.GetDirectoryName(typeof(ZstandardInterop).Assembly.Location); + var path = Environment.Is64BitProcess ? "x64" : "x86"; + var file = Path.Combine(root, path, "libzstd.dll"); + LoadLibraryEx(file, IntPtr.Zero, LoadLibraryFlags.LOAD_LIBRARY_SEARCH_APPLICATION_DIR); + } + } + + [StructLayout(LayoutKind.Sequential)] + public class Buffer + { + public IntPtr Data = IntPtr.Zero; + public UIntPtr Size = UIntPtr.Zero; + public UIntPtr Position = UIntPtr.Zero; + } + + public static void ThrowIfError(UIntPtr code) + { + if (ZSTD_isError(code)) + { + var errorPtr = ZSTD_getErrorName(code); + var errorMsg = Marshal.PtrToStringAnsi(errorPtr); + throw new IOException(errorMsg); + } + } + + //----------------------------------------------------------------------------------------- + //----------------------------------------------------------------------------------------- + + [Flags] + private enum LoadLibraryFlags : uint + { + DONT_RESOLVE_DLL_REFERENCES = 0x00000001, + LOAD_IGNORE_CODE_AUTHZ_LEVEL = 0x00000010, + LOAD_LIBRARY_AS_DATAFILE = 0x00000002, + LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE = 0x00000040, + LOAD_LIBRARY_AS_IMAGE_RESOURCE = 0x00000020, + LOAD_LIBRARY_SEARCH_APPLICATION_DIR = 0x00000200, + LOAD_LIBRARY_SEARCH_DEFAULT_DIRS = 0x00001000, + LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR = 0x00000100, + LOAD_LIBRARY_SEARCH_SYSTEM32 = 0x00000800, + LOAD_LIBRARY_SEARCH_USER_DIRS = 0x00000400, + LOAD_WITH_ALTERED_SEARCH_PATH = 0x00000008 + } + + [DllImport("kernel32", SetLastError = true)] + private static extern IntPtr LoadLibraryEx(string lpFileName, IntPtr hReservedNull, LoadLibraryFlags dwFlags); + + //----------------------------------------------------------------------------------------- + //----------------------------------------------------------------------------------------- + + [DllImport("libzstd", CallingConvention = CallingConvention.Cdecl)] + public static extern uint ZSTD_versionNumber(); + + [DllImport("libzstd", CallingConvention = CallingConvention.Cdecl)] + public static extern int ZSTD_maxCLevel(); + + //----------------------------------------------------------------------------------------- + + [DllImport("libzstd", CallingConvention = CallingConvention.Cdecl)] + public static extern IntPtr ZSTD_createCStream(); + + [DllImport("libzstd", CallingConvention = CallingConvention.Cdecl)] + public static extern UIntPtr ZSTD_initCStream(IntPtr zcs, int compressionLevel); + + [DllImport("libzstd", CallingConvention = CallingConvention.Cdecl)] + public static extern UIntPtr ZSTD_freeCStream(IntPtr zcs); + + [DllImport("libzstd", CallingConvention = CallingConvention.Cdecl)] + public static extern UIntPtr ZSTD_CStreamInSize(); + + [DllImport("libzstd", CallingConvention = CallingConvention.Cdecl)] + public static extern UIntPtr ZSTD_CStreamOutSize(); + + [DllImport("libzstd", CallingConvention = CallingConvention.Cdecl)] + public static extern UIntPtr ZSTD_compressStream(IntPtr zcs, [MarshalAs(UnmanagedType.LPStruct)] Buffer outputBuffer, [MarshalAs(UnmanagedType.LPStruct)] Buffer inputBuffer); + + [DllImport("libzstd", CallingConvention = CallingConvention.Cdecl)] + public static extern IntPtr ZSTD_createCDict(IntPtr dictBuffer, UIntPtr dictSize, int compressionLevel); + + [DllImport("libzstd", CallingConvention = CallingConvention.Cdecl)] + public static extern UIntPtr ZSTD_freeCDict(IntPtr cdict); + + [DllImport("libzstd", CallingConvention = CallingConvention.Cdecl)] + public static extern UIntPtr ZSTD_initCStream_usingCDict(IntPtr zcs, IntPtr cdict); + + //----------------------------------------------------------------------------------------- + + [DllImport("libzstd", CallingConvention = CallingConvention.Cdecl)] + public static extern IntPtr ZSTD_createDStream(); + + [DllImport("libzstd", CallingConvention = CallingConvention.Cdecl)] + public static extern UIntPtr ZSTD_initDStream(IntPtr zds); + + [DllImport("libzstd", CallingConvention = CallingConvention.Cdecl)] + public static extern UIntPtr ZSTD_freeDStream(IntPtr zds); + + [DllImport("libzstd", CallingConvention = CallingConvention.Cdecl)] + public static extern UIntPtr ZSTD_DStreamInSize(); + + [DllImport("libzstd", CallingConvention = CallingConvention.Cdecl)] + public static extern UIntPtr ZSTD_DStreamOutSize(); + + [DllImport("libzstd", CallingConvention = CallingConvention.Cdecl)] + public static extern UIntPtr ZSTD_decompressStream(IntPtr zds, [MarshalAs(UnmanagedType.LPStruct)] Buffer outputBuffer, [MarshalAs(UnmanagedType.LPStruct)] Buffer inputBuffer); + + [DllImport("libzstd", CallingConvention = CallingConvention.Cdecl)] + public static extern IntPtr ZSTD_createDDict(IntPtr dictBuffer, UIntPtr dictSize); + + [DllImport("libzstd", CallingConvention = CallingConvention.Cdecl)] + public static extern UIntPtr ZSTD_freeDDict(IntPtr ddict); + + [DllImport("libzstd", CallingConvention = CallingConvention.Cdecl)] + public static extern UIntPtr ZSTD_initDStream_usingDDict(IntPtr zds, IntPtr ddict); + + //----------------------------------------------------------------------------------------- + + [DllImport("libzstd", CallingConvention = CallingConvention.Cdecl)] + public static extern UIntPtr ZSTD_flushStream(IntPtr zcs, [MarshalAs(UnmanagedType.LPStruct)] Buffer outputBuffer); + + [DllImport("libzstd", CallingConvention = CallingConvention.Cdecl)] + public static extern UIntPtr ZSTD_endStream(IntPtr zcs, [MarshalAs(UnmanagedType.LPStruct)] Buffer outputBuffer); + + //----------------------------------------------------------------------------------------- + + [DllImport("libzstd", CallingConvention = CallingConvention.Cdecl)] + internal static extern bool ZSTD_isError(UIntPtr code); + + [DllImport("libzstd", CallingConvention = CallingConvention.Cdecl)] + private static extern IntPtr ZSTD_getErrorName(UIntPtr code); + } +} \ No newline at end of file diff --git a/SabreTools.Library/External/Compress/SevenZip/Compress/ZSTD/ZstandardStream.cs b/SabreTools.Library/External/Compress/SevenZip/Compress/ZSTD/ZstandardStream.cs new file mode 100644 index 00000000..5d36f335 --- /dev/null +++ b/SabreTools.Library/External/Compress/SevenZip/Compress/ZSTD/ZstandardStream.cs @@ -0,0 +1,398 @@ +using System; +using System.IO; +using System.IO.Compression; +using System.Runtime.InteropServices; +using Interop = Zstandard.Net.ZstandardInterop; + +namespace Zstandard.Net +{ + /// + /// Provides methods and properties for compressing and decompressing streams by using the Zstandard algorithm. + /// + public class ZstandardStream : Stream + { + private Stream stream; + private CompressionMode mode; + private Boolean leaveOpen; + private Boolean isClosed = false; + private Boolean isDisposed = false; + private Boolean isInitialized = false; + + private IntPtr zstream; + private uint zstreamInputSize; + private uint zstreamOutputSize; + + private byte[] data; + private bool dataDepleted = false; + private bool dataSkipRead = false; + private int dataPosition = 0; + private int dataSize = 0; + + private long position = 0; + + private Interop.Buffer outputBuffer = new Interop.Buffer(); + private Interop.Buffer inputBuffer = new Interop.Buffer(); + + /// + /// Initializes a new instance of the class by using the specified stream and compression mode, and optionally leaves the stream open. + /// + /// The stream to compress. + /// One of the enumeration values that indicates whether to compress or decompress the stream. + /// true to leave the stream open after disposing the object; otherwise, false. + public ZstandardStream(Stream stream, CompressionMode mode, bool leaveOpen = false) + { + this.stream = stream ?? throw new ArgumentNullException(nameof(stream)); + this.mode = mode; + this.leaveOpen = leaveOpen; + position = 0; + + if (mode == CompressionMode.Compress) + { + this.zstreamInputSize = Interop.ZSTD_CStreamInSize().ToUInt32(); + this.zstreamOutputSize = Interop.ZSTD_CStreamOutSize().ToUInt32(); + this.zstream = Interop.ZSTD_createCStream(); + this.data = new byte[(int)this.zstreamOutputSize]; + } + + if (mode == CompressionMode.Decompress) + { + this.zstreamInputSize = Interop.ZSTD_DStreamInSize().ToUInt32(); + this.zstreamOutputSize = Interop.ZSTD_DStreamOutSize().ToUInt32(); + this.zstream = Interop.ZSTD_createDStream(); + this.data = new byte[(int)this.zstreamInputSize]; + } + } + + /// + /// Initializes a new instance of the class by using the specified stream and compression level, and optionally leaves the stream open. + /// + /// The stream to compress. + /// The compression level. + /// true to leave the stream open after disposing the object; otherwise, false. + public ZstandardStream(Stream stream, int compressionLevel, bool leaveOpen = false) : this(stream, CompressionMode.Compress, leaveOpen) + { + this.CompressionLevel = compressionLevel; + } + + //----------------------------------------------------------------------------------------- + //----------------------------------------------------------------------------------------- + + /// + /// The version of the native Zstd library. + /// + public static Version Version + { + get + { + var version = (int)Interop.ZSTD_versionNumber(); + return new Version((version / 10000) % 100, (version / 100) % 100, version % 100); + } + } + + /// + /// The maximum compression level supported by the native Zstd library. + /// + public static int MaxCompressionLevel + { + get + { + return Interop.ZSTD_maxCLevel(); + } + } + + //----------------------------------------------------------------------------------------- + //----------------------------------------------------------------------------------------- + + /// + /// Gets or sets the compression level to use, the default is 6. + /// + /// + /// To get the maximum compression level see . + /// + public int CompressionLevel { get; set; } = 6; + + /// + /// Gets or sets the compression dictionary tp use, the default is null. + /// + /// + /// The compression dictionary. + /// + public ZstandardDictionary CompressionDictionary { get; set; } = null; + + /// + /// Gets whether the current stream supports reading. + /// + public override bool CanRead => this.stream.CanRead && this.mode == CompressionMode.Decompress; + + /// + /// Gets whether the current stream supports writing. + /// + public override bool CanWrite => this.stream.CanWrite && this.mode == CompressionMode.Compress; + + /// + /// Gets whether the current stream supports seeking. + /// + public override bool CanSeek => false; + + /// + /// Gets the length in bytes of the stream. + /// + public override long Length => throw new NotSupportedException(); + + /// + /// Gets or sets the position within the current stream. + /// + public override long Position + { + get => position; + set => throw new NotSupportedException(); + } + + //----------------------------------------------------------------------------------------- + //----------------------------------------------------------------------------------------- + + protected override void Dispose(bool disposing) + { + base.Dispose(disposing); + + if (this.isDisposed == false) + { + if (!this.isClosed) ReleaseResources(flushStream: false); + this.isDisposed = true; + this.data = null; + } + } + + public override void Close() + { + if (this.isClosed) return; + + try + { + ReleaseResources(flushStream: true); + } + finally + { + this.isClosed = true; + base.Close(); + } + } + + private void ReleaseResources(bool flushStream) + { + if (this.mode == CompressionMode.Compress) + { + try + { + if (flushStream) + { + this.ProcessStream((zcs, buffer) => Interop.ThrowIfError(Interop.ZSTD_flushStream(zcs, buffer))); + this.ProcessStream((zcs, buffer) => Interop.ThrowIfError(Interop.ZSTD_endStream(zcs, buffer))); + this.stream.Flush(); + } + } + finally + { + Interop.ZSTD_freeCStream(this.zstream); + if (!this.leaveOpen) this.stream.Close(); + } + } + else if (this.mode == CompressionMode.Decompress) + { + Interop.ZSTD_freeDStream(this.zstream); + if (!this.leaveOpen) this.stream.Close(); + } + } + + public override void Flush() + { + if (this.mode == CompressionMode.Compress) + { + this.ProcessStream((zcs, buffer) => Interop.ThrowIfError(Interop.ZSTD_flushStream(zcs, buffer))); + this.stream.Flush(); + } + } + + public override int Read(byte[] buffer, int offset, int count) + { + if (this.CanRead == false) throw new NotSupportedException(); + + // prevent the buffers from being moved around by the garbage collector + var alloc1 = GCHandle.Alloc(buffer, GCHandleType.Pinned); + var alloc2 = GCHandle.Alloc(this.data, GCHandleType.Pinned); + + try + { + var length = 0; + + if (this.isInitialized == false) + { + this.isInitialized = true; + + var result = this.CompressionDictionary == null + ? Interop.ZSTD_initDStream(this.zstream) + : Interop.ZSTD_initDStream_usingDDict(this.zstream, this.CompressionDictionary.GetDecompressionDictionary()); + } + + while (count > 0) + { + var inputSize = this.dataSize - this.dataPosition; + + // read data from input stream + if (inputSize <= 0 && !this.dataDepleted && !this.dataSkipRead) + { + this.dataSize = this.stream.Read(this.data, 0, (int)this.zstreamInputSize); + this.dataDepleted = this.dataSize <= 0; + this.dataPosition = 0; + inputSize = this.dataDepleted ? 0 : this.dataSize; + + // skip stream.Read until the internal buffer is depleted + // avoids a Read timeout for applications that know the exact number of bytes in the stream + this.dataSkipRead = true; + } + + // configure the inputBuffer + this.inputBuffer.Data = inputSize <= 0 ? IntPtr.Zero : Marshal.UnsafeAddrOfPinnedArrayElement(this.data, this.dataPosition); + this.inputBuffer.Size = inputSize <= 0 ? UIntPtr.Zero : new UIntPtr((uint)inputSize); + this.inputBuffer.Position = UIntPtr.Zero; + + // configure the outputBuffer + this.outputBuffer.Data = Marshal.UnsafeAddrOfPinnedArrayElement(buffer, offset); + this.outputBuffer.Size = new UIntPtr((uint)count); + this.outputBuffer.Position = UIntPtr.Zero; + + // decompress inputBuffer to outputBuffer + Interop.ThrowIfError(Interop.ZSTD_decompressStream(this.zstream, this.outputBuffer, this.inputBuffer)); + + // calculate progress in outputBuffer + var outputBufferPosition = (int)this.outputBuffer.Position.ToUInt32(); + if (outputBufferPosition == 0) + { + // the internal buffer is depleted, we're either done + if (this.dataDepleted) break; + + // or we need more bytes + this.dataSkipRead = false; + } + length += outputBufferPosition; + offset += outputBufferPosition; + count -= outputBufferPosition; + + // calculate progress in inputBuffer + var inputBufferPosition = (int)inputBuffer.Position.ToUInt32(); + this.dataPosition += inputBufferPosition; + } + + position += length; + return length; + } + finally + { + alloc1.Free(); + alloc2.Free(); + } + } + + public override void Write(byte[] buffer, int offset, int count) + { + if (this.CanWrite == false) throw new NotSupportedException(); + + // prevent the buffers from being moved around by the garbage collector + var alloc1 = GCHandle.Alloc(buffer, GCHandleType.Pinned); + var alloc2 = GCHandle.Alloc(this.data, GCHandleType.Pinned); + + try + { + if (this.isInitialized == false) + { + this.isInitialized = true; + + var result = this.CompressionDictionary == null + ? Interop.ZSTD_initCStream(this.zstream, this.CompressionLevel) + : Interop.ZSTD_initCStream_usingCDict(this.zstream, this.CompressionDictionary.GetCompressionDictionary(this.CompressionLevel)); + + Interop.ThrowIfError(result); + } + + while (count > 0) + { + var inputSize = Math.Min((uint)count, this.zstreamInputSize); + + // configure the outputBuffer + this.outputBuffer.Data = Marshal.UnsafeAddrOfPinnedArrayElement(this.data, 0); + this.outputBuffer.Size = new UIntPtr(this.zstreamOutputSize); + this.outputBuffer.Position = UIntPtr.Zero; + + // configure the inputBuffer + this.inputBuffer.Data = Marshal.UnsafeAddrOfPinnedArrayElement(buffer, offset); + this.inputBuffer.Size = new UIntPtr((uint)inputSize); + this.inputBuffer.Position = UIntPtr.Zero; + + // compress inputBuffer to outputBuffer + Interop.ThrowIfError(Interop.ZSTD_compressStream(this.zstream, this.outputBuffer, this.inputBuffer)); + + // write data to output stream + var outputBufferPosition = (int)this.outputBuffer.Position.ToUInt32(); + this.stream.Write(this.data, 0, outputBufferPosition); + + // calculate progress in inputBuffer + var inputBufferPosition = (int)this.inputBuffer.Position.ToUInt32(); + offset += inputBufferPosition; + count -= inputBufferPosition; + } + } + finally + { + alloc1.Free(); + alloc2.Free(); + } + } + + public override long Seek(long offset, SeekOrigin origin) + { + if (origin != SeekOrigin.Current) + throw new NotImplementedException(); + + byte[] tmpBuff = new byte[1024]; + long sizeToGo = offset; + while (sizeToGo > 0) + { + int sizenow = sizeToGo > 1024 ? 1024 : (int)sizeToGo; + Read(tmpBuff, 0, sizenow); + sizeToGo -= sizenow; + } + + position += offset; + return offset; + } + + public override void SetLength(long value) + { + throw new NotImplementedException(); + } + + //----------------------------------------------------------------------------------------- + //----------------------------------------------------------------------------------------- + + private void ProcessStream(Action outputAction) + { + var alloc = GCHandle.Alloc(this.data, GCHandleType.Pinned); + + try + { + this.outputBuffer.Data = Marshal.UnsafeAddrOfPinnedArrayElement(this.data, 0); + this.outputBuffer.Size = new UIntPtr(this.zstreamOutputSize); + this.outputBuffer.Position = UIntPtr.Zero; + + outputAction(this.zstream, this.outputBuffer); + + var outputBufferPosition = (int)this.outputBuffer.Position.ToUInt32(); + this.stream.Write(this.data, 0, outputBufferPosition); + } + finally + { + alloc.Free(); + } + } + } +} \ No newline at end of file diff --git a/SabreTools.Library/External/Compress/SevenZip/Filters/BCJ2Filter.cs b/SabreTools.Library/External/Compress/SevenZip/Filters/BCJ2Filter.cs index 09664ee5..4ea96fa4 100644 --- a/SabreTools.Library/External/Compress/SevenZip/Filters/BCJ2Filter.cs +++ b/SabreTools.Library/External/Compress/SevenZip/Filters/BCJ2Filter.cs @@ -51,7 +51,7 @@ namespace Compress.SevenZip.Filters code = 0; range = 0xFFFFFFFF; - byte[] controlbuf=new byte[5]; + byte[] controlbuf = new byte[5]; control.Read(controlbuf, 0, 5); for (i = 0; i < 5; i++) @@ -139,7 +139,7 @@ namespace Compress.SevenZip.Filters if (range < kTopValue) { range <<= 8; - code = (code << 8) | (byte)control.ReadByte(); + code = (code << 8) | (byte)control.ReadByte(); } prevByte = b; } @@ -201,4 +201,4 @@ namespace Compress.SevenZip.Filters throw new NotImplementedException(); } } -} +} \ No newline at end of file diff --git a/SabreTools.Library/External/Compress/SevenZip/Filters/BCJFilter.cs b/SabreTools.Library/External/Compress/SevenZip/Filters/BCJFilter.cs index 666ce0ce..e2aaae64 100644 --- a/SabreTools.Library/External/Compress/SevenZip/Filters/BCJFilter.cs +++ b/SabreTools.Library/External/Compress/SevenZip/Filters/BCJFilter.cs @@ -4,7 +4,7 @@ namespace Compress.SevenZip.Filters { public class BCJFilter : Filter { - private static readonly bool[] MASK_TO_ALLOWED_STATUS = new bool[] {true, true, true, false, true, false, false, false}; + private static readonly bool[] MASK_TO_ALLOWED_STATUS = new bool[] { true, true, true, false, true, false, false, false }; private static readonly int[] MASK_TO_BIT_NUMBER = new int[] { 0, 1, 2, 2, 3, 3, 3, 3 }; private int pos; @@ -26,18 +26,24 @@ namespace Compress.SevenZip.Filters int end = offset + count - 5; int i; - for (i = offset; i <= end; ++i) { + for (i = offset; i <= end; ++i) + { if ((buffer[i] & 0xFE) != 0xE8) continue; prevPos = i - prevPos; - if ((prevPos & ~3) != 0) { // (unsigned)prevPos > 3 + if ((prevPos & ~3) != 0) + { // (unsigned)prevPos > 3 prevMask = 0; - } else { + } + else + { prevMask = (prevMask << (prevPos - 1)) & 7; - if (prevMask != 0) { + if (prevMask != 0) + { if (!MASK_TO_ALLOWED_STATUS[prevMask] || test86MSByte( - buffer[i + 4 - MASK_TO_BIT_NUMBER[prevMask]])) { + buffer[i + 4 - MASK_TO_BIT_NUMBER[prevMask]])) + { prevPos = i; prevMask = (prevMask << 1) | 1; continue; @@ -47,13 +53,15 @@ namespace Compress.SevenZip.Filters prevPos = i; - if (test86MSByte(buffer[i + 4])) { + if (test86MSByte(buffer[i + 4])) + { int src = buffer[i + 1] | (buffer[i + 2] << 8) | (buffer[i + 3] << 16) | (buffer[i + 4] << 24); int dest; - while (true) { + while (true) + { if (isEncoder) dest = src + (pos + i - offset); else @@ -74,7 +82,9 @@ namespace Compress.SevenZip.Filters buffer[i + 3] = (byte)(dest >> 16); buffer[i + 4] = (byte)(~(((dest >> 24) & 1) - 1)); i += 4; - } else { + } + else + { prevMask = (prevMask << 1) | 1; } } @@ -87,4 +97,4 @@ namespace Compress.SevenZip.Filters return i; } } -} +} \ No newline at end of file diff --git a/SabreTools.Library/External/Compress/SevenZip/Filters/Delta.cs b/SabreTools.Library/External/Compress/SevenZip/Filters/Delta.cs index 400e03a4..ce102e54 100644 --- a/SabreTools.Library/External/Compress/SevenZip/Filters/Delta.cs +++ b/SabreTools.Library/External/Compress/SevenZip/Filters/Delta.cs @@ -97,4 +97,4 @@ namespace Compress.SevenZip.Filters } } } -} +} \ No newline at end of file diff --git a/SabreTools.Library/External/Compress/SevenZip/Filters/Filter.cs b/SabreTools.Library/External/Compress/SevenZip/Filters/Filter.cs index cd1f80d9..e9443eda 100644 --- a/SabreTools.Library/External/Compress/SevenZip/Filters/Filter.cs +++ b/SabreTools.Library/External/Compress/SevenZip/Filters/Filter.cs @@ -149,7 +149,7 @@ namespace Compress.SevenZip.Filters while (seekToGo > 0) { long get = seekToGo > bufferSize ? bufferSize : seekToGo; - Read(seekBuffer, 0, (int) get); + Read(seekBuffer, 0, (int)get); seekToGo -= get; } return position; @@ -168,4 +168,4 @@ namespace Compress.SevenZip.Filters protected abstract int Transform(byte[] buffer, int offset, int count); } -} +} \ No newline at end of file diff --git a/SabreTools.Library/External/Compress/SevenZip/SevenZip.cs b/SabreTools.Library/External/Compress/SevenZip/SevenZip.cs index d5693afb..a0c9a230 100644 --- a/SabreTools.Library/External/Compress/SevenZip/SevenZip.cs +++ b/SabreTools.Library/External/Compress/SevenZip/SevenZip.cs @@ -1,21 +1,38 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; +using System.Collections.Generic; using System.IO; using System.Text; -using Compress.SevenZip.Compress.BZip2; -using Compress.SevenZip.Compress.LZMA; -using Compress.SevenZip.Compress.PPmd; -using Compress.SevenZip.Filters; using Compress.SevenZip.Structure; -using Compress.Utils; using FileInfo = RVIO.FileInfo; -using FileStream = RVIO.FileStream; namespace Compress.SevenZip { - public class SevenZ : ICompress + public partial class SevenZ : ICompress { + public static bool supportZstd + { + get; + private set; + } + + public static void TestForZstd() + { + supportZstd = RVIO.File.Exists("libzstd.dll"); + } + + + + private class LocalFile + { + public string FileName; + public ulong UncompressedSize; + public bool IsDirectory; + public byte[] CRC; + public int StreamIndex; + public ulong StreamOffset; + public ZipReturn FileStatus = ZipReturn.ZipUntested; + } + + private List _localFiles = new List(); private FileInfo _zipFileInfo; @@ -105,186 +122,13 @@ namespace Compress.SevenZip } - public void ZipFileAddDirectory(string filename) - { - string fName = filename; - if (fName.Substring(fName.Length - 1, 1) == @"/") - fName = fName.Substring(0, fName.Length - 1); - - LocalFile lf = new LocalFile - { - FileName = fName, - UncompressedSize = 0, - IsDirectory = true, - StreamOffset = 0 - }; - _localFiles.Add(lf); - } - - private class LocalFile - { - public string FileName; - public ulong UncompressedSize; - public bool IsDirectory; - public byte[] CRC; - public int StreamIndex; - public ulong StreamOffset; - public ZipReturn FileStatus = ZipReturn.ZipUntested; - } - - #region open 7z files - - public ZipReturn ZipFileOpen(string filename, long timestamp, bool readHeaders) - { - ZipFileClose(); - Debug.WriteLine(filename); - #region open file stream - - try - { - if (!RVIO.File.Exists(filename)) - { - ZipFileClose(); - return ZipReturn.ZipErrorFileNotFound; - } - _zipFileInfo = new FileInfo(filename); - if ((timestamp != -1) && (_zipFileInfo.LastWriteTime != timestamp)) - { - ZipFileClose(); - return ZipReturn.ZipErrorTimeStamp; - } - int errorCode = FileStream.OpenFileRead(filename, out _zipFs); - if (errorCode != 0) - { - ZipFileClose(); - return ZipReturn.ZipErrorOpeningFile; - } - } - catch (PathTooLongException) - { - ZipFileClose(); - return ZipReturn.ZipFileNameToLong; - } - catch (IOException) - { - ZipFileClose(); - return ZipReturn.ZipErrorOpeningFile; - } - - #endregion - - ZipOpen = ZipOpenType.OpenRead; - ZipStatus = ZipStatus.None; - - return ZipFileReadHeaders(); - } - - public ZipReturn ZipFileOpen(Stream inStream) - { - ZipFileClose(); - _zipFileInfo = null; - _zipFs = inStream; - ZipOpen = ZipOpenType.OpenRead; - ZipStatus = ZipStatus.None; - return ZipFileReadHeaders(); - } - - private ZipReturn ZipFileReadHeaders() - { - try - { - SignatureHeader signatureHeader = new SignatureHeader(); - if (!signatureHeader.Read(_zipFs)) - { - return ZipReturn.ZipSignatureError; - } - - _baseOffset = _zipFs.Position; - - //_zipFs.Seek(_baseOffset + (long)signatureHeader.NextHeaderOffset, SeekOrigin.Begin); - //byte[] mainHeader = new byte[signatureHeader.NextHeaderSize]; - //_zipFs.Read(mainHeader, 0, (int)signatureHeader.NextHeaderSize); - //if (!CRC.VerifyDigest(signatureHeader.NextHeaderCRC, mainHeader, 0, (uint)signatureHeader.NextHeaderSize)) - // return ZipReturn.Zip64EndOfCentralDirError; - - if (signatureHeader.NextHeaderSize != 0) - { - _zipFs.Seek(_baseOffset + (long)signatureHeader.NextHeaderOffset, SeekOrigin.Begin); - ZipReturn zr = Header.ReadHeaderOrPackedHeader(_zipFs, _baseOffset, out _header); - if (zr != ZipReturn.ZipGood) - { - return zr; - } - } - - _zipFs.Seek(_baseOffset + (long)(signatureHeader.NextHeaderOffset + signatureHeader.NextHeaderSize), SeekOrigin.Begin); - - ZipStatus = ZipStatus.None; - - ZipStatus |= IsRomVault7Z() ? ZipStatus.TrrntZip : ZipStatus.None; - ZipStatus |= Istorrent7Z() ? ZipStatus.Trrnt7Zip : ZipStatus.None; - PopulateLocalFiles(out _localFiles); - - return ZipReturn.ZipGood; - } - catch - { - ZipFileClose(); - return ZipReturn.ZipErrorReadingFile; - } - } - private void PopulateLocalFiles(out List localFiles) - { - int emptyFileIndex = 0; - int folderIndex = 0; - int unpackedStreamsIndex = 0; - ulong streamOffset = 0; - localFiles = new List(); - if (_header == null) - return; - for (int i = 0; i < _header.FileInfo.Names.Length; i++) - { - LocalFile lf = new LocalFile { FileName = _header.FileInfo.Names[i] }; - if ((_header.FileInfo.EmptyStreamFlags == null) || !_header.FileInfo.EmptyStreamFlags[i]) - { - lf.StreamIndex = folderIndex; - lf.StreamOffset = streamOffset; - lf.UncompressedSize = _header.StreamsInfo.Folders[folderIndex].UnpackedStreamInfo[unpackedStreamsIndex].UnpackedSize; - lf.CRC = Util.uinttobytes(_header.StreamsInfo.Folders[folderIndex].UnpackedStreamInfo[unpackedStreamsIndex].Crc); - streamOffset += lf.UncompressedSize; - unpackedStreamsIndex++; - if (unpackedStreamsIndex >= _header.StreamsInfo.Folders[folderIndex].UnpackedStreamInfo.Length) - { - folderIndex++; - unpackedStreamsIndex = 0; - streamOffset = 0; - } - } - else - { - lf.UncompressedSize = 0; - lf.CRC = new byte[] { 0, 0, 0, 0 }; - lf.IsDirectory = (_header.FileInfo.EmptyFileFlags == null) || !_header.FileInfo.EmptyFileFlags[emptyFileIndex++]; - - if (lf.IsDirectory) - { - if (lf.FileName.Substring(lf.FileName.Length - 1, 1) != "/") - { - lf.FileName += "/"; - } - } - } - - localFiles.Add(lf); - } - } public void ZipFileClose() @@ -312,6 +156,7 @@ namespace Compress.SevenZip ZipOpen = ZipOpenType.Closed; } + private Header _header; public StringBuilder HeaderReport() @@ -328,755 +173,5 @@ namespace Compress.SevenZip return sb; } - - // not finalized yet, so do not use - private void WriteRomVault7Zip(BinaryWriter bw, ulong headerPos, ulong headerLength, uint headerCRC) - { - const string sig = "RomVault7Z01"; - byte[] RV7Zid = Util.Enc.GetBytes(sig); - - // RomVault 7Zip torrent header - // 12 bytes : RomVault7Zip - // 4 bytes : HeaderCRC - // 8 bytes : HeaderPos - // 8 bytes : HeaderLength - - bw.Write(RV7Zid); - bw.Write(headerCRC); - bw.Write(headerPos); - bw.Write(headerLength); - - ZipStatus = ZipStatus.TrrntZip; - } - - - private bool IsRomVault7Z() - { - long length = _zipFs.Length; - if (length < 32) - { - return false; - } - - _zipFs.Seek(length - 32, SeekOrigin.Begin); - - const string sig = "RomVault7Z01"; - byte[] rv7Zid = Util.Enc.GetBytes(sig); - - byte[] header = new byte[12]; - _zipFs.Read(header, 0, 12); - for (int i = 0; i < 12; i++) - { - if (header[i] != rv7Zid[i]) - { - return false; - } - } - - uint headerCRC; - ulong headerOffset; - ulong headerSize; - using (BinaryReader br = new BinaryReader(_zipFs, Encoding.UTF8, true)) - { - headerCRC = br.ReadUInt32(); - headerOffset = br.ReadUInt64(); - headerSize = br.ReadUInt64(); - } - - if ((ulong)length < headerOffset) - { - return false; - } - - _zipFs.Seek((long)headerOffset, SeekOrigin.Begin); - - byte[] mainHeader = new byte[headerSize]; - int bytesread = _zipFs.Read(mainHeader, 0, (int)headerSize); - - return ((ulong)bytesread == headerSize) && - Utils.CRC.VerifyDigest(headerCRC, mainHeader, 0, (uint)headerSize); - - } - - private bool Istorrent7Z() - { - const int crcsz = 128; - const int t7ZsigSize = 16 + 1 + 9 + 4 + 4; - byte[] kSignature = { (byte)'7', (byte)'z', 0xBC, 0xAF, 0x27, 0x1C }; - int kSignatureSize = kSignature.Length; - const string sig = "\xa9\x9f\xd1\x57\x08\xa9\xd7\xea\x29\x64\xb2\x36\x1b\x83\x52\x33\x01torrent7z_0.9beta"; - byte[] t7Zid = Util.Enc.GetBytes(sig); - int t7ZidSize = t7Zid.Length; - - const int tmpbufsize = 256 + t7ZsigSize + 8 + 4; - byte[] buffer = new byte[tmpbufsize]; - - // read fist 128 bytes, pad with zeros if less bytes - int bufferPos = 0; - _zipFs.Seek(0, SeekOrigin.Begin); - int ar = _zipFs.Read(buffer, bufferPos, crcsz); - if (ar < crcsz) - { - Util.memset(buffer, bufferPos + ar, 0, crcsz - ar); - } - bufferPos = crcsz; - - long foffs = _zipFs.Length; - int endReadLength = crcsz + t7ZsigSize + 4; - foffs = foffs < endReadLength ? 0 : foffs - endReadLength; - - _zipFs.Seek(foffs, SeekOrigin.Begin); - - ar = _zipFs.Read(buffer, bufferPos, endReadLength); - if (ar < endReadLength) - { - if (ar >= t7ZsigSize + 4) - { - ar -= t7ZsigSize + 4; - } - if (ar < kSignatureSize) - { - ar = kSignatureSize; - } - Util.memset(buffer, bufferPos + ar, 0, crcsz - ar); - Util.memcpyr(buffer, crcsz * 2 + 8, buffer, bufferPos + ar, t7ZsigSize + 4); - } - else - { - Util.memcpyr(buffer, crcsz * 2 + 8, buffer, crcsz * 2, t7ZsigSize + 4); - } - - foffs = _zipFs.Length; - foffs -= t7ZsigSize + 4; - - //memcpy(buffer, crcsz * 2, &foffs, 8); - buffer[crcsz * 2 + 0] = (byte)((foffs >> 0) & 0xff); - buffer[crcsz * 2 + 1] = (byte)((foffs >> 8) & 0xff); - buffer[crcsz * 2 + 2] = (byte)((foffs >> 16) & 0xff); - buffer[crcsz * 2 + 3] = (byte)((foffs >> 24) & 0xff); - buffer[crcsz * 2 + 4] = 0; - buffer[crcsz * 2 + 5] = 0; - buffer[crcsz * 2 + 6] = 0; - buffer[crcsz * 2 + 7] = 0; - - if (Util.memcmp(buffer, 0, kSignature, kSignatureSize)) - { - t7Zid[16] = buffer[crcsz * 2 + 4 + 8 + 16]; - if (Util.memcmp(buffer, crcsz * 2 + 4 + 8, t7Zid, t7ZidSize)) - { - uint inCrc32 = (uint)(buffer[crcsz * 2 + 8 + 0] + - (buffer[crcsz * 2 + 8 + 1] << 8) + - (buffer[crcsz * 2 + 8 + 2] << 16) + - (buffer[crcsz * 2 + 8 + 3] << 24)); - - buffer[crcsz * 2 + 8 + 0] = 0xff; - buffer[crcsz * 2 + 8 + 1] = 0xff; - buffer[crcsz * 2 + 8 + 2] = 0xff; - buffer[crcsz * 2 + 8 + 3] = 0xff; - - uint calcCrc32 = Utils.CRC.CalculateDigest(buffer, 0, crcsz * 2 + 8 + t7ZsigSize + 4); - - if (inCrc32 == calcCrc32) - { - return true; - } - } - } - - return false; - } - - #endregion - - #region read 7z file - - private int _streamIndex = -1; - private Stream _stream; - - public ZipReturn ZipFileOpenReadStream(int index, out Stream stream, out ulong unCompressedSize) - { - Debug.WriteLine("Opening File " + _localFiles[index].FileName); - stream = null; - unCompressedSize = 0; - - try - { - if (ZipOpen != ZipOpenType.OpenRead) - { - return ZipReturn.ZipErrorGettingDataStream; - } - - if (IsDirectory(index)) - { - return ZipReturn.ZipTryingToAccessADirectory; - } - - unCompressedSize = _localFiles[index].UncompressedSize; - int thisStreamIndex = _localFiles[index].StreamIndex; - ulong streamOffset = _localFiles[index].StreamOffset; - - if ((thisStreamIndex == _streamIndex) && (streamOffset >= (ulong)_stream.Position)) - { - stream = _stream; - stream.Seek((long)_localFiles[index].StreamOffset - _stream.Position, SeekOrigin.Current); - return ZipReturn.ZipGood; - } - - ZipFileCloseReadStream(); - _streamIndex = thisStreamIndex; - - - Folder folder = _header.StreamsInfo.Folders[_streamIndex]; - - // first make the List of Decompressors streams - int codersNeeded = folder.Coders.Length; - - List allInputStreams = new List(); - for (int i = 0; i < codersNeeded; i++) - { - folder.Coders[i].DecoderStream = null; - allInputStreams.AddRange(folder.Coders[i].InputStreamsSourceInfo); - } - - // now use the binding pairs to links the outputs to the inputs - int bindPairsCount = folder.BindPairs.Length; - for (int i = 0; i < bindPairsCount; i++) - { - allInputStreams[(int)folder.BindPairs[i].InIndex].InStreamSource = InStreamSource.CompStreamOutput; - allInputStreams[(int)folder.BindPairs[i].InIndex].InStreamIndex = folder.BindPairs[i].OutIndex; - folder.Coders[(int)folder.BindPairs[i].OutIndex].OutputUsedInternally = true; - } - - // next use the stream indises to connect the remaining input streams from the sourcefile - int packedStreamsCount = folder.PackedStreamIndices.Length; - for (int i = 0; i < packedStreamsCount; i++) - { - ulong packedStreamIndex = (ulong)i + folder.PackedStreamIndexBase; - - // create and open the source file stream if needed - if (_header.StreamsInfo.PackedStreams[packedStreamIndex].PackedStream == null) - { - _header.StreamsInfo.PackedStreams[packedStreamIndex].PackedStream = CloneStream(_zipFs); - } - _header.StreamsInfo.PackedStreams[packedStreamIndex].PackedStream.Seek( - _baseOffset + (long)_header.StreamsInfo.PackedStreams[packedStreamIndex].StreamPosition, SeekOrigin.Begin); - - - allInputStreams[(int)folder.PackedStreamIndices[i]].InStreamSource = InStreamSource.FileStream; - allInputStreams[(int)folder.PackedStreamIndices[i]].InStreamIndex = packedStreamIndex; - } - - List inputCoders = new List(); - - bool allCodersComplete = false; - while (!allCodersComplete) - { - allCodersComplete = true; - for (int i = 0; i < codersNeeded; i++) - { - Coder coder = folder.Coders[i]; - - // check is decoder already processed - if (coder.DecoderStream != null) - { - continue; - } - - inputCoders.Clear(); - for (int j = 0; j < (int)coder.NumInStreams; j++) - { - if (coder.InputStreamsSourceInfo[j].InStreamSource == InStreamSource.FileStream) - { - inputCoders.Add(_header.StreamsInfo.PackedStreams[coder.InputStreamsSourceInfo[j].InStreamIndex].PackedStream); - } - else if (coder.InputStreamsSourceInfo[j].InStreamSource == InStreamSource.CompStreamOutput) - { - if (folder.Coders[coder.InputStreamsSourceInfo[j].InStreamIndex].DecoderStream == null) - { - break; - } - inputCoders.Add(folder.Coders[coder.InputStreamsSourceInfo[j].InStreamIndex].DecoderStream); - } - else - { - // unknown input type so error - return ZipReturn.ZipDecodeError; - } - } - - if (inputCoders.Count == (int)coder.NumInStreams) - { - // all inputs streams are available to make the decoder stream - switch (coder.DecoderType) - { - case DecompressType.Stored: - coder.DecoderStream = inputCoders[0]; - break; - case DecompressType.Delta: - coder.DecoderStream = new Delta(folder.Coders[i].Properties, inputCoders[0]); - break; - case DecompressType.LZMA: - coder.DecoderStream = new LzmaStream(folder.Coders[i].Properties, inputCoders[0]); - break; - case DecompressType.LZMA2: - coder.DecoderStream = new LzmaStream(folder.Coders[i].Properties, inputCoders[0]); - break; - case DecompressType.PPMd: - coder.DecoderStream = new PpmdStream(new PpmdProperties(folder.Coders[i].Properties), inputCoders[0], false); - break; - case DecompressType.BZip2: - coder.DecoderStream = new CBZip2InputStream(inputCoders[0], false); - break; - case DecompressType.BCJ: - coder.DecoderStream = new BCJFilter(false, inputCoders[0]); - break; - case DecompressType.BCJ2: - coder.DecoderStream = new BCJ2Filter(inputCoders[0], inputCoders[1], inputCoders[2], inputCoders[3]); - break; - default: - return ZipReturn.ZipDecodeError; - } - } - - // if skipped a coder need to loop round again - if (coder.DecoderStream == null) - { - allCodersComplete = false; - } - } - } - // find the final output stream and return it. - int outputStream = -1; - for (int i = 0; i < codersNeeded; i++) - { - Coder coder = folder.Coders[i]; - if (!coder.OutputUsedInternally) - { - outputStream = i; - } - } - - stream = folder.Coders[outputStream].DecoderStream; - stream.Seek((long)_localFiles[index].StreamOffset, SeekOrigin.Current); - - _stream = stream; - - return ZipReturn.ZipGood; - - } - catch (Exception e) - { - return ZipReturn.ZipErrorGettingDataStream; - } - - } - - private Stream CloneStream(Stream s) - { - switch (s) - { - case System.IO.FileStream _: - int errorCode = FileStream.OpenFileRead(ZipFilename, out Stream streamOut); - return errorCode != 0 ? null : streamOut; - - case MemoryStream memStream: - long pos = memStream.Position; - memStream.Position = 0; - byte[] newStream = new byte[memStream.Length]; - memStream.Read(newStream, 0, (int)memStream.Length); - MemoryStream ret = new MemoryStream(newStream, false); - memStream.Position = pos; - return ret; - } - - return null; - } - - public ZipReturn ZipFileCloseReadStream() - { - if (_streamIndex != -1) - { - Folder folder = _header.StreamsInfo.Folders[_streamIndex]; - - foreach (Coder c in folder.Coders) - { - Stream ds = c?.DecoderStream; - if (ds == null) - { - continue; - } - ds.Close(); - ds.Dispose(); - c.DecoderStream = null; - } - } - _streamIndex = -1; - - if (_header?.StreamsInfo != null) - { - foreach (PackedStreamInfo psi in _header.StreamsInfo.PackedStreams) - { - if (psi?.PackedStream == null) - { - continue; - } - psi.PackedStream.Close(); - psi.PackedStream.Dispose(); - psi.PackedStream = null; - } - } - return ZipReturn.ZipGood; - } - - #endregion - - #region write 7z File - - private LzmaStream _lzmaStream; - private ulong _packStreamStart; - private ulong _packStreamSize; - private ulong _unpackedStreamSize; - private byte[] _codeMSbytes; - - - public void ZipFileAddDirectory() - { - // do nothing here for 7zip - } - - public ZipReturn ZipFileCreate(string newFilename) - { - return ZipFileCreate(newFilename, true); - } - - - public ZipReturn ZipFileCreateFromUncompressedSize(string newFilename, ulong unCompressedSize) - { - return ZipFileCreate(newFilename,true, GetDictionarySizeFromUncompressedSize(unCompressedSize)); - } - - public ZipReturn ZipFileCreate(string newFilename, bool compressOutput, int dictionarySize = 1 << 24, int numFastBytes = 64) - { - if (ZipOpen != ZipOpenType.Closed) - { - return ZipReturn.ZipFileAlreadyOpen; - } - - DirUtil.CreateDirForFile(newFilename); - _zipFileInfo = new FileInfo(newFilename); - - int errorCode = FileStream.OpenFileWrite(newFilename, out _zipFs); - if (errorCode != 0) - { - ZipFileClose(); - return ZipReturn.ZipErrorOpeningFile; - } - ZipOpen = ZipOpenType.OpenWrite; - - _signatureHeader = new SignatureHeader(); - _header = new Header(); - - using (BinaryWriter bw = new BinaryWriter(_zipFs, Encoding.UTF8, true)) - { - _signatureHeader.Write(bw); - } - - _compressed = compressOutput; - - _unpackedStreamSize = 0; - if (_compressed) - { - LzmaEncoderProperties ep = new LzmaEncoderProperties(true, dictionarySize, numFastBytes); - _lzmaStream = new LzmaStream(ep, false, _zipFs); - _codeMSbytes = _lzmaStream.Properties; - _packStreamStart = (ulong)_zipFs.Position; - } - - return ZipReturn.ZipGood; - } - - public ZipReturn ZipFileOpenWriteStream(bool raw, bool trrntzip, string filename, ulong uncompressedSize, ushort compressionMethod, out Stream stream) - { - return ZipFileOpenWriteStream(filename, uncompressedSize, out stream); - } - - private ZipReturn ZipFileOpenWriteStream(string filename, ulong uncompressedSize, out Stream stream) - { - LocalFile lf = new LocalFile - { - FileName = filename, - UncompressedSize = uncompressedSize, - StreamOffset = (ulong)(_zipFs.Position - _signatureHeader.BaseOffset) - }; - - _unpackedStreamSize += uncompressedSize; - - _localFiles.Add(lf); - stream = _compressed ? _lzmaStream : _zipFs; - return ZipReturn.ZipGood; - } - - - public ZipReturn ZipFileCloseWriteStream(byte[] crc32) - { - _localFiles[_localFiles.Count - 1].CRC = new[] { crc32[3], crc32[2], crc32[1], crc32[0] }; - return ZipReturn.ZipGood; - } - - - private void CloseWriting7Zip() - { - if (_compressed) - { - _lzmaStream.Close(); - } - - _packStreamSize = (ulong)_zipFs.Position - _packStreamStart; - - Create7ZStructure(); - - byte[] newHeaderByte; - using (Stream headerMem = new MemoryStream()) - { - using (BinaryWriter headerBw = new BinaryWriter(headerMem, Encoding.UTF8, true)) - { - _header.WriteHeader(headerBw); - newHeaderByte = new byte[headerMem.Length]; - headerMem.Position = 0; - headerMem.Read(newHeaderByte, 0, newHeaderByte.Length); - } - } - - uint mainHeaderCRC = Utils.CRC.CalculateDigest(newHeaderByte, 0, (uint)newHeaderByte.Length); - - ulong headerpos = (ulong)_zipFs.Position; - _zipFs.Write(newHeaderByte,0,newHeaderByte.Length); - using (BinaryWriter bw = new BinaryWriter(_zipFs, Encoding.UTF8, true)) - { - _signatureHeader.WriteFinal(bw, headerpos, (ulong)newHeaderByte.Length, mainHeaderCRC); - WriteRomVault7Zip(bw, headerpos, (ulong)newHeaderByte.Length, mainHeaderCRC); - } - _zipFs.Flush(); - _zipFs.Close(); - _zipFs.Dispose(); - } - - - private void Create7ZStructure() - { - int fileCount = _localFiles.Count; - - //FileInfo - _header.FileInfo = new Structure.FileInfo - { - Names = new string[fileCount] - }; - - ulong emptyStreamCount = 0; - ulong emptyFileCount = 0; - for (int i = 0; i < fileCount; i++) - { - _header.FileInfo.Names[i] = _localFiles[i].FileName; - - if (_localFiles[i].UncompressedSize != 0) - { - continue; - } - - if (!_localFiles[i].IsDirectory) - { - emptyFileCount += 1; - } - - emptyStreamCount += 1; - } - ulong outFileCount = (ulong)_localFiles.Count - emptyStreamCount; - - _header.FileInfo.EmptyStreamFlags = null; - _header.FileInfo.EmptyFileFlags = null; - _header.FileInfo.Attributes = null; - - if (emptyStreamCount > 0) - { - if (emptyStreamCount != emptyFileCount) //then we found directories and need to set the attributes - { - _header.FileInfo.Attributes = new uint[fileCount]; - } - - if (emptyFileCount > 0) - { - _header.FileInfo.EmptyFileFlags = new bool[emptyStreamCount]; - } - - emptyStreamCount = 0; - _header.FileInfo.EmptyStreamFlags = new bool[fileCount]; - for (int i = 0; i < fileCount; i++) - { - if (_localFiles[i].UncompressedSize != 0) - { - continue; - } - - if (_localFiles[i].IsDirectory) - { - if (_header.FileInfo.Attributes != null) - _header.FileInfo.Attributes[i] = 0x10; // set attributes to directory - } - else - { - if (_header.FileInfo.EmptyFileFlags != null) - _header.FileInfo.EmptyFileFlags[emptyStreamCount] = true; // set empty file flag - } - - _header.FileInfo.EmptyStreamFlags[i] = true; - emptyStreamCount += 1; - } - } - - - //StreamsInfo - _header.StreamsInfo = new StreamsInfo { PackPosition = 0 }; - - //StreamsInfo.PackedStreamsInfo - if (_compressed) - { - _header.StreamsInfo.PackedStreams = new PackedStreamInfo[1]; - _header.StreamsInfo.PackedStreams[0] = new PackedStreamInfo { PackedSize = _packStreamSize }; - } - else - { - _header.StreamsInfo.PackedStreams = new PackedStreamInfo[outFileCount]; - int fileIndex = 0; - for (int i = 0; i < fileCount; i++) - { - if (_localFiles[i].UncompressedSize == 0) - { - continue; - } - _header.StreamsInfo.PackedStreams[fileIndex++] = new PackedStreamInfo { PackedSize = _localFiles[i].UncompressedSize }; - } - } - //StreamsInfo.PackedStreamsInfo, no CRC or StreamPosition required - - if (_compressed) - { - //StreamsInfo.Folders - _header.StreamsInfo.Folders = new Folder[1]; - - Folder folder = new Folder { Coders = new Coder[1] }; - - //StreamsInfo.Folders.Coder - // flags 0x23 - folder.Coders[0] = new Coder - { - Method = new byte[] { 3, 1, 1 }, - NumInStreams = 1, - NumOutStreams = 1, - Properties = _codeMSbytes - }; - folder.BindPairs = null; - folder.PackedStreamIndices = new[] { (ulong)0 }; - folder.UnpackedStreamSizes = new[] { _unpackedStreamSize }; - folder.UnpackCRC = null; - - folder.UnpackedStreamInfo = new UnpackedStreamInfo[outFileCount]; - int fileIndex = 0; - for (int i = 0; i < fileCount; i++) - { - if (_localFiles[i].UncompressedSize == 0) - { - continue; - } - UnpackedStreamInfo unpackedStreamInfo = new UnpackedStreamInfo - { - UnpackedSize = _localFiles[i].UncompressedSize, - Crc = Util.bytestouint(_localFiles[i].CRC) - }; - folder.UnpackedStreamInfo[fileIndex++] = unpackedStreamInfo; - } - _header.StreamsInfo.Folders[0] = folder; - } - else - { - _header.StreamsInfo.Folders = new Folder[outFileCount]; - int fileIndex = 0; - for (int i = 0; i < fileCount; i++) - { - if (_localFiles[i].UncompressedSize == 0) - { - continue; - } - Folder folder = new Folder { Coders = new Coder[1] }; - - //StreamsInfo.Folders.Coder - // flags 0x01 - folder.Coders[0] = new Coder - { - Method = new byte[] { 0 }, - NumInStreams = 1, - NumOutStreams = 1, - Properties = null - }; - - folder.BindPairs = null; - folder.PackedStreamIndices = new[] { (ulong)i }; - folder.UnpackedStreamSizes = new[] { _localFiles[i].UncompressedSize }; - folder.UnpackCRC = null; - - folder.UnpackedStreamInfo = new UnpackedStreamInfo[1]; - UnpackedStreamInfo unpackedStreamInfo = new UnpackedStreamInfo - { - UnpackedSize = _localFiles[i].UncompressedSize, - Crc = Util.bytestouint(_localFiles[i].CRC) - }; - folder.UnpackedStreamInfo[0] = unpackedStreamInfo; - - _header.StreamsInfo.Folders[fileIndex++] = folder; - } - } - } - - #endregion - - - private static readonly int[] DictionarySizes = - { - 0x10000, - 0x18000, - 0x20000, - 0x30000, - 0x40000, - 0x60000, - 0x80000, - 0xc0000, - - 0x100000, - 0x180000, - 0x200000, - 0x300000, - 0x400000, - 0x600000, - 0x800000, - 0xc00000, - - 0x1000000, - 0x1800000, - 0x2000000, - 0x3000000, - 0x4000000, - 0x6000000 - }; - - - private static int GetDictionarySizeFromUncompressedSize(ulong unCompressedSize) - { - foreach (int v in DictionarySizes) - { - if ((ulong)v >= unCompressedSize) - return v; - } - - return DictionarySizes[DictionarySizes.Length - 1]; - } } } \ No newline at end of file diff --git a/SabreTools.Library/External/Compress/SevenZip/SevenZipOpen.cs b/SabreTools.Library/External/Compress/SevenZip/SevenZipOpen.cs new file mode 100644 index 00000000..1199c080 --- /dev/null +++ b/SabreTools.Library/External/Compress/SevenZip/SevenZipOpen.cs @@ -0,0 +1,170 @@ +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using Compress.SevenZip.Structure; +using Compress.Utils; +using FileInfo = RVIO.FileInfo; +using FileStream = RVIO.FileStream; + +namespace Compress.SevenZip +{ + public partial class SevenZ + { + public ZipReturn ZipFileOpen(string filename, long timestamp, bool readHeaders) + { + ZipFileClose(); + Debug.WriteLine(filename); + #region open file stream + + try + { + if (!RVIO.File.Exists(filename)) + { + ZipFileClose(); + return ZipReturn.ZipErrorFileNotFound; + } + _zipFileInfo = new FileInfo(filename); + if ((timestamp != -1) && (_zipFileInfo.LastWriteTime != timestamp)) + { + ZipFileClose(); + return ZipReturn.ZipErrorTimeStamp; + } + int errorCode = FileStream.OpenFileRead(filename, out _zipFs); + if (errorCode != 0) + { + ZipFileClose(); + return ZipReturn.ZipErrorOpeningFile; + } + } + catch (PathTooLongException) + { + ZipFileClose(); + return ZipReturn.ZipFileNameToLong; + } + catch (IOException) + { + ZipFileClose(); + return ZipReturn.ZipErrorOpeningFile; + } + + #endregion + + ZipOpen = ZipOpenType.OpenRead; + ZipStatus = ZipStatus.None; + + return ZipFileReadHeaders(); + } + + + public ZipReturn ZipFileOpen(Stream inStream) + { + ZipFileClose(); + _zipFileInfo = null; + _zipFs = inStream; + ZipOpen = ZipOpenType.OpenRead; + ZipStatus = ZipStatus.None; + return ZipFileReadHeaders(); + } + + + + private ZipReturn ZipFileReadHeaders() + { + try + { + SignatureHeader signatureHeader = new SignatureHeader(); + if (!signatureHeader.Read(_zipFs)) + { + return ZipReturn.ZipSignatureError; + } + + _baseOffset = _zipFs.Position; + + _zipFs.Seek(_baseOffset + (long)signatureHeader.NextHeaderOffset, SeekOrigin.Begin); + byte[] mainHeader = new byte[signatureHeader.NextHeaderSize]; + _zipFs.Read(mainHeader, 0, (int)signatureHeader.NextHeaderSize); + if (!CRC.VerifyDigest(signatureHeader.NextHeaderCRC, mainHeader, 0, (uint)signatureHeader.NextHeaderSize)) + return ZipReturn.Zip64EndOfCentralDirError; + + if (signatureHeader.NextHeaderSize != 0) + { + _zipFs.Seek(_baseOffset + (long)signatureHeader.NextHeaderOffset, SeekOrigin.Begin); + ZipReturn zr = Header.ReadHeaderOrPackedHeader(_zipFs, _baseOffset, out _header); + if (zr != ZipReturn.ZipGood) + { + return zr; + } + } + + + ZipStatus = ZipStatus.None; + ZipStatus |= IsRomVault7Z(_baseOffset, signatureHeader.NextHeaderOffset, signatureHeader.NextHeaderSize, signatureHeader.NextHeaderCRC) ? ZipStatus.TrrntZip : ZipStatus.None; + + _zipFs.Seek(_baseOffset + (long)(signatureHeader.NextHeaderOffset + signatureHeader.NextHeaderSize), SeekOrigin.Begin); + ZipStatus |= Istorrent7Z() ? ZipStatus.Trrnt7Zip : ZipStatus.None; + PopulateLocalFiles(out _localFiles); + + return ZipReturn.ZipGood; + } + catch + { + ZipFileClose(); + return ZipReturn.ZipErrorReadingFile; + } + } + + + private void PopulateLocalFiles(out List localFiles) + { + int emptyFileIndex = 0; + int folderIndex = 0; + int unpackedStreamsIndex = 0; + ulong streamOffset = 0; + localFiles = new List(); + + if (_header == null) + return; + + for (int i = 0; i < _header.FileInfo.Names.Length; i++) + { + LocalFile lf = new LocalFile { FileName = _header.FileInfo.Names[i] }; + + if ((_header.FileInfo.EmptyStreamFlags == null) || !_header.FileInfo.EmptyStreamFlags[i]) + { + lf.StreamIndex = folderIndex; + lf.StreamOffset = streamOffset; + lf.UncompressedSize = _header.StreamsInfo.Folders[folderIndex].UnpackedStreamInfo[unpackedStreamsIndex].UnpackedSize; + lf.CRC = Util.uinttobytes(_header.StreamsInfo.Folders[folderIndex].UnpackedStreamInfo[unpackedStreamsIndex].Crc); + + streamOffset += lf.UncompressedSize; + unpackedStreamsIndex++; + + if (unpackedStreamsIndex >= _header.StreamsInfo.Folders[folderIndex].UnpackedStreamInfo.Length) + { + folderIndex++; + unpackedStreamsIndex = 0; + streamOffset = 0; + } + } + else + { + lf.UncompressedSize = 0; + lf.CRC = new byte[] { 0, 0, 0, 0 }; + lf.IsDirectory = (_header.FileInfo.EmptyFileFlags == null) || !_header.FileInfo.EmptyFileFlags[emptyFileIndex++]; + + if (lf.IsDirectory) + { + if (lf.FileName.Substring(lf.FileName.Length - 1, 1) != "/") + { + lf.FileName += "/"; + } + } + } + + localFiles.Add(lf); + } + } + + + } +} \ No newline at end of file diff --git a/SabreTools.Library/External/Compress/SevenZip/SevenZipOpenRead.cs b/SabreTools.Library/External/Compress/SevenZip/SevenZipOpenRead.cs new file mode 100644 index 00000000..8ce2d7ae --- /dev/null +++ b/SabreTools.Library/External/Compress/SevenZip/SevenZipOpenRead.cs @@ -0,0 +1,260 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.IO.Compression; +using Compress.SevenZip.Compress.BZip2; +using Compress.SevenZip.Compress.LZMA; +using Compress.SevenZip.Compress.PPmd; +using Compress.SevenZip.Filters; +using Compress.SevenZip.Structure; +using Zstandard.Net; +using FileStream = RVIO.FileStream; + +namespace Compress.SevenZip +{ + public partial class SevenZ + { + private int _streamIndex = -1; + private Stream _stream; + + public ZipReturn ZipFileOpenReadStream(int index, out Stream stream, out ulong unCompressedSize) + { + Debug.WriteLine("Opening File " + _localFiles[index].FileName); + stream = null; + unCompressedSize = 0; + + try + { + if (ZipOpen != ZipOpenType.OpenRead) + { + return ZipReturn.ZipErrorGettingDataStream; + } + + if (IsDirectory(index)) + { + return ZipReturn.ZipTryingToAccessADirectory; + } + + unCompressedSize = _localFiles[index].UncompressedSize; + int thisStreamIndex = _localFiles[index].StreamIndex; + ulong streamOffset = _localFiles[index].StreamOffset; + + if ((thisStreamIndex == _streamIndex) && (streamOffset >= (ulong)_stream.Position)) + { + stream = _stream; + stream.Seek((long)_localFiles[index].StreamOffset - _stream.Position, SeekOrigin.Current); + return ZipReturn.ZipGood; + } + + ZipFileCloseReadStream(); + _streamIndex = thisStreamIndex; + + + Folder folder = _header.StreamsInfo.Folders[_streamIndex]; + + // first make the List of Decompressors streams + int codersNeeded = folder.Coders.Length; + + List allInputStreams = new List(); + for (int i = 0; i < codersNeeded; i++) + { + folder.Coders[i].DecoderStream = null; + allInputStreams.AddRange(folder.Coders[i].InputStreamsSourceInfo); + } + + // now use the binding pairs to links the outputs to the inputs + int bindPairsCount = folder.BindPairs.Length; + for (int i = 0; i < bindPairsCount; i++) + { + allInputStreams[(int)folder.BindPairs[i].InIndex].InStreamSource = InStreamSource.CompStreamOutput; + allInputStreams[(int)folder.BindPairs[i].InIndex].InStreamIndex = folder.BindPairs[i].OutIndex; + folder.Coders[(int)folder.BindPairs[i].OutIndex].OutputUsedInternally = true; + } + + // next use the stream indises to connect the remaining input streams from the sourcefile + int packedStreamsCount = folder.PackedStreamIndices.Length; + for (int i = 0; i < packedStreamsCount; i++) + { + ulong packedStreamIndex = (ulong)i + folder.PackedStreamIndexBase; + + // create and open the source file stream if needed + if (_header.StreamsInfo.PackedStreams[packedStreamIndex].PackedStream == null) + { + _header.StreamsInfo.PackedStreams[packedStreamIndex].PackedStream = CloneStream(_zipFs); + } + _header.StreamsInfo.PackedStreams[packedStreamIndex].PackedStream.Seek( + _baseOffset + (long)_header.StreamsInfo.PackedStreams[packedStreamIndex].StreamPosition, SeekOrigin.Begin); + + + allInputStreams[(int)folder.PackedStreamIndices[i]].InStreamSource = InStreamSource.FileStream; + allInputStreams[(int)folder.PackedStreamIndices[i]].InStreamIndex = packedStreamIndex; + } + + List inputCoders = new List(); + + bool allCodersComplete = false; + while (!allCodersComplete) + { + allCodersComplete = true; + for (int i = 0; i < codersNeeded; i++) + { + Coder coder = folder.Coders[i]; + + // check is decoder already processed + if (coder.DecoderStream != null) + { + continue; + } + + inputCoders.Clear(); + for (int j = 0; j < (int)coder.NumInStreams; j++) + { + if (coder.InputStreamsSourceInfo[j].InStreamSource == InStreamSource.FileStream) + { + inputCoders.Add(_header.StreamsInfo.PackedStreams[coder.InputStreamsSourceInfo[j].InStreamIndex].PackedStream); + } + else if (coder.InputStreamsSourceInfo[j].InStreamSource == InStreamSource.CompStreamOutput) + { + if (folder.Coders[coder.InputStreamsSourceInfo[j].InStreamIndex].DecoderStream == null) + { + break; + } + inputCoders.Add(folder.Coders[coder.InputStreamsSourceInfo[j].InStreamIndex].DecoderStream); + } + else + { + // unknown input type so error + return ZipReturn.ZipDecodeError; + } + } + + if (inputCoders.Count == (int)coder.NumInStreams) + { + // all inputs streams are available to make the decoder stream + switch (coder.DecoderType) + { + case DecompressType.Stored: + coder.DecoderStream = inputCoders[0]; + break; + case DecompressType.Delta: + coder.DecoderStream = new Delta(folder.Coders[i].Properties, inputCoders[0]); + break; + case DecompressType.LZMA: + coder.DecoderStream = new LzmaStream(folder.Coders[i].Properties, inputCoders[0]); + break; + case DecompressType.LZMA2: + coder.DecoderStream = new LzmaStream(folder.Coders[i].Properties, inputCoders[0]); + break; + case DecompressType.PPMd: + coder.DecoderStream = new PpmdStream(new PpmdProperties(folder.Coders[i].Properties), inputCoders[0], false); + break; + case DecompressType.BZip2: + coder.DecoderStream = new CBZip2InputStream(inputCoders[0], false); + break; + case DecompressType.BCJ: + coder.DecoderStream = new BCJFilter(false, inputCoders[0]); + break; + case DecompressType.BCJ2: + coder.DecoderStream = new BCJ2Filter(inputCoders[0], inputCoders[1], inputCoders[2], inputCoders[3]); + break; + case DecompressType.ZSTD: + coder.DecoderStream = new ZstandardStream(inputCoders[0], CompressionMode.Decompress, true); + break; + default: + return ZipReturn.ZipDecodeError; + } + } + + // if skipped a coder need to loop round again + if (coder.DecoderStream == null) + { + allCodersComplete = false; + } + } + } + // find the final output stream and return it. + int outputStream = -1; + for (int i = 0; i < codersNeeded; i++) + { + Coder coder = folder.Coders[i]; + if (!coder.OutputUsedInternally) + { + outputStream = i; + } + } + + stream = folder.Coders[outputStream].DecoderStream; + stream.Seek((long)_localFiles[index].StreamOffset, SeekOrigin.Current); + + _stream = stream; + + return ZipReturn.ZipGood; + + } + catch (Exception e) + { + return ZipReturn.ZipErrorGettingDataStream; + } + + } + + private Stream CloneStream(Stream s) + { + switch (s) + { + case System.IO.FileStream _: + int errorCode = FileStream.OpenFileRead(ZipFilename, out Stream streamOut); + return errorCode != 0 ? null : streamOut; + + case MemoryStream memStream: + long pos = memStream.Position; + memStream.Position = 0; + byte[] newStream = new byte[memStream.Length]; + memStream.Read(newStream, 0, (int)memStream.Length); + MemoryStream ret = new MemoryStream(newStream, false); + memStream.Position = pos; + return ret; + } + + return null; + } + + public ZipReturn ZipFileCloseReadStream() + { + if (_streamIndex != -1) + { + Folder folder = _header.StreamsInfo.Folders[_streamIndex]; + + foreach (Coder c in folder.Coders) + { + Stream ds = c?.DecoderStream; + if (ds == null) + { + continue; + } + ds.Close(); + ds.Dispose(); + c.DecoderStream = null; + } + } + _streamIndex = -1; + + if (_header?.StreamsInfo != null) + { + foreach (PackedStreamInfo psi in _header.StreamsInfo.PackedStreams) + { + if (psi?.PackedStream == null) + { + continue; + } + psi.PackedStream.Close(); + psi.PackedStream.Dispose(); + psi.PackedStream = null; + } + } + return ZipReturn.ZipGood; + } + + } +} \ No newline at end of file diff --git a/SabreTools.Library/External/Compress/SevenZip/SevenZipTorrent.cs b/SabreTools.Library/External/Compress/SevenZip/SevenZipTorrent.cs new file mode 100644 index 00000000..51d89821 --- /dev/null +++ b/SabreTools.Library/External/Compress/SevenZip/SevenZipTorrent.cs @@ -0,0 +1,157 @@ +using System.IO; +using System.Text; + +namespace Compress.SevenZip +{ + public partial class SevenZ + { + // not finalized yet, so do not use + private void WriteRomVault7Zip(BinaryWriter bw, ulong headerPos, ulong headerLength, uint headerCRC) + { + const string sig = "RomVault7Z01"; + byte[] RV7Zid = Util.Enc.GetBytes(sig); + + // RomVault 7Zip torrent header + // 12 bytes : RomVault7Zip + // 4 bytes : HeaderCRC + // 8 bytes : HeaderPos + // 8 bytes : HeaderLength + + bw.Write(RV7Zid); + bw.Write(headerCRC); + bw.Write(headerPos); + bw.Write(headerLength); + + ZipStatus = ZipStatus.TrrntZip; + } + + private bool IsRomVault7Z(long testBaseOffset, ulong testHeaderPos, ulong testHeaderLength, uint testHeaderCRC) + { + long length = _zipFs.Length; + if (length < 32) + { + return false; + } + _zipFs.Seek(_baseOffset + (long)testHeaderPos - 32, SeekOrigin.Begin); + + const string sig = "RomVault7Z01"; + byte[] rv7Zid = Util.Enc.GetBytes(sig); + + byte[] header = new byte[12]; + _zipFs.Read(header, 0, 12); + for (int i = 0; i < 12; i++) + { + if (header[i] != rv7Zid[i]) + { + return false; + } + } + + uint headerCRC; + ulong headerOffset; // is location of header in file + ulong headerSize; + using (BinaryReader br = new BinaryReader(_zipFs, Encoding.UTF8, true)) + { + headerCRC = br.ReadUInt32(); + headerOffset = br.ReadUInt64(); + headerSize = br.ReadUInt64(); + } + + if (headerCRC != testHeaderCRC) + return false; + + if (headerOffset != testHeaderPos + (ulong)testBaseOffset) + return false; + + return headerSize == testHeaderLength; + } + private bool Istorrent7Z() + { + const int crcsz = 128; + const int t7ZsigSize = 16 + 1 + 9 + 4 + 4; + byte[] kSignature = { (byte)'7', (byte)'z', 0xBC, 0xAF, 0x27, 0x1C }; + int kSignatureSize = kSignature.Length; + const string sig = "\xa9\x9f\xd1\x57\x08\xa9\xd7\xea\x29\x64\xb2\x36\x1b\x83\x52\x33\x01torrent7z_0.9beta"; + byte[] t7Zid = Util.Enc.GetBytes(sig); + int t7ZidSize = t7Zid.Length; + + const int tmpbufsize = 256 + t7ZsigSize + 8 + 4; + byte[] buffer = new byte[tmpbufsize]; + + // read fist 128 bytes, pad with zeros if less bytes + int bufferPos = 0; + _zipFs.Seek(0, SeekOrigin.Begin); + int ar = _zipFs.Read(buffer, bufferPos, crcsz); + if (ar < crcsz) + { + Util.memset(buffer, bufferPos + ar, 0, crcsz - ar); + } + bufferPos = crcsz; + + long foffs = _zipFs.Length; + int endReadLength = crcsz + t7ZsigSize + 4; + foffs = foffs < endReadLength ? 0 : foffs - endReadLength; + + _zipFs.Seek(foffs, SeekOrigin.Begin); + + ar = _zipFs.Read(buffer, bufferPos, endReadLength); + if (ar < endReadLength) + { + if (ar >= t7ZsigSize + 4) + { + ar -= t7ZsigSize + 4; + } + if (ar < kSignatureSize) + { + ar = kSignatureSize; + } + Util.memset(buffer, bufferPos + ar, 0, crcsz - ar); + Util.memcpyr(buffer, crcsz * 2 + 8, buffer, bufferPos + ar, t7ZsigSize + 4); + } + else + { + Util.memcpyr(buffer, crcsz * 2 + 8, buffer, crcsz * 2, t7ZsigSize + 4); + } + + foffs = _zipFs.Length; + foffs -= t7ZsigSize + 4; + + //memcpy(buffer, crcsz * 2, &foffs, 8); + buffer[crcsz * 2 + 0] = (byte)((foffs >> 0) & 0xff); + buffer[crcsz * 2 + 1] = (byte)((foffs >> 8) & 0xff); + buffer[crcsz * 2 + 2] = (byte)((foffs >> 16) & 0xff); + buffer[crcsz * 2 + 3] = (byte)((foffs >> 24) & 0xff); + buffer[crcsz * 2 + 4] = 0; + buffer[crcsz * 2 + 5] = 0; + buffer[crcsz * 2 + 6] = 0; + buffer[crcsz * 2 + 7] = 0; + + if (Util.memcmp(buffer, 0, kSignature, kSignatureSize)) + { + t7Zid[16] = buffer[crcsz * 2 + 4 + 8 + 16]; + if (Util.memcmp(buffer, crcsz * 2 + 4 + 8, t7Zid, t7ZidSize)) + { + uint inCrc32 = (uint)(buffer[crcsz * 2 + 8 + 0] + + (buffer[crcsz * 2 + 8 + 1] << 8) + + (buffer[crcsz * 2 + 8 + 2] << 16) + + (buffer[crcsz * 2 + 8 + 3] << 24)); + + buffer[crcsz * 2 + 8 + 0] = 0xff; + buffer[crcsz * 2 + 8 + 1] = 0xff; + buffer[crcsz * 2 + 8 + 2] = 0xff; + buffer[crcsz * 2 + 8 + 3] = 0xff; + + uint calcCrc32 = Utils.CRC.CalculateDigest(buffer, 0, crcsz * 2 + 8 + t7ZsigSize + 4); + + if (inCrc32 == calcCrc32) + { + return true; + } + } + } + + return false; + } + + } +} \ No newline at end of file diff --git a/SabreTools.Library/External/Compress/SevenZip/SevenZipWrite.cs b/SabreTools.Library/External/Compress/SevenZip/SevenZipWrite.cs new file mode 100644 index 00000000..8189d239 --- /dev/null +++ b/SabreTools.Library/External/Compress/SevenZip/SevenZipWrite.cs @@ -0,0 +1,176 @@ +using System.IO; +using System.Text; +using Compress.SevenZip.Compress.LZMA; +using Compress.SevenZip.Structure; +using Compress.Utils; +using FileInfo = RVIO.FileInfo; +using FileStream = RVIO.FileStream; + +namespace Compress.SevenZip +{ + public partial class SevenZ + { + private Stream _lzmaStream; + private ulong _packStreamStart; + private ulong _packStreamSize; + private ulong _unpackedStreamSize; + private byte[] _codeMSbytes; + + + public ZipReturn ZipFileCreate(string newFilename) + { + return ZipFileCreate(newFilename, true); + } + + + public ZipReturn ZipFileCreateFromUncompressedSize(string newFilename, ulong unCompressedSize) + { + return ZipFileCreate(newFilename, true, GetDictionarySizeFromUncompressedSize(unCompressedSize)); + } + + public ZipReturn ZipFileCreate(string newFilename, bool compressOutput, int dictionarySize = 1 << 24, int numFastBytes = 64) + { + if (ZipOpen != ZipOpenType.Closed) + { + return ZipReturn.ZipFileAlreadyOpen; + } + + DirUtil.CreateDirForFile(newFilename); + _zipFileInfo = new FileInfo(newFilename); + + int errorCode = FileStream.OpenFileWrite(newFilename, out _zipFs); + if (errorCode != 0) + { + ZipFileClose(); + return ZipReturn.ZipErrorOpeningFile; + } + ZipOpen = ZipOpenType.OpenWrite; + + _signatureHeader = new SignatureHeader(); + _header = new Header(); + + using (BinaryWriter bw = new BinaryWriter(_zipFs, Encoding.UTF8, true)) + { + _signatureHeader.Write(bw); + } + + _baseOffset = _zipFs.Position; + + _compressed = compressOutput; + + _unpackedStreamSize = 0; + if (_compressed) + { + LzmaEncoderProperties ep = new LzmaEncoderProperties(true, dictionarySize, numFastBytes); + LzmaStream lzs = new LzmaStream(ep, false, _zipFs); + _codeMSbytes = lzs.Properties; + _lzmaStream = lzs; + + + /* + ZstandardStream zss = new ZstandardStream(_zipFs, 22, true); + _codeMSbytes = new byte[] { 1, 4, 18, 0, 0 }; + _lzmaStream = zss; + */ + _packStreamStart = (ulong)_zipFs.Position; + } + + return ZipReturn.ZipGood; + } + + public void ZipFileAddDirectory(string filename) + { + string fName = filename; + if (fName.Substring(fName.Length - 1, 1) == @"/") + fName = fName.Substring(0, fName.Length - 1); + + LocalFile lf = new LocalFile + { + FileName = fName, + UncompressedSize = 0, + IsDirectory = true, + StreamOffset = 0 + }; + _localFiles.Add(lf); + } + + public void ZipFileAddZeroLengthFile() + { + // do nothing here for 7zip + } + + public ZipReturn ZipFileOpenWriteStream(bool raw, bool trrntzip, string filename, ulong uncompressedSize, ushort compressionMethod, out Stream stream) + { + return ZipFileOpenWriteStream(filename, uncompressedSize, out stream); + } + + private ZipReturn ZipFileOpenWriteStream(string filename, ulong uncompressedSize, out Stream stream) + { + LocalFile lf = new LocalFile + { + FileName = filename, + UncompressedSize = uncompressedSize, + StreamOffset = (ulong)(_zipFs.Position - _signatureHeader.BaseOffset) + }; + if (uncompressedSize == 0 && filename.Substring(filename.Length - 1, 1) == "/") + { + lf.FileName = filename.Substring(0, filename.Length - 1); + lf.IsDirectory = true; + } + + _unpackedStreamSize += uncompressedSize; + + _localFiles.Add(lf); + stream = _compressed ? _lzmaStream : _zipFs; + return ZipReturn.ZipGood; + } + + + public ZipReturn ZipFileCloseWriteStream(byte[] crc32) + { + _localFiles[_localFiles.Count - 1].CRC = new[] { crc32[3], crc32[2], crc32[1], crc32[0] }; + return ZipReturn.ZipGood; + } + + + private static readonly int[] DictionarySizes = + { + 0x10000, + 0x18000, + 0x20000, + 0x30000, + 0x40000, + 0x60000, + 0x80000, + 0xc0000, + + 0x100000, + 0x180000, + 0x200000, + 0x300000, + 0x400000, + 0x600000, + 0x800000, + 0xc00000, + + 0x1000000, + 0x1800000, + 0x2000000, + 0x3000000, + 0x4000000, + 0x6000000 + }; + + + private static int GetDictionarySizeFromUncompressedSize(ulong unCompressedSize) + { + foreach (int v in DictionarySizes) + { + if ((ulong)v >= unCompressedSize) + return v; + } + + return DictionarySizes[DictionarySizes.Length - 1]; + } + } +} \ No newline at end of file diff --git a/SabreTools.Library/External/Compress/SevenZip/SevenZipWriteClose.cs b/SabreTools.Library/External/Compress/SevenZip/SevenZipWriteClose.cs new file mode 100644 index 00000000..bbc68d7c --- /dev/null +++ b/SabreTools.Library/External/Compress/SevenZip/SevenZipWriteClose.cs @@ -0,0 +1,293 @@ +using System.IO; +using System.Text; +using Compress.SevenZip.Compress.LZMA; +using Compress.SevenZip.Structure; +using Compress.Utils; +using Zstandard.Net; + +namespace Compress.SevenZip +{ + public partial class SevenZ + { + private void Create7ZStructure() + { + int fileCount = _localFiles.Count; + + //FileInfo + _header.FileInfo = new Structure.FileInfo + { + Names = new string[fileCount] + }; + + ulong emptyStreamCount = 0; + ulong emptyFileCount = 0; + for (int i = 0; i < fileCount; i++) + { + _header.FileInfo.Names[i] = _localFiles[i].FileName; + + if (_localFiles[i].UncompressedSize != 0) + { + continue; + } + + if (!_localFiles[i].IsDirectory) + { + emptyFileCount += 1; + } + + emptyStreamCount += 1; + } + ulong outFileCount = (ulong)_localFiles.Count - emptyStreamCount; + + _header.FileInfo.EmptyStreamFlags = null; + _header.FileInfo.EmptyFileFlags = null; + _header.FileInfo.Attributes = null; + + if (emptyStreamCount > 0) + { + if (emptyStreamCount != emptyFileCount) //then we found directories and need to set the attributes + { + _header.FileInfo.Attributes = new uint[fileCount]; + } + + if (emptyFileCount > 0) + { + _header.FileInfo.EmptyFileFlags = new bool[emptyStreamCount]; + } + + emptyStreamCount = 0; + _header.FileInfo.EmptyStreamFlags = new bool[fileCount]; + for (int i = 0; i < fileCount; i++) + { + if (_localFiles[i].UncompressedSize != 0) + { + continue; + } + + if (_localFiles[i].IsDirectory) + { + if (_header.FileInfo.Attributes != null) + _header.FileInfo.Attributes[i] = 0x10; // set attributes to directory + } + else + { + if (_header.FileInfo.EmptyFileFlags != null) + _header.FileInfo.EmptyFileFlags[emptyStreamCount] = true; // set empty file flag + } + + _header.FileInfo.EmptyStreamFlags[i] = true; + emptyStreamCount += 1; + } + } + + + //StreamsInfo + _header.StreamsInfo = new StreamsInfo { PackPosition = 0 }; + + //StreamsInfo.PackedStreamsInfo + if (_compressed) + { + _header.StreamsInfo.PackedStreams = new PackedStreamInfo[1]; + _header.StreamsInfo.PackedStreams[0] = new PackedStreamInfo { PackedSize = _packStreamSize }; + } + else + { + _header.StreamsInfo.PackedStreams = new PackedStreamInfo[outFileCount]; + int fileIndex = 0; + for (int i = 0; i < fileCount; i++) + { + if (_localFiles[i].UncompressedSize == 0) + { + continue; + } + _header.StreamsInfo.PackedStreams[fileIndex++] = new PackedStreamInfo { PackedSize = _localFiles[i].UncompressedSize }; + } + } + //StreamsInfo.PackedStreamsInfo, no CRC or StreamPosition required + + if (_compressed) + { + //StreamsInfo.Folders + _header.StreamsInfo.Folders = new Folder[1]; + + //StreamsInfo.Folders.Coder + // flags 0x23 + + Folder folder = new Folder + { + BindPairs = null, + Coders = new[] { + new Coder { + Method = new byte[] { 3, 1, 1 }, + NumInStreams = 1, + NumOutStreams = 1, + Properties = _codeMSbytes + } + }, + PackedStreamIndices = new[] { (ulong)0 }, + UnpackedStreamSizes = new[] { _unpackedStreamSize }, + UnpackedStreamInfo = new UnpackedStreamInfo[outFileCount], + UnpackCRC = null + }; + + switch (_lzmaStream) + { + case LzmaStream _: + folder.Coders[0].Method = new byte[] { 3, 1, 1 }; + break; + case ZstandardStream _: + folder.Coders[0].Method = new byte[] { 4, 247, 17, 1 }; + break; + } + + + int fileIndex = 0; + for (int i = 0; i < fileCount; i++) + { + if (_localFiles[i].UncompressedSize == 0) + { + continue; + } + UnpackedStreamInfo unpackedStreamInfo = new UnpackedStreamInfo + { + UnpackedSize = _localFiles[i].UncompressedSize, + Crc = Util.bytestouint(_localFiles[i].CRC) + }; + folder.UnpackedStreamInfo[fileIndex++] = unpackedStreamInfo; + } + _header.StreamsInfo.Folders[0] = folder; + } + else + { + _header.StreamsInfo.Folders = new Folder[outFileCount]; + int fileIndex = 0; + for (int i = 0; i < fileCount; i++) + { + if (_localFiles[i].UncompressedSize == 0) + { + continue; + } + + Folder folder = new Folder + { + BindPairs = null, + Coders = new[] { + new Coder { + Method = new byte[] {0}, + NumInStreams = 1, + NumOutStreams = 1, + Properties = null + } + }, + PackedStreamIndices = new[] { (ulong)i }, + UnpackedStreamSizes = new[] { _localFiles[i].UncompressedSize }, + UnpackCRC = null, + + UnpackedStreamInfo = new[] { + new UnpackedStreamInfo { + UnpackedSize = _localFiles[i].UncompressedSize, + Crc = Util.bytestouint(_localFiles[i].CRC) + } + } + }; + + _header.StreamsInfo.Folders[fileIndex++] = folder; + } + } + } + + + + private void CloseWriting7Zip() + { + if (_compressed) + { + _lzmaStream.Close(); + } + + _packStreamSize = (ulong)_zipFs.Position - _packStreamStart; + + Create7ZStructure(); + + byte[] newHeaderByte; + using (Stream headerMem = new MemoryStream()) + { + using (BinaryWriter headerBw = new BinaryWriter(headerMem, Encoding.UTF8, true)) + { + _header.WriteHeader(headerBw); + + newHeaderByte = new byte[headerMem.Length]; + headerMem.Position = 0; + headerMem.Read(newHeaderByte, 0, newHeaderByte.Length); + } + } + + uint mainHeaderCRC = CRC.CalculateDigest(newHeaderByte, 0, (uint)newHeaderByte.Length); + + #region Header Compression + long packedHeaderPos = _zipFs.Position; + LzmaEncoderProperties ep = new LzmaEncoderProperties(true, GetDictionarySizeFromUncompressedSize((ulong)newHeaderByte.Length), 64); + LzmaStream lzs = new LzmaStream(ep, false, _zipFs); + byte[] lzmaStreamProperties = lzs.Properties; + lzs.Write(newHeaderByte, 0, newHeaderByte.Length); + lzs.Close(); + + StreamsInfo streamsInfo = new StreamsInfo + { + PackPosition = (ulong)(packedHeaderPos - _baseOffset), + Folders = new[] { + new Folder { + BindPairs = new BindPair[0], + Coders = new [] { + new Coder { + Method = new byte[] { 3, 1, 1 }, + NumInStreams = 1, + NumOutStreams = 1, + Properties = lzmaStreamProperties + } + }, + UnpackedStreamSizes = new[] {(ulong) newHeaderByte.Length}, + UnpackCRC = mainHeaderCRC + } + }, + PackedStreams = new[] { + new PackedStreamInfo + { + PackedSize = (ulong)(_zipFs.Position - packedHeaderPos), + StreamPosition = 0 + } + } + }; + + using (Stream headerMem = new MemoryStream()) + { + using (BinaryWriter bw = new BinaryWriter(headerMem, Encoding.UTF8, true)) + { + bw.Write((byte)HeaderProperty.kEncodedHeader); + streamsInfo.WriteHeader(bw); + + newHeaderByte = new byte[headerMem.Length]; + headerMem.Position = 0; + headerMem.Read(newHeaderByte, 0, newHeaderByte.Length); + + } + } + mainHeaderCRC = CRC.CalculateDigest(newHeaderByte, 0, (uint)newHeaderByte.Length); + #endregion + + + using (BinaryWriter bw = new BinaryWriter(_zipFs, Encoding.UTF8, true)) + { + ulong headerPosition = (ulong)_zipFs.Position + 32; //tzip header is 32 bytes + WriteRomVault7Zip(bw, headerPosition, (ulong)newHeaderByte.Length, mainHeaderCRC); + + _zipFs.Write(newHeaderByte, 0, newHeaderByte.Length); + _signatureHeader.WriteFinal(bw, headerPosition, (ulong)newHeaderByte.Length, mainHeaderCRC); + } + _zipFs.Flush(); + _zipFs.Close(); + _zipFs.Dispose(); + } + + } +} \ No newline at end of file diff --git a/SabreTools.Library/External/Compress/SevenZip/Structure/Coder.cs b/SabreTools.Library/External/Compress/SevenZip/Structure/Coder.cs index 9aabcc45..f06aea87 100644 --- a/SabreTools.Library/External/Compress/SevenZip/Structure/Coder.cs +++ b/SabreTools.Library/External/Compress/SevenZip/Structure/Coder.cs @@ -29,7 +29,8 @@ namespace Compress.SevenZip.Structure BCJ2, PPMd, BZip2, - LZMA2 + LZMA2, + ZSTD } @@ -73,40 +74,42 @@ namespace Compress.SevenZip.Structure throw new NotSupportedException("External flag"); } - if ((Method.Length == 1) && (Method[0] == 0)) + if (Method.Length == 1 && Method[0] == 0) { DecoderType = DecompressType.Stored; } - else if ((Method.Length == 1) && (Method[0] == 3)) + else if (Method.Length == 1 && Method[0] == 3) { DecoderType = DecompressType.Delta; } - else if ((Method.Length == 3) && (Method[0] == 3) && (Method[1] == 1) && (Method[2] == 1)) + else if (Method.Length == 3 && Method[0] == 3 && Method[1] == 1 && Method[2] == 1) { DecoderType = DecompressType.LZMA; } - else if ((Method.Length == 4) && (Method[0] == 3) && (Method[1] == 3) && (Method[2] == 1) && - (Method[3] == 3)) + else if (Method.Length == 4 && Method[0] == 3 && Method[1] == 3 && Method[2] == 1 && Method[3] == 3) { DecoderType = DecompressType.BCJ; } - else if ((Method.Length == 4) && (Method[0] == 3) && (Method[1] == 3) && (Method[2] == 1) && - (Method[3] == 27)) + else if (Method.Length == 4 && Method[0] == 3 && Method[1] == 3 && Method[2] == 1 && Method[3] == 27) { DecoderType = DecompressType.BCJ2; } - else if ((Method.Length == 3) && (Method[0] == 3) && (Method[1] == 4) && (Method[2] == 1)) + else if (Method.Length == 3 && Method[0] == 3 && Method[1] == 4 && Method[2] == 1) { DecoderType = DecompressType.PPMd; } - else if ((Method.Length == 3) && (Method[0] == 4) && (Method[1] == 2) && (Method[2] == 2)) + else if (Method.Length == 3 && Method[0] == 4 && Method[1] == 2 && Method[2] == 2) { DecoderType = DecompressType.BZip2; } - else if ((Method.Length == 1) && (Method[0] == 33)) + else if (Method.Length == 1 && Method[0] == 33) { DecoderType = DecompressType.LZMA2; } + else if (SevenZ.supportZstd && Method.Length == 4 && Method[0] == 4 && Method[1] == 247 && Method[2] == 17 && Method[3] == 1) + { + DecoderType = DecompressType.ZSTD; + } InputStreamsSourceInfo = new InStreamSourceInfo[NumInStreams]; for (uint i = 0; i < NumInStreams; i++) diff --git a/SabreTools.Library/External/Compress/SevenZip/Structure/FileInfo.cs b/SabreTools.Library/External/Compress/SevenZip/Structure/FileInfo.cs index 2a6200b5..c2610dfb 100644 --- a/SabreTools.Library/External/Compress/SevenZip/Structure/FileInfo.cs +++ b/SabreTools.Library/External/Compress/SevenZip/Structure/FileInfo.cs @@ -18,9 +18,9 @@ namespace Compress.SevenZip.Structure ulong numEmptyFiles = 0; - for (;;) + for (; ; ) { - HeaderProperty hp = (HeaderProperty) br.ReadByte(); + HeaderProperty hp = (HeaderProperty)br.ReadByte(); if (hp == HeaderProperty.kEnd) { return; @@ -43,7 +43,7 @@ namespace Compress.SevenZip.Structure continue; case HeaderProperty.kEmptyStream: - EmptyStreamFlags = Util.ReadBoolFlags(br, (ulong) Names.Length); + EmptyStreamFlags = Util.ReadBoolFlags(br, (ulong)Names.Length); for (ulong i = 0; i < size; i++) { if (EmptyStreamFlags[i]) @@ -69,11 +69,11 @@ namespace Compress.SevenZip.Structure case HeaderProperty.kCreationTime: case HeaderProperty.kLastAccessTime: case HeaderProperty.kLastWriteTime: - br.ReadBytes((int) bytessize); + br.ReadBytes((int)bytessize); continue; case HeaderProperty.kDummy: - br.ReadBytes((int) bytessize); + br.ReadBytes((int)bytessize); continue; default: @@ -84,16 +84,16 @@ namespace Compress.SevenZip.Structure public void Write(BinaryWriter bw) { - bw.Write((byte) HeaderProperty.kFilesInfo); - bw.WriteEncodedUInt64((ulong) Names.Length); + bw.Write((byte)HeaderProperty.kFilesInfo); + bw.WriteEncodedUInt64((ulong)Names.Length); byte[] namebyte; using (MemoryStream nameMem = new MemoryStream()) { - using (BinaryWriter nameBw = new BinaryWriter(nameMem,Encoding.UTF8,true)) + using (BinaryWriter nameBw = new BinaryWriter(nameMem, Encoding.UTF8, true)) { - nameBw.Write((byte) 0); //not external + nameBw.Write((byte)0); //not external foreach (string name in Names) { nameBw.WriteName(name); @@ -105,29 +105,29 @@ namespace Compress.SevenZip.Structure } } - bw.Write((byte) HeaderProperty.kName); - bw.WriteEncodedUInt64((ulong) namebyte.Length); + bw.Write((byte)HeaderProperty.kName); + bw.WriteEncodedUInt64((ulong)namebyte.Length); bw.Write(namebyte); if (EmptyStreamFlags != null) { - bw.Write((byte) HeaderProperty.kEmptyStream); + bw.Write((byte)HeaderProperty.kEmptyStream); Util.WriteBoolFlags(bw, EmptyStreamFlags); } if (EmptyFileFlags != null) { - bw.Write((byte) HeaderProperty.kEmptyFile); + bw.Write((byte)HeaderProperty.kEmptyFile); Util.WriteBoolFlags(bw, EmptyFileFlags); } if (Attributes != null) { - bw.Write((byte) HeaderProperty.kWinAttributes); + bw.Write((byte)HeaderProperty.kWinAttributes); Util.WriteUint32Def(bw, Attributes); } - bw.Write((byte) HeaderProperty.kEnd); + bw.Write((byte)HeaderProperty.kEnd); } public void Report(ref StringBuilder sb) diff --git a/SabreTools.Library/External/Compress/SevenZip/Structure/Folder.cs b/SabreTools.Library/External/Compress/SevenZip/Structure/Folder.cs index 0df22996..d869be07 100644 --- a/SabreTools.Library/External/Compress/SevenZip/Structure/Folder.cs +++ b/SabreTools.Library/External/Compress/SevenZip/Structure/Folder.cs @@ -30,8 +30,8 @@ namespace Compress.SevenZip.Structure Coders[i] = new Coder(); Coders[i].Read(br); - numInStreams += (int) Coders[i].NumInStreams; - numOutStreams += (int) Coders[i].NumOutStreams; + numInStreams += (int)Coders[i].NumInStreams; + numOutStreams += (int)Coders[i].NumOutStreams; } int numBindPairs = numOutStreams - 1; @@ -127,63 +127,63 @@ namespace Compress.SevenZip.Structure public static void ReadUnPackInfo(BinaryReader br, out Folder[] Folders) { Folders = null; - for (;;) + for (; ; ) { - HeaderProperty hp = (HeaderProperty) br.ReadByte(); + HeaderProperty hp = (HeaderProperty)br.ReadByte(); switch (hp) { case HeaderProperty.kFolder: - { - ulong numFolders = br.ReadEncodedUInt64(); - - Folders = new Folder[numFolders]; - - byte external = br.ReadByte(); - switch (external) { - case 0: - { - ulong folderIndex = 0; - for (uint i = 0; i < numFolders; i++) - { - Folders[i] = new Folder(); - Folders[i].ReadFolder(br); - Folders[i].PackedStreamIndexBase = folderIndex; - folderIndex += (ulong) Folders[i].PackedStreamIndices.Length; - } + ulong numFolders = br.ReadEncodedUInt64(); - break; + Folders = new Folder[numFolders]; + + byte external = br.ReadByte(); + switch (external) + { + case 0: + { + ulong folderIndex = 0; + for (uint i = 0; i < numFolders; i++) + { + Folders[i] = new Folder(); + Folders[i].ReadFolder(br); + Folders[i].PackedStreamIndexBase = folderIndex; + folderIndex += (ulong)Folders[i].PackedStreamIndices.Length; + } + + break; + } + + case 1: + throw new NotSupportedException("External flag"); } - case 1: - throw new NotSupportedException("External flag"); + continue; } - continue; - } - case HeaderProperty.kCodersUnPackSize: - { - for (uint i = 0; i < Folders.Length; i++) { - Folders[i].ReadUnpackedStreamSize(br); - } + for (uint i = 0; i < Folders.Length; i++) + { + Folders[i].ReadUnpackedStreamSize(br); + } - continue; - } + continue; + } case HeaderProperty.kCRC: - { - uint?[] crcs; - Util.UnPackCRCs(br, (ulong) Folders.Length, out crcs); - for (int i = 0; i < Folders.Length; i++) { - Folders[i].UnpackCRC = crcs[i]; - } + uint?[] crcs; + Util.UnPackCRCs(br, (ulong)Folders.Length, out crcs); + for (int i = 0; i < Folders.Length; i++) + { + Folders[i].UnpackCRC = crcs[i]; + } - continue; - } + continue; + } case HeaderProperty.kEnd: return; @@ -196,91 +196,91 @@ namespace Compress.SevenZip.Structure public static void ReadSubStreamsInfo(BinaryReader br, ref Folder[] Folders) { - for (;;) + for (; ; ) { - HeaderProperty hp = (HeaderProperty) br.ReadByte(); + HeaderProperty hp = (HeaderProperty)br.ReadByte(); switch (hp) { case HeaderProperty.kNumUnPackStream: - { - for (int f = 0; f < Folders.Length; f++) { - int numStreams = (int) br.ReadEncodedUInt64(); - Folders[f].UnpackedStreamInfo = new UnpackedStreamInfo[numStreams]; - for (int i = 0; i < numStreams; i++) + for (int f = 0; f < Folders.Length; f++) { - Folders[f].UnpackedStreamInfo[i] = new UnpackedStreamInfo(); - } - } - - continue; - } - - case HeaderProperty.kSize: - { - for (int f = 0; f < Folders.Length; f++) - { - Folder folder = Folders[f]; - - if (folder.UnpackedStreamInfo.Length == 0) - { - continue; - } - - ulong sum = 0; - for (int i = 0; i < folder.UnpackedStreamInfo.Length - 1; i++) - { - ulong size = br.ReadEncodedUInt64(); - folder.UnpackedStreamInfo[i].UnpackedSize = size; - sum += size; - } - - folder.UnpackedStreamInfo[folder.UnpackedStreamInfo.Length - 1].UnpackedSize = - folder.GetUnpackSize() - sum; - } - - continue; - } - - case HeaderProperty.kCRC: - { - ulong numCRC = 0; - foreach (Folder folder in Folders) - { - if (folder.UnpackedStreamInfo == null) - { - folder.UnpackedStreamInfo = new UnpackedStreamInfo[1]; - folder.UnpackedStreamInfo[0] = new UnpackedStreamInfo(); - folder.UnpackedStreamInfo[0].UnpackedSize = folder.GetUnpackSize(); - } - - if ((folder.UnpackedStreamInfo.Length != 1) || !folder.UnpackCRC.HasValue) - { - numCRC += (ulong) folder.UnpackedStreamInfo.Length; - } - } - - int crcIndex = 0; - uint?[] crc; - Util.UnPackCRCs(br, numCRC, out crc); - for (uint i = 0; i < Folders.Length; i++) - { - Folder folder = Folders[i]; - if ((folder.UnpackedStreamInfo.Length == 1) && folder.UnpackCRC.HasValue) - { - folder.UnpackedStreamInfo[0].Crc = folder.UnpackCRC; - } - else - { - for (uint j = 0; j < folder.UnpackedStreamInfo.Length; j++, crcIndex++) + int numStreams = (int)br.ReadEncodedUInt64(); + Folders[f].UnpackedStreamInfo = new UnpackedStreamInfo[numStreams]; + for (int i = 0; i < numStreams; i++) { - folder.UnpackedStreamInfo[j].Crc = crc[crcIndex]; + Folders[f].UnpackedStreamInfo[i] = new UnpackedStreamInfo(); } } + + continue; } - continue; - } + case HeaderProperty.kSize: + { + for (int f = 0; f < Folders.Length; f++) + { + Folder folder = Folders[f]; + + if (folder.UnpackedStreamInfo.Length == 0) + { + continue; + } + + ulong sum = 0; + for (int i = 0; i < folder.UnpackedStreamInfo.Length - 1; i++) + { + ulong size = br.ReadEncodedUInt64(); + folder.UnpackedStreamInfo[i].UnpackedSize = size; + sum += size; + } + + folder.UnpackedStreamInfo[folder.UnpackedStreamInfo.Length - 1].UnpackedSize = + folder.GetUnpackSize() - sum; + } + + continue; + } + + case HeaderProperty.kCRC: + { + ulong numCRC = 0; + foreach (Folder folder in Folders) + { + if (folder.UnpackedStreamInfo == null) + { + folder.UnpackedStreamInfo = new UnpackedStreamInfo[1]; + folder.UnpackedStreamInfo[0] = new UnpackedStreamInfo(); + folder.UnpackedStreamInfo[0].UnpackedSize = folder.GetUnpackSize(); + } + + if ((folder.UnpackedStreamInfo.Length != 1) || !folder.UnpackCRC.HasValue) + { + numCRC += (ulong)folder.UnpackedStreamInfo.Length; + } + } + + int crcIndex = 0; + uint?[] crc; + Util.UnPackCRCs(br, numCRC, out crc); + for (uint i = 0; i < Folders.Length; i++) + { + Folder folder = Folders[i]; + if ((folder.UnpackedStreamInfo.Length == 1) && folder.UnpackCRC.HasValue) + { + folder.UnpackedStreamInfo[0].Crc = folder.UnpackCRC; + } + else + { + for (uint j = 0; j < folder.UnpackedStreamInfo.Length; j++, crcIndex++) + { + folder.UnpackedStreamInfo[j].Crc = crc[crcIndex]; + } + } + } + + continue; + } case HeaderProperty.kEnd: return; @@ -293,14 +293,14 @@ namespace Compress.SevenZip.Structure private void WriteFolder(BinaryWriter bw) { - ulong numCoders = (ulong) Coders.Length; + ulong numCoders = (ulong)Coders.Length; bw.WriteEncodedUInt64(numCoders); for (ulong i = 0; i < numCoders; i++) { Coders[i].Write(bw); } - ulong numBindingPairs = BindPairs == null ? 0 : (ulong) BindPairs.Length; + ulong numBindingPairs = BindPairs == null ? 0 : (ulong)BindPairs.Length; for (ulong i = 0; i < numBindingPairs; i++) { BindPairs[i].Write(bw); @@ -311,7 +311,7 @@ namespace Compress.SevenZip.Structure private void WriteUnpackedStreamSize(BinaryWriter bw) { - ulong numUnpackedStreamSizes = (ulong) UnpackedStreamSizes.Length; + ulong numUnpackedStreamSizes = (ulong)UnpackedStreamSizes.Length; for (ulong i = 0; i < numUnpackedStreamSizes; i++) { bw.WriteEncodedUInt64(UnpackedStreamSizes[i]); @@ -320,45 +320,53 @@ namespace Compress.SevenZip.Structure public static void WriteUnPackInfo(BinaryWriter bw, Folder[] Folders) { - bw.Write((byte) HeaderProperty.kUnPackInfo); + bw.Write((byte)HeaderProperty.kUnPackInfo); - bw.Write((byte) HeaderProperty.kFolder); - ulong numFolders = (ulong) Folders.Length; + bw.Write((byte)HeaderProperty.kFolder); + ulong numFolders = (ulong)Folders.Length; bw.WriteEncodedUInt64(numFolders); - bw.Write((byte) 0); //External Flag + bw.Write((byte)0); //External Flag for (ulong i = 0; i < numFolders; i++) { Folders[i].WriteFolder(bw); } - bw.Write((byte) HeaderProperty.kCodersUnPackSize); + bw.Write((byte)HeaderProperty.kCodersUnPackSize); for (ulong i = 0; i < numFolders; i++) { Folders[i].WriteUnpackedStreamSize(bw); } - if (Folders[0].UnpackCRC != null) + bool hasCRC = false; + uint?[] CRCs = new uint?[numFolders]; + for (ulong i = 0; i < numFolders; i++) { - bw.Write((byte) HeaderProperty.kCRC); - throw new NotImplementedException(); + CRCs[i] = Folders[i].UnpackCRC; + hasCRC |= (CRCs[i] != null); } - bw.Write((byte) HeaderProperty.kEnd); + if (hasCRC) + { + bw.Write((byte)HeaderProperty.kCRC); + Util.WritePackedCRCs(bw, CRCs); + } + + bw.Write((byte)HeaderProperty.kEnd); } public static void WriteSubStreamsInfo(BinaryWriter bw, Folder[] Folders) { - bw.Write((byte) HeaderProperty.kSubStreamsInfo); + bw.Write((byte)HeaderProperty.kSubStreamsInfo); - bw.Write((byte) HeaderProperty.kNumUnPackStream); + bw.Write((byte)HeaderProperty.kNumUnPackStream); for (int f = 0; f < Folders.Length; f++) { - ulong numStreams = (ulong) Folders[f].UnpackedStreamInfo.Length; + ulong numStreams = (ulong)Folders[f].UnpackedStreamInfo.Length; bw.WriteEncodedUInt64(numStreams); } - bw.Write((byte) HeaderProperty.kSize); + bw.Write((byte)HeaderProperty.kSize); for (int f = 0; f < Folders.Length; f++) { @@ -369,8 +377,8 @@ namespace Compress.SevenZip.Structure } } - bw.Write((byte) HeaderProperty.kCRC); - bw.Write((byte) 1); // crc flags default to true + bw.Write((byte)HeaderProperty.kCRC); + bw.Write((byte)1); // crc flags default to true for (int f = 0; f < Folders.Length; f++) { Folder folder = Folders[f]; @@ -380,7 +388,7 @@ namespace Compress.SevenZip.Structure } } - bw.Write((byte) HeaderProperty.kEnd); + bw.Write((byte)HeaderProperty.kEnd); } diff --git a/SabreTools.Library/External/Compress/SevenZip/Structure/Header.cs b/SabreTools.Library/External/Compress/SevenZip/Structure/Header.cs index bd303109..f2bf21a8 100644 --- a/SabreTools.Library/External/Compress/SevenZip/Structure/Header.cs +++ b/SabreTools.Library/External/Compress/SevenZip/Structure/Header.cs @@ -38,7 +38,7 @@ namespace Compress.SevenZip.Structure } - private void Write(BinaryWriter bw) + public void WriteHeader(BinaryWriter bw) { bw.Write((byte)HeaderProperty.kHeader); StreamsInfo.Write(bw); @@ -46,11 +46,6 @@ namespace Compress.SevenZip.Structure bw.Write((byte)HeaderProperty.kEnd); } - public void WriteHeader(BinaryWriter bw) - { - Write(bw); - } - public static ZipReturn ReadHeaderOrPackedHeader(Stream stream, long baseOffset, out Header header) { header = null; diff --git a/SabreTools.Library/External/Compress/SevenZip/Structure/PackedStreamInfo.cs b/SabreTools.Library/External/Compress/SevenZip/Structure/PackedStreamInfo.cs index 66d6bb03..7d6030af 100644 --- a/SabreTools.Library/External/Compress/SevenZip/Structure/PackedStreamInfo.cs +++ b/SabreTools.Library/External/Compress/SevenZip/Structure/PackedStreamInfo.cs @@ -26,9 +26,9 @@ namespace Compress.SevenZip.Structure ulong streamPosition = 0; - for (;;) + for (; ; ) { - HeaderProperty hp = (HeaderProperty) br.ReadByte(); + HeaderProperty hp = (HeaderProperty)br.ReadByte(); switch (hp) { case HeaderProperty.kSize: @@ -60,12 +60,12 @@ namespace Compress.SevenZip.Structure public static void Write(BinaryWriter bw, ulong packPosition, PackedStreamInfo[] packedStreams) { - ulong numPackStreams = (ulong) packedStreams.Length; - bw.Write((byte) HeaderProperty.kPackInfo); + ulong numPackStreams = (ulong)packedStreams.Length; + bw.Write((byte)HeaderProperty.kPackInfo); bw.WriteEncodedUInt64(packPosition); bw.WriteEncodedUInt64(numPackStreams); - bw.Write((byte) HeaderProperty.kSize); + bw.Write((byte)HeaderProperty.kSize); ulong streamPosition = 0; for (ulong i = 0; i < numPackStreams; i++) { @@ -77,14 +77,14 @@ namespace Compress.SevenZip.Structure // Only checking the first CRC assuming all the reset will be the same if (packedStreams[0].Crc != null) { - bw.Write((byte) HeaderProperty.kCRC); + bw.Write((byte)HeaderProperty.kCRC); for (ulong i = 0; i < numPackStreams; i++) { bw.WriteEncodedUInt64(packedStreams[i].Crc ?? 0); } } - bw.Write((byte) HeaderProperty.kEnd); + bw.Write((byte)HeaderProperty.kEnd); } diff --git a/SabreTools.Library/External/Compress/SevenZip/Structure/SignatureHeader.cs b/SabreTools.Library/External/Compress/SevenZip/Structure/SignatureHeader.cs index 1ca52a86..54326cef 100644 --- a/SabreTools.Library/External/Compress/SevenZip/Structure/SignatureHeader.cs +++ b/SabreTools.Library/External/Compress/SevenZip/Structure/SignatureHeader.cs @@ -5,7 +5,7 @@ namespace Compress.SevenZip.Structure { internal class SignatureHeader { - private static readonly byte[] Signature = {(byte) '7', (byte) 'z', 0xBC, 0xAF, 0x27, 0x1C}; + private static readonly byte[] Signature = { (byte)'7', (byte)'z', 0xBC, 0xAF, 0x27, 0x1C }; private byte _major; private byte _minor; @@ -38,7 +38,7 @@ namespace Compress.SevenZip.Structure long pos = br.BaseStream.Position; byte[] mainHeader = new byte[8 + 8 + 4]; br.BaseStream.Read(mainHeader, 0, mainHeader.Length); - if (!Utils.CRC.VerifyDigest(_startHeaderCRC, mainHeader, 0, (uint) mainHeader.Length)) + if (!Utils.CRC.VerifyDigest(_startHeaderCRC, mainHeader, 0, (uint)mainHeader.Length)) { return false; } @@ -61,18 +61,18 @@ namespace Compress.SevenZip.Structure //ArchiveVersion //{ - bw.Write((byte) 0); // BYTE Major - bw.Write((byte) 3); // BYTE Minor + bw.Write((byte)0); // BYTE Major + bw.Write((byte)3); // BYTE Minor //}; _crcOffset = bw.BaseStream.Position; - bw.Write((uint) 0); //HeaderCRC + bw.Write((uint)0); //HeaderCRC //StartHeader //{ - bw.Write((ulong) 0); //NextHeaderOffset - bw.Write((ulong) 0); //NextHeaderSize - bw.Write((uint) 0); //NextHeaderCRC + bw.Write((ulong)0); //NextHeaderOffset + bw.Write((ulong)0); //NextHeaderSize + bw.Write((uint)0); //NextHeaderCRC //} BaseOffset = bw.BaseStream.Position; @@ -86,9 +86,9 @@ namespace Compress.SevenZip.Structure byte[] sigHeaderBytes; using (MemoryStream sigHeaderMem = new MemoryStream()) { - using (BinaryWriter sigHeaderBw = new BinaryWriter(sigHeaderMem,Encoding.UTF8,true)) + using (BinaryWriter sigHeaderBw = new BinaryWriter(sigHeaderMem, Encoding.UTF8, true)) { - sigHeaderBw.Write((ulong) ((long) headerpos - BaseOffset)); //NextHeaderOffset + sigHeaderBw.Write((ulong)((long)headerpos - BaseOffset)); //NextHeaderOffset sigHeaderBw.Write(headerLength); //NextHeaderSize sigHeaderBw.Write(headerCRC); //NextHeaderCRC @@ -98,7 +98,7 @@ namespace Compress.SevenZip.Structure } } - uint sigHeaderCRC = Utils.CRC.CalculateDigest(sigHeaderBytes, 0, (uint) sigHeaderBytes.Length); + uint sigHeaderCRC = Utils.CRC.CalculateDigest(sigHeaderBytes, 0, (uint)sigHeaderBytes.Length); bw.BaseStream.Position = _crcOffset; bw.Write(sigHeaderCRC); //Header CRC diff --git a/SabreTools.Library/External/Compress/SevenZip/Structure/StreamsInfo.cs b/SabreTools.Library/External/Compress/SevenZip/Structure/StreamsInfo.cs index c78fff39..d440dc97 100644 --- a/SabreTools.Library/External/Compress/SevenZip/Structure/StreamsInfo.cs +++ b/SabreTools.Library/External/Compress/SevenZip/Structure/StreamsInfo.cs @@ -14,9 +14,9 @@ namespace Compress.SevenZip.Structure public void Read(BinaryReader br) { - for (;;) + for (; ; ) { - HeaderProperty hp = (HeaderProperty) br.ReadByte(); + HeaderProperty hp = (HeaderProperty)br.ReadByte(); switch (hp) { case HeaderProperty.kPackInfo: @@ -42,11 +42,18 @@ namespace Compress.SevenZip.Structure public void Write(BinaryWriter bw) { - bw.Write((byte) HeaderProperty.kMainStreamsInfo); + bw.Write((byte)HeaderProperty.kMainStreamsInfo); PackedStreamInfo.Write(bw, PackPosition, PackedStreams); Folder.WriteUnPackInfo(bw, Folders); Folder.WriteSubStreamsInfo(bw, Folders); - bw.Write((byte) HeaderProperty.kEnd); + bw.Write((byte)HeaderProperty.kEnd); + } + + public void WriteHeader(BinaryWriter bw) + { + PackedStreamInfo.Write(bw, PackPosition, PackedStreams); + Folder.WriteUnPackInfo(bw, Folders); + bw.Write((byte)HeaderProperty.kEnd); } diff --git a/SabreTools.Library/External/Compress/SevenZip/Structure/UnpackedStreamInfo.cs b/SabreTools.Library/External/Compress/SevenZip/Structure/UnpackedStreamInfo.cs index 9eceb3c7..65e4af11 100644 --- a/SabreTools.Library/External/Compress/SevenZip/Structure/UnpackedStreamInfo.cs +++ b/SabreTools.Library/External/Compress/SevenZip/Structure/UnpackedStreamInfo.cs @@ -7,7 +7,7 @@ namespace Compress.SevenZip.Structure { public ulong UnpackedSize; public uint? Crc; - + public void Report(ref StringBuilder sb) { sb.AppendLine($" Crc = {Crc.ToHex()} , Size = {UnpackedSize}"); diff --git a/SabreTools.Library/External/Compress/SevenZip/Util.cs b/SabreTools.Library/External/Compress/SevenZip/Util.cs index fb14b00b..a4c748d1 100644 --- a/SabreTools.Library/External/Compress/SevenZip/Util.cs +++ b/SabreTools.Library/External/Compress/SevenZip/Util.cs @@ -111,12 +111,12 @@ namespace Compress.SevenZip { if ((firstByte & mask) == 0) { - ulong highPart = (ulong) (firstByte & (mask - 1)); - value += highPart << (8*i); + ulong highPart = (ulong)(firstByte & (mask - 1)); + value += highPart << (8 * i); return value; } byte b = br.ReadByte(); - value |= (ulong) b << (8*i); + value |= (ulong)b << (8 * i); mask >>= 1; } return value; @@ -129,9 +129,9 @@ namespace Compress.SevenZip int i; for (i = 0; i < 8; i++) { - if (value < (ulong) 1 << (7*(i + 1))) + if (value < (ulong)1 << (7 * (i + 1))) { - firstByte |= (byte) (value >> (8*i)); + firstByte |= (byte)(value >> (8 * i)); break; } firstByte |= mask; @@ -140,7 +140,7 @@ namespace Compress.SevenZip bw.Write(firstByte); for (; i > 0; i--) { - bw.Write((byte) value); + bw.Write((byte)value); value >>= 8; } } @@ -148,9 +148,9 @@ namespace Compress.SevenZip public static string ReadName(this BinaryReader br) { StringBuilder stringBuilder = new StringBuilder(); - for (;;) + for (; ; ) { - char c = (char) br.ReadUInt16(); + char c = (char)br.ReadUInt16(); if (c == 0) { return stringBuilder.ToString(); @@ -164,9 +164,9 @@ namespace Compress.SevenZip char[] chars = name.ToCharArray(); for (int i = 0; i < chars.Length; i++) { - bw.Write((ushort) chars[i]); + bw.Write((ushort)chars[i]); } - bw.Write((ushort) 0); + bw.Write((ushort)0); } @@ -183,6 +183,23 @@ namespace Compress.SevenZip } } + public static void WritePackedCRCs(BinaryWriter bw, uint?[] digests) + { + bool[] digestsDefined = new bool[digests.Length]; + for (int i = 0; i < digests.Length; i++) + { + digestsDefined[i] = digests[i] != null; + } + WriteBoolFlagsDefaultTrue(bw, digestsDefined); + for (int i = 0; i < digests.Length; i++) + { + if (digestsDefined[i]) + { + bw.Write((uint)digests[i]); + } + } + } + private static bool[] ReadBoolFlagsDefaultTrue(BinaryReader br, ulong numItems) { byte allAreDefined = br.ReadByte(); @@ -198,6 +215,22 @@ namespace Compress.SevenZip return flags; } + private static void WriteBoolFlagsDefaultTrue(BinaryWriter bw, bool[] bArray) + { + bool allTrue = true; + foreach (bool b in bArray) + { + allTrue &= b; + } + + if (allTrue) + { + bw.Write((byte)1); + return; + } + WriteBoolFlags(bw, bArray); + } + public static bool[] ReadBoolFlags(BinaryReader br, ulong numItems) { byte b = 0; @@ -238,9 +271,9 @@ namespace Compress.SevenZip public static void WriteUint32Def(BinaryWriter br, uint[] values) { - br.WriteEncodedUInt64((ulong) (values.Length*4 + 2)); - br.Write((byte) 1); - br.Write((byte) 0); + br.WriteEncodedUInt64((ulong)(values.Length * 4 + 2)); + br.Write((byte)1); + br.Write((byte)0); for (int i = 0; i < values.Length; i++) { br.Write(values[i]); @@ -275,7 +308,7 @@ namespace Compress.SevenZip public static void WriteBoolFlags(BinaryWriter bw, bool[] bArray) { - bw.WriteEncodedUInt64((ulong) ((bArray.Length + 7)/8)); + bw.WriteEncodedUInt64((ulong)((bArray.Length + 7) / 8)); byte mask = 0x80; byte tmpOut = 0; for (int i = 0; i < bArray.Length; i++) @@ -307,13 +340,13 @@ namespace Compress.SevenZip { return null; } - uint c = (uint) crc; + uint c = (uint)crc; byte[] b = new byte[4]; - b[0] = (byte) ((c >> 24) & 0xff); - b[1] = (byte) ((c >> 16) & 0xff); - b[2] = (byte) ((c >> 8) & 0xff); - b[3] = (byte) ((c >> 0) & 0xff); + b[0] = (byte)((c >> 24) & 0xff); + b[1] = (byte)((c >> 16) & 0xff); + b[2] = (byte)((c >> 8) & 0xff); + b[3] = (byte)((c >> 0) & 0xff); return b; } @@ -324,7 +357,7 @@ namespace Compress.SevenZip return null; } - return (uint?) ((crc[0] << 24) | (crc[1] << 16) | (crc[2] << 8) | (crc[3] << 0)); + return (uint?)((crc[0] << 24) | (crc[1] << 16) | (crc[2] << 8) | (crc[3] << 0)); } public static bool ByteArrCompare(byte[] b0, byte[] b1) diff --git a/SabreTools.Library/External/Compress/ThreadReaders/ThreadCRC.cs b/SabreTools.Library/External/Compress/ThreadReaders/ThreadCRC.cs index 5be20fe8..35341179 100644 --- a/SabreTools.Library/External/Compress/ThreadReaders/ThreadCRC.cs +++ b/SabreTools.Library/External/Compress/ThreadReaders/ThreadCRC.cs @@ -5,7 +5,7 @@ namespace Compress.ThreadReaders { public class ThreadCRC : IDisposable { - private Utils.CRC crc; + private Utils.CRC crc; private readonly AutoResetEvent _waitEvent; private readonly AutoResetEvent _outEvent; private readonly Thread _tWorker; @@ -17,7 +17,7 @@ namespace Compress.ThreadReaders public ThreadCRC() { - crc=new Utils.CRC(); + crc = new Utils.CRC(); _waitEvent = new AutoResetEvent(false); _outEvent = new AutoResetEvent(false); _finished = false; @@ -44,7 +44,7 @@ namespace Compress.ThreadReaders break; } - crc.SlurpBlock(_buffer,0,_size); + crc.SlurpBlock(_buffer, 0, _size); _outEvent.Set(); } diff --git a/SabreTools.Library/External/Compress/Utils/CRCStream.cs b/SabreTools.Library/External/Compress/Utils/CRCStream.cs index 5c416e82..5f266fd2 100644 --- a/SabreTools.Library/External/Compress/Utils/CRCStream.cs +++ b/SabreTools.Library/External/Compress/Utils/CRCStream.cs @@ -340,4 +340,4 @@ namespace Compress.Utils } -} +} \ No newline at end of file diff --git a/SabreTools.Library/External/Compress/Utils/Reporter.cs b/SabreTools.Library/External/Compress/Utils/Reporter.cs index a6a60588..3dd98e53 100644 --- a/SabreTools.Library/External/Compress/Utils/Reporter.cs +++ b/SabreTools.Library/External/Compress/Utils/Reporter.cs @@ -40,4 +40,4 @@ namespace Compress.Utils return v == null ? "NULL" : ((ulong)v).ToString("X8"); } } -} +} \ No newline at end of file diff --git a/SabreTools.Library/External/Compress/ZipEnums.cs b/SabreTools.Library/External/Compress/ZipEnums.cs index f51773af..f4a89a5a 100644 --- a/SabreTools.Library/External/Compress/ZipEnums.cs +++ b/SabreTools.Library/External/Compress/ZipEnums.cs @@ -29,6 +29,7 @@ namespace Compress ZipErrorTimeStamp, ZipErrorRollBackFile, ZipTryingToAccessADirectory, + ZipErrorWritingToOutputStream, ZipUntested } @@ -49,4 +50,4 @@ namespace Compress ExtraData = 0x2, Trrnt7Zip = 0x4 } -} +} \ No newline at end of file diff --git a/SabreTools.Library/External/Compress/ZipFile/ZLib/Deflate.cs b/SabreTools.Library/External/Compress/ZipFile/ZLib/Deflate.cs index 83830228..0b7b3460 100644 --- a/SabreTools.Library/External/Compress/ZipFile/ZLib/Deflate.cs +++ b/SabreTools.Library/External/Compress/ZipFile/ZLib/Deflate.cs @@ -545,10 +545,10 @@ namespace Compress.ZipFile.ZLib internal void send_tree(short[] tree, int max_code) { int n; // iterates over all tree elements - int prevlen = -1; // last emitted length + int prevlen = -1; // last emitted length int curlen; // length of current code - int nextlen = tree[0 * 2 + 1]; // length of next code - int count = 0; // repeat count of the current code + int nextlen = tree[0 * 2 + 1]; // length of next code + int count = 0; // repeat count of the current code int max_count = 7; // max repeat count int min_count = 4; // min repeat count @@ -729,7 +729,7 @@ namespace Compress.ZipFile.ZLib * * * this code is not turned on by default in ZLIB Trrntzip code * * * - * ************************************************************* + * ************************************************************* */ if (false) //CompSettings { diff --git a/SabreTools.Library/External/Compress/ZipFile/ZLib/InfTree.cs b/SabreTools.Library/External/Compress/ZipFile/ZLib/InfTree.cs index 587f9c10..502d497a 100644 --- a/SabreTools.Library/External/Compress/ZipFile/ZLib/InfTree.cs +++ b/SabreTools.Library/External/Compress/ZipFile/ZLib/InfTree.cs @@ -1,45 +1,45 @@ // Inftree.cs // ------------------------------------------------------------------ // -// Copyright (c) 2009 Dino Chiesa and Microsoft Corporation. +// Copyright (c) 2009 Dino Chiesa and Microsoft Corporation. // All rights reserved. // // This code module is part of DotNetZip, a zipfile class library. // // ------------------------------------------------------------------ // -// This code is licensed under the Microsoft Public License. +// This code is licensed under the Microsoft Public License. // See the file License.txt for the license details. // More info on: http://dotnetzip.codeplex.com // // ------------------------------------------------------------------ // -// last saved (in emacs): +// last saved (in emacs): // Time-stamp: <2009-October-28 12:43:54> // // ------------------------------------------------------------------ // // This module defines classes used in decompression. This code is derived -// from the jzlib implementation of zlib. In keeping with the license for jzlib, +// from the jzlib implementation of zlib. In keeping with the license for jzlib, // the copyright to that code is below. // // ------------------------------------------------------------------ -// +// // Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved. -// +// // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: -// +// // 1. Redistributions of source code must retain the above copyright notice, // this list of conditions and the following disclaimer. -// -// 2. Redistributions in binary form must reproduce the above copyright -// notice, this list of conditions and the following disclaimer in +// +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in // the documentation and/or other materials provided with the distribution. -// +// // 3. The names of the authors may not be used to endorse or promote products // derived from this software without specific prior written permission. -// +// // THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND // FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT, @@ -50,7 +50,7 @@ // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// +// // ----------------------------------------------------------------------- // // This program is based on zlib-1.1.3; credit to authors @@ -64,373 +64,373 @@ using System; namespace Compress.ZipFile.ZLib { - - sealed class InfTree - { - - private const int MANY = 1440; - - private const int Z_OK = 0; - private const int Z_STREAM_END = 1; - private const int Z_NEED_DICT = 2; - private const int Z_ERRNO = - 1; - private const int Z_STREAM_ERROR = - 2; - private const int Z_DATA_ERROR = - 3; - private const int Z_MEM_ERROR = - 4; - private const int Z_BUF_ERROR = - 5; - private const int Z_VERSION_ERROR = - 6; - - internal const int fixed_bl = 9; - internal const int fixed_bd = 5; - - //UPGRADE_NOTE: Final was removed from the declaration of 'fixed_tl'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'" - internal static readonly int[] fixed_tl = new int[]{96, 7, 256, 0, 8, 80, 0, 8, 16, 84, 8, 115, 82, 7, 31, 0, 8, 112, 0, 8, 48, 0, 9, 192, 80, 7, 10, 0, 8, 96, 0, 8, 32, 0, 9, 160, 0, 8, 0, 0, 8, 128, 0, 8, 64, 0, 9, 224, 80, 7, 6, 0, 8, 88, 0, 8, 24, 0, 9, 144, 83, 7, 59, 0, 8, 120, 0, 8, 56, 0, 9, 208, 81, 7, 17, 0, 8, 104, 0, 8, 40, 0, 9, 176, 0, 8, 8, 0, 8, 136, 0, 8, 72, 0, 9, 240, 80, 7, 4, 0, 8, 84, 0, 8, 20, 85, 8, 227, 83, 7, 43, 0, 8, 116, 0, 8, 52, 0, 9, 200, 81, 7, 13, 0, 8, 100, 0, 8, 36, 0, 9, 168, 0, 8, 4, 0, 8, 132, 0, 8, 68, 0, 9, 232, 80, 7, 8, 0, 8, 92, 0, 8, 28, 0, 9, 152, 84, 7, 83, 0, 8, 124, 0, 8, 60, 0, 9, 216, 82, 7, 23, 0, 8, 108, 0, 8, 44, 0, 9, 184, 0, 8, 12, 0, 8, 140, 0, 8, 76, 0, 9, 248, 80, 7, 3, 0, 8, 82, 0, 8, 18, 85, 8, 163, 83, 7, 35, 0, 8, 114, 0, 8, 50, 0, 9, 196, 81, 7, 11, 0, 8, 98, 0, 8, 34, 0, 9, 164, 0, 8, 2, 0, 8, 130, 0, 8, 66, 0, 9, 228, 80, 7, 7, 0, 8, 90, 0, 8, 26, 0, 9, 148, 84, 7, 67, 0, 8, 122, 0, 8, 58, 0, 9, 212, 82, 7, 19, 0, 8, 106, 0, 8, 42, 0, 9, 180, 0, 8, 10, 0, 8, 138, 0, 8, 74, 0, 9, 244, 80, 7, 5, 0, 8, 86, 0, 8, 22, 192, 8, 0, 83, 7, 51, 0, 8, 118, 0, 8, 54, 0, 9, 204, 81, 7, 15, 0, 8, 102, 0, 8, 38, 0, 9, 172, 0, 8, 6, 0, 8, 134, 0, 8, 70, 0, 9, 236, 80, 7, 9, 0, 8, 94, 0, 8, 30, 0, 9, 156, 84, 7, 99, 0, 8, 126, 0, 8, 62, 0, 9, 220, 82, 7, 27, 0, 8, 110, 0, 8, 46, 0, 9, 188, 0, 8, 14, 0, 8, 142, 0, 8, 78, 0, 9, 252, 96, 7, 256, 0, 8, 81, 0, 8, 17, 85, 8, 131, 82, 7, 31, 0, 8, 113, 0, 8, 49, 0, 9, 194, 80, 7, 10, 0, 8, 97, 0, 8, 33, 0, 9, 162, 0, 8, 1, 0, 8, 129, 0, 8, 65, 0, 9, 226, 80, 7, 6, 0, 8, 89, 0, 8, 25, 0, 9, 146, 83, 7, 59, 0, 8, 121, 0, 8, 57, 0, 9, 210, 81, 7, 17, 0, 8, 105, 0, 8, 41, 0, 9, 178, 0, 8, 9, 0, 8, 137, 0, 8, 73, 0, 9, 242, 80, 7, 4, 0, 8, 85, 0, 8, 21, 80, 8, 258, 83, 7, 43, 0, 8, 117, 0, 8, 53, 0, 9, 202, 81, 7, 13, 0, 8, 101, 0, 8, 37, 0, 9, 170, 0, 8, 5, 0, 8, 133, 0, 8, 69, 0, 9, 234, 80, 7, 8, 0, 8, 93, 0, 8, 29, 0, 9, 154, 84, 7, 83, 0, 8, 125, 0, 8, 61, 0, 9, 218, 82, 7, 23, 0, 8, 109, 0, 8, 45, 0, 9, 186, - 0, 8, 13, 0, 8, 141, 0, 8, 77, 0, 9, 250, 80, 7, 3, 0, 8, 83, 0, 8, 19, 85, 8, 195, 83, 7, 35, 0, 8, 115, 0, 8, 51, 0, 9, 198, 81, 7, 11, 0, 8, 99, 0, 8, 35, 0, 9, 166, 0, 8, 3, 0, 8, 131, 0, 8, 67, 0, 9, 230, 80, 7, 7, 0, 8, 91, 0, 8, 27, 0, 9, 150, 84, 7, 67, 0, 8, 123, 0, 8, 59, 0, 9, 214, 82, 7, 19, 0, 8, 107, 0, 8, 43, 0, 9, 182, 0, 8, 11, 0, 8, 139, 0, 8, 75, 0, 9, 246, 80, 7, 5, 0, 8, 87, 0, 8, 23, 192, 8, 0, 83, 7, 51, 0, 8, 119, 0, 8, 55, 0, 9, 206, 81, 7, 15, 0, 8, 103, 0, 8, 39, 0, 9, 174, 0, 8, 7, 0, 8, 135, 0, 8, 71, 0, 9, 238, 80, 7, 9, 0, 8, 95, 0, 8, 31, 0, 9, 158, 84, 7, 99, 0, 8, 127, 0, 8, 63, 0, 9, 222, 82, 7, 27, 0, 8, 111, 0, 8, 47, 0, 9, 190, 0, 8, 15, 0, 8, 143, 0, 8, 79, 0, 9, 254, 96, 7, 256, 0, 8, 80, 0, 8, 16, 84, 8, 115, 82, 7, 31, 0, 8, 112, 0, 8, 48, 0, 9, 193, 80, 7, 10, 0, 8, 96, 0, 8, 32, 0, 9, 161, 0, 8, 0, 0, 8, 128, 0, 8, 64, 0, 9, 225, 80, 7, 6, 0, 8, 88, 0, 8, 24, 0, 9, 145, 83, 7, 59, 0, 8, 120, 0, 8, 56, 0, 9, 209, 81, 7, 17, 0, 8, 104, 0, 8, 40, 0, 9, 177, 0, 8, 8, 0, 8, 136, 0, 8, 72, 0, 9, 241, 80, 7, 4, 0, 8, 84, 0, 8, 20, 85, 8, 227, 83, 7, 43, 0, 8, 116, 0, 8, 52, 0, 9, 201, 81, 7, 13, 0, 8, 100, 0, 8, 36, 0, 9, 169, 0, 8, 4, 0, 8, 132, 0, 8, 68, 0, 9, 233, 80, 7, 8, 0, 8, 92, 0, 8, 28, 0, 9, 153, 84, 7, 83, 0, 8, 124, 0, 8, 60, 0, 9, 217, 82, 7, 23, 0, 8, 108, 0, 8, 44, 0, 9, 185, 0, 8, 12, 0, 8, 140, 0, 8, 76, 0, 9, 249, 80, 7, 3, 0, 8, 82, 0, 8, 18, 85, 8, 163, 83, 7, 35, 0, 8, 114, 0, 8, 50, 0, 9, 197, 81, 7, 11, 0, 8, 98, 0, 8, 34, 0, 9, 165, 0, 8, 2, 0, 8, 130, 0, 8, 66, 0, 9, 229, 80, 7, 7, 0, 8, 90, 0, 8, 26, 0, 9, 149, 84, 7, 67, 0, 8, 122, 0, 8, 58, 0, 9, 213, 82, 7, 19, 0, 8, 106, 0, 8, 42, 0, 9, 181, 0, 8, 10, 0, 8, 138, 0, 8, 74, 0, 9, 245, 80, 7, 5, 0, 8, 86, 0, 8, 22, 192, 8, 0, 83, 7, 51, 0, 8, 118, 0, 8, 54, 0, 9, 205, 81, 7, 15, 0, 8, 102, 0, 8, 38, 0, 9, 173, 0, 8, 6, 0, 8, 134, 0, 8, 70, 0, 9, 237, 80, 7, 9, 0, 8, 94, 0, 8, 30, 0, 9, 157, 84, 7, 99, 0, 8, 126, 0, 8, 62, 0, 9, 221, 82, 7, 27, 0, 8, 110, 0, 8, 46, 0, 9, 189, 0, 8, + + sealed class InfTree + { + + private const int MANY = 1440; + + private const int Z_OK = 0; + private const int Z_STREAM_END = 1; + private const int Z_NEED_DICT = 2; + private const int Z_ERRNO = -1; + private const int Z_STREAM_ERROR = -2; + private const int Z_DATA_ERROR = -3; + private const int Z_MEM_ERROR = -4; + private const int Z_BUF_ERROR = -5; + private const int Z_VERSION_ERROR = -6; + + internal const int fixed_bl = 9; + internal const int fixed_bd = 5; + + //UPGRADE_NOTE: Final was removed from the declaration of 'fixed_tl'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'" + internal static readonly int[] fixed_tl = new int[]{96, 7, 256, 0, 8, 80, 0, 8, 16, 84, 8, 115, 82, 7, 31, 0, 8, 112, 0, 8, 48, 0, 9, 192, 80, 7, 10, 0, 8, 96, 0, 8, 32, 0, 9, 160, 0, 8, 0, 0, 8, 128, 0, 8, 64, 0, 9, 224, 80, 7, 6, 0, 8, 88, 0, 8, 24, 0, 9, 144, 83, 7, 59, 0, 8, 120, 0, 8, 56, 0, 9, 208, 81, 7, 17, 0, 8, 104, 0, 8, 40, 0, 9, 176, 0, 8, 8, 0, 8, 136, 0, 8, 72, 0, 9, 240, 80, 7, 4, 0, 8, 84, 0, 8, 20, 85, 8, 227, 83, 7, 43, 0, 8, 116, 0, 8, 52, 0, 9, 200, 81, 7, 13, 0, 8, 100, 0, 8, 36, 0, 9, 168, 0, 8, 4, 0, 8, 132, 0, 8, 68, 0, 9, 232, 80, 7, 8, 0, 8, 92, 0, 8, 28, 0, 9, 152, 84, 7, 83, 0, 8, 124, 0, 8, 60, 0, 9, 216, 82, 7, 23, 0, 8, 108, 0, 8, 44, 0, 9, 184, 0, 8, 12, 0, 8, 140, 0, 8, 76, 0, 9, 248, 80, 7, 3, 0, 8, 82, 0, 8, 18, 85, 8, 163, 83, 7, 35, 0, 8, 114, 0, 8, 50, 0, 9, 196, 81, 7, 11, 0, 8, 98, 0, 8, 34, 0, 9, 164, 0, 8, 2, 0, 8, 130, 0, 8, 66, 0, 9, 228, 80, 7, 7, 0, 8, 90, 0, 8, 26, 0, 9, 148, 84, 7, 67, 0, 8, 122, 0, 8, 58, 0, 9, 212, 82, 7, 19, 0, 8, 106, 0, 8, 42, 0, 9, 180, 0, 8, 10, 0, 8, 138, 0, 8, 74, 0, 9, 244, 80, 7, 5, 0, 8, 86, 0, 8, 22, 192, 8, 0, 83, 7, 51, 0, 8, 118, 0, 8, 54, 0, 9, 204, 81, 7, 15, 0, 8, 102, 0, 8, 38, 0, 9, 172, 0, 8, 6, 0, 8, 134, 0, 8, 70, 0, 9, 236, 80, 7, 9, 0, 8, 94, 0, 8, 30, 0, 9, 156, 84, 7, 99, 0, 8, 126, 0, 8, 62, 0, 9, 220, 82, 7, 27, 0, 8, 110, 0, 8, 46, 0, 9, 188, 0, 8, 14, 0, 8, 142, 0, 8, 78, 0, 9, 252, 96, 7, 256, 0, 8, 81, 0, 8, 17, 85, 8, 131, 82, 7, 31, 0, 8, 113, 0, 8, 49, 0, 9, 194, 80, 7, 10, 0, 8, 97, 0, 8, 33, 0, 9, 162, 0, 8, 1, 0, 8, 129, 0, 8, 65, 0, 9, 226, 80, 7, 6, 0, 8, 89, 0, 8, 25, 0, 9, 146, 83, 7, 59, 0, 8, 121, 0, 8, 57, 0, 9, 210, 81, 7, 17, 0, 8, 105, 0, 8, 41, 0, 9, 178, 0, 8, 9, 0, 8, 137, 0, 8, 73, 0, 9, 242, 80, 7, 4, 0, 8, 85, 0, 8, 21, 80, 8, 258, 83, 7, 43, 0, 8, 117, 0, 8, 53, 0, 9, 202, 81, 7, 13, 0, 8, 101, 0, 8, 37, 0, 9, 170, 0, 8, 5, 0, 8, 133, 0, 8, 69, 0, 9, 234, 80, 7, 8, 0, 8, 93, 0, 8, 29, 0, 9, 154, 84, 7, 83, 0, 8, 125, 0, 8, 61, 0, 9, 218, 82, 7, 23, 0, 8, 109, 0, 8, 45, 0, 9, 186, + 0, 8, 13, 0, 8, 141, 0, 8, 77, 0, 9, 250, 80, 7, 3, 0, 8, 83, 0, 8, 19, 85, 8, 195, 83, 7, 35, 0, 8, 115, 0, 8, 51, 0, 9, 198, 81, 7, 11, 0, 8, 99, 0, 8, 35, 0, 9, 166, 0, 8, 3, 0, 8, 131, 0, 8, 67, 0, 9, 230, 80, 7, 7, 0, 8, 91, 0, 8, 27, 0, 9, 150, 84, 7, 67, 0, 8, 123, 0, 8, 59, 0, 9, 214, 82, 7, 19, 0, 8, 107, 0, 8, 43, 0, 9, 182, 0, 8, 11, 0, 8, 139, 0, 8, 75, 0, 9, 246, 80, 7, 5, 0, 8, 87, 0, 8, 23, 192, 8, 0, 83, 7, 51, 0, 8, 119, 0, 8, 55, 0, 9, 206, 81, 7, 15, 0, 8, 103, 0, 8, 39, 0, 9, 174, 0, 8, 7, 0, 8, 135, 0, 8, 71, 0, 9, 238, 80, 7, 9, 0, 8, 95, 0, 8, 31, 0, 9, 158, 84, 7, 99, 0, 8, 127, 0, 8, 63, 0, 9, 222, 82, 7, 27, 0, 8, 111, 0, 8, 47, 0, 9, 190, 0, 8, 15, 0, 8, 143, 0, 8, 79, 0, 9, 254, 96, 7, 256, 0, 8, 80, 0, 8, 16, 84, 8, 115, 82, 7, 31, 0, 8, 112, 0, 8, 48, 0, 9, 193, 80, 7, 10, 0, 8, 96, 0, 8, 32, 0, 9, 161, 0, 8, 0, 0, 8, 128, 0, 8, 64, 0, 9, 225, 80, 7, 6, 0, 8, 88, 0, 8, 24, 0, 9, 145, 83, 7, 59, 0, 8, 120, 0, 8, 56, 0, 9, 209, 81, 7, 17, 0, 8, 104, 0, 8, 40, 0, 9, 177, 0, 8, 8, 0, 8, 136, 0, 8, 72, 0, 9, 241, 80, 7, 4, 0, 8, 84, 0, 8, 20, 85, 8, 227, 83, 7, 43, 0, 8, 116, 0, 8, 52, 0, 9, 201, 81, 7, 13, 0, 8, 100, 0, 8, 36, 0, 9, 169, 0, 8, 4, 0, 8, 132, 0, 8, 68, 0, 9, 233, 80, 7, 8, 0, 8, 92, 0, 8, 28, 0, 9, 153, 84, 7, 83, 0, 8, 124, 0, 8, 60, 0, 9, 217, 82, 7, 23, 0, 8, 108, 0, 8, 44, 0, 9, 185, 0, 8, 12, 0, 8, 140, 0, 8, 76, 0, 9, 249, 80, 7, 3, 0, 8, 82, 0, 8, 18, 85, 8, 163, 83, 7, 35, 0, 8, 114, 0, 8, 50, 0, 9, 197, 81, 7, 11, 0, 8, 98, 0, 8, 34, 0, 9, 165, 0, 8, 2, 0, 8, 130, 0, 8, 66, 0, 9, 229, 80, 7, 7, 0, 8, 90, 0, 8, 26, 0, 9, 149, 84, 7, 67, 0, 8, 122, 0, 8, 58, 0, 9, 213, 82, 7, 19, 0, 8, 106, 0, 8, 42, 0, 9, 181, 0, 8, 10, 0, 8, 138, 0, 8, 74, 0, 9, 245, 80, 7, 5, 0, 8, 86, 0, 8, 22, 192, 8, 0, 83, 7, 51, 0, 8, 118, 0, 8, 54, 0, 9, 205, 81, 7, 15, 0, 8, 102, 0, 8, 38, 0, 9, 173, 0, 8, 6, 0, 8, 134, 0, 8, 70, 0, 9, 237, 80, 7, 9, 0, 8, 94, 0, 8, 30, 0, 9, 157, 84, 7, 99, 0, 8, 126, 0, 8, 62, 0, 9, 221, 82, 7, 27, 0, 8, 110, 0, 8, 46, 0, 9, 189, 0, 8, 14, 0, 8, 142, 0, 8, 78, 0, 9, 253, 96, 7, 256, 0, 8, 81, 0, 8, 17, 85, 8, 131, 82, 7, 31, 0, 8, 113, 0, 8, 49, 0, 9, 195, 80, 7, 10, 0, 8, 97, 0, 8, 33, 0, 9, 163, 0, 8, 1, 0, 8, 129, 0, 8, 65, 0, 9, 227, 80, 7, 6, 0, 8, 89, 0, 8, 25, 0, 9, 147, 83, 7, 59, 0, 8, 121, 0, 8, 57, 0, 9, 211, 81, 7, 17, 0, 8, 105, 0, 8, 41, 0, 9, 179, 0, 8, 9, 0, 8, 137, 0, 8, 73, 0, 9, 243, 80, 7, 4, 0, 8, 85, 0, 8, 21, 80, 8, 258, 83, 7, 43, 0, 8, 117, 0, 8, 53, 0, 9, 203, 81, 7, 13, 0, 8, 101, 0, 8, 37, 0, 9, 171, 0, 8, 5, 0, 8, 133, 0, 8, 69, 0, 9, 235, 80, 7, 8, 0, 8, 93, 0, 8, 29, 0, 9, 155, 84, 7, 83, 0, 8, 125, 0, 8, 61, 0, 9, 219, 82, 7, 23, 0, 8, 109, 0, 8, 45, 0, 9, 187, 0, 8, 13, 0, 8, 141, 0, 8, 77, 0, 9, 251, 80, 7, 3, 0, 8, 83, 0, 8, 19, 85, 8, 195, 83, 7, 35, 0, 8, 115, 0, 8, 51, 0, 9, 199, 81, 7, 11, 0, 8, 99, 0, 8, 35, 0, 9, 167, 0, 8, 3, 0, 8, 131, 0, 8, 67, 0, 9, 231, 80, 7, 7, 0, 8, 91, 0, 8, 27, 0, 9, 151, 84, 7, 67, 0, 8, 123, 0, 8, 59, 0, 9, 215, 82, 7, 19, 0, 8, 107, 0, 8, 43, 0, 9, 183, 0, 8, 11, 0, 8, 139, 0, 8, 75, 0, 9, 247, 80, 7, 5, 0, 8, 87, 0, 8, 23, 192, 8, 0, 83, 7, 51, 0, 8, 119, 0, 8, 55, 0, 9, 207, 81, 7, 15, 0, 8, 103, 0, 8, 39, 0, 9, 175, 0, 8, 7, 0, 8, 135, 0, 8, 71, 0, 9, 239, 80, 7, 9, 0, 8, 95, 0, 8, 31, 0, 9, 159, 84, 7, 99, 0, 8, 127, 0, 8, 63, 0, 9, 223, 82, 7, 27, 0, 8, 111, 0, 8, 47, 0, 9, 191, 0, 8, 15, 0, 8, 143, 0, 8, 79, 0, 9, 255}; - //UPGRADE_NOTE: Final was removed from the declaration of 'fixed_td'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'" - internal static readonly int[] fixed_td = new int[]{80, 5, 1, 87, 5, 257, 83, 5, 17, 91, 5, 4097, 81, 5, 5, 89, 5, 1025, 85, 5, 65, 93, 5, 16385, 80, 5, 3, 88, 5, 513, 84, 5, 33, 92, 5, 8193, 82, 5, 9, 90, 5, 2049, 86, 5, 129, 192, 5, 24577, 80, 5, 2, 87, 5, 385, 83, 5, 25, 91, 5, 6145, 81, 5, 7, 89, 5, 1537, 85, 5, 97, 93, 5, 24577, 80, 5, 4, 88, 5, 769, 84, 5, 49, 92, 5, 12289, 82, 5, 13, 90, 5, 3073, 86, 5, 193, 192, 5, 24577}; - - // Tables for deflate from PKZIP's appnote.txt. - //UPGRADE_NOTE: Final was removed from the declaration of 'cplens'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'" - internal static readonly int[] cplens = new int[]{3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0}; - - // see note #13 above about 258 - //UPGRADE_NOTE: Final was removed from the declaration of 'cplext'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'" - internal static readonly int[] cplext = new int[]{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, 0, 112, 112}; - - //UPGRADE_NOTE: Final was removed from the declaration of 'cpdist'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'" - internal static readonly int[] cpdist = new int[]{1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577}; - - //UPGRADE_NOTE: Final was removed from the declaration of 'cpdext'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'" - internal static readonly int[] cpdext = new int[]{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}; - - // If BMAX needs to be larger than 16, then h and x[] should be uLong. - internal const int BMAX = 15; // maximum bit length of any code - - internal int[] hn = null; // hufts used in space - internal int[] v = null; // work area for huft_build - internal int[] c = null; // bit length count table - internal int[] r = null; // table entry for structure assignment - internal int[] u = null; // table stack - internal int[] x = null; // bit offsets, then code stack - - private int huft_build(int[] b, int bindex, int n, int s, int[] d, int[] e, int[] t, int[] m, int[] hp, int[] hn, int[] v) + //UPGRADE_NOTE: Final was removed from the declaration of 'fixed_td'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'" + internal static readonly int[] fixed_td = new int[] { 80, 5, 1, 87, 5, 257, 83, 5, 17, 91, 5, 4097, 81, 5, 5, 89, 5, 1025, 85, 5, 65, 93, 5, 16385, 80, 5, 3, 88, 5, 513, 84, 5, 33, 92, 5, 8193, 82, 5, 9, 90, 5, 2049, 86, 5, 129, 192, 5, 24577, 80, 5, 2, 87, 5, 385, 83, 5, 25, 91, 5, 6145, 81, 5, 7, 89, 5, 1537, 85, 5, 97, 93, 5, 24577, 80, 5, 4, 88, 5, 769, 84, 5, 49, 92, 5, 12289, 82, 5, 13, 90, 5, 3073, 86, 5, 193, 192, 5, 24577 }; + + // Tables for deflate from PKZIP's appnote.txt. + //UPGRADE_NOTE: Final was removed from the declaration of 'cplens'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'" + internal static readonly int[] cplens = new int[] { 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0 }; + + // see note #13 above about 258 + //UPGRADE_NOTE: Final was removed from the declaration of 'cplext'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'" + internal static readonly int[] cplext = new int[] { 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, 0, 112, 112 }; + + //UPGRADE_NOTE: Final was removed from the declaration of 'cpdist'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'" + internal static readonly int[] cpdist = new int[] { 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577 }; + + //UPGRADE_NOTE: Final was removed from the declaration of 'cpdext'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'" + internal static readonly int[] cpdext = new int[] { 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 }; + + // If BMAX needs to be larger than 16, then h and x[] should be uLong. + internal const int BMAX = 15; // maximum bit length of any code + + internal int[] hn = null; // hufts used in space + internal int[] v = null; // work area for huft_build + internal int[] c = null; // bit length count table + internal int[] r = null; // table entry for structure assignment + internal int[] u = null; // table stack + internal int[] x = null; // bit offsets, then code stack + + private int huft_build(int[] b, int bindex, int n, int s, int[] d, int[] e, int[] t, int[] m, int[] hp, int[] hn, int[] v) + { + // Given a list of code lengths and a maximum table size, make a set of + // tables to decode that set of codes. Return Z_OK on success, Z_BUF_ERROR + // if the given code set is incomplete (the tables are still built in this + // case), Z_DATA_ERROR if the input is invalid (an over-subscribed set of + // lengths), or Z_MEM_ERROR if not enough memory. + + int a; // counter for codes of length k + int f; // i repeats in table every f entries + int g; // maximum code length + int h; // table level + int i; // counter, current code + int j; // counter + int k; // number of bits in current code + int l; // bits per table (returned in m) + int mask; // (1 << w) - 1, to avoid cc -O bug on HP + int p; // pointer into c[], b[], or v[] + int q; // points to current table + int w; // bits before this table == (l * h) + int xp; // pointer into x + int y; // number of dummy codes added + int z; // number of entries in current table + + // Generate counts for each bit length + + p = 0; i = n; + do + { + c[b[bindex + p]]++; p++; i--; // assume all entries <= BMAX + } + while (i != 0); + + if (c[0] == n) + { + // null input--all zero length codes + t[0] = -1; + m[0] = 0; + return Z_OK; + } + + // Find minimum and maximum length, bound *m by those + l = m[0]; + for (j = 1; j <= BMAX; j++) + if (c[j] != 0) + break; + k = j; // minimum code length + if (l < j) + { + l = j; + } + for (i = BMAX; i != 0; i--) + { + if (c[i] != 0) + break; + } + g = i; // maximum code length + if (l > i) + { + l = i; + } + m[0] = l; + + // Adjust last length count to fill out codes, if needed + for (y = 1 << j; j < i; j++, y <<= 1) + { + if ((y -= c[j]) < 0) { - // Given a list of code lengths and a maximum table size, make a set of - // tables to decode that set of codes. Return Z_OK on success, Z_BUF_ERROR - // if the given code set is incomplete (the tables are still built in this - // case), Z_DATA_ERROR if the input is invalid (an over-subscribed set of - // lengths), or Z_MEM_ERROR if not enough memory. - - int a; // counter for codes of length k - int f; // i repeats in table every f entries - int g; // maximum code length - int h; // table level - int i; // counter, current code - int j; // counter - int k; // number of bits in current code - int l; // bits per table (returned in m) - int mask; // (1 << w) - 1, to avoid cc -O bug on HP - int p; // pointer into c[], b[], or v[] - int q; // points to current table - int w; // bits before this table == (l * h) - int xp; // pointer into x - int y; // number of dummy codes added - int z; // number of entries in current table - - // Generate counts for each bit length - - p = 0; i = n; - do - { - c[b[bindex + p]]++; p++; i--; // assume all entries <= BMAX - } - while (i != 0); - - if (c[0] == n) - { - // null input--all zero length codes - t[0] = - 1; - m[0] = 0; - return Z_OK; - } - - // Find minimum and maximum length, bound *m by those - l = m[0]; - for (j = 1; j <= BMAX; j++) - if (c[j] != 0) - break; - k = j; // minimum code length - if (l < j) - { - l = j; - } - for (i = BMAX; i != 0; i--) - { - if (c[i] != 0) - break; - } - g = i; // maximum code length - if (l > i) - { - l = i; - } - m[0] = l; - - // Adjust last length count to fill out codes, if needed - for (y = 1 << j; j < i; j++, y <<= 1) - { - if ((y -= c[j]) < 0) - { - return Z_DATA_ERROR; - } - } - if ((y -= c[i]) < 0) - { - return Z_DATA_ERROR; - } - c[i] += y; - - // Generate starting offsets into the value table for each length - x[1] = j = 0; - p = 1; xp = 2; - while (--i != 0) - { - // note that i == g from above - x[xp] = (j += c[p]); - xp++; - p++; - } - - // Make a table of values in order of bit lengths - i = 0; p = 0; - do - { - if ((j = b[bindex + p]) != 0) - { - v[x[j]++] = i; - } - p++; - } - while (++i < n); - n = x[g]; // set n to length of v - - // Generate the Huffman codes and for each, make the table entries - x[0] = i = 0; // first Huffman code is zero - p = 0; // grab values in bit order - h = - 1; // no tables yet--level -1 - w = - l; // bits decoded == (l * h) - u[0] = 0; // just to keep compilers happy - q = 0; // ditto - z = 0; // ditto - - // go through the bit lengths (k already is bits in shortest code) - for (; k <= g; k++) - { - a = c[k]; - while (a-- != 0) - { - // here i is the Huffman code of length k bits for value *p - // make tables up to required level - while (k > w + l) - { - h++; - w += l; // previous table always l bits - // compute minimum size table less than or equal to l bits - z = g - w; - z = (z > l)?l:z; // table size upper limit - if ((f = 1 << (j = k - w)) > a + 1) - { - // try a k-w bit table - // too few codes for k-w bit table - f -= (a + 1); // deduct codes from patterns left - xp = k; - if (j < z) - { - while (++j < z) - { - // try smaller tables up to z bits - if ((f <<= 1) <= c[++xp]) - break; // enough codes to use up j bits - f -= c[xp]; // else deduct codes from patterns - } - } - } - z = 1 << j; // table entries for j-bit table - - // allocate new table - if (hn[0] + z > MANY) - { - // (note: doesn't matter for fixed) - return Z_DATA_ERROR; // overflow of MANY - } - u[h] = q = hn[0]; // DEBUG - hn[0] += z; - - // connect to last table, if there is one - if (h != 0) - { - x[h] = i; // save pattern for backing up - r[0] = (sbyte) j; // bits in this table - r[1] = (sbyte) l; // bits to dump before this table - j = SharedUtils.URShift(i, (w - l)); - r[2] = (int) (q - u[h - 1] - j); // offset to this table - Array.Copy(r, 0, hp, (u[h - 1] + j) * 3, 3); // connect to last table - } - else - { - t[0] = q; // first table is returned result - } - } - - // set up table entry in r - r[1] = (sbyte) (k - w); - if (p >= n) - { - r[0] = 128 + 64; // out of values--invalid code - } - else if (v[p] < s) - { - r[0] = (sbyte) (v[p] < 256?0:32 + 64); // 256 is end-of-block - r[2] = v[p++]; // simple code is just the value - } - else - { - r[0] = (sbyte) (e[v[p] - s] + 16 + 64); // non-simple--look up in lists - r[2] = d[v[p++] - s]; - } - - // fill code-like entries with r - f = 1 << (k - w); - for (j = SharedUtils.URShift(i, w); j < z; j += f) - { - Array.Copy(r, 0, hp, (q + j) * 3, 3); - } - - // backwards increment the k-bit code i - for (j = 1 << (k - 1); (i & j) != 0; j = SharedUtils.URShift(j, 1)) - { - i ^= j; - } - i ^= j; - - // backup over finished tables - mask = (1 << w) - 1; // needed on HP, cc -O bug - while ((i & mask) != x[h]) - { - h--; // don't need to update q - w -= l; - mask = (1 << w) - 1; - } - } - } - // Return Z_BUF_ERROR if we were given an incomplete table - return y != 0 && g != 1?Z_BUF_ERROR:Z_OK; + return Z_DATA_ERROR; } - - internal int inflate_trees_bits(int[] c, int[] bb, int[] tb, int[] hp, ZlibCodec z) + } + if ((y -= c[i]) < 0) + { + return Z_DATA_ERROR; + } + c[i] += y; + + // Generate starting offsets into the value table for each length + x[1] = j = 0; + p = 1; xp = 2; + while (--i != 0) + { + // note that i == g from above + x[xp] = (j += c[p]); + xp++; + p++; + } + + // Make a table of values in order of bit lengths + i = 0; p = 0; + do + { + if ((j = b[bindex + p]) != 0) { - int result; - initWorkArea(19); - hn[0] = 0; - result = huft_build(c, 0, 19, 19, null, null, tb, bb, hp, hn, v); - - if (result == Z_DATA_ERROR) - { - z.Message = "oversubscribed dynamic bit lengths tree"; - } - else if (result == Z_BUF_ERROR || bb[0] == 0) - { - z.Message = "incomplete dynamic bit lengths tree"; - result = Z_DATA_ERROR; - } - return result; + v[x[j]++] = i; } - - internal int inflate_trees_dynamic(int nl, int nd, int[] c, int[] bl, int[] bd, int[] tl, int[] td, int[] hp, ZlibCodec z) + p++; + } + while (++i < n); + n = x[g]; // set n to length of v + + // Generate the Huffman codes and for each, make the table entries + x[0] = i = 0; // first Huffman code is zero + p = 0; // grab values in bit order + h = -1; // no tables yet--level -1 + w = -l; // bits decoded == (l * h) + u[0] = 0; // just to keep compilers happy + q = 0; // ditto + z = 0; // ditto + + // go through the bit lengths (k already is bits in shortest code) + for (; k <= g; k++) + { + a = c[k]; + while (a-- != 0) { - int result; - - // build literal/length tree - initWorkArea(288); - hn[0] = 0; - result = huft_build(c, 0, nl, 257, cplens, cplext, tl, bl, hp, hn, v); - if (result != Z_OK || bl[0] == 0) + // here i is the Huffman code of length k bits for value *p + // make tables up to required level + while (k > w + l) + { + h++; + w += l; // previous table always l bits + // compute minimum size table less than or equal to l bits + z = g - w; + z = (z > l) ? l : z; // table size upper limit + if ((f = 1 << (j = k - w)) > a + 1) { - if (result == Z_DATA_ERROR) + // try a k-w bit table + // too few codes for k-w bit table + f -= (a + 1); // deduct codes from patterns left + xp = k; + if (j < z) + { + while (++j < z) { - z.Message = "oversubscribed literal/length tree"; + // try smaller tables up to z bits + if ((f <<= 1) <= c[++xp]) + break; // enough codes to use up j bits + f -= c[xp]; // else deduct codes from patterns } - else if (result != Z_MEM_ERROR) - { - z.Message = "incomplete literal/length tree"; - result = Z_DATA_ERROR; - } - return result; + } } - - // build distance tree - initWorkArea(288); - result = huft_build(c, nl, nd, 0, cpdist, cpdext, td, bd, hp, hn, v); - - if (result != Z_OK || (bd[0] == 0 && nl > 257)) + z = 1 << j; // table entries for j-bit table + + // allocate new table + if (hn[0] + z > MANY) { - if (result == Z_DATA_ERROR) - { - z.Message = "oversubscribed distance tree"; - } - else if (result == Z_BUF_ERROR) - { - z.Message = "incomplete distance tree"; - result = Z_DATA_ERROR; - } - else if (result != Z_MEM_ERROR) - { - z.Message = "empty distance tree with lengths"; - result = Z_DATA_ERROR; - } - return result; + // (note: doesn't matter for fixed) + return Z_DATA_ERROR; // overflow of MANY } - - return Z_OK; - } - - internal static int inflate_trees_fixed(int[] bl, int[] bd, int[][] tl, int[][] td, ZlibCodec z) - { - bl[0] = fixed_bl; - bd[0] = fixed_bd; - tl[0] = fixed_tl; - td[0] = fixed_td; - return Z_OK; - } - - private void initWorkArea(int vsize) - { - if (hn == null) + u[h] = q = hn[0]; // DEBUG + hn[0] += z; + + // connect to last table, if there is one + if (h != 0) { - hn = new int[1]; - v = new int[vsize]; - c = new int[BMAX + 1]; - r = new int[3]; - u = new int[BMAX]; - x = new int[BMAX + 1]; + x[h] = i; // save pattern for backing up + r[0] = (sbyte)j; // bits in this table + r[1] = (sbyte)l; // bits to dump before this table + j = SharedUtils.URShift(i, (w - l)); + r[2] = (int)(q - u[h - 1] - j); // offset to this table + Array.Copy(r, 0, hp, (u[h - 1] + j) * 3, 3); // connect to last table } else { - if (v.Length < vsize) - { - v = new int[vsize]; - } - Array.Clear(v,0,vsize); - Array.Clear(c,0,BMAX+1); - r[0]=0; r[1]=0; r[2]=0; - // for(int i=0; i= n) + { + r[0] = 128 + 64; // out of values--invalid code + } + else if (v[p] < s) + { + r[0] = (sbyte)(v[p] < 256 ? 0 : 32 + 64); // 256 is end-of-block + r[2] = v[p++]; // simple code is just the value + } + else + { + r[0] = (sbyte)(e[v[p] - s] + 16 + 64); // non-simple--look up in lists + r[2] = d[v[p++] - s]; + } + + // fill code-like entries with r + f = 1 << (k - w); + for (j = SharedUtils.URShift(i, w); j < z; j += f) + { + Array.Copy(r, 0, hp, (q + j) * 3, 3); + } + + // backwards increment the k-bit code i + for (j = 1 << (k - 1); (i & j) != 0; j = SharedUtils.URShift(j, 1)) + { + i ^= j; + } + i ^= j; + + // backup over finished tables + mask = (1 << w) - 1; // needed on HP, cc -O bug + while ((i & mask) != x[h]) + { + h--; // don't need to update q + w -= l; + mask = (1 << w) - 1; + } } + } + // Return Z_BUF_ERROR if we were given an incomplete table + return y != 0 && g != 1 ? Z_BUF_ERROR : Z_OK; } + + internal int inflate_trees_bits(int[] c, int[] bb, int[] tb, int[] hp, ZlibCodec z) + { + int result; + initWorkArea(19); + hn[0] = 0; + result = huft_build(c, 0, 19, 19, null, null, tb, bb, hp, hn, v); + + if (result == Z_DATA_ERROR) + { + z.Message = "oversubscribed dynamic bit lengths tree"; + } + else if (result == Z_BUF_ERROR || bb[0] == 0) + { + z.Message = "incomplete dynamic bit lengths tree"; + result = Z_DATA_ERROR; + } + return result; + } + + internal int inflate_trees_dynamic(int nl, int nd, int[] c, int[] bl, int[] bd, int[] tl, int[] td, int[] hp, ZlibCodec z) + { + int result; + + // build literal/length tree + initWorkArea(288); + hn[0] = 0; + result = huft_build(c, 0, nl, 257, cplens, cplext, tl, bl, hp, hn, v); + if (result != Z_OK || bl[0] == 0) + { + if (result == Z_DATA_ERROR) + { + z.Message = "oversubscribed literal/length tree"; + } + else if (result != Z_MEM_ERROR) + { + z.Message = "incomplete literal/length tree"; + result = Z_DATA_ERROR; + } + return result; + } + + // build distance tree + initWorkArea(288); + result = huft_build(c, nl, nd, 0, cpdist, cpdext, td, bd, hp, hn, v); + + if (result != Z_OK || (bd[0] == 0 && nl > 257)) + { + if (result == Z_DATA_ERROR) + { + z.Message = "oversubscribed distance tree"; + } + else if (result == Z_BUF_ERROR) + { + z.Message = "incomplete distance tree"; + result = Z_DATA_ERROR; + } + else if (result != Z_MEM_ERROR) + { + z.Message = "empty distance tree with lengths"; + result = Z_DATA_ERROR; + } + return result; + } + + return Z_OK; + } + + internal static int inflate_trees_fixed(int[] bl, int[] bd, int[][] tl, int[][] td, ZlibCodec z) + { + bl[0] = fixed_bl; + bd[0] = fixed_bd; + tl[0] = fixed_tl; + td[0] = fixed_td; + return Z_OK; + } + + private void initWorkArea(int vsize) + { + if (hn == null) + { + hn = new int[1]; + v = new int[vsize]; + c = new int[BMAX + 1]; + r = new int[3]; + u = new int[BMAX]; + x = new int[BMAX + 1]; + } + else + { + if (v.Length < vsize) + { + v = new int[vsize]; + } + Array.Clear(v, 0, vsize); + Array.Clear(c, 0, BMAX + 1); + r[0] = 0; r[1] = 0; r[2] = 0; + // for(int i=0; i>16) & 0xffff) != (b & 0xffff)) + if ((((~b) >> 16) & 0xffff) != (b & 0xffff)) { mode = InflateBlockMode.BAD; _codec.Message = "invalid stored block lengths"; @@ -533,7 +533,7 @@ namespace Compress.ZipFile.ZLib return Flush(r); } - c = (c == 16) ? blens[i-1] : 0; + c = (c == 16) ? blens[i - 1] : 0; do { blens[i++] = c; @@ -679,9 +679,9 @@ namespace Compress.ZipFile.ZLib { int nBytes; - for (int pass=0; pass < 2; pass++) + for (int pass = 0; pass < 2; pass++) { - if (pass==0) + if (pass == 0) { // compute number of bytes to copy as far as end of window nBytes = (int)((readAt <= writeAt ? writeAt : end) - readAt); @@ -752,15 +752,15 @@ namespace Compress.ZipFile.ZLib // waiting for "i:"=input, // "o:"=output, // "x:"=nothing - private const int START = 0; // x: set up for LEN - private const int LEN = 1; // i: get length/literal/eob next - private const int LENEXT = 2; // i: getting length extra (have base) - private const int DIST = 3; // i: get distance next + private const int START = 0; // x: set up for LEN + private const int LEN = 1; // i: get length/literal/eob next + private const int LENEXT = 2; // i: getting length extra (have base) + private const int DIST = 3; // i: get distance next private const int DISTEXT = 4; // i: getting distance extra - private const int COPY = 5; // o: copying bytes in window, waiting for space - private const int LIT = 6; // o: got literal, waiting for output space - private const int WASH = 7; // o: got eob, possibly still output waiting - private const int END = 8; // x: got eob and all data flushed + private const int COPY = 5; // o: copying bytes in window, waiting for space + private const int LIT = 6; // o: got literal, waiting for output space + private const int WASH = 7; // o: got eob, possibly still output waiting + private const int END = 8; // x: got eob and all data flushed private const int BADCODE = 9; // x: got error internal int mode; // current inflate_codes mode @@ -1413,19 +1413,19 @@ namespace Compress.ZipFile.ZLib private enum InflateManagerMode { METHOD = 0, // waiting for method byte - FLAG = 1, // waiting for flag byte - DICT4 = 2, // four dictionary check bytes to go - DICT3 = 3, // three dictionary check bytes to go - DICT2 = 4, // two dictionary check bytes to go - DICT1 = 5, // one dictionary check byte to go - DICT0 = 6, // waiting for inflateSetDictionary + FLAG = 1, // waiting for flag byte + DICT4 = 2, // four dictionary check bytes to go + DICT3 = 3, // three dictionary check bytes to go + DICT2 = 4, // two dictionary check bytes to go + DICT1 = 5, // one dictionary check byte to go + DICT0 = 6, // waiting for inflateSetDictionary BLOCKS = 7, // decompressing blocks CHECK4 = 8, // four check bytes to go CHECK3 = 9, // three check bytes to go CHECK2 = 10, // two check bytes to go CHECK1 = 11, // one check byte to go - DONE = 12, // finished check, done - BAD = 13, // got an error--stay here + DONE = 12, // finished check, done + BAD = 13, // got an error--stay here } private InflateManagerMode mode; // current inflate mode @@ -1518,9 +1518,9 @@ namespace Compress.ZipFile.ZLib if (_codec.InputBuffer == null) throw new ZlibException("InputBuffer is null. "); -// int f = (flush == FlushType.Finish) -// ? ZlibConstants.Z_BUF_ERROR -// : ZlibConstants.Z_OK; + // int f = (flush == FlushType.Finish) + // ? ZlibConstants.Z_BUF_ERROR + // : ZlibConstants.Z_OK; // workitem 8870 int f = ZlibConstants.Z_OK; diff --git a/SabreTools.Library/External/Compress/ZipFile/ZLib/Tree.cs b/SabreTools.Library/External/Compress/ZipFile/ZLib/Tree.cs index 98441bd7..b639b67f 100644 --- a/SabreTools.Library/External/Compress/ZipFile/ZLib/Tree.cs +++ b/SabreTools.Library/External/Compress/ZipFile/ZLib/Tree.cs @@ -1,20 +1,20 @@ // Tree.cs // ------------------------------------------------------------------ // -// Copyright (c) 2009 Dino Chiesa and Microsoft Corporation. +// Copyright (c) 2009 Dino Chiesa and Microsoft Corporation. // All rights reserved. // // This code module is part of DotNetZip, a zipfile class library. // // ------------------------------------------------------------------ // -// This code is licensed under the Microsoft Public License. +// This code is licensed under the Microsoft Public License. // See the file License.txt for the license details. // More info on: http://dotnetzip.codeplex.com // // ------------------------------------------------------------------ // -// last saved (in emacs): +// last saved (in emacs): // Time-stamp: <2009-October-28 13:29:50> // // ------------------------------------------------------------------ @@ -25,22 +25,22 @@ // code is below. // // ------------------------------------------------------------------ -// +// // Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved. -// +// // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: -// +// // 1. Redistributions of source code must retain the above copyright notice, // this list of conditions and the following disclaimer. -// -// 2. Redistributions in binary form must reproduce the above copyright -// notice, this list of conditions and the following disclaimer in +// +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in // the documentation and/or other materials provided with the distribution. -// +// // 3. The names of the authors may not be used to endorse or promote products // derived from this software without specific prior written permission. -// +// // THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND // FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT, @@ -51,7 +51,7 @@ // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// +// // ----------------------------------------------------------------------- // // This program is based on zlib-1.1.3; credit to authors @@ -66,72 +66,72 @@ namespace Compress.ZipFile.ZLib sealed class Tree { private static readonly int HEAP_SIZE = (2 * InternalConstants.L_CODES + 1); - + // extra bits for each length code internal static readonly int[] ExtraLengthBits = new int[] { 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, 0 }; - + // extra bits for each distance code internal static readonly int[] ExtraDistanceBits = new int[] { 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 }; - + // extra bits for each bit length code - internal static readonly int[] extra_blbits = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 7}; - - internal static readonly sbyte[] bl_order = new sbyte[]{16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; - - + internal static readonly int[] extra_blbits = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 7 }; + + internal static readonly sbyte[] bl_order = new sbyte[] { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 }; + + // The lengths of the bit length codes are sent in order of decreasing // probability, to avoid transmitting the lengths for unused bit // length codes. - + internal const int Buf_size = 8 * 2; - + // see definition of array dist_code below //internal const int DIST_CODE_LEN = 512; - + private static readonly sbyte[] _dist_code = new sbyte[] { - 0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, + 0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, - 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, - 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, - 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, - 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, - 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, - 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, - 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, - 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, - 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, - 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, - 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, - 0, 0, 16, 17, 18, 18, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, - 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23, - 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, - 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, - 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, - 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, - 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, - 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, - 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, - 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, - 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, - 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, - 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, - 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, - 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, + 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, + 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, + 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, + 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, + 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, + 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, + 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, + 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, + 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, + 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, + 0, 0, 16, 17, 18, 18, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, + 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23, + 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, + 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, + 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, + 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, + 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, + 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, + 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, + 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, + 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, + 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, + 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, + 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, + 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29 }; - + internal static readonly sbyte[] LengthCode = new sbyte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 9, 10, 10, 11, 11, @@ -151,14 +151,14 @@ namespace Compress.ZipFile.ZLib 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28 }; - + internal static readonly int[] LengthBase = new int[] { 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, 0 }; - + internal static readonly int[] DistanceBase = new int[] { @@ -166,11 +166,11 @@ namespace Compress.ZipFile.ZLib 256, 384, 512, 768, 1024, 1536, 2048, 3072, 4096, 6144, 8192, 12288, 16384, 24576 }; - + /// /// Map from a distance to a distance code. /// - /// + /// /// No side effects. _dist_code[256] and _dist_code[257] are never used. /// internal static int DistanceCode(int dist) @@ -183,7 +183,7 @@ namespace Compress.ZipFile.ZLib internal short[] dyn_tree; // the dynamic tree internal int max_code; // largest code with non zero frequency internal StaticTree staticTree; // the corresponding static tree - + // Compute the optimal bit lengths for a tree and update the total bit length // for the current block. // IN assertion: the fields freq and dad are set, heap[heap_max] and @@ -192,7 +192,7 @@ namespace Compress.ZipFile.ZLib // array bl_count contains the frequencies for each bit length. // The length opt_len is updated; static_len is also updated if stree is // not null. - internal void gen_bitlen(DeflateManager s) + internal void gen_bitlen(DeflateManager s) { short[] tree = dyn_tree; short[] stree = staticTree.treeCodes; @@ -205,14 +205,14 @@ namespace Compress.ZipFile.ZLib int xbits; // extra bits short f; // frequency int overflow = 0; // number of elements with bit length too large - + for (bits = 0; bits <= InternalConstants.MAX_BITS; bits++) s.bl_count[bits] = 0; - + // In a first pass, compute the optimal bit lengths (which may // overflow in the case of the bit length tree). tree[s.heap[s.heap_max] * 2 + 1] = 0; // root of the heap - + for (h = s.heap_max + 1; h < HEAP_SIZE; h++) { n = s.heap[h]; @@ -221,12 +221,12 @@ namespace Compress.ZipFile.ZLib { bits = max_length; overflow++; } - tree[n * 2 + 1] = (short) bits; + tree[n * 2 + 1] = (short)bits; // We overwrite tree[n*2+1] which is no longer needed - + if (n > max_code) continue; // not a leaf node - + s.bl_count[bits]++; xbits = 0; if (n >= base_Renamed) @@ -237,24 +237,24 @@ namespace Compress.ZipFile.ZLib s.static_len += f * (stree[n * 2 + 1] + xbits); } if (overflow == 0) - return ; - + return; + // This happens for example on obj2 and pic of the Calgary corpus // Find the first bit length which could increase: - do + do { bits = max_length - 1; while (s.bl_count[bits] == 0) bits--; s.bl_count[bits]--; // move one leaf down the tree - s.bl_count[bits + 1] = (short) (s.bl_count[bits + 1] + 2); // move one overflow item as its brother + s.bl_count[bits + 1] = (short)(s.bl_count[bits + 1] + 2); // move one overflow item as its brother s.bl_count[max_length]--; // The brother of the overflow item also moves one step up, // but this does not affect bl_count[max_length] overflow -= 2; } while (overflow > 0); - + for (bits = max_length; bits != 0; bits--) { n = s.bl_count[bits]; @@ -265,35 +265,35 @@ namespace Compress.ZipFile.ZLib continue; if (tree[m * 2 + 1] != bits) { - s.opt_len = (int) (s.opt_len + ((long) bits - (long) tree[m * 2 + 1]) * (long) tree[m * 2]); - tree[m * 2 + 1] = (short) bits; + s.opt_len = (int)(s.opt_len + ((long)bits - (long)tree[m * 2 + 1]) * (long)tree[m * 2]); + tree[m * 2 + 1] = (short)bits; } n--; } } } - + // Construct one Huffman tree and assigns the code bit strings and lengths. // Update the total bit length for the current block. // IN assertion: the field freq is set for all tree elements. // OUT assertions: the fields len and code are set to the optimal bit length // and corresponding code. The length opt_len is updated; static_len is // also updated if stree is not null. The field max_code is set. - internal void build_tree(DeflateManager s) + internal void build_tree(DeflateManager s) { - short[] tree = dyn_tree; + short[] tree = dyn_tree; short[] stree = staticTree.treeCodes; - int elems = staticTree.elems; + int elems = staticTree.elems; int n, m; // iterate over heap elements - int max_code = -1; // largest code with non zero frequency + int max_code = -1; // largest code with non zero frequency int node; // new node being created - + // Construct the initial heap, with least frequent element in // heap[1]. The sons of heap[n] are heap[2*n] and heap[2*n+1]. // heap[0] is not used. s.heap_len = 0; s.heap_max = HEAP_SIZE; - + for (n = 0; n < elems; n++) { if (tree[n * 2] != 0) @@ -306,14 +306,14 @@ namespace Compress.ZipFile.ZLib tree[n * 2 + 1] = 0; } } - + // The pkzip format requires that at least one distance code exists, // and that at least one bit should be sent even if there is only one // possible code. So to avoid special checks later on we force at least // two codes of non zero frequency. while (s.heap_len < 2) { - node = s.heap[++s.heap_len] = (max_code < 2?++max_code:0); + node = s.heap[++s.heap_len] = (max_code < 2 ? ++max_code : 0); tree[node * 2] = 1; s.depth[node] = 0; s.opt_len--; @@ -322,93 +322,94 @@ namespace Compress.ZipFile.ZLib // node is 0 or 1 so it does not have extra bits } this.max_code = max_code; - + // The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree, // establish sub-heaps of increasing lengths: - + for (n = s.heap_len / 2; n >= 1; n--) s.pqdownheap(tree, n); - + // Construct the Huffman tree by repeatedly combining the least two // frequent nodes. - + node = elems; // next internal node of the tree - do + do { // n = node of least frequency n = s.heap[1]; s.heap[1] = s.heap[s.heap_len--]; s.pqdownheap(tree, 1); m = s.heap[1]; // m = node of next least frequency - + s.heap[--s.heap_max] = n; // keep the nodes sorted by frequency s.heap[--s.heap_max] = m; - + // Create a new node father of n and m - tree[node * 2] = unchecked((short) (tree[n * 2] + tree[m * 2])); - s.depth[node] = (sbyte) (System.Math.Max((byte) s.depth[n], (byte) s.depth[m]) + 1); - tree[n * 2 + 1] = tree[m * 2 + 1] = (short) node; - + tree[node * 2] = unchecked((short)(tree[n * 2] + tree[m * 2])); + s.depth[node] = (sbyte)(System.Math.Max((byte)s.depth[n], (byte)s.depth[m]) + 1); + tree[n * 2 + 1] = tree[m * 2 + 1] = (short)node; + // and insert the new node in the heap s.heap[1] = node++; s.pqdownheap(tree, 1); } while (s.heap_len >= 2); - + s.heap[--s.heap_max] = s.heap[1]; - + // At this point, the fields freq and dad are set. We can now // generate the bit lengths. - + gen_bitlen(s); - + // The field len is now set, we can generate the bit codes gen_codes(tree, max_code, s.bl_count); } - + // Generate the codes for a given tree and bit counts (which need not be // optimal). // IN assertion: the array bl_count contains the bit length statistics for // the given tree and the field len is set for all tree elements. // OUT assertion: the field code is set for all tree elements of non // zero code length. - internal static void gen_codes(short[] tree, int max_code, short[] bl_count) + internal static void gen_codes(short[] tree, int max_code, short[] bl_count) { short[] next_code = new short[InternalConstants.MAX_BITS + 1]; // next code value for each bit length short code = 0; // running code value int bits; // bit index int n; // code index - + // The distribution counts are first used to generate the code values // without bit reversal. for (bits = 1; bits <= InternalConstants.MAX_BITS; bits++) - unchecked { - next_code[bits] = code = (short) ((code + bl_count[bits - 1]) << 1); + unchecked + { + next_code[bits] = code = (short)((code + bl_count[bits - 1]) << 1); } - + // Check that the bit counts in bl_count are consistent. The last code // must be all ones. //Assert (code + bl_count[MAX_BITS]-1 == (1<>= 1; //SharedUtils.URShift(code, 1); diff --git a/SabreTools.Library/External/Compress/ZipFile/ZLib/Zlib.cs b/SabreTools.Library/External/Compress/ZipFile/ZLib/Zlib.cs index 368a0455..f2da1fd9 100644 --- a/SabreTools.Library/External/Compress/ZipFile/ZLib/Zlib.cs +++ b/SabreTools.Library/External/Compress/ZipFile/ZLib/Zlib.cs @@ -145,7 +145,7 @@ namespace Compress.ZipFile.ZLib /// If you are producing ZIPs for use on Mac OSX, be aware that archives produced with CompressionLevel.None /// cannot be opened with the default zip reader. Use a different CompressionLevel. /// - None= 0, + None = 0, /// /// Same as None. /// @@ -249,7 +249,7 @@ namespace Compress.ZipFile.ZLib /// /// Used to specify that the stream should compress the data. /// - Compress= 0, + Compress = 0, /// /// Used to specify that the stream should decompress the data. /// @@ -299,24 +299,24 @@ namespace Compress.ZipFile.ZLib internal static class InternalConstants { - internal static readonly int MAX_BITS = 15; - internal static readonly int BL_CODES = 19; - internal static readonly int D_CODES = 30; - internal static readonly int LITERALS = 256; + internal static readonly int MAX_BITS = 15; + internal static readonly int BL_CODES = 19; + internal static readonly int D_CODES = 30; + internal static readonly int LITERALS = 256; internal static readonly int LENGTH_CODES = 29; - internal static readonly int L_CODES = (LITERALS + 1 + LENGTH_CODES); + internal static readonly int L_CODES = (LITERALS + 1 + LENGTH_CODES); // Bit length codes must not exceed MAX_BL_BITS bits - internal static readonly int MAX_BL_BITS = 7; + internal static readonly int MAX_BL_BITS = 7; // repeat previous bit length 3-6 times (2 bits of repeat count) - internal static readonly int REP_3_6 = 16; + internal static readonly int REP_3_6 = 16; // repeat a zero length 3-10 times (3 bits of repeat count) - internal static readonly int REPZ_3_10 = 17; + internal static readonly int REPZ_3_10 = 17; // repeat a zero length 11-138 times (7 bits of repeat count) - internal static readonly int REPZ_11_138 = 18; + internal static readonly int REPZ_11_138 = 18; } @@ -433,8 +433,8 @@ namespace Compress.ZipFile.ZLib if (buf == null) return 1; - uint s1 = (uint) (adler & 0xffff); - uint s2 = (uint) ((adler >> 16) & 0xffff); + uint s1 = (uint)(adler & 0xffff); + uint s2 = (uint)((adler >> 16) & 0xffff); while (len > 0) { diff --git a/SabreTools.Library/External/Compress/ZipFile/ZLib/ZlibBaseStream.cs b/SabreTools.Library/External/Compress/ZipFile/ZLib/ZlibBaseStream.cs index bb48fcb6..8d80b07e 100644 --- a/SabreTools.Library/External/Compress/ZipFile/ZLib/ZlibBaseStream.cs +++ b/SabreTools.Library/External/Compress/ZipFile/ZLib/ZlibBaseStream.cs @@ -554,4 +554,4 @@ namespace Compress.ZipFile.ZLib } -} +} \ No newline at end of file diff --git a/SabreTools.Library/External/Compress/ZipFile/ZLib/ZlibCodec.cs b/SabreTools.Library/External/Compress/ZipFile/ZLib/ZlibCodec.cs index 42bf00f7..29342aea 100644 --- a/SabreTools.Library/External/Compress/ZipFile/ZLib/ZlibCodec.cs +++ b/SabreTools.Library/External/Compress/ZipFile/ZLib/ZlibCodec.cs @@ -1,20 +1,20 @@ // ZlibCodec.cs // ------------------------------------------------------------------ // -// Copyright (c) 2009 Dino Chiesa and Microsoft Corporation. +// Copyright (c) 2009 Dino Chiesa and Microsoft Corporation. // All rights reserved. // // This code module is part of DotNetZip, a zipfile class library. // // ------------------------------------------------------------------ // -// This code is licensed under the Microsoft Public License. +// This code is licensed under the Microsoft Public License. // See the file License.txt for the license details. // More info on: http://dotnetzip.codeplex.com // // ------------------------------------------------------------------ // -// last saved (in emacs): +// last saved (in emacs): // Time-stamp: <2009-November-03 15:40:51> // // ------------------------------------------------------------------ @@ -28,22 +28,22 @@ // is included below. // // ------------------------------------------------------------------ -// +// // Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved. -// +// // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: -// +// // 1. Redistributions of source code must retain the above copyright notice, // this list of conditions and the following disclaimer. -// -// 2. Redistributions in binary form must reproduce the above copyright -// notice, this list of conditions and the following disclaimer in +// +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in // the documentation and/or other materials provided with the distribution. -// +// // 3. The names of the authors may not be used to endorse or promote products // derived from this software without specific prior written permission. -// +// // THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND // FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT, @@ -54,7 +54,7 @@ // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// +// // ----------------------------------------------------------------------- // // This program is based on zlib-1.1.3; credit to authors @@ -66,7 +66,7 @@ using System; using System.Runtime.InteropServices; -using Interop=System.Runtime.InteropServices; +using Interop = System.Runtime.InteropServices; namespace Compress.ZipFile.ZLib { @@ -82,7 +82,7 @@ namespace Compress.ZipFile.ZLib /// [Guid("ebc25cf6-9120-4283-b972-0e5520d0000D")] [Interop.ComVisible(true)] -#if !NETCF +#if !NETCF [Interop.ClassInterface(Interop.ClassInterfaceType.AutoDispatch)] #endif sealed public class ZlibCodec @@ -93,15 +93,15 @@ namespace Compress.ZipFile.ZLib public byte[] InputBuffer; /// - /// An index into the InputBuffer array, indicating where to start reading. + /// An index into the InputBuffer array, indicating where to start reading. /// public int NextIn; /// - /// The number of bytes available in the InputBuffer, starting at NextIn. + /// The number of bytes available in the InputBuffer, starting at NextIn. /// /// - /// Generally you should set this to InputBuffer.Length before the first Inflate() or Deflate() call. + /// Generally you should set this to InputBuffer.Length before the first Inflate() or Deflate() call. /// The class will update this number as calls to Inflate/Deflate are made. /// public int AvailableBytesIn; @@ -117,15 +117,15 @@ namespace Compress.ZipFile.ZLib public byte[] OutputBuffer; /// - /// An index into the OutputBuffer array, indicating where to start writing. + /// An index into the OutputBuffer array, indicating where to start writing. /// public int NextOut; /// - /// The number of bytes available in the OutputBuffer, starting at NextOut. + /// The number of bytes available in the OutputBuffer, starting at NextOut. /// /// - /// Generally you should set this to OutputBuffer.Length before the first Inflate() or Deflate() call. + /// Generally you should set this to OutputBuffer.Length before the first Inflate() or Deflate() call. /// The class will update this number as calls to Inflate/Deflate are made. /// public int AvailableBytesOut; @@ -151,13 +151,13 @@ namespace Compress.ZipFile.ZLib public CompressionLevel CompressLevel = CompressionLevel.Default; /// - /// The number of Window Bits to use. + /// The number of Window Bits to use. /// /// - /// This gauges the size of the sliding window, and hence the - /// compression effectiveness as well as memory consumption. It's best to just leave this + /// This gauges the size of the sliding window, and hence the + /// compression effectiveness as well as memory consumption. It's best to just leave this /// setting alone if you don't know what it is. The maximum value is 15 bits, which implies - /// a 32k window. + /// a 32k window. /// public int WindowBits = ZlibConstants.WindowBitsDefault; @@ -187,9 +187,9 @@ namespace Compress.ZipFile.ZLib /// Create a ZlibCodec. /// /// - /// If you use this default constructor, you will later have to explicitly call - /// InitializeInflate() or InitializeDeflate() before using the ZlibCodec to compress - /// or decompress. + /// If you use this default constructor, you will later have to explicitly call + /// InitializeInflate() or InitializeDeflate() before using the ZlibCodec to compress + /// or decompress. /// public ZlibCodec() { } @@ -215,10 +215,10 @@ namespace Compress.ZipFile.ZLib } /// - /// Initialize the inflation state. + /// Initialize the inflation state. /// /// - /// It is not necessary to call this before using the ZlibCodec to inflate data; + /// It is not necessary to call this before using the ZlibCodec to inflate data; /// It is implicitly called when you call the constructor. /// /// Z_OK if everything goes well. @@ -251,20 +251,20 @@ namespace Compress.ZipFile.ZLib } /// - /// Initialize the ZlibCodec for inflation, with the specified number of window bits. + /// Initialize the ZlibCodec for inflation, with the specified number of window bits. /// - /// The number of window bits to use. If you need to ask what that is, + /// The number of window bits to use. If you need to ask what that is, /// then you shouldn't be calling this initializer. /// Z_OK if all goes well. public int InitializeInflate(int windowBits) { - this.WindowBits = windowBits; + this.WindowBits = windowBits; return InitializeInflate(windowBits, true); } /// /// Initialize the inflation state with an explicit flag to govern the handling of - /// RFC1950 header bytes. + /// RFC1950 header bytes. /// /// /// @@ -276,9 +276,9 @@ namespace Compress.ZipFile.ZLib /// false. /// /// - /// whether to expect an RFC1950 header byte pair when reading + /// whether to expect an RFC1950 header byte pair when reading /// the stream of data to be inflated. - /// The number of window bits to use. If you need to ask what that is, + /// The number of window bits to use. If you need to ask what that is, /// then you shouldn't be calling this initializer. /// Z_OK if everything goes well. public int InitializeInflate(int windowBits, bool expectRfc1950Header) @@ -293,7 +293,7 @@ namespace Compress.ZipFile.ZLib /// Inflate the data in the InputBuffer, placing the result in the OutputBuffer. /// /// - /// You must have set InputBuffer and OutputBuffer, NextIn and NextOut, and AvailableBytesIn and + /// You must have set InputBuffer and OutputBuffer, NextIn and NextOut, and AvailableBytesIn and /// AvailableBytesOut before calling this method. /// /// @@ -303,48 +303,48 @@ namespace Compress.ZipFile.ZLib /// int bufferSize = 1024; /// byte[] buffer = new byte[bufferSize]; /// ZlibCodec decompressor = new ZlibCodec(); - /// + /// /// Console.WriteLine("\n============================================"); /// Console.WriteLine("Size of Buffer to Inflate: {0} bytes.", CompressedBytes.Length); /// MemoryStream ms = new MemoryStream(DecompressedBytes); - /// + /// /// int rc = decompressor.InitializeInflate(); - /// + /// /// decompressor.InputBuffer = CompressedBytes; /// decompressor.NextIn = 0; /// decompressor.AvailableBytesIn = CompressedBytes.Length; - /// + /// /// decompressor.OutputBuffer = buffer; - /// - /// // pass 1: inflate + /// + /// // pass 1: inflate /// do /// { /// decompressor.NextOut = 0; /// decompressor.AvailableBytesOut = buffer.Length; /// rc = decompressor.Inflate(FlushType.None); - /// + /// /// if (rc != ZlibConstants.Z_OK && rc != ZlibConstants.Z_STREAM_END) /// throw new Exception("inflating: " + decompressor.Message); - /// + /// /// ms.Write(decompressor.OutputBuffer, 0, buffer.Length - decompressor.AvailableBytesOut); /// } /// while (decompressor.AvailableBytesIn > 0 || decompressor.AvailableBytesOut == 0); - /// + /// /// // pass 2: finish and flush /// do /// { /// decompressor.NextOut = 0; /// decompressor.AvailableBytesOut = buffer.Length; /// rc = decompressor.Inflate(FlushType.Finish); - /// + /// /// if (rc != ZlibConstants.Z_STREAM_END && rc != ZlibConstants.Z_OK) /// throw new Exception("inflating: " + decompressor.Message); - /// + /// /// if (buffer.Length - decompressor.AvailableBytesOut > 0) /// ms.Write(buffer, 0, buffer.Length - decompressor.AvailableBytesOut); /// } /// while (decompressor.AvailableBytesIn > 0 || decompressor.AvailableBytesOut == 0); - /// + /// /// decompressor.EndInflate(); /// } /// @@ -361,10 +361,10 @@ namespace Compress.ZipFile.ZLib /// - /// Ends an inflation session. + /// Ends an inflation session. /// /// - /// Call this after successively calling Inflate(). This will cause all buffers to be flushed. + /// Call this after successively calling Inflate(). This will cause all buffers to be flushed. /// After calling this you cannot call Inflate() without a intervening call to one of the /// InitializeInflate() overloads. /// @@ -400,32 +400,32 @@ namespace Compress.ZipFile.ZLib /// int bufferSize = 40000; /// byte[] CompressedBytes = new byte[bufferSize]; /// byte[] DecompressedBytes = new byte[bufferSize]; - /// + /// /// ZlibCodec compressor = new ZlibCodec(); - /// + /// /// compressor.InitializeDeflate(CompressionLevel.Default); - /// + /// /// compressor.InputBuffer = System.Text.ASCIIEncoding.ASCII.GetBytes(TextToCompress); /// compressor.NextIn = 0; /// compressor.AvailableBytesIn = compressor.InputBuffer.Length; - /// + /// /// compressor.OutputBuffer = CompressedBytes; /// compressor.NextOut = 0; /// compressor.AvailableBytesOut = CompressedBytes.Length; - /// + /// /// while (compressor.TotalBytesIn != TextToCompress.Length && compressor.TotalBytesOut < bufferSize) /// { /// compressor.Deflate(FlushType.None); /// } - /// + /// /// while (true) /// { /// int rc= compressor.Deflate(FlushType.Finish); /// if (rc == ZlibConstants.Z_STREAM_END) break; /// } - /// + /// /// compressor.EndDeflate(); - /// + /// /// /// /// Z_OK if all goes well. You generally don't need to check the return code. @@ -451,7 +451,7 @@ namespace Compress.ZipFile.ZLib /// - /// Initialize the ZlibCodec for deflation operation, using the specified CompressionLevel, + /// Initialize the ZlibCodec for deflation operation, using the specified CompressionLevel, /// and the explicit flag governing whether to emit an RFC1950 header byte pair. /// /// @@ -459,7 +459,7 @@ namespace Compress.ZipFile.ZLib /// If you want to generate a zlib stream, you should specify true for /// wantRfc1950Header. In this case, the library will emit a ZLIB /// header, as defined in RFC - /// 1950, in the compressed stream. + /// 1950, in the compressed stream. /// /// The compression level for the codec. /// whether to emit an initial RFC1950 byte pair in the compressed stream. @@ -472,8 +472,8 @@ namespace Compress.ZipFile.ZLib /// - /// Initialize the ZlibCodec for deflation operation, using the specified CompressionLevel, - /// and the specified number of window bits. + /// Initialize the ZlibCodec for deflation operation, using the specified CompressionLevel, + /// and the specified number of window bits. /// /// /// The codec will use the specified number of window bits and the specified CompressionLevel. @@ -527,59 +527,59 @@ namespace Compress.ZipFile.ZLib /// int bufferSize = 1024; /// byte[] buffer = new byte[bufferSize]; /// ZlibCodec compressor = new ZlibCodec(); - /// + /// /// Console.WriteLine("\n============================================"); /// Console.WriteLine("Size of Buffer to Deflate: {0} bytes.", UncompressedBytes.Length); /// MemoryStream ms = new MemoryStream(); - /// + /// /// int rc = compressor.InitializeDeflate(level); - /// + /// /// compressor.InputBuffer = UncompressedBytes; /// compressor.NextIn = 0; /// compressor.AvailableBytesIn = UncompressedBytes.Length; - /// + /// /// compressor.OutputBuffer = buffer; - /// - /// // pass 1: deflate + /// + /// // pass 1: deflate /// do /// { /// compressor.NextOut = 0; /// compressor.AvailableBytesOut = buffer.Length; /// rc = compressor.Deflate(FlushType.None); - /// + /// /// if (rc != ZlibConstants.Z_OK && rc != ZlibConstants.Z_STREAM_END) /// throw new Exception("deflating: " + compressor.Message); - /// + /// /// ms.Write(compressor.OutputBuffer, 0, buffer.Length - compressor.AvailableBytesOut); /// } /// while (compressor.AvailableBytesIn > 0 || compressor.AvailableBytesOut == 0); - /// + /// /// // pass 2: finish and flush /// do /// { /// compressor.NextOut = 0; /// compressor.AvailableBytesOut = buffer.Length; /// rc = compressor.Deflate(FlushType.Finish); - /// + /// /// if (rc != ZlibConstants.Z_STREAM_END && rc != ZlibConstants.Z_OK) /// throw new Exception("deflating: " + compressor.Message); - /// + /// /// if (buffer.Length - compressor.AvailableBytesOut > 0) /// ms.Write(buffer, 0, buffer.Length - compressor.AvailableBytesOut); /// } /// while (compressor.AvailableBytesIn > 0 || compressor.AvailableBytesOut == 0); - /// + /// /// compressor.EndDeflate(); - /// + /// /// ms.Seek(0, SeekOrigin.Begin); /// CompressedBytes = new byte[compressor.TotalBytesOut]; /// ms.Read(CompressedBytes, 0, CompressedBytes.Length); /// } /// /// - /// whether to flush all data as you deflate. Generally you will want to - /// use Z_NO_FLUSH here, in a series of calls to Deflate(), and then call EndDeflate() to - /// flush everything. + /// whether to flush all data as you deflate. Generally you will want to + /// use Z_NO_FLUSH here, in a series of calls to Deflate(), and then call EndDeflate() to + /// flush everything. /// /// Z_OK if all goes well. public int Deflate(FlushType flush) @@ -677,10 +677,10 @@ namespace Compress.ZipFile.ZLib Array.Copy(dstate.pending, dstate.nextPending, OutputBuffer, NextOut, len); - NextOut += len; - dstate.nextPending += len; - TotalBytesOut += len; - AvailableBytesOut -= len; + NextOut += len; + dstate.nextPending += len; + TotalBytesOut += len; + AvailableBytesOut -= len; dstate.pendingCount -= len; if (dstate.pendingCount == 0) { diff --git a/SabreTools.Library/External/Compress/ZipFile/ZLib/ZlibConstants.cs b/SabreTools.Library/External/Compress/ZipFile/ZLib/ZlibConstants.cs index 332684aa..ec86b0a4 100644 --- a/SabreTools.Library/External/Compress/ZipFile/ZLib/ZlibConstants.cs +++ b/SabreTools.Library/External/Compress/ZipFile/ZLib/ZlibConstants.cs @@ -1,20 +1,20 @@ // ZlibConstants.cs // ------------------------------------------------------------------ // -// Copyright (c) 2009 Dino Chiesa and Microsoft Corporation. +// Copyright (c) 2009 Dino Chiesa and Microsoft Corporation. // All rights reserved. // // This code module is part of DotNetZip, a zipfile class library. // // ------------------------------------------------------------------ // -// This code is licensed under the Microsoft Public License. +// This code is licensed under the Microsoft Public License. // See the file License.txt for the license details. // More info on: http://dotnetzip.codeplex.com // // ------------------------------------------------------------------ // -// last saved (in emacs): +// last saved (in emacs): // Time-stamp: <2009-November-03 18:50:19> // // ------------------------------------------------------------------ @@ -25,22 +25,22 @@ // copyright to that code is included here. // // ------------------------------------------------------------------ -// +// // Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved. -// +// // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: -// +// // 1. Redistributions of source code must retain the above copyright notice, // this list of conditions and the following disclaimer. -// -// 2. Redistributions in binary form must reproduce the above copyright -// notice, this list of conditions and the following disclaimer in +// +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in // the documentation and/or other materials provided with the distribution. -// +// // 3. The names of the authors may not be used to endorse or promote products // derived from this software without specific prior written permission. -// +// // THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND // FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT, @@ -51,7 +51,7 @@ // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// +// // ----------------------------------------------------------------------- // // This program is based on zlib-1.1.3; credit to authors @@ -89,7 +89,7 @@ namespace Compress.ZipFile.ZLib public const int Z_STREAM_END = 1; /// - /// The operation ended in need of a dictionary. + /// The operation ended in need of a dictionary. /// public const int Z_NEED_DICT = 2; @@ -111,10 +111,10 @@ namespace Compress.ZipFile.ZLib /// /// The size of the working buffer used in the ZlibCodec class. Defaults to 8192 bytes. /// -#if NETCF +#if NETCF public const int WorkingBufferSizeDefault = 8192; #else - public const int WorkingBufferSizeDefault = 16384; + public const int WorkingBufferSizeDefault = 16384; #endif /// /// The minimum size of the working buffer used in the ZlibCodec class. Currently it is 128 bytes. @@ -123,4 +123,3 @@ namespace Compress.ZipFile.ZLib } } - diff --git a/SabreTools.Library/External/Compress/ZipFile/zipFile.cs b/SabreTools.Library/External/Compress/ZipFile/zipFile.cs index 1d7c80d7..77a5dec1 100644 --- a/SabreTools.Library/External/Compress/ZipFile/zipFile.cs +++ b/SabreTools.Library/External/Compress/ZipFile/zipFile.cs @@ -138,10 +138,6 @@ namespace Compress.ZipFile bool lTrrntzip = true; _centerDirStart = (ulong)_zipFs.Position; - if (_centerDirStart >= 0xffffffff) - { - _zip64 = true; - } using (CrcCalculatorStream crcCs = new CrcCalculatorStream(_zipFs, true)) { @@ -160,6 +156,9 @@ namespace Compress.ZipFile _fileComment = lTrrntzip ? GetBytes("TORRENTZIPPED-" + crcCs.Crc.ToString("X8")) : new byte[0]; ZipStatus = lTrrntzip ? ZipStatus.TrrntZip : ZipStatus.None; } + _zip64 |= _centerDirStart >= 0xffffffff; + _zip64 |= _centerDirSize >= 0xffffffff; + _zip64 |= _localFiles.Count >= 0xffff; if (_zip64) { @@ -218,7 +217,6 @@ namespace Compress.ZipFile return ZipReturn.ZipGood; } - // TODO: Figure out how to re-add the file time functionality to this public ZipReturn ZipFileOpenWriteStream(bool raw, bool trrntzip, string filename, ulong uncompressedSize, ushort compressionMethod, out Stream stream) { stream = null; @@ -271,9 +269,9 @@ namespace Compress.ZipFile return ZipReturn.ZipGood; } - public void ZipFileAddDirectory() + public void ZipFileAddZeroLengthFile() { - LocalFile.LocalFileAddDirectory(_zipFs); + LocalFile.LocalFileAddZeroLengthFile(_zipFs); } /* @@ -375,7 +373,7 @@ namespace Compress.ZipFile _localFilesCount = zipBr.ReadUInt16(); // TotalNumberOfEnteriesDisk - tushort = zipBr.ReadUInt16(); // TotalNumber of enteries in the central directory + tushort = zipBr.ReadUInt16(); // TotalNumber of enteries in the central directory if (tushort != _localFilesCount) { return ZipReturn.ZipEndOfCentralDirectoryError; @@ -405,7 +403,7 @@ namespace Compress.ZipFile bw.Write((ushort)0); // NumberOfThisDisk bw.Write((ushort)0); // NumberOfThisDiskCenterDir bw.Write((ushort)(_localFiles.Count >= 0xffff ? 0xffff : _localFiles.Count)); // TotalNumberOfEnteriesDisk - bw.Write((ushort)(_localFiles.Count >= 0xffff ? 0xffff : _localFiles.Count)); // TotalNumber of enteries in the central directory + bw.Write((ushort)(_localFiles.Count >= 0xffff ? 0xffff : _localFiles.Count)); // TotalNumber of enteries in the central directory bw.Write((uint)(_centerDirSize >= 0xffffffff ? 0xffffffff : _centerDirSize)); bw.Write((uint)(_centerDirStart >= 0xffffffff ? 0xffffffff : _centerDirStart)); bw.Write((ushort)_fileComment.Length); @@ -528,7 +526,7 @@ namespace Compress.ZipFile - // TODO: Figure out the timestamp instead of using ticks like we were before + public ZipReturn ZipFileOpen(string newFilename, long timestamp, bool readHeaders) { ZipFileClose(); @@ -1799,9 +1797,13 @@ namespace Compress.ZipFile if (_compressedSize == 0 && UncompressedSize == 0) { - LocalFileAddDirectory(zipFs); + LocalFileAddZeroLengthFile(zipFs); _compressedSize = (ulong)zipFs.Position - _dataLocation; } + else if (_compressedSize == 0 && UncompressedSize != 0) + { + return ZipReturn.ZipErrorWritingToOutputStream; + } CRC = crc32; WriteCompressedSize(zipFs); @@ -1814,7 +1816,7 @@ namespace Compress.ZipFile long posNow = zipFs.Position; using (BinaryWriter bw = new BinaryWriter(zipFs, Encoding.UTF8, true)) { - // _crc32Loction - 10 needs set to 45 + // _crc32Loction - 10 needs set to 45 zipFs.Seek((long)_crc32Location - 10, SeekOrigin.Begin); ushort versionNeededToExtract = 45; bw.Write(versionNeededToExtract); @@ -1892,7 +1894,7 @@ namespace Compress.ZipFile zipFs.Seek(posNow, SeekOrigin.Begin); } - public static void LocalFileAddDirectory(Stream zipFs) + public static void LocalFileAddZeroLengthFile(Stream zipFs) { zipFs.WriteByte(03); zipFs.WriteByte(00); diff --git a/SabreTools.Library/External/Compress/gZip/gZip.cs b/SabreTools.Library/External/Compress/gZip/gZip.cs index 7b5b04ff..9a94f554 100644 --- a/SabreTools.Library/External/Compress/gZip/gZip.cs +++ b/SabreTools.Library/External/Compress/gZip/gZip.cs @@ -189,7 +189,7 @@ namespace Compress.gZip uint crc16 = zipBr.ReadUInt16(); } - CompressedSize = (ulong) (_zipFs.Length - _zipFs.Position) - 8; + CompressedSize = (ulong)(_zipFs.Length - _zipFs.Position) - 8; dataStartPos = _zipFs.Position; @@ -212,7 +212,7 @@ namespace Compress.gZip } else { - CRC = new[] {gzcrc[3], gzcrc[2], gzcrc[1], gzcrc[0]}; + CRC = new[] { gzcrc[3], gzcrc[2], gzcrc[1], gzcrc[0] }; } if (UnCompressedSize != 0) @@ -272,23 +272,23 @@ namespace Compress.gZip { UnCompressedSize = unCompressedSize; - zipBw.Write((byte) 0x1f); // ID1 = 0x1f - zipBw.Write((byte) 0x8b); // ID2 = 0x8b - zipBw.Write((byte) 0x08); // CM = 0x08 - zipBw.Write((byte) 0x04); // FLG = 0x04 - zipBw.Write((uint) 0); // MTime = 0 - zipBw.Write((byte) 0x00); // XFL = 0x00 - zipBw.Write((byte) 0xff); // OS = 0x00 + zipBw.Write((byte)0x1f); // ID1 = 0x1f + zipBw.Write((byte)0x8b); // ID2 = 0x8b + zipBw.Write((byte)0x08); // CM = 0x08 + zipBw.Write((byte)0x04); // FLG = 0x04 + zipBw.Write((uint)0); // MTime = 0 + zipBw.Write((byte)0x00); // XFL = 0x00 + zipBw.Write((byte)0xff); // OS = 0x00 if (ExtraData == null) { - zipBw.Write((short) 12); + zipBw.Write((short)12); headerStartPos = zipBw.BaseStream.Position; zipBw.Write(new byte[12]); } else { - zipBw.Write((short) ExtraData.Length); // XLEN 16+4+8+1+16+20+4+8 + zipBw.Write((short)ExtraData.Length); // XLEN 16+4+8+1+16+20+4+8 headerStartPos = zipBw.BaseStream.Position; zipBw.Write(ExtraData); } @@ -326,7 +326,7 @@ namespace Compress.gZip public long TimeStamp => _zipFileInfo?.LastWriteTime ?? 0; - public void ZipFileAddDirectory() + public void ZipFileAddZeroLengthFile() { throw new NotImplementedException(); } @@ -354,15 +354,15 @@ namespace Compress.gZip public ZipReturn ZipFileCloseWriteStream(byte[] crc32) { - using (BinaryWriter zipBw = new BinaryWriter(_zipFs,Encoding.UTF8,true)) + using (BinaryWriter zipBw = new BinaryWriter(_zipFs, Encoding.UTF8, true)) { - CompressedSize = (ulong) (zipBw.BaseStream.Position - dataStartPos); + CompressedSize = (ulong)(zipBw.BaseStream.Position - dataStartPos); zipBw.Write(CRC[3]); zipBw.Write(CRC[2]); zipBw.Write(CRC[1]); zipBw.Write(CRC[0]); - zipBw.Write((uint) UnCompressedSize); + zipBw.Write((uint)UnCompressedSize); long endpos = _zipFs.Position; @@ -455,4 +455,4 @@ namespace Compress.gZip } } -} +} \ No newline at end of file diff --git a/SabreTools.Library/External/RVIO/RVIO.cs b/SabreTools.Library/External/RVIO/RVIO.cs index e9eb0985..1f8ff3d5 100644 --- a/SabreTools.Library/External/RVIO/RVIO.cs +++ b/SabreTools.Library/External/RVIO/RVIO.cs @@ -282,7 +282,7 @@ namespace RVIO if (!Win32Native.MoveFile(fullsourceDirName, fulldestDirName)) { int hr = Marshal.GetLastWin32Error(); - if (hr == Win32Native.ERROR_FILE_NOT_FOUND) // Source dir not found + if (hr == Win32Native.ERROR_FILE_NOT_FOUND) // Source dir not found { throw new Exception("ERROR_PATH_NOT_FOUND " + fullsourceDirName); } @@ -363,7 +363,7 @@ namespace RVIO bool r = Win32Native.CopyFile(fullSourceFileName, fullDestFileName, !overwrite); if (!r) { - // Save Win32 error because subsequent checks will overwrite this HRESULT. + // Save Win32 error because subsequent checks will overwrite this HRESULT. int errorCode = Marshal.GetLastWin32Error(); string fileName = destFileName; @@ -371,7 +371,7 @@ namespace RVIO if (errorCode != Win32Native.ERROR_FILE_EXISTS) { // For a number of error codes (sharing violation, path - // not found, etc) we don't know if the problem was with + // not found, etc) we don't know if the problem was with // the source or dest file. Try reading the source file. using (SafeFileHandle handle = Win32Native.UnsafeCreateFile(fullSourceFileName, FileStream.GENERIC_READ, FileShare.Read, null, FileMode.Open, 0, IntPtr.Zero)) { @@ -416,7 +416,7 @@ namespace RVIO throw new Exception(GetErrorCode(hr), new Exception("ERROR_MOVING_FILE. (" + fullSourceFileName + " to " + fullDestFileName + ")")); } } - + public static void Delete(string path) { if (unix.IsUnix) @@ -443,6 +443,8 @@ namespace RVIO { case 5: return "ERROR_ACCESS_DENIED: Access is denied."; case 32: return "ERROR_FILE_IN_USE: The file is in use by another process."; + case 39: return "ERROR_HANDLE_DISK_FULL: The disk is full."; + case 112: return "ERROR_DISK_FULL: There is not enough space on the disk."; case 123: return "ERROR_INVALID_NAME: The filename, directory name, or volume label syntax is incorrect."; case 183: return "ERROR_ALREADY_EXISTS: Cannot create a file when that file already exists."; } @@ -464,7 +466,7 @@ namespace RVIO return false; } } - + string fullPath = NameFix.AddLongPathPrefix(path); return Win32Native.SetFileAttributes(fullPath, (int)fileAttributes); } @@ -781,4 +783,4 @@ namespace RVIO } } -} +} \ No newline at end of file diff --git a/SabreTools.Library/External/RVIO/Win32Native.cs b/SabreTools.Library/External/RVIO/Win32Native.cs index 08d4359c..749e7bf0 100644 --- a/SabreTools.Library/External/RVIO/Win32Native.cs +++ b/SabreTools.Library/External/RVIO/Win32Native.cs @@ -140,39 +140,39 @@ namespace RVIO internal static class Convert { private const long TicksPerMillisecond = 10000; - private const long TicksPerSecond = TicksPerMillisecond*1000; - private const long TicksPerMinute = TicksPerSecond*60; - private const long TicksPerHour = TicksPerMinute*60; - private const long TicksPerDay = TicksPerHour*24; + private const long TicksPerSecond = TicksPerMillisecond * 1000; + private const long TicksPerMinute = TicksPerSecond * 60; + private const long TicksPerHour = TicksPerMinute * 60; + private const long TicksPerDay = TicksPerHour * 24; - // Number of days in a non-leap year + // Number of days in a non-leap year private const int DaysPerYear = 365; - // Number of days in 4 years - private const int DaysPer4Years = DaysPerYear*4 + 1; + // Number of days in 4 years + private const int DaysPer4Years = DaysPerYear * 4 + 1; // Number of days in 100 years - private const int DaysPer100Years = DaysPer4Years*25 - 1; + private const int DaysPer100Years = DaysPer4Years * 25 - 1; // Number of days in 400 years - private const int DaysPer400Years = DaysPer100Years*4 + 1; + private const int DaysPer400Years = DaysPer100Years * 4 + 1; - // Number of days from 1/1/0001 to 12/31/1600 - private const int DaysTo1601 = DaysPer400Years*4; - public const long FileTimeOffset = DaysTo1601*TicksPerDay; + // Number of days from 1/1/0001 to 12/31/1600 + private const int DaysTo1601 = DaysPer400Years * 4; + public const long FileTimeOffset = DaysTo1601 * TicksPerDay; // Number of days from 1/1/0001 to 12/31/9999 - private const int DaysTo10000 = DaysPer400Years*25 - 366; + private const int DaysTo10000 = DaysPer400Years * 25 - 366; private const long MinTicks = 0; - private const long MaxTicks = DaysTo10000*TicksPerDay - 1; + private const long MaxTicks = DaysTo10000 * TicksPerDay - 1; public static long Length(int high, int low) { - return ((long) high << 32) | (low & 0xFFFFFFFFL); + return ((long)high << 32) | (low & 0xFFFFFFFFL); } public static long Time(uint high, uint low) { - return ((long) high << 32) | low; + return ((long)high << 32) | low; } } } \ No newline at end of file diff --git a/SabreTools.Library/FileTypes/SevenZipArchive.cs b/SabreTools.Library/FileTypes/SevenZipArchive.cs index da5350f7..1d0c17d7 100644 --- a/SabreTools.Library/FileTypes/SevenZipArchive.cs +++ b/SabreTools.Library/FileTypes/SevenZipArchive.cs @@ -18,18 +18,16 @@ using MemoryStream = System.IO.MemoryStream; using SeekOrigin = System.IO.SeekOrigin; using Stream = System.IO.Stream; #endif +using Compress; +using Compress.SevenZip; using Compress.ZipFile; -using SevenZip; // TODO: Remove this when 7zip write is implemented in SharpCompress -using SharpCompress.Archives; -using SharpCompress.Archives.SevenZip; -using SharpCompress.Readers; +using NaturalSort; namespace SabreTools.Library.FileTypes { /// /// Represents a Torrent7zip archive for reading and writing /// - /// TODO: Torrent 7-zip: https://sourceforge.net/p/t7z/code/HEAD/tree/ public class SevenZipArchive : BaseArchive { #region Constructors @@ -74,13 +72,62 @@ namespace SabreTools.Library.FileTypes Directory.CreateDirectory(outDir); // Extract all files to the temp directory - SharpCompress.Archives.SevenZip.SevenZipArchive sza = SharpCompress.Archives.SevenZip.SevenZipArchive.Open(Utilities.TryOpenRead(this.Filename)); - foreach (SevenZipArchiveEntry entry in sza.Entries) + SevenZ zf = new SevenZ(); + ZipReturn zr = zf.ZipFileOpen(this.Filename, -1, true); + if (zr != ZipReturn.ZipGood) { - entry.WriteToDirectory(outDir, new SharpCompress.Common.ExtractionOptions { PreserveFileTime = true, ExtractFullPath = true, Overwrite = true }); + throw new Exception(ZipFile.ZipErrorMessageText(zr)); } + + for (int i = 0; i < zf.LocalFilesCount() && zr == ZipReturn.ZipGood; i++) + { + // Open the read stream + zr = zf.ZipFileOpenReadStream(i, out Stream readStream, out ulong streamsize); + + // Create the rest of the path, if needed + if (!String.IsNullOrWhiteSpace(Path.GetDirectoryName(zf.Filename(i)))) + { + Directory.CreateDirectory(Path.Combine(outDir, Path.GetDirectoryName(zf.Filename(i)))); + } + + // If the entry ends with a directory separator, continue to the next item, if any + if (zf.Filename(i).EndsWith(Path.DirectorySeparatorChar.ToString()) + || zf.Filename(i).EndsWith(Path.AltDirectorySeparatorChar.ToString()) + || zf.Filename(i).EndsWith(Path.PathSeparator.ToString())) + { + zf.ZipFileCloseReadStream(); + continue; + } + + FileStream writeStream = Utilities.TryCreate(Path.Combine(outDir, zf.Filename(i))); + + // If the stream is smaller than the buffer, just run one loop through to avoid issues + if (streamsize < _bufferSize) + { + byte[] ibuffer = new byte[streamsize]; + int ilen = readStream.Read(ibuffer, 0, (int)streamsize); + writeStream.Write(ibuffer, 0, ilen); + writeStream.Flush(); + } + // Otherwise, we do the normal loop + else + { + int realBufferSize = (streamsize < _bufferSize ? (int)streamsize : _bufferSize); + byte[] ibuffer = new byte[realBufferSize]; + int ilen; + while ((ilen = readStream.Read(ibuffer, 0, realBufferSize)) > 0) + { + writeStream.Write(ibuffer, 0, ilen); + writeStream.Flush(); + } + } + + zr = zf.ZipFileCloseReadStream(); + writeStream.Dispose(); + } + + zf.ZipFileClose(); encounteredErrors = false; - sza.Dispose(); } catch (EndOfStreamException) { @@ -158,18 +205,52 @@ namespace SabreTools.Library.FileTypes try { - SharpCompress.Archives.SevenZip.SevenZipArchive sza = SharpCompress.Archives.SevenZip.SevenZipArchive.Open(this.Filename, new ReaderOptions { LeaveStreamOpen = false, }); - foreach (SevenZipArchiveEntry entry in sza.Entries) + SevenZ zf = new SevenZ(); + ZipReturn zr = zf.ZipFileOpen(this.Filename, -1, true); + if (zr != ZipReturn.ZipGood) { - if (entry != null && !entry.IsDirectory && entry.Key.Contains(entryName)) + throw new Exception(ZipFile.ZipErrorMessageText(zr)); + } + + for (int i = 0; i < zf.LocalFilesCount() && zr == ZipReturn.ZipGood; i++) + { + if (zf.Filename(i).Contains(entryName)) { - // Write the file out - realEntry = entry.Key; - entry.WriteTo(ms); - break; + // Open the read stream + realEntry = zf.Filename(i); + zr = zf.ZipFileOpenReadStream(i, out Stream readStream, out ulong streamsize); + + // If the stream is smaller than the buffer, just run one loop through to avoid issues + if (streamsize < _bufferSize) + { + byte[] ibuffer = new byte[streamsize]; + int ilen = readStream.Read(ibuffer, 0, (int)streamsize); + ms.Write(ibuffer, 0, ilen); + ms.Flush(); + } + // Otherwise, we do the normal loop + else + { + byte[] ibuffer = new byte[_bufferSize]; + int ilen; + while (streamsize > _bufferSize) + { + ilen = readStream.Read(ibuffer, 0, _bufferSize); + ms.Write(ibuffer, 0, ilen); + ms.Flush(); + streamsize -= _bufferSize; + } + + ilen = readStream.Read(ibuffer, 0, (int)streamsize); + ms.Write(ibuffer, 0, ilen); + ms.Flush(); + } + + zr = zf.ZipFileCloseReadStream(); } } - sza.Dispose(); + + zf.ZipFileClose(); } catch (Exception ex) { @@ -199,18 +280,47 @@ namespace SabreTools.Library.FileTypes try { - SharpCompress.Archives.SevenZip.SevenZipArchive sza = SharpCompress.Archives.SevenZip.SevenZipArchive.Open(Utilities.TryOpenRead(this.Filename)); - foreach (SevenZipArchiveEntry entry in sza.Entries.Where(e => e != null && !e.IsDirectory)) + SevenZ zf = new SevenZ(); + ZipReturn zr = zf.ZipFileOpen(this.Filename, -1, true); + if (zr != ZipReturn.ZipGood) { + throw new Exception(ZipFile.ZipErrorMessageText(zr)); + } + + for (int i = 0; i < zf.LocalFilesCount(); i++) + { + // If the entry is a directory (or looks like a directory), we don't want to open it + if (zf.IsDirectory(i) + || zf.Filename(i).EndsWith(Path.DirectorySeparatorChar.ToString()) + || zf.Filename(i).EndsWith(Path.AltDirectorySeparatorChar.ToString()) + || zf.Filename(i).EndsWith(Path.PathSeparator.ToString())) + { + continue; + } + + // Open the read stream + zr = zf.ZipFileOpenReadStream(i, out Stream readStream, out ulong streamsize); + + // If we get a read error, log it and continue + if (zr != ZipReturn.ZipGood) + { + Globals.Logger.Warning("An error occurred while reading archive {0}: Zip Error - {1}", this.Filename, zr); + zr = zf.ZipFileCloseReadStream(); + continue; + } + // If secure hashes are disabled, do a quickscan if (omitFromScan == Hash.SecureHashes) { + string newname = zf.Filename(i); + long newsize = (long)zf.UncompressedSize(i); + byte[] newcrc = zf.CRC32(i); + found.Add(new BaseFile { - Filename = entry.Key, - Size = entry.Size, - CRC = BitConverter.GetBytes(entry.Crc), - Date = (date && entry.LastModifiedTime != null ? entry.LastModifiedTime?.ToString("yyyy/MM/dd hh:mm:ss") : null), + Filename = newname, + Size = newsize, + CRC = newcrc, Parent = gamename, }); @@ -218,18 +328,16 @@ namespace SabreTools.Library.FileTypes // Otherwise, use the stream directly else { - Stream entryStream = entry.OpenEntryStream(); - BaseFile sevenZipEntryRom = Utilities.GetStreamInfo(entryStream, entry.Size, omitFromScan: omitFromScan); - sevenZipEntryRom.Filename = entry.Key; - sevenZipEntryRom.Parent = gamename; - sevenZipEntryRom.Date = (date && entry.LastModifiedTime != null ? entry.LastModifiedTime?.ToString("yyyy/MM/dd hh:mm:ss") : null); - found.Add(sevenZipEntryRom); - entryStream.Dispose(); + BaseFile zipEntryRom = Utilities.GetStreamInfo(readStream, (long)zf.UncompressedSize(i), omitFromScan: omitFromScan, keepReadOpen: true); + zipEntryRom.Filename = zf.Filename(i); + zipEntryRom.Parent = gamename; + found.Add(zipEntryRom); } } // Dispose of the archive - sza.Dispose(); + zr = zf.ZipFileCloseReadStream(); + zf.ZipFileClose(); } catch (Exception ex) { @@ -251,24 +359,36 @@ namespace SabreTools.Library.FileTypes try { - SharpCompress.Archives.SevenZip.SevenZipArchive sza = SharpCompress.Archives.SevenZip.SevenZipArchive.Open(this.Filename, new ReaderOptions { LeaveStreamOpen = false }); - List sevenZipEntries = sza.Entries.OrderBy(e => e.Key, new NaturalSort.NaturalReversedComparer()).ToList(); - string lastSevenZipEntry = null; - foreach (SevenZipArchiveEntry entry in sevenZipEntries) + SevenZ zf = new SevenZ(); + ZipReturn zr = zf.ZipFileOpen(this.Filename, -1, true); + if (zr != ZipReturn.ZipGood) { - if (entry != null) + throw new Exception(ZipFile.ZipErrorMessageText(zr)); + } + + List<(string, bool)> zipEntries = new List<(string, bool)>(); + for (int i = 0; i < zf.LocalFilesCount(); i++) + { + zipEntries.Add((zf.Filename(i), zf.IsDirectory(i))); + } + + zipEntries = zipEntries.OrderBy(p => p.Item1, new NaturalReversedComparer()).ToList(); + string lastZipEntry = null; + foreach ((string, bool) entry in zipEntries) + { + // If the current is a superset of last, we skip it + if (lastZipEntry != null && lastZipEntry.StartsWith(entry.Item1)) { - // If the current is a superset of last, we skip it - if (lastSevenZipEntry != null && lastSevenZipEntry.StartsWith(entry.Key)) + // No-op + } + // If the entry is a directory, we add it + else + { + if (entry.Item2) { - // No-op - } - // If the entry is a directory, we add it - else if (entry.IsDirectory) - { - empties.Add(entry.Key); - lastSevenZipEntry = entry.Key; + empties.Add(entry.Item1); } + lastZipEntry = entry.Item1; } } } @@ -283,43 +403,16 @@ namespace SabreTools.Library.FileTypes /// /// Check whether the input file is a standardized format /// - /// TODO: Finish reading T7z information public override bool IsTorrent() { - bool ist7z = false; - - if (File.Exists(this.Filename)) + SevenZ zf = new SevenZ(); + ZipReturn zr = zf.ZipFileOpen(this.Filename, -1, true); + if (zr != ZipReturn.ZipGood) { - try - { - Stream fread = Utilities.TryOpenRead(this.Filename); - uint ar, offs = 0; - fread.Seek(0, SeekOrigin.Begin); - byte[] buffer = new byte[128]; - ar = (uint)fread.Read(buffer, 0, 4 + Constants.Torrent7ZipSignature.Length + 4); - if (ar < (4 + Constants.Torrent7ZipSignature.Length + 4)) - { - if (ar >= Constants.Torrent7ZipSignature.Length + 4) - { - ar -= (uint)(Constants.Torrent7ZipSignature.Length + 4); - } - if (ar <= Constants.Torrent7ZipHeader.Length) - { - ar = (uint)Constants.Torrent7ZipHeader.Length; - } - // memset(buffer+offs+ar,0,crcsz-ar) - } - - fread.Dispose(); - } - catch - { - Globals.Logger.Warning("File '{0}' could not be opened", this.Filename); - ist7z = false; - } + throw new Exception(ZipFile.ZipErrorMessageText(zr)); } - return ist7z; + return zf.ZipStatus == ZipStatus.Trrnt7Zip; } #endregion @@ -371,123 +464,156 @@ namespace SabreTools.Library.FileTypes inputStream.Seek(0, SeekOrigin.Begin); // Get the output archive name from the first rebuild rom - string archiveFileName = Path.Combine(outDir, Utilities.RemovePathUnsafeCharacters(rom.MachineName) + (rom.MachineName.EndsWith(".7z") ? "" : ".7z")); + string archiveFileName = Path.Combine(outDir, Utilities.RemovePathUnsafeCharacters(rom.MachineName) + (rom.MachineName.EndsWith(".zip") ? "" : ".zip")); // Set internal variables - SevenZipBase.SetLibraryPath("7za.dll"); - SevenZipExtractor oldZipFile = null; - SevenZipCompressor zipFile; + Stream writeStream = null; + SevenZ oldZipFile = new SevenZ(); + SevenZ zipFile = new SevenZ(); + ZipReturn zipReturn = ZipReturn.ZipGood; try { // If the full output path doesn't exist, create it - if (!Directory.Exists(Path.GetDirectoryName(tempFile))) + if (!Directory.Exists(Path.GetDirectoryName(archiveFileName))) { - Directory.CreateDirectory(Path.GetDirectoryName(tempFile)); + Directory.CreateDirectory(Path.GetDirectoryName(archiveFileName)); } // If the archive doesn't exist, create it and put the single file if (!File.Exists(archiveFileName)) { - zipFile = new SevenZipCompressor() - { - ArchiveFormat = OutArchiveFormat.SevenZip, - CompressionLevel = CompressionLevel.Normal, - }; + inputStream.Seek(0, SeekOrigin.Begin); + zipReturn = zipFile.ZipFileCreate(tempFile); - // Create the temp directory - string tempPath = Path.Combine(outDir, Guid.NewGuid().ToString()); - if (!Directory.Exists(tempPath)) + // Open the input file for reading + ulong istreamSize = (ulong)(inputStream.Length); + + DateTime dt = DateTime.Now; + if (date && !String.IsNullOrWhiteSpace(rom.Date) && DateTime.TryParse(rom.Date.Replace('\\', '/'), out dt)) { - Directory.CreateDirectory(tempPath); + uint msDosDateTime = Utilities.ConvertDateTimeToMsDosTimeFormat(dt); + zipFile.ZipFileOpenWriteStream(false, false, rom.Name.Replace('\\', '/'), istreamSize, 0, out writeStream); + } + else + { + zipFile.ZipFileOpenWriteStream(false, true, rom.Name.Replace('\\', '/'), istreamSize, 0, out writeStream); } - // Create a stream dictionary - Dictionary dict = new Dictionary(); - dict.Add(rom.Name, inputStream); - - // Now add the stream - zipFile.CompressStreamDictionary(dict, tempFile); + // Copy the input stream to the output + byte[] ibuffer = new byte[_bufferSize]; + int ilen; + while ((ilen = inputStream.Read(ibuffer, 0, _bufferSize)) > 0) + { + writeStream.Write(ibuffer, 0, ilen); + writeStream.Flush(); + } + inputStream.Dispose(); + zipFile.ZipFileCloseWriteStream(Utilities.StringToByteArray(rom.CRC)); } // Otherwise, sort the input files and write out in the correct order else { // Open the old archive for reading - using (oldZipFile = new SevenZipExtractor(archiveFileName)) + oldZipFile.ZipFileOpen(archiveFileName, -1, true); + + // Map all inputs to index + Dictionary inputIndexMap = new Dictionary(); + var oldZipFileContents = new List(); + for (int i = 0; i < oldZipFile.LocalFilesCount(); i++) { - // Map all inputs to index - Dictionary inputIndexMap = new Dictionary(); + oldZipFileContents.Add(oldZipFile.Filename(i)); + } - // If the old one doesn't contain the new file, then add it - if (!oldZipFile.ArchiveFileNames.Contains(rom.Name.Replace('\\', '/'))) + // If the old one doesn't contain the new file, then add it + if (!oldZipFileContents.Contains(rom.Name.Replace('\\', '/'))) + { + inputIndexMap.Add(rom.Name.Replace('\\', '/'), -1); + } + + // Then add all of the old entries to it too + for (int i = 0; i < oldZipFile.LocalFilesCount(); i++) + { + inputIndexMap.Add(oldZipFile.Filename(i), i); + } + + // If the number of entries is the same as the old archive, skip out + if (inputIndexMap.Keys.Count <= oldZipFile.LocalFilesCount()) + { + success = true; + return success; + } + + // Otherwise, process the old zipfile + zipFile.ZipFileCreate(tempFile); + + // Get the order for the entries with the new file + List keys = inputIndexMap.Keys.ToList(); + keys.Sort(ZipFile.TrrntZipStringCompare); + + // Copy over all files to the new archive + foreach (string key in keys) + { + // Get the index mapped to the key + int index = inputIndexMap[key]; + + // If we have the input file, add it now + if (index < 0) { - inputIndexMap.Add(rom.Name.Replace('\\', '/'), -1); - } + // Open the input file for reading + ulong istreamSize = (ulong)(inputStream.Length); - // Then add all of the old entries to it too - for (int i = 0; i < oldZipFile.FilesCount; i++) - { - inputIndexMap.Add(oldZipFile.ArchiveFileNames[i], i); - } - - // If the number of entries is the same as the old archive, skip out - if (inputIndexMap.Keys.Count <= oldZipFile.FilesCount) - { - success = true; - return success; - } - - // Otherwise, process the old zipfile - zipFile = new SevenZipCompressor() - { - ArchiveFormat = OutArchiveFormat.SevenZip, - CompressionLevel = CompressionLevel.Normal, - }; - - // Get the order for the entries with the new file - List keys = inputIndexMap.Keys.ToList(); - keys.Sort(ZipFile.TrrntZipStringCompare); - - // Copy over all files to the new archive - foreach (string key in keys) - { - // Get the index mapped to the key - int index = inputIndexMap[key]; - - // If we have the input file, add it now - if (index < 0) + DateTime dt = DateTime.Now; + if (date && !String.IsNullOrWhiteSpace(rom.Date) && DateTime.TryParse(rom.Date.Replace('\\', '/'), out dt)) { - // Create a stream dictionary - Dictionary dict = new Dictionary(); - dict.Add(rom.Name, inputStream); - - // Now add the stream - zipFile.CompressStreamDictionary(dict, tempFile); + uint msDosDateTime = Utilities.ConvertDateTimeToMsDosTimeFormat(dt); + zipFile.ZipFileOpenWriteStream(false, false, rom.Name.Replace('\\', '/'), istreamSize, 0, out writeStream); } - - // Otherwise, copy the file from the old archive else { - Stream oldZipFileEntryStream = new MemoryStream(); - oldZipFile.ExtractFile(index, oldZipFileEntryStream); - oldZipFileEntryStream.Seek(0, SeekOrigin.Begin); - - // Create a stream dictionary - Dictionary dict = new Dictionary(); - dict.Add(oldZipFile.ArchiveFileNames[index], oldZipFileEntryStream); - - // Now add the stream - zipFile.CompressStreamDictionary(dict, tempFile); - oldZipFileEntryStream.Dispose(); + zipFile.ZipFileOpenWriteStream(false, true, rom.Name.Replace('\\', '/'), istreamSize, 0, out writeStream); } - // After the first file, make sure we're in append mode - zipFile.CompressionMode = CompressionMode.Append; + // Copy the input stream to the output + byte[] ibuffer = new byte[_bufferSize]; + int ilen; + while ((ilen = inputStream.Read(ibuffer, 0, _bufferSize)) > 0) + { + writeStream.Write(ibuffer, 0, ilen); + writeStream.Flush(); + } + + inputStream.Dispose(); + zipFile.ZipFileCloseWriteStream(Utilities.StringToByteArray(rom.CRC)); + } + + // Otherwise, copy the file from the old archive + else + { + // Instantiate the streams + oldZipFile.ZipFileOpenReadStream(index, out Stream zreadStream, out ulong istreamSize); + zipFile.ZipFileOpenWriteStream(false, true, oldZipFile.Filename(index), istreamSize, 0, out writeStream); + + // Copy the input stream to the output + byte[] ibuffer = new byte[_bufferSize]; + int ilen; + while ((ilen = zreadStream.Read(ibuffer, 0, _bufferSize)) > 0) + { + writeStream.Write(ibuffer, 0, ilen); + writeStream.Flush(); + } + + oldZipFile.ZipFileCloseReadStream(); + zipFile.ZipFileCloseWriteStream(oldZipFile.CRC32(index)); } } } + // Close the output zip file + zipFile.ZipFileClose(); + oldZipFile.ZipFileClose(); + success = true; } catch (Exception ex) @@ -507,33 +633,6 @@ namespace SabreTools.Library.FileTypes } File.Move(tempFile, archiveFileName); - // Now make the file T7Z - // TODO: Add ACTUAL T7Z compatible code - - BinaryWriter bw = new BinaryWriter(Utilities.TryOpenReadWrite(archiveFileName)); - bw.Seek(0, SeekOrigin.Begin); - bw.Write(Constants.Torrent7ZipHeader); - bw.Seek(0, SeekOrigin.End); - - using (oldZipFile = new SevenZipExtractor(Utilities.TryOpenReadWrite(archiveFileName))) - { - - // Get the correct signature to use (Default 0, Unicode 1, SingleFile 2, StripFileNames 4) - byte[] tempsig = Constants.Torrent7ZipSignature; - if (oldZipFile.FilesCount > 1) - { - tempsig[16] = 0x2; - } - else - { - tempsig[16] = 0; - } - - bw.Write(tempsig); - bw.Flush(); - bw.Dispose(); - } - return true; } @@ -573,12 +672,13 @@ namespace SabreTools.Library.FileTypes } // Get the output archive name from the first rebuild rom - string archiveFileName = Path.Combine(outDir, Utilities.RemovePathUnsafeCharacters(roms[0].MachineName) + (roms[0].MachineName.EndsWith(".7z") ? "" : ".7z")); + string archiveFileName = Path.Combine(outDir, Utilities.RemovePathUnsafeCharacters(roms[0].MachineName) + (roms[0].MachineName.EndsWith(".zip") ? "" : ".zip")); // Set internal variables - SevenZipBase.SetLibraryPath("7za.dll"); - SevenZipExtractor oldZipFile; - SevenZipCompressor zipFile; + Stream writeStream = null; + SevenZ oldZipFile = new SevenZ(); + SevenZ zipFile = new SevenZ(); + ZipReturn zipReturn = ZipReturn.ZipGood; try { @@ -591,11 +691,7 @@ namespace SabreTools.Library.FileTypes // If the archive doesn't exist, create it and put the single file if (!File.Exists(archiveFileName)) { - zipFile = new SevenZipCompressor() - { - ArchiveFormat = OutArchiveFormat.SevenZip, - CompressionLevel = CompressionLevel.Normal, - }; + zipReturn = zipFile.ZipFileCreate(tempFile); // Map all inputs to index Dictionary inputIndexMap = new Dictionary(); @@ -608,114 +704,147 @@ namespace SabreTools.Library.FileTypes List keys = inputIndexMap.Keys.ToList(); keys.Sort(ZipFile.TrrntZipStringCompare); - // Create the temp directory - string tempPath = Path.Combine(outDir, Guid.NewGuid().ToString()); - if (!Directory.Exists(tempPath)) - { - Directory.CreateDirectory(tempPath); - } - // Now add all of the files in order foreach (string key in keys) { - string newkey = Path.Combine(tempPath, key); + // Get the index mapped to the key + int index = inputIndexMap[key]; - File.Move(inputFiles[inputIndexMap[key]], newkey); - zipFile.CompressFiles(tempFile, newkey); - File.Move(newkey, inputFiles[inputIndexMap[key]]); + // Open the input file for reading + Stream freadStream = Utilities.TryOpenRead(inputFiles[index]); + ulong istreamSize = (ulong)(new FileInfo(inputFiles[index]).Length); - // After the first file, make sure we're in append mode - zipFile.CompressionMode = CompressionMode.Append; + DateTime dt = DateTime.Now; + if (date && !String.IsNullOrWhiteSpace(roms[index].Date) && DateTime.TryParse(roms[index].Date.Replace('\\', '/'), out dt)) + { + uint msDosDateTime = Utilities.ConvertDateTimeToMsDosTimeFormat(dt); + zipFile.ZipFileOpenWriteStream(false, false, roms[index].Name.Replace('\\', '/'), istreamSize, 0, out writeStream); + } + else + { + zipFile.ZipFileOpenWriteStream(false, true, roms[index].Name.Replace('\\', '/'), istreamSize, 0, out writeStream); + } + + // Copy the input stream to the output + byte[] ibuffer = new byte[_bufferSize]; + int ilen; + while ((ilen = freadStream.Read(ibuffer, 0, _bufferSize)) > 0) + { + writeStream.Write(ibuffer, 0, ilen); + writeStream.Flush(); + } + + freadStream.Dispose(); + zipFile.ZipFileCloseWriteStream(Utilities.StringToByteArray(roms[index].CRC)); } - - Utilities.CleanDirectory(tempPath); - Utilities.TryDeleteDirectory(tempPath); } // Otherwise, sort the input files and write out in the correct order else { // Open the old archive for reading - using (oldZipFile = new SevenZipExtractor(archiveFileName)) + oldZipFile.ZipFileOpen(archiveFileName, -1, true); + + // Map all inputs to index + Dictionary inputIndexMap = new Dictionary(); + for (int i = 0; i < inputFiles.Count; i++) { - // Map all inputs to index - Dictionary inputIndexMap = new Dictionary(); - for (int i = 0; i < inputFiles.Count; i++) + var oldZipFileContents = new List(); + for (int j = 0; j < oldZipFile.LocalFilesCount(); j++) { - // If the old one contains the new file, then just skip out - if (oldZipFile.ArchiveFileNames.Contains(roms[i].Name.Replace('\\', '/'))) + oldZipFileContents.Add(oldZipFile.Filename(j)); + } + + // If the old one contains the new file, then just skip out + if (oldZipFileContents.Contains(roms[i].Name.Replace('\\', '/'))) + { + continue; + } + + inputIndexMap.Add(roms[i].Name.Replace('\\', '/'), -(i + 1)); + } + + // Then add all of the old entries to it too + for (int i = 0; i < oldZipFile.LocalFilesCount(); i++) + { + inputIndexMap.Add(oldZipFile.Filename(i), i); + } + + // If the number of entries is the same as the old archive, skip out + if (inputIndexMap.Keys.Count <= oldZipFile.LocalFilesCount()) + { + success = true; + return success; + } + + // Otherwise, process the old zipfile + zipFile.ZipFileCreate(tempFile); + + // Get the order for the entries with the new file + List keys = inputIndexMap.Keys.ToList(); + keys.Sort(ZipFile.TrrntZipStringCompare); + + // Copy over all files to the new archive + foreach (string key in keys) + { + // Get the index mapped to the key + int index = inputIndexMap[key]; + + // If we have the input file, add it now + if (index < 0) + { + // Open the input file for reading + Stream freadStream = Utilities.TryOpenRead(inputFiles[-index - 1]); + ulong istreamSize = (ulong)(new FileInfo(inputFiles[-index - 1]).Length); + + DateTime dt = DateTime.Now; + if (date && !String.IsNullOrWhiteSpace(roms[-index - 1].Date) && DateTime.TryParse(roms[-index - 1].Date.Replace('\\', '/'), out dt)) { - continue; + uint msDosDateTime = Utilities.ConvertDateTimeToMsDosTimeFormat(dt); + zipFile.ZipFileOpenWriteStream(false, false, roms[-index - 1].Name.Replace('\\', '/'), istreamSize, 0, out writeStream); } - - inputIndexMap.Add(roms[i].Name.Replace('\\', '/'), -(i + 1)); - } - - // Then add all of the old entries to it too - for (int i = 0; i < oldZipFile.FilesCount; i++) - { - inputIndexMap.Add(oldZipFile.ArchiveFileNames[i], i); - } - - // If the number of entries is the same as the old archive, skip out - if (inputIndexMap.Keys.Count <= oldZipFile.FilesCount) - { - success = true; - return success; - } - - // Otherwise, process the old zipfile - zipFile = new SevenZipCompressor() - { - ArchiveFormat = OutArchiveFormat.SevenZip, - CompressionLevel = CompressionLevel.Normal, - }; - - // Get the order for the entries with the new file - List keys = inputIndexMap.Keys.ToList(); - keys.Sort(ZipFile.TrrntZipStringCompare); - - // Copy over all files to the new archive - foreach (string key in keys) - { - // Get the index mapped to the key - int index = inputIndexMap[key]; - - // If we have the input file, add it now - if (index < 0) - { - FileStream inputStream = Utilities.TryOpenRead(inputFiles[-index - 1]); - - // Create a stream dictionary - Dictionary dict = new Dictionary(); - dict.Add(key, inputStream); - - // Now add the stream - zipFile.CompressStreamDictionary(dict, tempFile); - } - - // Otherwise, copy the file from the old archive else { - Stream oldZipFileEntryStream = new MemoryStream(); - oldZipFile.ExtractFile(index, oldZipFileEntryStream); - oldZipFileEntryStream.Seek(0, SeekOrigin.Begin); - - // Create a stream dictionary - Dictionary dict = new Dictionary(); - dict.Add(oldZipFile.ArchiveFileNames[index], oldZipFileEntryStream); - - // Now add the stream - zipFile.CompressStreamDictionary(dict, tempFile); - oldZipFileEntryStream.Dispose(); + zipFile.ZipFileOpenWriteStream(false, true, roms[-index - 1].Name.Replace('\\', '/'), istreamSize, 0, out writeStream); } - // After the first file, make sure we're in append mode - zipFile.CompressionMode = CompressionMode.Append; + // Copy the input stream to the output + byte[] ibuffer = new byte[_bufferSize]; + int ilen; + while ((ilen = freadStream.Read(ibuffer, 0, _bufferSize)) > 0) + { + writeStream.Write(ibuffer, 0, ilen); + writeStream.Flush(); + } + freadStream.Dispose(); + zipFile.ZipFileCloseWriteStream(Utilities.StringToByteArray(roms[-index - 1].CRC)); + } + + // Otherwise, copy the file from the old archive + else + { + // Instantiate the streams + oldZipFile.ZipFileOpenReadStream(index, out Stream zreadStream, out ulong istreamSize); + zipFile.ZipFileOpenWriteStream(false, true, oldZipFile.Filename(index), istreamSize, 0, out writeStream); + + // Copy the input stream to the output + byte[] ibuffer = new byte[_bufferSize]; + int ilen; + while ((ilen = zreadStream.Read(ibuffer, 0, _bufferSize)) > 0) + { + writeStream.Write(ibuffer, 0, ilen); + writeStream.Flush(); + } + + zipFile.ZipFileCloseWriteStream(oldZipFile.CRC32(index)); } } } + // Close the output zip file + zipFile.ZipFileClose(); + oldZipFile.ZipFileClose(); + success = true; } catch (Exception ex) @@ -731,32 +860,6 @@ namespace SabreTools.Library.FileTypes } File.Move(tempFile, archiveFileName); - // Now make the file T7Z - // TODO: Add ACTUAL T7Z compatible code - - BinaryWriter bw = new BinaryWriter(Utilities.TryOpenReadWrite(archiveFileName)); - bw.Seek(0, SeekOrigin.Begin); - bw.Write(Constants.Torrent7ZipHeader); - bw.Seek(0, SeekOrigin.End); - - using (oldZipFile = new SevenZipExtractor(Utilities.TryOpenReadWrite(archiveFileName))) - { - // Get the correct signature to use (Default 0, Unicode 1, SingleFile 2, StripFileNames 4) - byte[] tempsig = Constants.Torrent7ZipSignature; - if (oldZipFile.FilesCount > 1) - { - tempsig[16] = 0x2; - } - else - { - tempsig[16] = 0; - } - - bw.Write(tempsig); - bw.Flush(); - bw.Dispose(); - } - return true; } diff --git a/SabreTools.Library/FileTypes/ZipArchive.cs b/SabreTools.Library/FileTypes/ZipArchive.cs index 5d7d3b96..9754704e 100644 --- a/SabreTools.Library/FileTypes/ZipArchive.cs +++ b/SabreTools.Library/FileTypes/ZipArchive.cs @@ -19,7 +19,7 @@ using Stream = System.IO.Stream; #endif using Compress; using Compress.ZipFile; -using SharpCompress.Readers; +using NaturalSort; namespace SabreTools.Library.FileTypes { @@ -93,6 +93,7 @@ namespace SabreTools.Library.FileTypes || zf.Filename(i).EndsWith(Path.AltDirectorySeparatorChar.ToString()) || zf.Filename(i).EndsWith(Path.PathSeparator.ToString())) { + zf.ZipFileCloseReadStream(); continue; } @@ -286,6 +287,15 @@ namespace SabreTools.Library.FileTypes for (int i = 0; i < zf.LocalFilesCount(); i++) { + // If the entry is a directory (or looks like a directory), we don't want to open it + if (zf.IsDirectory(i) + || zf.Filename(i).EndsWith(Path.DirectorySeparatorChar.ToString()) + || zf.Filename(i).EndsWith(Path.AltDirectorySeparatorChar.ToString()) + || zf.Filename(i).EndsWith(Path.PathSeparator.ToString())) + { + continue; + } + // Open the read stream zr = zf.ZipFileOpenReadStream(i, false, out Stream readStream, out ulong streamsize, out ushort cm); @@ -297,15 +307,6 @@ namespace SabreTools.Library.FileTypes continue; } - // If the entry ends with a directory separator, continue to the next item, if any - if (zf.Filename(i).EndsWith(Path.DirectorySeparatorChar.ToString()) - || zf.Filename(i).EndsWith(Path.AltDirectorySeparatorChar.ToString()) - || zf.Filename(i).EndsWith(Path.PathSeparator.ToString())) - { - zr = zf.ZipFileCloseReadStream(); - continue; - } - // If secure hashes are disabled, do a quickscan if (omitFromScan == Hash.SecureHashes) { @@ -360,27 +361,36 @@ namespace SabreTools.Library.FileTypes try { - SharpCompress.Archives.Zip.ZipArchive za = SharpCompress.Archives.Zip.ZipArchive.Open(this.Filename, new ReaderOptions { LeaveStreamOpen = false }); - List zipEntries = za.Entries.OrderBy(e => e.Key, new NaturalSort.NaturalReversedComparer()).ToList(); - string lastZipEntry = null; - foreach (SharpCompress.Archives.Zip.ZipArchiveEntry entry in zipEntries) + ZipFile zf = new ZipFile(); + ZipReturn zr = zf.ZipFileOpen(this.Filename, -1, true); + if (zr != ZipReturn.ZipGood) { - if (entry != null) + throw new Exception(ZipFile.ZipErrorMessageText(zr)); + } + + List<(string, bool)> zipEntries = new List<(string, bool)>(); + for (int i = 0; i < zf.LocalFilesCount(); i++) + { + zipEntries.Add((zf.Filename(i), zf.IsDirectory(i))); + } + + zipEntries = zipEntries.OrderBy(p => p.Item1, new NaturalReversedComparer()).ToList(); + string lastZipEntry = null; + foreach ((string, bool) entry in zipEntries) + { + // If the current is a superset of last, we skip it + if (lastZipEntry != null && lastZipEntry.StartsWith(entry.Item1)) { - // If the current is a superset of last, we skip it - if (lastZipEntry != null && lastZipEntry.StartsWith(entry.Key)) + // No-op + } + // If the entry is a directory, we add it + else + { + if (entry.Item2) { - // No-op - } - // If the entry is a directory, we add it - else - { - if (entry.IsDirectory) - { - empties.Add(entry.Key); - } - lastZipEntry = entry.Key; + empties.Add(entry.Item1); } + lastZipEntry = entry.Item1; } } } @@ -397,7 +407,14 @@ namespace SabreTools.Library.FileTypes /// public override bool IsTorrent() { - throw new NotImplementedException(); + ZipFile zf = new ZipFile(); + ZipReturn zr = zf.ZipFileOpen(this.Filename, -1, true); + if (zr != ZipReturn.ZipGood) + { + throw new Exception(ZipFile.ZipErrorMessageText(zr)); + } + + return zf.ZipStatus == ZipStatus.TrrntZip; } #endregion @@ -501,7 +518,7 @@ namespace SabreTools.Library.FileTypes else { // Open the old archive for reading - oldZipFile.ZipFileOpen(archiveFileName, new FileInfo(archiveFileName).LastWriteTime.Ticks, true); + oldZipFile.ZipFileOpen(archiveFileName, -1, true); // Map all inputs to index Dictionary inputIndexMap = new Dictionary(); @@ -728,7 +745,7 @@ namespace SabreTools.Library.FileTypes else { // Open the old archive for reading - oldZipFile.ZipFileOpen(archiveFileName, new FileInfo(archiveFileName).LastWriteTime.Ticks, true); + oldZipFile.ZipFileOpen(archiveFileName, -1, true); // Map all inputs to index Dictionary inputIndexMap = new Dictionary(); diff --git a/SabreTools.Library/SabreTools.Library.csproj b/SabreTools.Library/SabreTools.Library.csproj index 6f496cd7..c4a9f734 100644 --- a/SabreTools.Library/SabreTools.Library.csproj +++ b/SabreTools.Library/SabreTools.Library.csproj @@ -47,7 +47,6 @@ - @@ -61,9 +60,6 @@ - - Always - Always @@ -100,7 +96,7 @@ - +