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 @@ public abstract class AbstractArchive : IArchive, IArchiveExtra { 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 @@ public abstract class AbstractWritableArchive { 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 @@ public class GZipArchive : AbstractWritableArchive { 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 @@ public class GZipArchive : AbstractWritableArchive { 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 @@ internal class ArchiveReader ); break; default: - throw new InvalidOperationException(); + throw new InvalidFormatException(); } } } @@ -843,7 +843,7 @@ internal class ArchiveReader 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 @@ internal class ArchiveReader != 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 class SevenZipFilePart : FilePart { 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 @@ internal class SevenZipFilePart : FilePart 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 @@ internal sealed class TarHeader 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; 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 sealed class Deflate64Stream : Stream { // 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 @@ internal sealed class HuffmanTree 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 @@ internal sealed class HuffmanTree 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 @@ internal sealed class HuffmanTree // 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 @@ internal sealed class InflaterManaged } else { - throw new InvalidDataException("Deflate64: unknown block type"); + throw new ZlibException("Deflate64: unknown block type"); } } @@ -411,7 +412,7 @@ internal sealed class InflaterManaged } else { - throw new InvalidDataException("Deflate64: unknown block type"); + throw new ZlibException("Deflate64: unknown block type"); } // @@ -473,7 +474,7 @@ internal sealed class InflaterManaged // 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 @@ internal sealed class InflaterManaged 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 @@ internal sealed class InflaterManaged { 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 @@ internal sealed class InflaterManaged 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 @@ internal sealed class InflaterManaged 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 @@ internal sealed class InflaterManaged 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 @@ internal sealed class InflaterManaged if (_loopCounter + repeatCount > _codeArraySize) { - throw new InvalidDataException(); + throw new ZlibException(); } for (var j = 0; j < repeatCount; j++) @@ -809,7 +810,7 @@ internal sealed class InflaterManaged if (_loopCounter + repeatCount > _codeArraySize) { - throw new InvalidDataException(); + throw new ZlibException(); } for (var j = 0; j < repeatCount; j++) @@ -830,7 +831,7 @@ internal sealed class InflaterManaged if (_loopCounter + repeatCount > _codeArraySize) { - throw new InvalidDataException(); + throw new ZlibException(); } for (var j = 0; j < repeatCount; j++) @@ -846,7 +847,7 @@ internal sealed class InflaterManaged 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 @@ internal sealed class InflaterManaged // 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 sealed class BranchExecFilter 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 @@ internal static class DecoderStreamHelper } } - 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 @@ internal static class DecoderStreamHelper { if (foundPrimaryOutStream) { - throw new NotSupportedException("Multiple output streams."); + throw new InvalidFormatException("Multiple output streams."); } foundPrimaryOutStream = true; @@ -87,7 +88,7 @@ internal static class DecoderStreamHelper 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 sealed class LZipStream : Stream 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 sealed class LZipStream : Stream /// 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 sealed class LZipStream : 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 @@ public sealed class LZipStream : Stream (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 @@ internal class Encoder : ICoder, ISetCoderProperties, IWriteCoderProperties { 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 class ArmFilter : BlockFilter { 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 class ArmThumbFilter : BlockFilter { 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 abstract class BlockFilter : ReadOnlyStream 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 class IA64Filter : BlockFilter { 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 class Lzma2Filter : BlockFilter { 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 class PowerPCFilter : BlockFilter { 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 class SparcFilter : BlockFilter { 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 class X86Filter : BlockFilter { 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 @@ internal static class MultiByteIntegers { 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; 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 @@ public sealed class XZBlock : XZReadOnlyStream 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 @@ public sealed class XZBlock : XZReadOnlyStream 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 @@ public sealed class XZBlock : XZReadOnlyStream if (reserved != 0) { - throw new InvalidDataException( + throw new InvalidFormatException( "Reserved bytes used, perhaps an unknown XZ implementation" ); } @@ -189,7 +190,7 @@ public sealed class XZBlock : XZReadOnlyStream || (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 @@ public sealed class XZBlock : XZReadOnlyStream } if (nonLastSizeChangers > 2) { - throw new InvalidDataException( + throw new InvalidFormatException( "More than two non-last block filters cannot change stream size" ); } @@ -212,7 +213,7 @@ public sealed class XZBlock : XZReadOnlyStream 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 class XZFooter 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 class XZFooter 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 @@ public class XZHeader 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 @@ public class XZHeader { 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.Collections.Generic; using System.IO; using System.Linq; using System.Text; +using SharpCompress.Common; using SharpCompress.IO; namespace SharpCompress.Compressors.Xz; @@ -59,7 +60,7 @@ public class XZIndex 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 @@ public class XZIndex 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 abstract class XZReadOnlyStream : ReadOnlyStream 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 @@ public sealed class XZStream : XZReadOnlyStream 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 @@ internal class MarkingBinaryReader : BinaryReader 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 class ReaderFactory } } - 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 class BcjTests : XzTestsBase [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 class Lzma2Tests : XzTestsBase [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 class XzBlockTests : XzTestsBase 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 class XzHeaderTests : XzTestsBase 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 class XzHeaderTests : XzTestsBase 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 class XzHeaderTests : XzTestsBase 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 class XzIndexTests : XzTestsBase 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]