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 ea46e8d0..58f98ab9 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/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 class GZipArchiveEntry : GZipEntry, IArchiveEntry { 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 class RarArchiveEntry : RarEntry, IArchiveEntry { 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/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/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/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/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..0aa9fc0d 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 @@ internal class FileHeader : RarHeader if (NewSubHeaderType.SUBHEAD_TYPE_RR.Equals(fileNameBytes)) { + if (SubData is null) + { + throw new InvalidFormatException(); + } RecoverySectors = SubData[8] + (SubData[9] << 8) @@ -340,12 +341,16 @@ internal class FileHeader : RarHeader 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 @@ internal class FileHeader : RarHeader 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 @@ internal class FileHeader : RarHeader } //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 @@ internal class FileHeader : RarHeader 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 class FileHeader : RarHeader 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 @@ internal class RarHeader : IRarHeader { 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..902e595f 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,15 @@ public class RarHeaderFactory { fh.PackedStream = new RarCryptoWrapper( ms, - fh.R4Salt is null ? fh.Rar5CryptoInfo.Salt : fh.R4Salt, fh.R4Salt is null - ? new CryptKey5(Options.Password!, fh.Rar5CryptoInfo) - : new CryptKey3(Options.Password!) + ? 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/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 d05e16c2..4bb2f84a 100644 --- a/src/SharpCompress/Common/Rar/RarVolume.cs +++ b/src/SharpCompress/Common/Rar/RarVolume.cs @@ -62,7 +62,7 @@ public abstract class RarVolume : Volume if (fh.FileName == "CMT") { var buffer = new byte[fh.CompressedSize]; - fh.PackedStream.Read(buffer, 0, buffer.Length); + fh.PackedStream.NotNull().ReadFully(buffer); Comment = Encoding.UTF8.GetString(buffer, 0, buffer.Length - 1); } } 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 d2d05b54..288a7298 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,7 +854,7 @@ internal class ArchiveReader != folder._unpackCrc ) { - throw new InvalidOperationException( + 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 d1b882fb..23a12d56 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; @@ -41,7 +40,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 +85,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..ee0ef2a5 --- /dev/null +++ b/src/SharpCompress/Common/SharpCompressException.cs @@ -0,0 +1,48 @@ +using System; + +namespace SharpCompress.Common; + +public class SharpCompressException : Exception +{ + 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 0dcbbde2..1a04740f 100644 --- a/src/SharpCompress/Common/Tar/Headers/TarHeader.cs +++ b/src/SharpCompress/Common/Tar/Headers/TarHeader.cs @@ -216,7 +216,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/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 93405983..9ba751d8 100644 --- a/src/SharpCompress/Compressors/Deflate/Zlib.cs +++ b/src/SharpCompress/Compressors/Deflate/Zlib.cs @@ -62,8 +62,8 @@ // // ----------------------------------------------------------------------- -using System; using System.IO; +using SharpCompress.Common; namespace SharpCompress.Compressors.Deflate; @@ -177,7 +177,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..aac88b05 100644 --- a/src/SharpCompress/Compressors/Deflate64/HuffmanTree.cs +++ b/src/SharpCompress/Compressors/Deflate64/HuffmanTree.cs @@ -4,7 +4,7 @@ using System; using System.Diagnostics; -using System.IO; +using SharpCompress.Common; namespace SharpCompress.Compressors.Deflate64; @@ -192,7 +192,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 +234,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 +307,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 34aa63d6..5caf257d 100644 --- a/src/SharpCompress/Compressors/Deflate64/InflaterManaged.cs +++ b/src/SharpCompress/Compressors/Deflate64/InflaterManaged.cs @@ -30,7 +30,7 @@ using System; using System.Diagnostics; -using System.IO; +using SharpCompress.Compressors.Deflate; namespace SharpCompress.Compressors.Deflate64; @@ -385,7 +385,7 @@ internal sealed class InflaterManaged } else { - throw new InvalidDataException("Deflate64: unknown block type"); + throw new ZlibException("Deflate64: unknown block type"); } } @@ -411,7 +411,7 @@ internal sealed class InflaterManaged } else { - throw new InvalidDataException("Deflate64: unknown block type"); + throw new ZlibException("Deflate64: unknown block type"); } // @@ -473,7 +473,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 +507,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 +569,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 +591,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 +649,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 +781,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 +789,7 @@ internal sealed class InflaterManaged if (_loopCounter + repeatCount > _codeArraySize) { - throw new InvalidDataException(); + throw new ZlibException(); } for (var j = 0; j < repeatCount; j++) @@ -809,7 +809,7 @@ internal sealed class InflaterManaged if (_loopCounter + repeatCount > _codeArraySize) { - throw new InvalidDataException(); + throw new ZlibException(); } for (var j = 0; j < repeatCount; j++) @@ -830,7 +830,7 @@ internal sealed class InflaterManaged if (_loopCounter + repeatCount > _codeArraySize) { - throw new InvalidDataException(); + throw new ZlibException(); } for (var j = 0; j < repeatCount; j++) @@ -846,7 +846,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 +865,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..df95c838 100644 --- a/src/SharpCompress/Compressors/Filters/BranchExecFilter.cs +++ b/src/SharpCompress/Compressors/Filters/BranchExecFilter.cs @@ -5,8 +5,8 @@ */ using System; -using System.IO; using System.Runtime.CompilerServices; +using SharpCompress.Common; namespace SharpCompress.Compressors.Filters; @@ -244,7 +244,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/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/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 3609641b..d1bb7246 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,33 +194,25 @@ 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 // but encoded as a single byte in the format LzmaStream expects. // literal_context_bits = 3 @@ -236,5 +224,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 f3abf990..e9fa1bab 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/Rar/RarBLAKE2spStream.cs b/src/SharpCompress/Compressors/Rar/RarBLAKE2spStream.cs index 63759bc1..0cf7c85c 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 @@ internal class RarBLAKE2spStream : RarStream { 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 03fa85f2..8bb69ead 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/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 76ec4e03..eba9b1ac 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; @@ -49,7 +50,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 aec35470..cdb075ef 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 9a0b70d6..945449fa 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 8971e9f5..4cac8136 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; @@ -30,7 +31,7 @@ public sealed class XZStream : XZReadOnlyStream case CheckType.SHA256: break; 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/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/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/NotNullExtensions.cs b/src/SharpCompress/NotNullExtensions.cs index 29245114..2245612e 100644 --- a/src/SharpCompress/NotNullExtensions.cs +++ b/src/SharpCompress/NotNullExtensions.cs @@ -8,9 +8,11 @@ namespace SharpCompress.Helpers; internal 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 +23,7 @@ internal static class NotNullExtensions } #if NETFRAMEWORK || NETSTANDARD + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T NotNull(this T? obj, string? message = null) where T : class { @@ -31,6 +34,7 @@ internal static class NotNullExtensions return obj; } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T NotNull(this T? obj, string? message = null) where T : struct { @@ -42,6 +46,7 @@ internal static class NotNullExtensions } #else + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T NotNull( [NotNull] this T? obj, [CallerArgumentExpression(nameof(obj))] string? paramName = null @@ -52,6 +57,7 @@ internal static class NotNullExtensions 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 96ac0a58..ac4e1784 100644 --- a/src/SharpCompress/Readers/AbstractReader.cs +++ b/src/SharpCompress/Readers/AbstractReader.cs @@ -192,7 +192,8 @@ public abstract class AbstractReader : IReader, IReaderExtracti /// /// 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 184ba62d..aa73e30f 100644 --- a/src/SharpCompress/Readers/Rar/RarReader.cs +++ b/src/SharpCompress/Readers/Rar/RarReader.cs @@ -93,7 +93,7 @@ public abstract class RarReader : AbstractReader 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 0cf4c2ea..82e3d776 100644 --- a/src/SharpCompress/Readers/ReaderFactory.cs +++ b/src/SharpCompress/Readers/ReaderFactory.cs @@ -1,6 +1,6 @@ -using System; using System.IO; using System.Linq; +using SharpCompress.Common; using SharpCompress.IO; namespace SharpCompress.Readers; @@ -29,7 +29,7 @@ public static class ReaderFactory } } - throw new InvalidOperationException( + throw new InvalidFormatException( "Cannot determine compressed stream type. Supported Reader Formats: Arc, Zip, GZip, BZip2, Tar, Rar, LZip, XZ" ); } diff --git a/src/SharpCompress/packages.lock.json b/src/SharpCompress/packages.lock.json index de18ff8d..0e470246 100644 --- a/src/SharpCompress/packages.lock.json +++ b/src/SharpCompress/packages.lock.json @@ -307,9 +307,9 @@ "net8.0": { "Microsoft.NET.ILLink.Tasks": { "type": "Direct", - "requested": "[8.0.12, )", - "resolved": "8.0.12", - "contentHash": "FV4HnQ3JI15PHnJ5PGTbz+rYvrih42oLi/7UMIshNwCwUZhTq13UzrggtXk4ygrcMcN+4jsS6hhshx2p/Zd0ig==" + "requested": "[8.0.15, )", + "resolved": "8.0.15", + "contentHash": "s4eXlcRGyHeCgFUGQnhq0e/SCHBPp0jOHgMqZg3fQ2OCHJSm1aOUhI6RFWuVIcEb9ig2WgI2kWukk8wu72EbUQ==" }, "Microsoft.SourceLink.GitHub": { "type": "Direct", diff --git a/tests/SharpCompress.Test/GZip/GZipArchiveTests.cs b/tests/SharpCompress.Test/GZip/GZipArchiveTests.cs index a8e215f7..6a2b9795 100644 --- a/tests/SharpCompress.Test/GZip/GZipArchiveTests.cs +++ b/tests/SharpCompress.Test/GZip/GZipArchiveTests.cs @@ -1,4 +1,3 @@ -using System; using System.IO; using System.Linq; using SharpCompress.Archives; @@ -63,7 +62,7 @@ public class GZipArchiveTests : ArchiveTests 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 bc16789d..a53565b7 100644 --- a/tests/SharpCompress.Test/Xz/Filters/BCJTests.cs +++ b/tests/SharpCompress.Test/Xz/Filters/BCJTests.cs @@ -3,7 +3,7 @@ * */ -using System.IO; +using SharpCompress.Common; using SharpCompress.Compressors.Xz.Filters; using Xunit; @@ -66,23 +66,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..12db0746 100644 --- a/tests/SharpCompress.Test/Xz/Filters/Lzma2Tests.cs +++ b/tests/SharpCompress.Test/Xz/Filters/Lzma2Tests.cs @@ -1,5 +1,5 @@ using System; -using System.IO; +using SharpCompress.Common; using SharpCompress.Compressors.Xz.Filters; using Xunit; @@ -52,14 +52,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 e129f4ec..44245075 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; @@ -43,7 +44,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]