From 8cb621ebed2557916840049c652ccccdbd31a37a Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Tue, 23 Apr 2024 14:47:40 +0100 Subject: [PATCH 1/5] Add SharpCompressException and use it or children in most places --- src/SharpCompress/Archives/AbstractArchive.cs | 2 +- .../Archives/AbstractWritableArchive.cs | 2 +- .../Archives/GZip/GZipArchive.cs | 4 +- src/SharpCompress/Common/ArchiveException.cs | 9 ---- .../Common/CryptographicException.cs | 9 ---- .../Common/ExtractionException.cs | 12 ----- .../Common/IncompleteArchiveException.cs | 7 --- .../Common/InvalidFormatException.cs | 12 ----- .../Common/MultiVolumeExtractionException.cs | 12 ----- .../MultipartStreamRequiredException.cs | 7 --- .../Common/ReaderCancelledException.cs | 12 ----- .../Common/SevenZip/ArchiveReader.cs | 10 ++-- .../Common/SevenZip/SevenZipFilePart.cs | 4 +- .../Common/SharpCompressException.cs | 52 +++++++++++++++++++ .../Common/Tar/Headers/TarHeader.cs | 2 +- src/SharpCompress/Compressors/Deflate/Zlib.cs | 3 +- .../Compressors/Deflate64/Deflate64Stream.cs | 3 +- .../Compressors/Deflate64/HuffmanTree.cs | 7 +-- .../Compressors/Deflate64/InflaterManaged.cs | 27 +++++----- .../Compressors/Filters/BranchExecFilter.cs | 3 +- .../Compressors/LZMA/DecoderStream.cs | 7 +-- .../Compressors/LZMA/LZipStream.cs | 29 ++++------- .../Compressors/LZMA/LzmaEncoder.cs | 3 +- .../Compressors/Xz/Filters/ArmFilter.cs | 7 +-- .../Compressors/Xz/Filters/ArmThumbFilter.cs | 7 +-- .../Compressors/Xz/Filters/BlockFilter.cs | 3 +- .../Compressors/Xz/Filters/IA64Filter.cs | 7 +-- .../Compressors/Xz/Filters/Lzma2Filter.cs | 5 +- .../Compressors/Xz/Filters/PowerPCFilter.cs | 7 +-- .../Compressors/Xz/Filters/SparcFilter.cs | 7 +-- .../Compressors/Xz/Filters/X86Filter.cs | 7 +-- .../Compressors/Xz/MultiByteIntegers.cs | 5 +- src/SharpCompress/Compressors/Xz/XZBlock.cs | 13 ++--- src/SharpCompress/Compressors/Xz/XZFooter.cs | 5 +- src/SharpCompress/Compressors/Xz/XZHeader.cs | 7 +-- src/SharpCompress/Compressors/Xz/XZIndex.cs | 5 +- .../Compressors/Xz/XZReadOnlyStream.cs | 3 +- src/SharpCompress/Compressors/Xz/XZStream.cs | 3 +- src/SharpCompress/IO/MarkingBinaryReader.cs | 3 +- src/SharpCompress/Readers/ReaderFactory.cs | 7 +-- .../SharpCompress.Test/Xz/Filters/BCJTests.cs | 15 +++--- .../Xz/Filters/Lzma2Tests.cs | 5 +- tests/SharpCompress.Test/Xz/XZBlockTests.cs | 2 +- tests/SharpCompress.Test/Xz/XZHeaderTests.cs | 7 +-- tests/SharpCompress.Test/Xz/XZIndexTests.cs | 3 +- 45 files changed, 180 insertions(+), 191 deletions(-) delete mode 100644 src/SharpCompress/Common/ArchiveException.cs delete mode 100644 src/SharpCompress/Common/CryptographicException.cs delete mode 100644 src/SharpCompress/Common/ExtractionException.cs delete mode 100644 src/SharpCompress/Common/IncompleteArchiveException.cs delete mode 100644 src/SharpCompress/Common/InvalidFormatException.cs delete mode 100644 src/SharpCompress/Common/MultiVolumeExtractionException.cs delete mode 100644 src/SharpCompress/Common/MultipartStreamRequiredException.cs delete mode 100644 src/SharpCompress/Common/ReaderCancelledException.cs create mode 100644 src/SharpCompress/Common/SharpCompressException.cs diff --git a/src/SharpCompress/Archives/AbstractArchive.cs b/src/SharpCompress/Archives/AbstractArchive.cs index df54d781..bf7fb9ed 100644 --- a/src/SharpCompress/Archives/AbstractArchive.cs +++ b/src/SharpCompress/Archives/AbstractArchive.cs @@ -53,7 +53,7 @@ private static Stream CheckStreams(Stream stream) { if (!stream.CanSeek || !stream.CanRead) { - throw new ArgumentException("Archive streams must be Readable and Seekable"); + throw new ArchiveException("Archive streams must be Readable and Seekable"); } return stream; } diff --git a/src/SharpCompress/Archives/AbstractWritableArchive.cs b/src/SharpCompress/Archives/AbstractWritableArchive.cs index 614489fe..082b9631 100644 --- a/src/SharpCompress/Archives/AbstractWritableArchive.cs +++ b/src/SharpCompress/Archives/AbstractWritableArchive.cs @@ -151,7 +151,7 @@ bool closeStream { if (!source.CanRead || !source.CanSeek) { - throw new ArgumentException( + throw new ArchiveException( "Streams must be readable and seekable to use the Writing Archive API" ); } diff --git a/src/SharpCompress/Archives/GZip/GZipArchive.cs b/src/SharpCompress/Archives/GZip/GZipArchive.cs index cec2f640..7a33a10a 100644 --- a/src/SharpCompress/Archives/GZip/GZipArchive.cs +++ b/src/SharpCompress/Archives/GZip/GZipArchive.cs @@ -162,7 +162,7 @@ bool closeStream { if (Entries.Any()) { - throw new InvalidOperationException("Only one entry is allowed in a GZip Archive"); + throw new InvalidFormatException("Only one entry is allowed in a GZip Archive"); } return new GZipWritableArchiveEntry(this, source, filePath, size, modified, closeStream); } @@ -176,7 +176,7 @@ IEnumerable newEntries { if (Entries.Count > 1) { - throw new InvalidOperationException("Only one entry is allowed in a GZip Archive"); + throw new InvalidFormatException("Only one entry is allowed in a GZip Archive"); } using var writer = new GZipWriter(stream, new GZipWriterOptions(options)); foreach (var entry in oldEntries.Concat(newEntries).Where(x => !x.IsDirectory)) diff --git a/src/SharpCompress/Common/ArchiveException.cs b/src/SharpCompress/Common/ArchiveException.cs deleted file mode 100644 index 507d5fd8..00000000 --- a/src/SharpCompress/Common/ArchiveException.cs +++ /dev/null @@ -1,9 +0,0 @@ -using System; - -namespace SharpCompress.Common; - -public class ArchiveException : Exception -{ - public ArchiveException(string message) - : base(message) { } -} diff --git a/src/SharpCompress/Common/CryptographicException.cs b/src/SharpCompress/Common/CryptographicException.cs deleted file mode 100644 index 6127524a..00000000 --- a/src/SharpCompress/Common/CryptographicException.cs +++ /dev/null @@ -1,9 +0,0 @@ -using System; - -namespace SharpCompress.Common; - -public class CryptographicException : Exception -{ - public CryptographicException(string message) - : base(message) { } -} diff --git a/src/SharpCompress/Common/ExtractionException.cs b/src/SharpCompress/Common/ExtractionException.cs deleted file mode 100644 index 4bc4f00c..00000000 --- a/src/SharpCompress/Common/ExtractionException.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; - -namespace SharpCompress.Common; - -public class ExtractionException : Exception -{ - public ExtractionException(string message) - : base(message) { } - - public ExtractionException(string message, Exception inner) - : base(message, inner) { } -} diff --git a/src/SharpCompress/Common/IncompleteArchiveException.cs b/src/SharpCompress/Common/IncompleteArchiveException.cs deleted file mode 100644 index a033001a..00000000 --- a/src/SharpCompress/Common/IncompleteArchiveException.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace SharpCompress.Common; - -public class IncompleteArchiveException : ArchiveException -{ - public IncompleteArchiveException(string message) - : base(message) { } -} diff --git a/src/SharpCompress/Common/InvalidFormatException.cs b/src/SharpCompress/Common/InvalidFormatException.cs deleted file mode 100644 index 8f14df14..00000000 --- a/src/SharpCompress/Common/InvalidFormatException.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; - -namespace SharpCompress.Common; - -public class InvalidFormatException : ExtractionException -{ - public InvalidFormatException(string message) - : base(message) { } - - public InvalidFormatException(string message, Exception inner) - : base(message, inner) { } -} diff --git a/src/SharpCompress/Common/MultiVolumeExtractionException.cs b/src/SharpCompress/Common/MultiVolumeExtractionException.cs deleted file mode 100644 index 764ac808..00000000 --- a/src/SharpCompress/Common/MultiVolumeExtractionException.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; - -namespace SharpCompress.Common; - -public class MultiVolumeExtractionException : ExtractionException -{ - public MultiVolumeExtractionException(string message) - : base(message) { } - - public MultiVolumeExtractionException(string message, Exception inner) - : base(message, inner) { } -} diff --git a/src/SharpCompress/Common/MultipartStreamRequiredException.cs b/src/SharpCompress/Common/MultipartStreamRequiredException.cs deleted file mode 100644 index 33a842d9..00000000 --- a/src/SharpCompress/Common/MultipartStreamRequiredException.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace SharpCompress.Common; - -public class MultipartStreamRequiredException : ExtractionException -{ - public MultipartStreamRequiredException(string message) - : base(message) { } -} diff --git a/src/SharpCompress/Common/ReaderCancelledException.cs b/src/SharpCompress/Common/ReaderCancelledException.cs deleted file mode 100644 index 918e5abb..00000000 --- a/src/SharpCompress/Common/ReaderCancelledException.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; - -namespace SharpCompress.Common; - -public class ReaderCancelledException : Exception -{ - public ReaderCancelledException(string message) - : base(message) { } - - public ReaderCancelledException(string message, Exception inner) - : base(message, inner) { } -} diff --git a/src/SharpCompress/Common/SevenZip/ArchiveReader.cs b/src/SharpCompress/Common/SevenZip/ArchiveReader.cs index 3e506e0e..45eb702f 100644 --- a/src/SharpCompress/Common/SevenZip/ArchiveReader.cs +++ b/src/SharpCompress/Common/SevenZip/ArchiveReader.cs @@ -784,7 +784,7 @@ out digests ); break; default: - throw new InvalidOperationException(); + throw new InvalidFormatException(); } } } @@ -843,7 +843,7 @@ out var digests outStream.ReadExact(data, 0, data.Length); if (outStream.ReadByte() >= 0) { - throw new InvalidOperationException("Decoded stream is longer than expected."); + throw new InvalidFormatException("Decoded stream is longer than expected."); } dataVector.Add(data); @@ -854,9 +854,9 @@ out var digests != folder._unpackCrc ) { - throw new InvalidOperationException( - "Decoded stream does not match expected CRC." - ); + throw new InvalidFormatException( + "Decoded stream does not match expected CRC." + ); } } } diff --git a/src/SharpCompress/Common/SevenZip/SevenZipFilePart.cs b/src/SharpCompress/Common/SevenZip/SevenZipFilePart.cs index fe221b53..ac355828 100644 --- a/src/SharpCompress/Common/SevenZip/SevenZipFilePart.cs +++ b/src/SharpCompress/Common/SevenZip/SevenZipFilePart.cs @@ -41,7 +41,7 @@ internal override Stream GetCompressedStream() { if (!Header.HasStream) { - throw new InvalidOperationException("File does not have a stream."); + throw new InvalidFormatException("File does not have a stream."); } var folderStream = _database.GetFolderStream(_stream, Folder!, _database.PasswordProvider); @@ -86,7 +86,7 @@ private CompressionType GetCompression() K_LZMA or K_LZMA2 => CompressionType.LZMA, K_PPMD => CompressionType.PPMd, K_B_ZIP2 => CompressionType.BZip2, - _ => throw new NotImplementedException() + _ => throw new InvalidFormatException() }; } diff --git a/src/SharpCompress/Common/SharpCompressException.cs b/src/SharpCompress/Common/SharpCompressException.cs new file mode 100644 index 00000000..7c68863d --- /dev/null +++ b/src/SharpCompress/Common/SharpCompressException.cs @@ -0,0 +1,52 @@ +using System; + +namespace SharpCompress.Common; + +public class SharpCompressException : ApplicationException +{ + public SharpCompressException() + { } + public SharpCompressException(string message) + : base(message) { } + + public SharpCompressException(string message, Exception inner) + : base(message, inner) { } +} + + + +public class ArchiveException(string message) : SharpCompressException(message); + +public class IncompleteArchiveException(string message) : ArchiveException(message); + + +public class CryptographicException(string message) : SharpCompressException(message); + +public class ReaderCancelledException(string message) : SharpCompressException(message); + +public class ExtractionException : SharpCompressException +{ + public ExtractionException() + { } + public ExtractionException(string message) + : base(message) { } + + public ExtractionException(string message, Exception inner) + : base(message, inner) { } +} + +public class MultipartStreamRequiredException(string message) : ExtractionException(message); + +public class MultiVolumeExtractionException(string message) : ExtractionException(message); + +public class InvalidFormatException : ExtractionException +{ + public InvalidFormatException() + { } + public InvalidFormatException(string message) + : base(message) { } + + public InvalidFormatException(string message, Exception inner) + : base(message, inner) { } +} + diff --git a/src/SharpCompress/Common/Tar/Headers/TarHeader.cs b/src/SharpCompress/Common/Tar/Headers/TarHeader.cs index 68a72e8a..8f87a028 100644 --- a/src/SharpCompress/Common/Tar/Headers/TarHeader.cs +++ b/src/SharpCompress/Common/Tar/Headers/TarHeader.cs @@ -188,7 +188,7 @@ private static byte[] ReadBlock(BinaryReader reader) if (buffer.Length != 0 && buffer.Length < BLOCK_SIZE) { - throw new InvalidOperationException("Buffer is invalid size"); + throw new InvalidFormatException("Buffer is invalid size"); } return buffer; } diff --git a/src/SharpCompress/Compressors/Deflate/Zlib.cs b/src/SharpCompress/Compressors/Deflate/Zlib.cs index 6fbeb4d9..b395029e 100644 --- a/src/SharpCompress/Compressors/Deflate/Zlib.cs +++ b/src/SharpCompress/Compressors/Deflate/Zlib.cs @@ -64,6 +64,7 @@ using System; using System.IO; +using SharpCompress.Common; namespace SharpCompress.Compressors.Deflate; @@ -177,7 +178,7 @@ public enum CompressionStrategy /// /// A general purpose exception class for exceptions in the Zlib library. /// -public class ZlibException : Exception +public class ZlibException : SharpCompressException { /// /// The ZlibException class captures exception information generated diff --git a/src/SharpCompress/Compressors/Deflate64/Deflate64Stream.cs b/src/SharpCompress/Compressors/Deflate64/Deflate64Stream.cs index da4117b9..e6a3fdfd 100644 --- a/src/SharpCompress/Compressors/Deflate64/Deflate64Stream.cs +++ b/src/SharpCompress/Compressors/Deflate64/Deflate64Stream.cs @@ -8,6 +8,7 @@ using System.Diagnostics; using System.IO; using System.Runtime.CompilerServices; +using SharpCompress.Common; using SharpCompress.Common.Zip; namespace SharpCompress.Compressors.Deflate64; @@ -151,7 +152,7 @@ public override int Read(byte[] array, int offset, int count) { // The stream is either malicious or poorly implemented and returned a number of // bytes larger than the buffer supplied to it. - throw new InvalidDataException("Deflate64: invalid data"); + throw new InvalidFormatException("Deflate64: invalid data"); } _inflater.SetInput(_buffer, 0, bytes); diff --git a/src/SharpCompress/Compressors/Deflate64/HuffmanTree.cs b/src/SharpCompress/Compressors/Deflate64/HuffmanTree.cs index e37802bb..4c9e7e8b 100644 --- a/src/SharpCompress/Compressors/Deflate64/HuffmanTree.cs +++ b/src/SharpCompress/Compressors/Deflate64/HuffmanTree.cs @@ -5,6 +5,7 @@ using System; using System.Diagnostics; using System.IO; +using SharpCompress.Common; namespace SharpCompress.Compressors.Deflate64; @@ -192,7 +193,7 @@ private void CreateTable() var increment = 1 << len; if (start >= increment) { - throw new InvalidDataException("Deflate64: invalid Huffman data"); + throw new InvalidFormatException("Deflate64: invalid Huffman data"); } // Note the bits in the table are reverted. @@ -234,7 +235,7 @@ private void CreateTable() if (value > 0) { // prevent an IndexOutOfRangeException from array[index] - throw new InvalidDataException("Deflate64: invalid Huffman data"); + throw new InvalidFormatException("Deflate64: invalid Huffman data"); } Debug.Assert( @@ -307,7 +308,7 @@ public int GetNextSymbol(InputBuffer input) // huffman code lengths must be at least 1 bit long if (codeLength <= 0) { - throw new InvalidDataException("Deflate64: invalid Huffman data"); + throw new InvalidFormatException("Deflate64: invalid Huffman data"); } // diff --git a/src/SharpCompress/Compressors/Deflate64/InflaterManaged.cs b/src/SharpCompress/Compressors/Deflate64/InflaterManaged.cs index 4e11854f..2e4d8377 100644 --- a/src/SharpCompress/Compressors/Deflate64/InflaterManaged.cs +++ b/src/SharpCompress/Compressors/Deflate64/InflaterManaged.cs @@ -31,6 +31,7 @@ using System; using System.Diagnostics; using System.IO; +using SharpCompress.Compressors.Deflate; namespace SharpCompress.Compressors.Deflate64; @@ -385,7 +386,7 @@ private bool Decode() } else { - throw new InvalidDataException("Deflate64: unknown block type"); + throw new ZlibException("Deflate64: unknown block type"); } } @@ -411,7 +412,7 @@ private bool Decode() } else { - throw new InvalidDataException("Deflate64: unknown block type"); + throw new ZlibException("Deflate64: unknown block type"); } // @@ -473,7 +474,7 @@ private bool DecodeUncompressedBlock(out bool endOfBlock) // make sure complement matches if ((ushort)_blockLength != (ushort)(~blockLengthComplement)) { - throw new InvalidDataException("Deflate64: invalid block length"); + throw new ZlibException("Deflate64: invalid block length"); } } @@ -507,7 +508,7 @@ private bool DecodeUncompressedBlock(out bool endOfBlock) default: Debug. /*Fail*/ Assert(false, "check why we are here!"); - throw new InvalidDataException("Deflate64: unknown state"); + throw new ZlibException("Deflate64: unknown state"); } } } @@ -569,7 +570,7 @@ private bool DecodeBlock(out bool endOfBlockCodeSeen) { if (symbol < 0 || symbol >= S_EXTRA_LENGTH_BITS.Length) { - throw new InvalidDataException("Deflate64: invalid data"); + throw new ZlibException("Deflate64: invalid data"); } _extraBits = S_EXTRA_LENGTH_BITS[symbol]; Debug.Assert(_extraBits != 0, "We handle other cases separately!"); @@ -591,7 +592,7 @@ private bool DecodeBlock(out bool endOfBlockCodeSeen) if (_length < 0 || _length >= S_LENGTH_BASE.Length) { - throw new InvalidDataException("Deflate64: invalid data"); + throw new ZlibException("Deflate64: invalid data"); } _length = S_LENGTH_BASE[_length] + bits; } @@ -649,7 +650,7 @@ private bool DecodeBlock(out bool endOfBlockCodeSeen) default: Debug. /*Fail*/ Assert(false, "check why we are here!"); - throw new InvalidDataException("Deflate64: unknown state"); + throw new ZlibException("Deflate64: unknown state"); } } @@ -781,7 +782,7 @@ private bool DecodeDynamicBlockHeader() if (_loopCounter == 0) { // can't have "prev code" on first code - throw new InvalidDataException(); + throw new ZlibException(); } var previousCode = _codeList[_loopCounter - 1]; @@ -789,7 +790,7 @@ private bool DecodeDynamicBlockHeader() if (_loopCounter + repeatCount > _codeArraySize) { - throw new InvalidDataException(); + throw new ZlibException(); } for (var j = 0; j < repeatCount; j++) @@ -809,7 +810,7 @@ private bool DecodeDynamicBlockHeader() if (_loopCounter + repeatCount > _codeArraySize) { - throw new InvalidDataException(); + throw new ZlibException(); } for (var j = 0; j < repeatCount; j++) @@ -830,7 +831,7 @@ private bool DecodeDynamicBlockHeader() if (_loopCounter + repeatCount > _codeArraySize) { - throw new InvalidDataException(); + throw new ZlibException(); } for (var j = 0; j < repeatCount; j++) @@ -846,7 +847,7 @@ private bool DecodeDynamicBlockHeader() default: Debug. /*Fail*/ Assert(false, "check why we are here!"); - throw new InvalidDataException("Deflate64: unknown state"); + throw new ZlibException("Deflate64: unknown state"); } var literalTreeCodeLength = new byte[HuffmanTree.MAX_LITERAL_TREE_ELEMENTS]; @@ -865,7 +866,7 @@ private bool DecodeDynamicBlockHeader() // Make sure there is an end-of-block code, otherwise how could we ever end? if (literalTreeCodeLength[HuffmanTree.END_OF_BLOCK_CODE] == 0) { - throw new InvalidDataException(); + throw new ZlibException(); } _literalLengthTree = new HuffmanTree(literalTreeCodeLength); diff --git a/src/SharpCompress/Compressors/Filters/BranchExecFilter.cs b/src/SharpCompress/Compressors/Filters/BranchExecFilter.cs index d198cf8f..7f887720 100644 --- a/src/SharpCompress/Compressors/Filters/BranchExecFilter.cs +++ b/src/SharpCompress/Compressors/Filters/BranchExecFilter.cs @@ -7,6 +7,7 @@ using System; using System.IO; using System.Runtime.CompilerServices; +using SharpCompress.Common; namespace SharpCompress.Compressors.Filters; @@ -244,7 +245,7 @@ public static void IA64Converter(byte[] data, uint ip) long size = data.Length; if (size < 16) { - throw new InvalidDataException("Unexpected data size"); + throw new InvalidFormatException("Unexpected data size"); } size -= 16; diff --git a/src/SharpCompress/Compressors/LZMA/DecoderStream.cs b/src/SharpCompress/Compressors/LZMA/DecoderStream.cs index a3dbf37f..b54d89e3 100644 --- a/src/SharpCompress/Compressors/LZMA/DecoderStream.cs +++ b/src/SharpCompress/Compressors/LZMA/DecoderStream.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using SharpCompress.Common; using SharpCompress.Common.SevenZip; using SharpCompress.Compressors.LZMA.Utilites; using SharpCompress.IO; @@ -46,7 +47,7 @@ private static int FindCoderIndexForOutStreamIndex(CFolder folderInfo, int outSt } } - throw new InvalidOperationException("Could not link output stream to coder."); + throw new InvalidFormatException("Could not link output stream to coder."); } private static void FindPrimaryOutStreamIndex( @@ -75,7 +76,7 @@ out int primaryOutStreamIndex { if (foundPrimaryOutStream) { - throw new NotSupportedException("Multiple output streams."); + throw new InvalidFormatException("Multiple output streams."); } foundPrimaryOutStream = true; @@ -87,7 +88,7 @@ out int primaryOutStreamIndex if (!foundPrimaryOutStream) { - throw new NotSupportedException("No output stream."); + throw new InvalidFormatException("No output stream."); } } diff --git a/src/SharpCompress/Compressors/LZMA/LZipStream.cs b/src/SharpCompress/Compressors/LZMA/LZipStream.cs index 5e987214..b7c69150 100644 --- a/src/SharpCompress/Compressors/LZMA/LZipStream.cs +++ b/src/SharpCompress/Compressors/LZMA/LZipStream.cs @@ -1,6 +1,7 @@ using System; using System.Buffers.Binary; using System.IO; +using SharpCompress.Common; using SharpCompress.Crypto; using SharpCompress.IO; @@ -32,7 +33,7 @@ public LZipStream(Stream stream, CompressionMode mode) var dSize = ValidateAndReadSize(stream); if (dSize == 0) { - throw new IOException("Not an LZip stream"); + throw new InvalidFormatException("Not an LZip stream"); } var properties = GetProperties(dSize); _stream = new LzmaStream(properties, stream); @@ -167,11 +168,6 @@ public override void WriteByte(byte value) /// public static int ValidateAndReadSize(Stream stream) { - if (stream is null) - { - throw new ArgumentNullException(nameof(stream)); - } - // Read the header Span header = stackalloc byte[6]; var n = stream.Read(header); @@ -198,34 +194,27 @@ public static int ValidateAndReadSize(Stream stream) return (1 << basePower) - (subtractionNumerator * (1 << (basePower - 4))); } - private static readonly byte[] headerBytes = new byte[6] - { + private static readonly byte[] headerBytes = + [ (byte)'L', (byte)'Z', (byte)'I', (byte)'P', 1, 113 - }; + ]; - public static void WriteHeaderSize(Stream stream) - { - if (stream is null) - { - throw new ArgumentNullException(nameof(stream)); - } + public static void WriteHeaderSize(Stream stream) => // hard coding the dictionary size encoding stream.Write(headerBytes, 0, 6); - } /// /// Creates a byte array to communicate the parameters and dictionary size to LzmaStream. /// private static byte[] GetProperties(int dictionarySize) => - new byte[] - { - // Parameters as per http://www.nongnu.org/lzip/manual/lzip_manual.html#Stream-format + [ + // Parameters as per http://www.nongnu.org/lzip/manual/lzip_manual.html#Stream-format // but encoded as a single byte in the format LzmaStream expects. // literal_context_bits = 3 // literal_pos_state_bits = 0 @@ -236,5 +225,5 @@ private static byte[] GetProperties(int dictionarySize) => (byte)((dictionarySize >> 8) & 0xff), (byte)((dictionarySize >> 16) & 0xff), (byte)((dictionarySize >> 24) & 0xff) - }; + ]; } diff --git a/src/SharpCompress/Compressors/LZMA/LzmaEncoder.cs b/src/SharpCompress/Compressors/LZMA/LzmaEncoder.cs index 509e54c8..0c2a91dc 100644 --- a/src/SharpCompress/Compressors/LZMA/LzmaEncoder.cs +++ b/src/SharpCompress/Compressors/LZMA/LzmaEncoder.cs @@ -2,6 +2,7 @@ using System; using System.IO; +using SharpCompress.Common; using SharpCompress.Compressors.LZMA.LZ; using SharpCompress.Compressors.LZMA.RangeCoder; @@ -1611,7 +1612,7 @@ public void Train(Stream trainStream) { if (_nowPos64 > 0) { - throw new InvalidOperationException(); + throw new InvalidFormatException(); } _trainSize = (uint)trainStream.Length; if (_trainSize > 0) diff --git a/src/SharpCompress/Compressors/Xz/Filters/ArmFilter.cs b/src/SharpCompress/Compressors/Xz/Filters/ArmFilter.cs index af1e99d3..cff95778 100644 --- a/src/SharpCompress/Compressors/Xz/Filters/ArmFilter.cs +++ b/src/SharpCompress/Compressors/Xz/Filters/ArmFilter.cs @@ -5,6 +5,7 @@ */ using System.IO; +using SharpCompress.Common; using SharpCompress.Compressors.Filters; namespace SharpCompress.Compressors.Xz.Filters; @@ -25,19 +26,19 @@ public override void Init(byte[] properties) { if (properties.Length != 0 && properties.Length != 4) { - throw new InvalidDataException("ARM properties unexpected length"); + throw new InvalidFormatException("ARM properties unexpected length"); } if (properties.Length == 4) { // Even XZ doesn't support it. - throw new InvalidDataException("ARM properties offset is not supported"); + throw new InvalidFormatException("ARM properties offset is not supported"); //_offset = BitConverter.ToUInt32(properties, 0); // //if (_offset % (UInt32)BranchExec.Alignment.ARCH_ARM_ALIGNMENT != 0) //{ - // throw new InvalidDataException("Filter offset does not match alignment"); + // throw new InvalidFormatException("Filter offset does not match alignment"); //} } } diff --git a/src/SharpCompress/Compressors/Xz/Filters/ArmThumbFilter.cs b/src/SharpCompress/Compressors/Xz/Filters/ArmThumbFilter.cs index f3ec7b1b..1bcfcdc9 100644 --- a/src/SharpCompress/Compressors/Xz/Filters/ArmThumbFilter.cs +++ b/src/SharpCompress/Compressors/Xz/Filters/ArmThumbFilter.cs @@ -5,6 +5,7 @@ */ using System.IO; +using SharpCompress.Common; using SharpCompress.Compressors.Filters; namespace SharpCompress.Compressors.Xz.Filters; @@ -25,19 +26,19 @@ public override void Init(byte[] properties) { if (properties.Length != 0 && properties.Length != 4) { - throw new InvalidDataException("ARM Thumb properties unexpected length"); + throw new InvalidFormatException("ARM Thumb properties unexpected length"); } if (properties.Length == 4) { // Even XZ doesn't support it. - throw new InvalidDataException("ARM Thumb properties offset is not supported"); + throw new InvalidFormatException("ARM Thumb properties offset is not supported"); //_offset = BitConverter.ToUInt32(properties, 0); // //if (_offset % (UInt32)BranchExec.Alignment.ARCH_ARMTHUMB_ALIGNMENT != 0) //{ - // throw new InvalidDataException("Filter offset does not match alignment"); + // throw new InvalidFormatException("Filter offset does not match alignment"); //} } } diff --git a/src/SharpCompress/Compressors/Xz/Filters/BlockFilter.cs b/src/SharpCompress/Compressors/Xz/Filters/BlockFilter.cs index 936a3a0d..a7e3482a 100644 --- a/src/SharpCompress/Compressors/Xz/Filters/BlockFilter.cs +++ b/src/SharpCompress/Compressors/Xz/Filters/BlockFilter.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.IO; +using SharpCompress.Common; namespace SharpCompress.Compressors.Xz.Filters; @@ -50,7 +51,7 @@ public static BlockFilter Read(BinaryReader reader) var sizeOfProperties = reader.ReadXZInteger(); if (sizeOfProperties > int.MaxValue) { - throw new InvalidDataException("Block filter information too large"); + throw new InvalidFormatException("Block filter information too large"); } var properties = reader.ReadBytes((int)sizeOfProperties); diff --git a/src/SharpCompress/Compressors/Xz/Filters/IA64Filter.cs b/src/SharpCompress/Compressors/Xz/Filters/IA64Filter.cs index dc04c71b..14f8cdad 100644 --- a/src/SharpCompress/Compressors/Xz/Filters/IA64Filter.cs +++ b/src/SharpCompress/Compressors/Xz/Filters/IA64Filter.cs @@ -5,6 +5,7 @@ */ using System.IO; +using SharpCompress.Common; using SharpCompress.Compressors.Filters; namespace SharpCompress.Compressors.Xz.Filters; @@ -25,19 +26,19 @@ public override void Init(byte[] properties) { if (properties.Length != 0 && properties.Length != 4) { - throw new InvalidDataException("IA64 properties unexpected length"); + throw new InvalidFormatException("IA64 properties unexpected length"); } if (properties.Length == 4) { // Even XZ doesn't support it. - throw new InvalidDataException("IA64 properties offset is not supported"); + throw new InvalidFormatException("IA64 properties offset is not supported"); //_offset = BitConverter.ToUInt32(properties, 0); // //if (_offset % (UInt32)BranchExec.Alignment.ARCH_IA64_ALIGNMENT != 0) //{ - // throw new InvalidDataException("Filter offset does not match alignment"); + // throw new InvalidFormatException("Filter offset does not match alignment"); //} } } diff --git a/src/SharpCompress/Compressors/Xz/Filters/Lzma2Filter.cs b/src/SharpCompress/Compressors/Xz/Filters/Lzma2Filter.cs index bed59b76..ea078c9d 100644 --- a/src/SharpCompress/Compressors/Xz/Filters/Lzma2Filter.cs +++ b/src/SharpCompress/Compressors/Xz/Filters/Lzma2Filter.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using SharpCompress.Common; using SharpCompress.Compressors.LZMA; namespace SharpCompress.Compressors.Xz.Filters; @@ -35,14 +36,14 @@ public override void Init(byte[] properties) { if (properties.Length != 1) { - throw new InvalidDataException("LZMA properties unexpected length"); + throw new InvalidFormatException("LZMA properties unexpected length"); } _dictionarySize = (byte)(properties[0] & 0x3F); var reserved = properties[0] & 0xC0; if (reserved != 0) { - throw new InvalidDataException("Reserved bits used in LZMA properties"); + throw new InvalidFormatException("Reserved bits used in LZMA properties"); } } diff --git a/src/SharpCompress/Compressors/Xz/Filters/PowerPCFilter.cs b/src/SharpCompress/Compressors/Xz/Filters/PowerPCFilter.cs index 7a03a3fe..b171fa6c 100644 --- a/src/SharpCompress/Compressors/Xz/Filters/PowerPCFilter.cs +++ b/src/SharpCompress/Compressors/Xz/Filters/PowerPCFilter.cs @@ -5,6 +5,7 @@ */ using System.IO; +using SharpCompress.Common; using SharpCompress.Compressors.Filters; namespace SharpCompress.Compressors.Xz.Filters; @@ -25,19 +26,19 @@ public override void Init(byte[] properties) { if (properties.Length != 0 && properties.Length != 4) { - throw new InvalidDataException("PPC properties unexpected length"); + throw new InvalidFormatException("PPC properties unexpected length"); } if (properties.Length == 4) { // Even XZ doesn't support it. - throw new InvalidDataException("PPC properties offset is not supported"); + throw new InvalidFormatException("PPC properties offset is not supported"); //_offset = BitConverter.ToUInt32(properties, 0); // //if (_offset % (UInt32)BranchExec.Alignment.ARCH_PowerPC_ALIGNMENT != 0) //{ - // throw new InvalidDataException("Filter offset does not match alignment"); + // throw new InvalidFormatException("Filter offset does not match alignment"); //} } } diff --git a/src/SharpCompress/Compressors/Xz/Filters/SparcFilter.cs b/src/SharpCompress/Compressors/Xz/Filters/SparcFilter.cs index 9b74d344..8b3a4532 100644 --- a/src/SharpCompress/Compressors/Xz/Filters/SparcFilter.cs +++ b/src/SharpCompress/Compressors/Xz/Filters/SparcFilter.cs @@ -5,6 +5,7 @@ */ using System.IO; +using SharpCompress.Common; using SharpCompress.Compressors.Filters; namespace SharpCompress.Compressors.Xz.Filters; @@ -25,19 +26,19 @@ public override void Init(byte[] properties) { if (properties.Length != 0 && properties.Length != 4) { - throw new InvalidDataException("SPARC properties unexpected length"); + throw new InvalidFormatException("SPARC properties unexpected length"); } if (properties.Length == 4) { // Even XZ doesn't support it. - throw new InvalidDataException("SPARC properties offset is not supported"); + throw new InvalidFormatException("SPARC properties offset is not supported"); //_offset = BitConverter.ToUInt32(properties, 0); // //if (_offset % (UInt32)BranchExec.Alignment.ARCH_SPARC_ALIGNMENT != 0) //{ - // throw new InvalidDataException("Filter offset does not match alignment"); + // throw new InvalidFormatException("Filter offset does not match alignment"); //} } } diff --git a/src/SharpCompress/Compressors/Xz/Filters/X86Filter.cs b/src/SharpCompress/Compressors/Xz/Filters/X86Filter.cs index 74dbfb1d..fd8ef42a 100644 --- a/src/SharpCompress/Compressors/Xz/Filters/X86Filter.cs +++ b/src/SharpCompress/Compressors/Xz/Filters/X86Filter.cs @@ -5,6 +5,7 @@ */ using System.IO; +using SharpCompress.Common; using SharpCompress.Compressors.Filters; namespace SharpCompress.Compressors.Xz.Filters; @@ -27,19 +28,19 @@ public override void Init(byte[] properties) { if (properties.Length != 0 && properties.Length != 4) { - throw new InvalidDataException("X86 properties unexpected length"); + throw new InvalidFormatException("X86 properties unexpected length"); } if (properties.Length == 4) { // Even XZ doesn't support it. - throw new InvalidDataException("X86 properties offset is not supported"); + throw new InvalidFormatException("X86 properties offset is not supported"); //_offset = BitConverter.ToUInt32(properties, 0); // //if (_offset % (UInt32)BranchExec.Alignment.ARCH_x86_ALIGNMENT != 0) //{ - // throw new InvalidDataException("Filter offset does not match alignment"); + // throw new InvalidFormatException("Filter offset does not match alignment"); //} } } diff --git a/src/SharpCompress/Compressors/Xz/MultiByteIntegers.cs b/src/SharpCompress/Compressors/Xz/MultiByteIntegers.cs index a38505da..8a0d81a3 100644 --- a/src/SharpCompress/Compressors/Xz/MultiByteIntegers.cs +++ b/src/SharpCompress/Compressors/Xz/MultiByteIntegers.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using SharpCompress.Common; namespace SharpCompress.Compressors.Xz; @@ -25,13 +26,13 @@ public static ulong ReadXZInteger(this BinaryReader reader, int MaxBytes = 9) { if (++i >= MaxBytes) { - throw new InvalidDataException(); + throw new InvalidFormatException(); } LastByte = reader.ReadByte(); if (LastByte == 0) { - throw new InvalidDataException(); + throw new InvalidFormatException(); } Output |= ((ulong)(LastByte & 0x7F)) << (i * 7); diff --git a/src/SharpCompress/Compressors/Xz/XZBlock.cs b/src/SharpCompress/Compressors/Xz/XZBlock.cs index ddd7eaf3..34ce2333 100644 --- a/src/SharpCompress/Compressors/Xz/XZBlock.cs +++ b/src/SharpCompress/Compressors/Xz/XZBlock.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using SharpCompress.Common; using SharpCompress.Compressors.Xz.Filters; namespace SharpCompress.Compressors.Xz; @@ -80,7 +81,7 @@ private void SkipPadding() BaseStream.Read(paddingBytes, 0, paddingBytes.Length); if (paddingBytes.Any(b => b != 0)) { - throw new InvalidDataException("Padding bytes were non-null"); + throw new InvalidFormatException("Padding bytes were non-null"); } } _paddingSkipped = true; @@ -145,7 +146,7 @@ private byte[] CacheHeader() var calcCrc = Crc32.Compute(blockHeaderWithoutCrc); if (crc != calcCrc) { - throw new InvalidDataException("Block header corrupt"); + throw new InvalidFormatException("Block header corrupt"); } return blockHeaderWithoutCrc; @@ -159,7 +160,7 @@ private void ReadBlockFlags(BinaryReader reader) if (reserved != 0) { - throw new InvalidDataException( + throw new InvalidFormatException( "Reserved bytes used, perhaps an unknown XZ implementation" ); } @@ -189,7 +190,7 @@ private void ReadFilters(BinaryReader reader, long baseStreamOffset = 0) || (i + 1 < _numFilters && !filter.AllowAsNonLast) ) { - throw new InvalidDataException("Block Filters in bad order"); + throw new InvalidFormatException("Block Filters in bad order"); } if (filter.ChangesDataSize && i + 1 < _numFilters) @@ -202,7 +203,7 @@ private void ReadFilters(BinaryReader reader, long baseStreamOffset = 0) } if (nonLastSizeChangers > 2) { - throw new InvalidDataException( + throw new InvalidFormatException( "More than two non-last block filters cannot change stream size" ); } @@ -212,7 +213,7 @@ private void ReadFilters(BinaryReader reader, long baseStreamOffset = 0) var blockHeaderPadding = reader.ReadBytes(blockHeaderPaddingSize); if (!blockHeaderPadding.All(b => b == 0)) { - throw new InvalidDataException("Block header contains unknown fields"); + throw new InvalidFormatException("Block header contains unknown fields"); } } } diff --git a/src/SharpCompress/Compressors/Xz/XZFooter.cs b/src/SharpCompress/Compressors/Xz/XZFooter.cs index 9751fc2b..9b2dd6e2 100644 --- a/src/SharpCompress/Compressors/Xz/XZFooter.cs +++ b/src/SharpCompress/Compressors/Xz/XZFooter.cs @@ -1,6 +1,7 @@ using System; using System.IO; using System.Text; +using SharpCompress.Common; using SharpCompress.IO; namespace SharpCompress.Compressors.Xz; @@ -35,7 +36,7 @@ public void Process() var myCrc = Crc32.Compute(footerBytes); if (crc != myCrc) { - throw new InvalidDataException("Footer corrupt"); + throw new InvalidFormatException("Footer corrupt"); } using (var stream = new MemoryStream(footerBytes)) @@ -47,7 +48,7 @@ public void Process() var magBy = _reader.ReadBytes(2); if (!magBy.AsSpan().SequenceEqual(_magicBytes)) { - throw new InvalidDataException("Magic footer missing"); + throw new InvalidFormatException("Magic footer missing"); } } } diff --git a/src/SharpCompress/Compressors/Xz/XZHeader.cs b/src/SharpCompress/Compressors/Xz/XZHeader.cs index a5ed8c4a..1dfe296a 100644 --- a/src/SharpCompress/Compressors/Xz/XZHeader.cs +++ b/src/SharpCompress/Compressors/Xz/XZHeader.cs @@ -1,6 +1,7 @@ using System.IO; using System.Linq; using System.Text; +using SharpCompress.Common; using SharpCompress.IO; namespace SharpCompress.Compressors.Xz; @@ -37,14 +38,14 @@ private void ProcessStreamFlags() var calcCrc = Crc32.Compute(streamFlags); if (crc != calcCrc) { - throw new InvalidDataException("Stream header corrupt"); + throw new InvalidFormatException("Stream header corrupt"); } BlockCheckType = (CheckType)(streamFlags[1] & 0x0F); var futureUse = (byte)(streamFlags[1] & 0xF0); if (futureUse != 0 || streamFlags[0] != 0) { - throw new InvalidDataException("Unknown XZ Stream Version"); + throw new InvalidFormatException("Unknown XZ Stream Version"); } } @@ -52,7 +53,7 @@ private void CheckMagicBytes(byte[] header) { if (!header.SequenceEqual(MagicHeader)) { - throw new InvalidDataException("Invalid XZ Stream"); + throw new InvalidFormatException("Invalid XZ Stream"); } } } diff --git a/src/SharpCompress/Compressors/Xz/XZIndex.cs b/src/SharpCompress/Compressors/Xz/XZIndex.cs index ddfd7c99..386c15da 100644 --- a/src/SharpCompress/Compressors/Xz/XZIndex.cs +++ b/src/SharpCompress/Compressors/Xz/XZIndex.cs @@ -3,6 +3,7 @@ using System.IO; using System.Linq; using System.Text; +using SharpCompress.Common; using SharpCompress.IO; namespace SharpCompress.Compressors.Xz; @@ -59,7 +60,7 @@ private void VerifyIndexMarker() var marker = _reader.ReadByte(); if (marker != 0) { - throw new InvalidDataException("Not an index block"); + throw new InvalidFormatException("Not an index block"); } } @@ -71,7 +72,7 @@ private void SkipPadding() var paddingBytes = _reader.ReadBytes(4 - bytes); if (paddingBytes.Any(b => b != 0)) { - throw new InvalidDataException("Padding bytes were non-null"); + throw new InvalidFormatException("Padding bytes were non-null"); } } } diff --git a/src/SharpCompress/Compressors/Xz/XZReadOnlyStream.cs b/src/SharpCompress/Compressors/Xz/XZReadOnlyStream.cs index 4c1aa02b..fd947cc9 100644 --- a/src/SharpCompress/Compressors/Xz/XZReadOnlyStream.cs +++ b/src/SharpCompress/Compressors/Xz/XZReadOnlyStream.cs @@ -1,4 +1,5 @@ using System.IO; +using SharpCompress.Common; namespace SharpCompress.Compressors.Xz; @@ -9,7 +10,7 @@ public XZReadOnlyStream(Stream stream) BaseStream = stream; if (!BaseStream.CanRead) { - throw new InvalidDataException("Must be able to read from stream"); + throw new InvalidFormatException("Must be able to read from stream"); } } } diff --git a/src/SharpCompress/Compressors/Xz/XZStream.cs b/src/SharpCompress/Compressors/Xz/XZStream.cs index 26d3dcb2..eff08a1e 100644 --- a/src/SharpCompress/Compressors/Xz/XZStream.cs +++ b/src/SharpCompress/Compressors/Xz/XZStream.cs @@ -2,6 +2,7 @@ using System; using System.IO; +using SharpCompress.Common; namespace SharpCompress.Compressors.Xz; @@ -33,7 +34,7 @@ private void AssertBlockCheckTypeIsSupported() case CheckType.SHA256: throw new NotImplementedException(); default: - throw new NotSupportedException("Check Type unknown to this version of decoder."); + throw new InvalidFormatException("Check Type unknown to this version of decoder."); } } diff --git a/src/SharpCompress/IO/MarkingBinaryReader.cs b/src/SharpCompress/IO/MarkingBinaryReader.cs index 424b9e08..be26df86 100644 --- a/src/SharpCompress/IO/MarkingBinaryReader.cs +++ b/src/SharpCompress/IO/MarkingBinaryReader.cs @@ -1,6 +1,7 @@ using System; using System.Buffers.Binary; using System.IO; +using SharpCompress.Common; namespace SharpCompress.IO; @@ -44,7 +45,7 @@ public override byte[] ReadBytes(int count) var bytes = base.ReadBytes(count); if (bytes.Length != count) { - throw new EndOfStreamException( + throw new InvalidFormatException( string.Format( "Could not read the requested amount of bytes. End of stream reached. Requested: {0} Read: {1}", count, diff --git a/src/SharpCompress/Readers/ReaderFactory.cs b/src/SharpCompress/Readers/ReaderFactory.cs index a079c235..2cb4865f 100644 --- a/src/SharpCompress/Readers/ReaderFactory.cs +++ b/src/SharpCompress/Readers/ReaderFactory.cs @@ -1,6 +1,7 @@ using System; using System.IO; using System.Linq; +using SharpCompress.Common; using SharpCompress.IO; namespace SharpCompress.Readers; @@ -29,8 +30,8 @@ public static IReader Open(Stream stream, ReaderOptions? options = null) } } - throw new InvalidOperationException( - "Cannot determine compressed stream type. Supported Reader Formats: Zip, GZip, BZip2, Tar, Rar, LZip, XZ" - ); + throw new InvalidFormatException( + "Cannot determine compressed stream type. Supported Reader Formats: Zip, GZip, BZip2, Tar, Rar, LZip, XZ" + ); } } diff --git a/tests/SharpCompress.Test/Xz/Filters/BCJTests.cs b/tests/SharpCompress.Test/Xz/Filters/BCJTests.cs index bc16789d..9dbdf348 100644 --- a/tests/SharpCompress.Test/Xz/Filters/BCJTests.cs +++ b/tests/SharpCompress.Test/Xz/Filters/BCJTests.cs @@ -4,6 +4,7 @@ */ using System.IO; +using SharpCompress.Common; using SharpCompress.Compressors.Xz.Filters; using Xunit; @@ -66,23 +67,23 @@ public void ChangesStreamSize() [InlineData(new byte[] { 0, 0, 0, 0, 0 })] public void OnlyAcceptsOneByte(byte[] bytes) { - InvalidDataException ex; - ex = Assert.Throws(() => _armFilter.Init(bytes)); + InvalidFormatException ex; + ex = Assert.Throws(() => _armFilter.Init(bytes)); Assert.Equal("ARM properties unexpected length", ex.Message); - ex = Assert.Throws(() => _armtFilter.Init(bytes)); + ex = Assert.Throws(() => _armtFilter.Init(bytes)); Assert.Equal("ARM Thumb properties unexpected length", ex.Message); - ex = Assert.Throws(() => _ia64Filter.Init(bytes)); + ex = Assert.Throws(() => _ia64Filter.Init(bytes)); Assert.Equal("IA64 properties unexpected length", ex.Message); - ex = Assert.Throws(() => _ppcFilter.Init(bytes)); + ex = Assert.Throws(() => _ppcFilter.Init(bytes)); Assert.Equal("PPC properties unexpected length", ex.Message); - ex = Assert.Throws(() => _sparcFilter.Init(bytes)); + ex = Assert.Throws(() => _sparcFilter.Init(bytes)); Assert.Equal("SPARC properties unexpected length", ex.Message); - ex = Assert.Throws(() => _x86Filter.Init(bytes)); + ex = Assert.Throws(() => _x86Filter.Init(bytes)); Assert.Equal("X86 properties unexpected length", ex.Message); } } diff --git a/tests/SharpCompress.Test/Xz/Filters/Lzma2Tests.cs b/tests/SharpCompress.Test/Xz/Filters/Lzma2Tests.cs index e77bc2f8..133332c9 100644 --- a/tests/SharpCompress.Test/Xz/Filters/Lzma2Tests.cs +++ b/tests/SharpCompress.Test/Xz/Filters/Lzma2Tests.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using SharpCompress.Common; using SharpCompress.Compressors.Xz.Filters; using Xunit; @@ -52,14 +53,14 @@ public void CalculatesDictionarySizeError() [InlineData(new byte[] { 0, 0 })] public void OnlyAcceptsOneByte(byte[] bytes) { - var ex = Assert.Throws(() => _filter.Init(bytes)); + var ex = Assert.Throws(() => _filter.Init(bytes)); Assert.Equal("LZMA properties unexpected length", ex.Message); } [Fact] public void ReservedBytesThrow() { - var ex = Assert.Throws(() => _filter.Init([0xC0])); + var ex = Assert.Throws(() => _filter.Init([0xC0])); Assert.Equal("Reserved bits used in LZMA properties", ex.Message); } } diff --git a/tests/SharpCompress.Test/Xz/XZBlockTests.cs b/tests/SharpCompress.Test/Xz/XZBlockTests.cs index 78873590..f2ac9b68 100644 --- a/tests/SharpCompress.Test/Xz/XZBlockTests.cs +++ b/tests/SharpCompress.Test/Xz/XZBlockTests.cs @@ -43,7 +43,7 @@ public void CrcIncorrectThrows() using Stream badCrcStream = new MemoryStream(bytes); Rewind(badCrcStream); var xzBlock = new XZBlock(badCrcStream, CheckType.CRC64, 8); - var ex = Assert.Throws(() => + var ex = Assert.Throws(() => { ReadBytes(xzBlock, 1); }); diff --git a/tests/SharpCompress.Test/Xz/XZHeaderTests.cs b/tests/SharpCompress.Test/Xz/XZHeaderTests.cs index 8815b7a9..c8f3ac5e 100644 --- a/tests/SharpCompress.Test/Xz/XZHeaderTests.cs +++ b/tests/SharpCompress.Test/Xz/XZHeaderTests.cs @@ -1,4 +1,5 @@ using System.IO; +using SharpCompress.Common; using SharpCompress.Compressors.Xz; using Xunit; @@ -14,7 +15,7 @@ public void ChecksMagicNumber() using Stream badMagicNumberStream = new MemoryStream(bytes); var br = new BinaryReader(badMagicNumberStream); var header = new XZHeader(br); - var ex = Assert.Throws(() => + var ex = Assert.Throws(() => { header.Process(); }); @@ -29,7 +30,7 @@ public void CorruptHeaderThrows() using Stream badCrcStream = new MemoryStream(bytes); var br = new BinaryReader(badCrcStream); var header = new XZHeader(br); - var ex = Assert.Throws(() => + var ex = Assert.Throws(() => { header.Process(); }); @@ -47,7 +48,7 @@ public void BadVersionIfCrcOkButStreamFlagUnknown() using Stream badFlagStream = new MemoryStream(bytes); var br = new BinaryReader(badFlagStream); var header = new XZHeader(br); - var ex = Assert.Throws(() => + var ex = Assert.Throws(() => { header.Process(); }); diff --git a/tests/SharpCompress.Test/Xz/XZIndexTests.cs b/tests/SharpCompress.Test/Xz/XZIndexTests.cs index b00b9d1a..5e1b55a8 100644 --- a/tests/SharpCompress.Test/Xz/XZIndexTests.cs +++ b/tests/SharpCompress.Test/Xz/XZIndexTests.cs @@ -1,4 +1,5 @@ using System.IO; +using SharpCompress.Common; using SharpCompress.Compressors.Xz; using Xunit; @@ -27,7 +28,7 @@ public void ThrowsIfHasNoIndexMarker() using Stream badStream = new MemoryStream([1, 2, 3, 4, 5]); var br = new BinaryReader(badStream); var index = new XZIndex(br, false); - Assert.Throws(() => index.Process()); + Assert.Throws(() => index.Process()); } [Fact] From a283d99e1b833ea2ea8181779593fe17d021fbd3 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Tue, 23 Apr 2024 14:48:33 +0100 Subject: [PATCH 2/5] compiles --- tests/SharpCompress.Test/Xz/XZBlockTests.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/SharpCompress.Test/Xz/XZBlockTests.cs b/tests/SharpCompress.Test/Xz/XZBlockTests.cs index f2ac9b68..0d6c7d9a 100644 --- a/tests/SharpCompress.Test/Xz/XZBlockTests.cs +++ b/tests/SharpCompress.Test/Xz/XZBlockTests.cs @@ -1,5 +1,6 @@ using System.IO; using System.Text; +using SharpCompress.Common; using SharpCompress.Compressors.Xz; using Xunit; From e31238d12134b8efa75046f9090cad56b0717709 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Tue, 23 Apr 2024 14:48:50 +0100 Subject: [PATCH 3/5] fmt --- .../Common/SevenZip/ArchiveReader.cs | 4 ++-- .../Common/SharpCompressException.cs | 16 ++++++---------- src/SharpCompress/Compressors/LZMA/LZipStream.cs | 7 +++---- src/SharpCompress/Readers/ReaderFactory.cs | 4 ++-- 4 files changed, 13 insertions(+), 18 deletions(-) diff --git a/src/SharpCompress/Common/SevenZip/ArchiveReader.cs b/src/SharpCompress/Common/SevenZip/ArchiveReader.cs index 45eb702f..96488124 100644 --- a/src/SharpCompress/Common/SevenZip/ArchiveReader.cs +++ b/src/SharpCompress/Common/SevenZip/ArchiveReader.cs @@ -855,8 +855,8 @@ out var digests ) { throw new InvalidFormatException( - "Decoded stream does not match expected CRC." - ); + "Decoded stream does not match expected CRC." + ); } } } diff --git a/src/SharpCompress/Common/SharpCompressException.cs b/src/SharpCompress/Common/SharpCompressException.cs index 7c68863d..78e8b9e3 100644 --- a/src/SharpCompress/Common/SharpCompressException.cs +++ b/src/SharpCompress/Common/SharpCompressException.cs @@ -4,8 +4,8 @@ namespace SharpCompress.Common; public class SharpCompressException : ApplicationException { - public SharpCompressException() - { } + public SharpCompressException() { } + public SharpCompressException(string message) : base(message) { } @@ -13,21 +13,18 @@ public SharpCompressException(string message, Exception inner) : base(message, inner) { } } - - public class ArchiveException(string message) : SharpCompressException(message); public class IncompleteArchiveException(string message) : ArchiveException(message); - public class CryptographicException(string message) : SharpCompressException(message); public class ReaderCancelledException(string message) : SharpCompressException(message); public class ExtractionException : SharpCompressException { - public ExtractionException() - { } + public ExtractionException() { } + public ExtractionException(string message) : base(message) { } @@ -41,12 +38,11 @@ public class MultiVolumeExtractionException(string message) : ExtractionExceptio public class InvalidFormatException : ExtractionException { - public InvalidFormatException() - { } + public InvalidFormatException() { } + public InvalidFormatException(string message) : base(message) { } public InvalidFormatException(string message, Exception inner) : base(message, inner) { } } - diff --git a/src/SharpCompress/Compressors/LZMA/LZipStream.cs b/src/SharpCompress/Compressors/LZMA/LZipStream.cs index b7c69150..5bb35a75 100644 --- a/src/SharpCompress/Compressors/LZMA/LZipStream.cs +++ b/src/SharpCompress/Compressors/LZMA/LZipStream.cs @@ -205,7 +205,6 @@ public static int ValidateAndReadSize(Stream stream) ]; public static void WriteHeaderSize(Stream stream) => - // hard coding the dictionary size encoding stream.Write(headerBytes, 0, 6); @@ -213,8 +212,8 @@ public static void WriteHeaderSize(Stream stream) => /// Creates a byte array to communicate the parameters and dictionary size to LzmaStream. /// private static byte[] GetProperties(int dictionarySize) => - [ - // Parameters as per http://www.nongnu.org/lzip/manual/lzip_manual.html#Stream-format + [ + // Parameters as per http://www.nongnu.org/lzip/manual/lzip_manual.html#Stream-format // but encoded as a single byte in the format LzmaStream expects. // literal_context_bits = 3 // literal_pos_state_bits = 0 @@ -225,5 +224,5 @@ private static byte[] GetProperties(int dictionarySize) => (byte)((dictionarySize >> 8) & 0xff), (byte)((dictionarySize >> 16) & 0xff), (byte)((dictionarySize >> 24) & 0xff) - ]; + ]; } diff --git a/src/SharpCompress/Readers/ReaderFactory.cs b/src/SharpCompress/Readers/ReaderFactory.cs index 2cb4865f..d56e30c1 100644 --- a/src/SharpCompress/Readers/ReaderFactory.cs +++ b/src/SharpCompress/Readers/ReaderFactory.cs @@ -31,7 +31,7 @@ public static IReader Open(Stream stream, ReaderOptions? options = null) } throw new InvalidFormatException( - "Cannot determine compressed stream type. Supported Reader Formats: Zip, GZip, BZip2, Tar, Rar, LZip, XZ" - ); + "Cannot determine compressed stream type. Supported Reader Formats: Zip, GZip, BZip2, Tar, Rar, LZip, XZ" + ); } } From e4d5b56951e66a1e7d3bbe95b74eb64e329e8595 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Tue, 23 Apr 2024 15:08:32 +0100 Subject: [PATCH 4/5] fix more nulls and tests --- .../Archives/GZip/GZipArchiveEntry.cs | 2 +- .../Archives/Rar/RarArchiveEntry.cs | 2 +- .../Archives/Tar/TarArchiveEntry.cs | 2 +- .../Archives/Zip/ZipArchiveEntry.cs | 2 +- src/SharpCompress/Common/FilePart.cs | 2 +- src/SharpCompress/Common/Rar/CryptKey5.cs | 2 +- .../Common/Rar/Headers/ArchiveCryptHeader.cs | 3 -- .../Common/Rar/Headers/FileHeader.cs | 49 +++++++++++-------- .../Common/Rar/Headers/RarHeader.cs | 3 +- .../Common/Rar/Headers/RarHeaderFactory.cs | 8 ++- src/SharpCompress/Common/Rar/RarEntry.cs | 4 +- src/SharpCompress/Common/Rar/RarVolume.cs | 2 +- .../Common/SevenZip/SevenZipFilePart.cs | 1 - .../Common/Zip/StreamingZipFilePart.cs | 1 - src/SharpCompress/Compressors/Deflate/Zlib.cs | 1 - .../Compressors/Deflate64/HuffmanTree.cs | 1 - .../Compressors/Deflate64/InflaterManaged.cs | 1 - .../Compressors/Filters/BranchExecFilter.cs | 1 - .../Compressors/Filters/DeltaFilter.cs | 1 - .../Compressors/Rar/RarBLAKE2spStream.cs | 3 +- .../Rar/UnpackV2017/Unpack.unpack50_cpp.cs | 1 - .../Compressors/Shrink/BitStream.cs | 6 --- .../Compressors/Shrink/ShrinkStream.cs | 4 -- src/SharpCompress/IO/DataDescriptorStream.cs | 1 - src/SharpCompress/NotNullExtensions.cs | 7 ++- src/SharpCompress/Readers/AbstractReader.cs | 2 +- .../Readers/Rar/NonSeekableStreamFilePart.cs | 2 +- src/SharpCompress/Readers/Rar/RarReader.cs | 2 +- src/SharpCompress/Readers/ReaderFactory.cs | 1 - .../GZip/GZipArchiveTests.cs | 4 +- .../SharpCompress.Test/Xz/Filters/BCJTests.cs | 1 - .../Xz/Filters/Lzma2Tests.cs | 1 - 32 files changed, 54 insertions(+), 69 deletions(-) diff --git a/src/SharpCompress/Archives/GZip/GZipArchiveEntry.cs b/src/SharpCompress/Archives/GZip/GZipArchiveEntry.cs index 459d042d..f00e889c 100644 --- a/src/SharpCompress/Archives/GZip/GZipArchiveEntry.cs +++ b/src/SharpCompress/Archives/GZip/GZipArchiveEntry.cs @@ -17,7 +17,7 @@ public virtual Stream OpenEntryStream() { part.GetRawStream().Position = part.EntryStartPosition; } - return Parts.Single().GetCompressedStream(); + return Parts.Single().GetCompressedStream().NotNull(); } #region IArchiveEntry Members diff --git a/src/SharpCompress/Archives/Rar/RarArchiveEntry.cs b/src/SharpCompress/Archives/Rar/RarArchiveEntry.cs index fa59b295..262d7cbe 100644 --- a/src/SharpCompress/Archives/Rar/RarArchiveEntry.cs +++ b/src/SharpCompress/Archives/Rar/RarArchiveEntry.cs @@ -42,7 +42,7 @@ public override long Crc { CheckIncomplete(); return BitConverter.ToUInt32( - parts.Select(fp => fp.FileHeader).Single(fh => !fh.IsSplitAfter).FileCrc, + parts.Select(fp => fp.FileHeader).Single(fh => !fh.IsSplitAfter).FileCrc.NotNull(), 0 ); } diff --git a/src/SharpCompress/Archives/Tar/TarArchiveEntry.cs b/src/SharpCompress/Archives/Tar/TarArchiveEntry.cs index d04c4ef8..770a7109 100644 --- a/src/SharpCompress/Archives/Tar/TarArchiveEntry.cs +++ b/src/SharpCompress/Archives/Tar/TarArchiveEntry.cs @@ -10,7 +10,7 @@ public class TarArchiveEntry : TarEntry, IArchiveEntry internal TarArchiveEntry(TarArchive archive, TarFilePart? part, CompressionType compressionType) : base(part, compressionType) => Archive = archive; - public virtual Stream OpenEntryStream() => Parts.Single().GetCompressedStream(); + public virtual Stream OpenEntryStream() => Parts.Single().GetCompressedStream().NotNull(); #region IArchiveEntry Members diff --git a/src/SharpCompress/Archives/Zip/ZipArchiveEntry.cs b/src/SharpCompress/Archives/Zip/ZipArchiveEntry.cs index a94ed2c6..f13faee7 100644 --- a/src/SharpCompress/Archives/Zip/ZipArchiveEntry.cs +++ b/src/SharpCompress/Archives/Zip/ZipArchiveEntry.cs @@ -9,7 +9,7 @@ public class ZipArchiveEntry : ZipEntry, IArchiveEntry internal ZipArchiveEntry(ZipArchive archive, SeekableZipFilePart? part) : base(part) => Archive = archive; - public virtual Stream OpenEntryStream() => Parts.Single().GetCompressedStream(); + public virtual Stream OpenEntryStream() => Parts.Single().GetCompressedStream().NotNull(); #region IArchiveEntry Members diff --git a/src/SharpCompress/Common/FilePart.cs b/src/SharpCompress/Common/FilePart.cs index 23b8b400..54e3c9f9 100644 --- a/src/SharpCompress/Common/FilePart.cs +++ b/src/SharpCompress/Common/FilePart.cs @@ -11,7 +11,7 @@ public abstract class FilePart internal abstract string? FilePartName { get; } public int Index { get; set; } - internal abstract Stream GetCompressedStream(); + internal abstract Stream? GetCompressedStream(); internal abstract Stream? GetRawStream(); internal bool Skipped { get; set; } } diff --git a/src/SharpCompress/Common/Rar/CryptKey5.cs b/src/SharpCompress/Common/Rar/CryptKey5.cs index 0b802691..90778c5a 100644 --- a/src/SharpCompress/Common/Rar/CryptKey5.cs +++ b/src/SharpCompress/Common/Rar/CryptKey5.cs @@ -17,7 +17,7 @@ internal class CryptKey5 : ICryptKey private byte[] _pswCheck = { }; private byte[] _hashKey = { }; - public CryptKey5(string password, Rar5CryptoInfo rar5CryptoInfo) + public CryptKey5(string? password, Rar5CryptoInfo rar5CryptoInfo) { _password = password ?? ""; _cryptoInfo = rar5CryptoInfo; diff --git a/src/SharpCompress/Common/Rar/Headers/ArchiveCryptHeader.cs b/src/SharpCompress/Common/Rar/Headers/ArchiveCryptHeader.cs index 5aa29d49..f819b478 100644 --- a/src/SharpCompress/Common/Rar/Headers/ArchiveCryptHeader.cs +++ b/src/SharpCompress/Common/Rar/Headers/ArchiveCryptHeader.cs @@ -1,8 +1,5 @@ #nullable disable -using System; -using System.Security.Cryptography; -using SharpCompress.Common.Rar.Headers; using SharpCompress.IO; namespace SharpCompress.Common.Rar.Headers; diff --git a/src/SharpCompress/Common/Rar/Headers/FileHeader.cs b/src/SharpCompress/Common/Rar/Headers/FileHeader.cs index eea8293d..d1bf7b8a 100644 --- a/src/SharpCompress/Common/Rar/Headers/FileHeader.cs +++ b/src/SharpCompress/Common/Rar/Headers/FileHeader.cs @@ -1,9 +1,6 @@ -#nullable disable - using System; using System.IO; using System.Linq; -using System.Security.Cryptography; using System.Text; using SharpCompress.IO; #if !Rar2017_64bit @@ -18,7 +15,7 @@ namespace SharpCompress.Common.Rar.Headers; internal class FileHeader : RarHeader { - private byte[] _hash; + private byte[]? _hash; public FileHeader(RarHeader header, RarCrcBinaryReader reader, HeaderType headerType) : base(header, reader, headerType) { } @@ -319,6 +316,10 @@ private void ReadFromReaderV4(MarkingBinaryReader reader) if (NewSubHeaderType.SUBHEAD_TYPE_RR.Equals(fileNameBytes)) { + if (SubData is null) + { + throw new InvalidFormatException(); + } RecoverySectors = SubData[8] + (SubData[9] << 8) @@ -340,12 +341,16 @@ private void ReadFromReaderV4(MarkingBinaryReader reader) if (RemainingHeaderBytes(reader) >= 2) { var extendedFlags = reader.ReadUInt16(); - FileLastModifiedTime = ProcessExtendedTimeV4( - extendedFlags, - FileLastModifiedTime, - reader, - 0 - ); + if (FileLastModifiedTime is not null) + { + FileLastModifiedTime = ProcessExtendedTimeV4( + extendedFlags, + FileLastModifiedTime, + reader, + 0 + ); + } + FileCreatedTime = ProcessExtendedTimeV4(extendedFlags, null, reader, 1); FileLastAccessedTime = ProcessExtendedTimeV4(extendedFlags, null, reader, 2); FileArchivedTime = ProcessExtendedTimeV4(extendedFlags, null, reader, 3); @@ -377,7 +382,7 @@ int i var dosTime = reader.ReadUInt32(); time = Utility.DosDateToDateTime(dosTime); } - if ((rmode & 4) == 0) + if ((rmode & 4) == 0 && time is not null) { time = time.Value.AddSeconds(1); } @@ -390,7 +395,11 @@ int i } //10^-7 to 10^-3 - return time.Value.AddMilliseconds(nanosecondHundreds * Math.Pow(10, -4)); + if (time is not null) + { + return time.Value.AddMilliseconds(nanosecondHundreds * Math.Pow(10, -4)); + } + return null; } private static string ConvertPathV4(string path) @@ -406,13 +415,13 @@ private static string ConvertPathV4(string path) return path; } - public override string ToString() => FileName; + public override string ToString() => FileName ?? "FileHeader"; private ushort Flags { get; set; } private bool HasFlag(ushort flag) => (Flags & flag) == flag; - internal byte[] FileCrc + internal byte[]? FileCrc { get => _hash; private set => _hash = value; @@ -441,22 +450,22 @@ internal byte[] FileCrc public bool IsRedir => RedirType != 0; public byte RedirFlags { get; private set; } public bool IsRedirDirectory => (RedirFlags & RedirFlagV5.DIRECTORY) != 0; - public string RedirTargetName { get; private set; } + public string? RedirTargetName { get; private set; } // unused for UnpackV1 implementation (limitation) internal size_t WindowSize { get; private set; } - internal byte[] R4Salt { get; private set; } - internal Rar5CryptoInfo Rar5CryptoInfo { get; private set; } + internal byte[]? R4Salt { get; private set; } + internal Rar5CryptoInfo? Rar5CryptoInfo { get; private set; } private byte HostOs { get; set; } internal uint FileAttributes { get; private set; } internal long CompressedSize { get; private set; } internal long UncompressedSize { get; private set; } - internal string FileName { get; private set; } - internal byte[] SubData { get; private set; } + internal string? FileName { get; private set; } + internal byte[]? SubData { get; private set; } internal int RecoverySectors { get; private set; } internal long DataStartPosition { get; set; } - public Stream PackedStream { get; set; } + public Stream? PackedStream { get; set; } public bool IsSplitBefore => IsRar5 ? HasHeaderFlag(HeaderFlagsV5.SPLIT_BEFORE) : HasFlag(FileFlagsV4.SPLIT_BEFORE); diff --git a/src/SharpCompress/Common/Rar/Headers/RarHeader.cs b/src/SharpCompress/Common/Rar/Headers/RarHeader.cs index 0d8648e8..2ae1b9f3 100644 --- a/src/SharpCompress/Common/Rar/Headers/RarHeader.cs +++ b/src/SharpCompress/Common/Rar/Headers/RarHeader.cs @@ -1,5 +1,4 @@ using System; -using System.IO; using SharpCompress.IO; namespace SharpCompress.Common.Rar.Headers; @@ -21,7 +20,7 @@ ArchiveEncoding archiveEncoding { return new RarHeader(reader, isRar5, archiveEncoding); } - catch (EndOfStreamException) + catch (InvalidFormatException) { return null; } diff --git a/src/SharpCompress/Common/Rar/Headers/RarHeaderFactory.cs b/src/SharpCompress/Common/Rar/Headers/RarHeaderFactory.cs index 74d68fc7..a702c4d1 100644 --- a/src/SharpCompress/Common/Rar/Headers/RarHeaderFactory.cs +++ b/src/SharpCompress/Common/Rar/Headers/RarHeaderFactory.cs @@ -1,7 +1,5 @@ using System.Collections.Generic; using System.IO; -using System.Linq; -using SharpCompress.Common.Rar; using SharpCompress.IO; using SharpCompress.Readers; @@ -160,10 +158,10 @@ public IEnumerable ReadHeaders(Stream stream) { fh.PackedStream = new RarCryptoWrapper( ms, - fh.R4Salt is null ? fh.Rar5CryptoInfo.Salt : fh.R4Salt, + fh.R4Salt is null ? fh.Rar5CryptoInfo.NotNull().Salt : fh.R4Salt, fh.R4Salt is null - ? new CryptKey5(Options.Password!, fh.Rar5CryptoInfo) - : new CryptKey3(Options.Password!) + ? new CryptKey5(Options.Password, fh.Rar5CryptoInfo.NotNull()) + : new CryptKey3(Options.Password) ); } } diff --git a/src/SharpCompress/Common/Rar/RarEntry.cs b/src/SharpCompress/Common/Rar/RarEntry.cs index a064c2f8..c76c72b6 100644 --- a/src/SharpCompress/Common/Rar/RarEntry.cs +++ b/src/SharpCompress/Common/Rar/RarEntry.cs @@ -20,7 +20,7 @@ public abstract class RarEntry : Entry /// /// The File's 32 bit CRC Hash /// - public override long Crc => BitConverter.ToUInt32(FileHeader.FileCrc, 0); + public override long Crc => BitConverter.ToUInt32(FileHeader.FileCrc.NotNull(), 0); /// /// The path of the file internal to the Rar Archive. @@ -68,7 +68,7 @@ public abstract class RarEntry : Entry public bool IsRedir => FileHeader.IsRedir; - public string RedirTargetName => FileHeader.RedirTargetName; + public string? RedirTargetName => FileHeader.RedirTargetName; public override string ToString() => string.Format( diff --git a/src/SharpCompress/Common/Rar/RarVolume.cs b/src/SharpCompress/Common/Rar/RarVolume.cs index f3d55393..274ac0a2 100644 --- a/src/SharpCompress/Common/Rar/RarVolume.cs +++ b/src/SharpCompress/Common/Rar/RarVolume.cs @@ -63,7 +63,7 @@ internal IEnumerable GetVolumeFileParts() { var part = CreateFilePart(lastMarkHeader!, fh); var buffer = new byte[fh.CompressedSize]; - part.GetCompressedStream().Read(buffer, 0, buffer.Length); + part.GetCompressedStream().NotNull().Read(buffer, 0, buffer.Length); Comment = Encoding.UTF8.GetString(buffer, 0, buffer.Length - 1); } } diff --git a/src/SharpCompress/Common/SevenZip/SevenZipFilePart.cs b/src/SharpCompress/Common/SevenZip/SevenZipFilePart.cs index ac355828..31c4a24b 100644 --- a/src/SharpCompress/Common/SevenZip/SevenZipFilePart.cs +++ b/src/SharpCompress/Common/SevenZip/SevenZipFilePart.cs @@ -1,4 +1,3 @@ -using System; using System.IO; using System.Linq; using SharpCompress.IO; diff --git a/src/SharpCompress/Common/Zip/StreamingZipFilePart.cs b/src/SharpCompress/Common/Zip/StreamingZipFilePart.cs index 97e44b6b..a3fa3caa 100644 --- a/src/SharpCompress/Common/Zip/StreamingZipFilePart.cs +++ b/src/SharpCompress/Common/Zip/StreamingZipFilePart.cs @@ -1,5 +1,4 @@ using System.IO; -using System.Net.Sockets; using SharpCompress.Common.Zip.Headers; using SharpCompress.Compressors.Deflate; using SharpCompress.IO; diff --git a/src/SharpCompress/Compressors/Deflate/Zlib.cs b/src/SharpCompress/Compressors/Deflate/Zlib.cs index b395029e..1358e01b 100644 --- a/src/SharpCompress/Compressors/Deflate/Zlib.cs +++ b/src/SharpCompress/Compressors/Deflate/Zlib.cs @@ -62,7 +62,6 @@ // // ----------------------------------------------------------------------- -using System; using System.IO; using SharpCompress.Common; diff --git a/src/SharpCompress/Compressors/Deflate64/HuffmanTree.cs b/src/SharpCompress/Compressors/Deflate64/HuffmanTree.cs index 4c9e7e8b..aac88b05 100644 --- a/src/SharpCompress/Compressors/Deflate64/HuffmanTree.cs +++ b/src/SharpCompress/Compressors/Deflate64/HuffmanTree.cs @@ -4,7 +4,6 @@ using System; using System.Diagnostics; -using System.IO; using SharpCompress.Common; namespace SharpCompress.Compressors.Deflate64; diff --git a/src/SharpCompress/Compressors/Deflate64/InflaterManaged.cs b/src/SharpCompress/Compressors/Deflate64/InflaterManaged.cs index 2e4d8377..101188db 100644 --- a/src/SharpCompress/Compressors/Deflate64/InflaterManaged.cs +++ b/src/SharpCompress/Compressors/Deflate64/InflaterManaged.cs @@ -30,7 +30,6 @@ using System; using System.Diagnostics; -using System.IO; using SharpCompress.Compressors.Deflate; namespace SharpCompress.Compressors.Deflate64; diff --git a/src/SharpCompress/Compressors/Filters/BranchExecFilter.cs b/src/SharpCompress/Compressors/Filters/BranchExecFilter.cs index 7f887720..df95c838 100644 --- a/src/SharpCompress/Compressors/Filters/BranchExecFilter.cs +++ b/src/SharpCompress/Compressors/Filters/BranchExecFilter.cs @@ -5,7 +5,6 @@ */ using System; -using System.IO; using System.Runtime.CompilerServices; using SharpCompress.Common; diff --git a/src/SharpCompress/Compressors/Filters/DeltaFilter.cs b/src/SharpCompress/Compressors/Filters/DeltaFilter.cs index 85ec9b15..a6954116 100644 --- a/src/SharpCompress/Compressors/Filters/DeltaFilter.cs +++ b/src/SharpCompress/Compressors/Filters/DeltaFilter.cs @@ -1,4 +1,3 @@ -using System; using System.IO; namespace SharpCompress.Compressors.Filters diff --git a/src/SharpCompress/Compressors/Rar/RarBLAKE2spStream.cs b/src/SharpCompress/Compressors/Rar/RarBLAKE2spStream.cs index 7c897176..cf93cffb 100644 --- a/src/SharpCompress/Compressors/Rar/RarBLAKE2spStream.cs +++ b/src/SharpCompress/Compressors/Rar/RarBLAKE2spStream.cs @@ -1,7 +1,6 @@ using System; using System.IO; using System.Linq; -using System.Runtime.CompilerServices; using SharpCompress.Common; using SharpCompress.Common.Rar.Headers; @@ -93,7 +92,7 @@ MultiVolumeReadOnlyStream readStream { this.readStream = readStream; disableCRCCheck = fileHeader.IsEncrypted; - _hash = fileHeader.FileCrc; + _hash = fileHeader.FileCrc.NotNull(); _blake2sp = new BLAKE2SP(); ResetCrc(); } diff --git a/src/SharpCompress/Compressors/Rar/UnpackV2017/Unpack.unpack50_cpp.cs b/src/SharpCompress/Compressors/Rar/UnpackV2017/Unpack.unpack50_cpp.cs index 143e3d31..2ba0d80c 100644 --- a/src/SharpCompress/Compressors/Rar/UnpackV2017/Unpack.unpack50_cpp.cs +++ b/src/SharpCompress/Compressors/Rar/UnpackV2017/Unpack.unpack50_cpp.cs @@ -3,7 +3,6 @@ using System; using static SharpCompress.Compressors.Rar.UnpackV2017.PackDef; using static SharpCompress.Compressors.Rar.UnpackV2017.UnpackGlobal; -using int64 = System.Int64; #if !Rar2017_64bit using size_t = System.UInt32; #else diff --git a/src/SharpCompress/Compressors/Shrink/BitStream.cs b/src/SharpCompress/Compressors/Shrink/BitStream.cs index 949ee6c3..9dc534e0 100644 --- a/src/SharpCompress/Compressors/Shrink/BitStream.cs +++ b/src/SharpCompress/Compressors/Shrink/BitStream.cs @@ -1,9 +1,3 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - namespace SharpCompress.Compressors.Shrink { internal class BitStream diff --git a/src/SharpCompress/Compressors/Shrink/ShrinkStream.cs b/src/SharpCompress/Compressors/Shrink/ShrinkStream.cs index 65b2eb37..258f2c42 100644 --- a/src/SharpCompress/Compressors/Shrink/ShrinkStream.cs +++ b/src/SharpCompress/Compressors/Shrink/ShrinkStream.cs @@ -1,9 +1,5 @@ using System; -using System.Collections.Generic; using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace SharpCompress.Compressors.Shrink; diff --git a/src/SharpCompress/IO/DataDescriptorStream.cs b/src/SharpCompress/IO/DataDescriptorStream.cs index 1802556c..801f9fa7 100644 --- a/src/SharpCompress/IO/DataDescriptorStream.cs +++ b/src/SharpCompress/IO/DataDescriptorStream.cs @@ -1,6 +1,5 @@ using System; using System.IO; -using System.Runtime.CompilerServices; namespace SharpCompress.IO; diff --git a/src/SharpCompress/NotNullExtensions.cs b/src/SharpCompress/NotNullExtensions.cs index 88e95c41..c8ae4762 100644 --- a/src/SharpCompress/NotNullExtensions.cs +++ b/src/SharpCompress/NotNullExtensions.cs @@ -8,9 +8,10 @@ namespace SharpCompress; public static class NotNullExtensions { + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable Empty(this IEnumerable? source) => source ?? Enumerable.Empty(); - + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable Empty(this T? source) { if (source is null) @@ -21,6 +22,7 @@ public static IEnumerable Empty(this T? source) } #if NETFRAMEWORK || NETSTANDARD +[MethodImpl(MethodImplOptions.AggressiveInlining)] public static T NotNull(this T? obj, string? message = null) where T : class { @@ -31,6 +33,7 @@ public static T NotNull(this T? obj, string? message = null) return obj; } +[MethodImpl(MethodImplOptions.AggressiveInlining)] public static T NotNull(this T? obj, string? message = null) where T : struct { @@ -42,6 +45,7 @@ public static T NotNull(this T? obj, string? message = null) } #else + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T NotNull( [NotNull] this T? obj, [CallerArgumentExpression(nameof(obj))] string? paramName = null @@ -52,6 +56,7 @@ public static T NotNull( return obj; } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T NotNull( [NotNull] this T? obj, [CallerArgumentExpression(nameof(obj))] string? paramName = null diff --git a/src/SharpCompress/Readers/AbstractReader.cs b/src/SharpCompress/Readers/AbstractReader.cs index b36acd53..c99c3966 100644 --- a/src/SharpCompress/Readers/AbstractReader.cs +++ b/src/SharpCompress/Readers/AbstractReader.cs @@ -192,7 +192,7 @@ public EntryStream OpenEntryStream() /// /// Retains a reference to the entry stream, so we can check whether it completed later. /// - protected EntryStream CreateEntryStream(Stream decompressed) => new(this, decompressed); + protected EntryStream CreateEntryStream(Stream? decompressed) => new(this, decompressed.NotNull()); protected virtual EntryStream GetEntryStream() => CreateEntryStream(Entry.Parts.First().GetCompressedStream()); diff --git a/src/SharpCompress/Readers/Rar/NonSeekableStreamFilePart.cs b/src/SharpCompress/Readers/Rar/NonSeekableStreamFilePart.cs index 3a43e60c..94ee6bf2 100644 --- a/src/SharpCompress/Readers/Rar/NonSeekableStreamFilePart.cs +++ b/src/SharpCompress/Readers/Rar/NonSeekableStreamFilePart.cs @@ -9,7 +9,7 @@ internal class NonSeekableStreamFilePart : RarFilePart internal NonSeekableStreamFilePart(MarkHeader mh, FileHeader fh, int index = 0) : base(mh, fh, index) { } - internal override Stream GetCompressedStream() => FileHeader.PackedStream; + internal override Stream? GetCompressedStream() => FileHeader.PackedStream; internal override Stream? GetRawStream() => FileHeader.PackedStream; diff --git a/src/SharpCompress/Readers/Rar/RarReader.cs b/src/SharpCompress/Readers/Rar/RarReader.cs index 2398cfac..245001e3 100644 --- a/src/SharpCompress/Readers/Rar/RarReader.cs +++ b/src/SharpCompress/Readers/Rar/RarReader.cs @@ -78,7 +78,7 @@ protected override EntryStream GetEntryStream() return CreateEntryStream(new RarCrcStream(UnpackV1.Value, Entry.FileHeader, stream)); } - if (Entry.FileHeader.FileCrc.Length > 5) + if (Entry.FileHeader.FileCrc?.Length > 5) { return CreateEntryStream( new RarBLAKE2spStream(UnpackV2017.Value, Entry.FileHeader, stream) diff --git a/src/SharpCompress/Readers/ReaderFactory.cs b/src/SharpCompress/Readers/ReaderFactory.cs index d56e30c1..de085d92 100644 --- a/src/SharpCompress/Readers/ReaderFactory.cs +++ b/src/SharpCompress/Readers/ReaderFactory.cs @@ -1,4 +1,3 @@ -using System; using System.IO; using System.Linq; using SharpCompress.Common; diff --git a/tests/SharpCompress.Test/GZip/GZipArchiveTests.cs b/tests/SharpCompress.Test/GZip/GZipArchiveTests.cs index 80a9a9a1..56b5cd2b 100644 --- a/tests/SharpCompress.Test/GZip/GZipArchiveTests.cs +++ b/tests/SharpCompress.Test/GZip/GZipArchiveTests.cs @@ -1,9 +1,9 @@ -using System; using System.IO; using System.Linq; using SharpCompress.Archives; using SharpCompress.Archives.GZip; using SharpCompress.Archives.Tar; +using SharpCompress.Common; using Xunit; namespace SharpCompress.Test.GZip; @@ -62,7 +62,7 @@ public void GZip_Archive_NoAdd() var jpg = Path.Combine(ORIGINAL_FILES_PATH, "jpg", "test.jpg"); using Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz")); using var archive = GZipArchive.Open(stream); - Assert.Throws(() => archive.AddEntry("jpg\\test.jpg", jpg)); + Assert.Throws(() => archive.AddEntry("jpg\\test.jpg", jpg)); archive.SaveTo(Path.Combine(SCRATCH_FILES_PATH, "Tar.tar.gz")); } diff --git a/tests/SharpCompress.Test/Xz/Filters/BCJTests.cs b/tests/SharpCompress.Test/Xz/Filters/BCJTests.cs index 9dbdf348..a53565b7 100644 --- a/tests/SharpCompress.Test/Xz/Filters/BCJTests.cs +++ b/tests/SharpCompress.Test/Xz/Filters/BCJTests.cs @@ -3,7 +3,6 @@ * */ -using System.IO; using SharpCompress.Common; using SharpCompress.Compressors.Xz.Filters; using Xunit; diff --git a/tests/SharpCompress.Test/Xz/Filters/Lzma2Tests.cs b/tests/SharpCompress.Test/Xz/Filters/Lzma2Tests.cs index 133332c9..12db0746 100644 --- a/tests/SharpCompress.Test/Xz/Filters/Lzma2Tests.cs +++ b/tests/SharpCompress.Test/Xz/Filters/Lzma2Tests.cs @@ -1,5 +1,4 @@ using System; -using System.IO; using SharpCompress.Common; using SharpCompress.Compressors.Xz.Filters; using Xunit; From 5d9c99508dd9406c738de01a1ae15d329de6ff6e Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Tue, 23 Apr 2024 15:08:50 +0100 Subject: [PATCH 5/5] fmt --- src/SharpCompress/Common/Rar/Headers/FileHeader.cs | 10 +++++----- .../Common/Rar/Headers/RarHeaderFactory.cs | 9 +++++++-- src/SharpCompress/NotNullExtensions.cs | 5 +++-- src/SharpCompress/Readers/AbstractReader.cs | 3 ++- 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/SharpCompress/Common/Rar/Headers/FileHeader.cs b/src/SharpCompress/Common/Rar/Headers/FileHeader.cs index d1bf7b8a..0aa9fc0d 100644 --- a/src/SharpCompress/Common/Rar/Headers/FileHeader.cs +++ b/src/SharpCompress/Common/Rar/Headers/FileHeader.cs @@ -344,11 +344,11 @@ private void ReadFromReaderV4(MarkingBinaryReader reader) if (FileLastModifiedTime is not null) { FileLastModifiedTime = ProcessExtendedTimeV4( - extendedFlags, - FileLastModifiedTime, - reader, - 0 - ); + extendedFlags, + FileLastModifiedTime, + reader, + 0 + ); } FileCreatedTime = ProcessExtendedTimeV4(extendedFlags, null, reader, 1); diff --git a/src/SharpCompress/Common/Rar/Headers/RarHeaderFactory.cs b/src/SharpCompress/Common/Rar/Headers/RarHeaderFactory.cs index a702c4d1..902e595f 100644 --- a/src/SharpCompress/Common/Rar/Headers/RarHeaderFactory.cs +++ b/src/SharpCompress/Common/Rar/Headers/RarHeaderFactory.cs @@ -158,9 +158,14 @@ public IEnumerable ReadHeaders(Stream stream) { fh.PackedStream = new RarCryptoWrapper( ms, - fh.R4Salt is null ? fh.Rar5CryptoInfo.NotNull().Salt : fh.R4Salt, fh.R4Salt is null - ? new CryptKey5(Options.Password, fh.Rar5CryptoInfo.NotNull()) + ? fh.Rar5CryptoInfo.NotNull().Salt + : fh.R4Salt, + fh.R4Salt is null + ? new CryptKey5( + Options.Password, + fh.Rar5CryptoInfo.NotNull() + ) : new CryptKey3(Options.Password) ); } diff --git a/src/SharpCompress/NotNullExtensions.cs b/src/SharpCompress/NotNullExtensions.cs index c8ae4762..956a4fde 100644 --- a/src/SharpCompress/NotNullExtensions.cs +++ b/src/SharpCompress/NotNullExtensions.cs @@ -11,6 +11,7 @@ public static class NotNullExtensions [MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable Empty(this IEnumerable? source) => source ?? Enumerable.Empty(); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable Empty(this T? source) { @@ -22,7 +23,7 @@ public static IEnumerable Empty(this T? source) } #if NETFRAMEWORK || NETSTANDARD -[MethodImpl(MethodImplOptions.AggressiveInlining)] + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T NotNull(this T? obj, string? message = null) where T : class { @@ -33,7 +34,7 @@ public static T NotNull(this T? obj, string? message = null) return obj; } -[MethodImpl(MethodImplOptions.AggressiveInlining)] + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T NotNull(this T? obj, string? message = null) where T : struct { diff --git a/src/SharpCompress/Readers/AbstractReader.cs b/src/SharpCompress/Readers/AbstractReader.cs index c99c3966..da6279a9 100644 --- a/src/SharpCompress/Readers/AbstractReader.cs +++ b/src/SharpCompress/Readers/AbstractReader.cs @@ -192,7 +192,8 @@ public EntryStream OpenEntryStream() /// /// Retains a reference to the entry stream, so we can check whether it completed later. /// - protected EntryStream CreateEntryStream(Stream? decompressed) => new(this, decompressed.NotNull()); + protected EntryStream CreateEntryStream(Stream? decompressed) => + new(this, decompressed.NotNull()); protected virtual EntryStream GetEntryStream() => CreateEntryStream(Entry.Parts.First().GetCompressedStream());