diff --git a/src/SharpCompress/Archives/ArchiveFactory.Async.cs b/src/SharpCompress/Archives/ArchiveFactory.Async.cs index 4af94a57..de357c50 100644 --- a/src/SharpCompress/Archives/ArchiveFactory.Async.cs +++ b/src/SharpCompress/Archives/ArchiveFactory.Async.cs @@ -62,7 +62,7 @@ public static partial class ArchiveFactory var filesArray = fileInfos.ToArray(); if (filesArray.Length == 0) { - throw new InvalidOperationException("No files to open"); + throw new ArchiveOperationException("No files to open"); } var fileInfo = filesArray[0]; @@ -91,7 +91,7 @@ public static partial class ArchiveFactory var streamsArray = streams.ToArray(); if (streamsArray.Length == 0) { - throw new InvalidOperationException("No streams"); + throw new ArchiveOperationException("No streams"); } var firstStream = streamsArray[0]; @@ -164,7 +164,7 @@ public static partial class ArchiveFactory var extensions = string.Join(", ", factories.Select(item => item.Name)); - throw new InvalidOperationException( + throw new ArchiveOperationException( $"Cannot determine compressed stream type. Supported Archive Formats: {extensions}" ); } diff --git a/src/SharpCompress/Archives/ArchiveFactory.cs b/src/SharpCompress/Archives/ArchiveFactory.cs index ea31d457..79d187b9 100644 --- a/src/SharpCompress/Archives/ArchiveFactory.cs +++ b/src/SharpCompress/Archives/ArchiveFactory.cs @@ -54,7 +54,7 @@ public static partial class ArchiveFactory var filesArray = fileInfos.ToArray(); if (filesArray.Length == 0) { - throw new InvalidOperationException("No files to open"); + throw new ArchiveOperationException("No files to open"); } var fileInfo = filesArray[0]; @@ -75,7 +75,7 @@ public static partial class ArchiveFactory var streamsArray = streams.ToArray(); if (streamsArray.Length == 0) { - throw new InvalidOperationException("No streams"); + throw new ArchiveOperationException("No streams"); } var firstStream = streamsArray[0]; @@ -143,7 +143,7 @@ public static partial class ArchiveFactory var extensions = string.Join(", ", factories.Select(item => item.Name)); - throw new InvalidOperationException( + throw new ArchiveOperationException( $"Cannot determine compressed stream type. Supported Archive Formats: {extensions}" ); } diff --git a/src/SharpCompress/Archives/ArchiveVolumeFactory.cs b/src/SharpCompress/Archives/ArchiveVolumeFactory.cs index 9bf86ce2..92ed6818 100644 --- a/src/SharpCompress/Archives/ArchiveVolumeFactory.cs +++ b/src/SharpCompress/Archives/ArchiveVolumeFactory.cs @@ -1,6 +1,7 @@ using System; using System.IO; using System.Text.RegularExpressions; +using SharpCompress.Common; namespace SharpCompress.Archives; @@ -20,7 +21,7 @@ internal abstract class ArchiveVolumeFactory String.Concat( m.Groups[1].Value, (index + 1) - .ToString(global::SharpCompress.Common.Constants.DefaultCultureInfo) + .ToString(Constants.DefaultCultureInfo) .PadLeft(m.Groups[2].Value.Length, '0') ) ) diff --git a/src/SharpCompress/Archives/Rar/RarArchiveVolumeFactory.cs b/src/SharpCompress/Archives/Rar/RarArchiveVolumeFactory.cs index 33ad21a9..1e464876 100644 --- a/src/SharpCompress/Archives/Rar/RarArchiveVolumeFactory.cs +++ b/src/SharpCompress/Archives/Rar/RarArchiveVolumeFactory.cs @@ -1,6 +1,7 @@ using System; using System.IO; using System.Text.RegularExpressions; +using SharpCompress.Common; namespace SharpCompress.Archives.Rar; @@ -20,7 +21,7 @@ internal static class RarArchiveVolumeFactory String.Concat( m.Groups[1].Value, (index + 1) - .ToString(global::SharpCompress.Common.Constants.DefaultCultureInfo) + .ToString(Constants.DefaultCultureInfo) .PadLeft(m.Groups[2].Value.Length, '0'), m.Groups[3].Value ) diff --git a/src/SharpCompress/Archives/Zip/ZipArchiveVolumeFactory.cs b/src/SharpCompress/Archives/Zip/ZipArchiveVolumeFactory.cs index 257c6db0..ca2f0304 100644 --- a/src/SharpCompress/Archives/Zip/ZipArchiveVolumeFactory.cs +++ b/src/SharpCompress/Archives/Zip/ZipArchiveVolumeFactory.cs @@ -1,6 +1,7 @@ using System; using System.IO; using System.Text.RegularExpressions; +using SharpCompress.Common; namespace SharpCompress.Archives.Zip; @@ -21,9 +22,7 @@ internal static class ZipArchiveVolumeFactory String.Concat( m.Groups[1].Value, Regex.Replace(m.Groups[2].Value, @"[^xz]", ""), - index - .ToString(global::SharpCompress.Common.Constants.DefaultCultureInfo) - .PadLeft(2, '0') + index.ToString(Constants.DefaultCultureInfo).PadLeft(2, '0') ) ) ); diff --git a/src/SharpCompress/Common/Ace/Headers/AceHeader.Async.cs b/src/SharpCompress/Common/Ace/Headers/AceHeader.Async.cs index a427ec45..86cf2d01 100644 --- a/src/SharpCompress/Common/Ace/Headers/AceHeader.Async.cs +++ b/src/SharpCompress/Common/Ace/Headers/AceHeader.Async.cs @@ -48,7 +48,7 @@ public abstract partial class AceHeader var checksum = AceCrc.AceCrc16(body); if (checksum != HeaderCrc) { - throw new InvalidDataException("Header checksum is invalid"); + throw new InvalidFormatException("Header checksum is invalid"); } return body; } diff --git a/src/SharpCompress/Common/Ace/Headers/AceHeader.cs b/src/SharpCompress/Common/Ace/Headers/AceHeader.cs index 5e3657bc..8d270558 100644 --- a/src/SharpCompress/Common/Ace/Headers/AceHeader.cs +++ b/src/SharpCompress/Common/Ace/Headers/AceHeader.cs @@ -89,7 +89,7 @@ public abstract partial class AceHeader var checksum = AceCrc.AceCrc16(body); if (checksum != HeaderCrc) { - throw new InvalidDataException("Header checksum is invalid"); + throw new InvalidFormatException("Header checksum is invalid"); } return body; } diff --git a/src/SharpCompress/Common/Ace/Headers/AceMainHeader.Async.cs b/src/SharpCompress/Common/Ace/Headers/AceMainHeader.Async.cs index b21ae450..39205945 100644 --- a/src/SharpCompress/Common/Ace/Headers/AceMainHeader.Async.cs +++ b/src/SharpCompress/Common/Ace/Headers/AceMainHeader.Async.cs @@ -39,7 +39,7 @@ public sealed partial class AceMainHeader // Skip signature "**ACE**" (7 bytes) if (!CheckMagicBytes(headerData, offset)) { - throw new InvalidDataException("Invalid ACE archive signature."); + throw new InvalidFormatException("Invalid ACE archive signature."); } offset += 7; diff --git a/src/SharpCompress/Common/Ace/Headers/AceMainHeader.cs b/src/SharpCompress/Common/Ace/Headers/AceMainHeader.cs index 01a19bcf..e0083c22 100644 --- a/src/SharpCompress/Common/Ace/Headers/AceMainHeader.cs +++ b/src/SharpCompress/Common/Ace/Headers/AceMainHeader.cs @@ -54,7 +54,7 @@ public sealed partial class AceMainHeader : AceHeader // Skip signature "**ACE**" (7 bytes) if (!CheckMagicBytes(headerData, offset)) { - throw new InvalidDataException("Invalid ACE archive signature."); + throw new InvalidFormatException("Invalid ACE archive signature."); } offset += 7; diff --git a/src/SharpCompress/Common/Arj/Headers/ArjHeader.Async.cs b/src/SharpCompress/Common/Arj/Headers/ArjHeader.Async.cs index 686d15f5..660fbc53 100644 --- a/src/SharpCompress/Common/Arj/Headers/ArjHeader.Async.cs +++ b/src/SharpCompress/Common/Arj/Headers/ArjHeader.Async.cs @@ -28,7 +28,7 @@ public abstract partial class ArjHeader if (!CheckMagicBytes(magic)) { - throw new InvalidDataException("Not an ARJ file (wrong magic bytes)"); + throw new InvalidFormatException("Not an ARJ file (wrong magic bytes)"); } // read header_size @@ -55,7 +55,7 @@ public abstract partial class ArjHeader // Compute the hash value if (checksum != BitConverter.ToUInt32(crc, 0)) { - throw new InvalidDataException("Header checksum is invalid"); + throw new InvalidFormatException("Header checksum is invalid"); } return body; } @@ -75,7 +75,7 @@ public abstract partial class ArjHeader .ConfigureAwait(false); if (bytesRead < 2) { - throw new EndOfStreamException( + throw new IncompleteArchiveException( "Unexpected end of stream while reading extended header size." ); } @@ -92,7 +92,7 @@ public abstract partial class ArjHeader .ConfigureAwait(false); if (bytesRead < extHeaderSize) { - throw new EndOfStreamException( + throw new IncompleteArchiveException( "Unexpected end of stream while reading extended header data." ); } @@ -103,7 +103,7 @@ public abstract partial class ArjHeader .ConfigureAwait(false); if (bytesRead < 4) { - throw new EndOfStreamException( + throw new IncompleteArchiveException( "Unexpected end of stream while reading extended header CRC." ); } @@ -111,7 +111,7 @@ public abstract partial class ArjHeader var checksum = Crc32Stream.Compute(header); if (checksum != BitConverter.ToUInt32(crcextended, 0)) { - throw new InvalidDataException("Extended header checksum is invalid"); + throw new InvalidFormatException("Extended header checksum is invalid"); } extendedHeader.Add(header); diff --git a/src/SharpCompress/Common/Arj/Headers/ArjHeader.cs b/src/SharpCompress/Common/Arj/Headers/ArjHeader.cs index d0dd4ad3..f974e44a 100644 --- a/src/SharpCompress/Common/Arj/Headers/ArjHeader.cs +++ b/src/SharpCompress/Common/Arj/Headers/ArjHeader.cs @@ -44,7 +44,7 @@ public abstract partial class ArjHeader if (!CheckMagicBytes(magic)) { - throw new InvalidDataException("Not an ARJ file (wrong magic bytes)"); + throw new InvalidFormatException("Not an ARJ file (wrong magic bytes)"); } // read header_size @@ -69,7 +69,7 @@ public abstract partial class ArjHeader // Compute the hash value if (checksum != BitConverter.ToUInt32(crc, 0)) { - throw new InvalidDataException("Header checksum is invalid"); + throw new InvalidFormatException("Header checksum is invalid"); } return body; } @@ -86,7 +86,7 @@ public abstract partial class ArjHeader int bytesRead = reader.Read(buffer, 0, 2); if (bytesRead < 2) { - throw new EndOfStreamException( + throw new IncompleteArchiveException( "Unexpected end of stream while reading extended header size." ); } @@ -101,7 +101,7 @@ public abstract partial class ArjHeader bytesRead = reader.Read(header, 0, extHeaderSize); if (bytesRead < extHeaderSize) { - throw new EndOfStreamException( + throw new IncompleteArchiveException( "Unexpected end of stream while reading extended header data." ); } @@ -110,7 +110,7 @@ public abstract partial class ArjHeader bytesRead = reader.Read(crc, 0, 4); if (bytesRead < 4) { - throw new EndOfStreamException( + throw new IncompleteArchiveException( "Unexpected end of stream while reading extended header CRC." ); } @@ -118,7 +118,7 @@ public abstract partial class ArjHeader var checksum = Crc32Stream.Compute(header); if (checksum != BitConverter.ToUInt32(crc, 0)) { - throw new InvalidDataException("Extended header checksum is invalid"); + throw new InvalidFormatException("Extended header checksum is invalid"); } extendedHeader.Add(header); diff --git a/src/SharpCompress/Common/Arj/Headers/ArjLocalHeader.cs b/src/SharpCompress/Common/Arj/Headers/ArjLocalHeader.cs index 5e5145f5..1a91a77f 100644 --- a/src/SharpCompress/Common/Arj/Headers/ArjLocalHeader.cs +++ b/src/SharpCompress/Common/Arj/Headers/ArjLocalHeader.cs @@ -66,7 +66,7 @@ public partial class ArjLocalHeader : ArjHeader { if (offset + 1 >= headerBytes.Length) { - throw new EndOfStreamException(); + throw new IncompleteArchiveException("Unexpected end of stream."); } var v = headerBytes[offset] & 0xFF | (headerBytes[offset + 1] & 0xFF) << 8; offset += 2; @@ -76,7 +76,7 @@ public partial class ArjLocalHeader : ArjHeader { if (offset + 3 >= headerBytes.Length) { - throw new EndOfStreamException(); + throw new IncompleteArchiveException("Unexpected end of stream."); } long v = headerBytes[offset] & 0xFF diff --git a/src/SharpCompress/Common/Arj/Headers/ArjMainHeader.cs b/src/SharpCompress/Common/Arj/Headers/ArjMainHeader.cs index e81d2669..74fb0542 100644 --- a/src/SharpCompress/Common/Arj/Headers/ArjMainHeader.cs +++ b/src/SharpCompress/Common/Arj/Headers/ArjMainHeader.cs @@ -54,7 +54,7 @@ public partial class ArjMainHeader : ArjHeader { if (offset >= headerBytes.Length) { - throw new EndOfStreamException(); + throw new IncompleteArchiveException("Unexpected end of stream."); } return (byte)(headerBytes[offset++] & 0xFF); } @@ -63,7 +63,7 @@ public partial class ArjMainHeader : ArjHeader { if (offset + 1 >= headerBytes.Length) { - throw new EndOfStreamException(); + throw new IncompleteArchiveException("Unexpected end of stream."); } var v = headerBytes[offset] & 0xFF | (headerBytes[offset + 1] & 0xFF) << 8; offset += 2; @@ -74,7 +74,7 @@ public partial class ArjMainHeader : ArjHeader { if (offset + 3 >= headerBytes.Length) { - throw new EndOfStreamException(); + throw new IncompleteArchiveException("Unexpected end of stream."); } long v = headerBytes[offset] & 0xFF diff --git a/src/SharpCompress/Common/Rar/AsyncMarkingBinaryReader.cs b/src/SharpCompress/Common/Rar/AsyncMarkingBinaryReader.cs index 27d0e324..c0b25ebb 100644 --- a/src/SharpCompress/Common/Rar/AsyncMarkingBinaryReader.cs +++ b/src/SharpCompress/Common/Rar/AsyncMarkingBinaryReader.cs @@ -124,7 +124,7 @@ internal class AsyncMarkingBinaryReader : IDisposable shift += 7; } while (shift <= maxShift); - throw new FormatException("malformed vint"); + throw new InvalidFormatException("malformed vint"); } public async ValueTask ReadRarVIntUInt32Async( @@ -188,7 +188,7 @@ internal class AsyncMarkingBinaryReader : IDisposable shift += 7; } while (shift <= maxShift); - throw new FormatException("malformed vint"); + throw new InvalidFormatException("malformed vint"); } public virtual void Dispose() => _reader.Dispose(); diff --git a/src/SharpCompress/Common/Rar/Headers/MarkHeader.Async.cs b/src/SharpCompress/Common/Rar/Headers/MarkHeader.Async.cs index 588a3916..36ca061b 100644 --- a/src/SharpCompress/Common/Rar/Headers/MarkHeader.Async.cs +++ b/src/SharpCompress/Common/Rar/Headers/MarkHeader.Async.cs @@ -20,7 +20,7 @@ internal partial class MarkHeader { return buffer[0]; } - throw new EndOfStreamException(); + throw new IncompleteArchiveException("Unexpected end of stream."); } public static async ValueTask ReadAsync( diff --git a/src/SharpCompress/Common/Rar/Headers/MarkHeader.cs b/src/SharpCompress/Common/Rar/Headers/MarkHeader.cs index 33cf5ae2..28b27cdb 100644 --- a/src/SharpCompress/Common/Rar/Headers/MarkHeader.cs +++ b/src/SharpCompress/Common/Rar/Headers/MarkHeader.cs @@ -24,7 +24,7 @@ internal partial class MarkHeader : IRarHeader { return (byte)b; } - throw new EndOfStreamException(); + throw new IncompleteArchiveException("Unexpected end of stream."); } public static MarkHeader Read(Stream stream, bool leaveStreamOpen, bool lookForHeader) diff --git a/src/SharpCompress/Common/Rar/RarEntry.cs b/src/SharpCompress/Common/Rar/RarEntry.cs index 3393f06c..506b69e6 100644 --- a/src/SharpCompress/Common/Rar/RarEntry.cs +++ b/src/SharpCompress/Common/Rar/RarEntry.cs @@ -76,7 +76,7 @@ public abstract class RarEntry : Entry public override string ToString() => string.Format( - global::SharpCompress.Common.Constants.DefaultCultureInfo, + Constants.DefaultCultureInfo, "Entry Path: {0} Compressed Size: {1} Uncompressed Size: {2} CRC: {3}", Key, CompressedSize, diff --git a/src/SharpCompress/Common/Rar/RarVolume.cs b/src/SharpCompress/Common/Rar/RarVolume.cs index e2bc6a45..1b7de043 100644 --- a/src/SharpCompress/Common/Rar/RarVolume.cs +++ b/src/SharpCompress/Common/Rar/RarVolume.cs @@ -134,7 +134,7 @@ public abstract class RarVolume : Volume { if (Mode == StreamingMode.Streaming) { - throw new InvalidOperationException( + throw new ArchiveOperationException( "ArchiveHeader should never been null in a streaming read." ); } @@ -243,7 +243,7 @@ public abstract class RarVolume : Volume { if (Mode == StreamingMode.Streaming) { - throw new InvalidOperationException( + throw new ArchiveOperationException( "ArchiveHeader should never been null in a streaming read." ); } diff --git a/src/SharpCompress/Common/SevenZip/ArchiveDatabase.cs b/src/SharpCompress/Common/SevenZip/ArchiveDatabase.cs index 3c461522..dbad5353 100644 --- a/src/SharpCompress/Common/SevenZip/ArchiveDatabase.cs +++ b/src/SharpCompress/Common/SevenZip/ArchiveDatabase.cs @@ -87,7 +87,7 @@ internal partial class ArchiveDatabase { if (folderIndex >= _folders.Count) { - throw new InvalidOperationException(); + throw new ArchiveOperationException(); } _folderStartFileIndex.Add(i); // check it diff --git a/src/SharpCompress/Common/SevenZip/ArchiveReader.Async.cs b/src/SharpCompress/Common/SevenZip/ArchiveReader.Async.cs index a9ab2109..52964077 100644 --- a/src/SharpCompress/Common/SevenZip/ArchiveReader.Async.cs +++ b/src/SharpCompress/Common/SevenZip/ArchiveReader.Async.cs @@ -66,7 +66,7 @@ internal sealed partial class ArchiveReader if (db._majorVersion != 0) { - throw new InvalidOperationException(); + throw new ArchiveOperationException(); } var crcFromArchive = DataReader.Get32(_header, 8); @@ -82,7 +82,7 @@ internal sealed partial class ArchiveReader if (crc != crcFromArchive) { - throw new InvalidOperationException(); + throw new ArchiveOperationException(); } db._startPositionAfterHeader = _streamOrigin + 0x20; @@ -96,12 +96,12 @@ internal sealed partial class ArchiveReader if (nextHeaderOffset < 0 || nextHeaderSize < 0 || nextHeaderSize > int.MaxValue) { - throw new InvalidOperationException(); + throw new ArchiveOperationException(); } if (nextHeaderOffset > _streamEnding - db._startPositionAfterHeader) { - throw new InvalidOperationException("nextHeaderOffset is invalid"); + throw new ArchiveOperationException("nextHeaderOffset is invalid"); } _stream.Seek(nextHeaderOffset, SeekOrigin.Current); @@ -113,7 +113,7 @@ internal sealed partial class ArchiveReader if (Crc.Finish(Crc.Update(Crc.INIT_CRC, header, 0, header.Length)) != nextHeaderCrc) { - throw new InvalidOperationException(); + throw new ArchiveOperationException(); } using (var streamSwitch = new CStreamSwitch()) @@ -125,7 +125,7 @@ internal sealed partial class ArchiveReader { if (type != BlockType.EncodedHeader) { - throw new InvalidOperationException(); + throw new ArchiveOperationException(); } var dataVector = await ReadAndDecodePackedStreamsAsync( @@ -144,14 +144,14 @@ internal sealed partial class ArchiveReader if (dataVector.Count != 1) { - throw new InvalidOperationException(); + throw new ArchiveOperationException(); } streamSwitch.Set(this, dataVector[0]); if (ReadId() != BlockType.Header) { - throw new InvalidOperationException(); + throw new ArchiveOperationException(); } } @@ -305,7 +305,7 @@ internal sealed partial class ArchiveReader if (type != BlockType.FilesInfo) { - throw new InvalidOperationException(); + throw new ArchiveOperationException(); } var numFiles = ReadNum(); @@ -424,7 +424,7 @@ internal sealed partial class ArchiveReader { if (ReadByte() != 0) { - throw new InvalidOperationException(); + throw new ArchiveOperationException(); } } break; @@ -436,7 +436,7 @@ internal sealed partial class ArchiveReader var checkRecordsSize = (db._majorVersion > 0 || db._minorVersion > 2); if (checkRecordsSize && _currentReader.Offset - oldPos != size) { - throw new InvalidOperationException(); + throw new ArchiveOperationException(); } } diff --git a/src/SharpCompress/Common/SevenZip/ArchiveReader.cs b/src/SharpCompress/Common/SevenZip/ArchiveReader.cs index 020e8e40..3a4e5622 100644 --- a/src/SharpCompress/Common/SevenZip/ArchiveReader.cs +++ b/src/SharpCompress/Common/SevenZip/ArchiveReader.cs @@ -72,7 +72,7 @@ internal partial class ArchiveReader } if (type == BlockType.End) { - throw new InvalidOperationException(); + throw new ArchiveOperationException(); } SkipData(); } @@ -749,7 +749,7 @@ internal partial class ArchiveReader if (type != BlockType.FilesInfo) { - throw new InvalidOperationException(); + throw new ArchiveOperationException(); } var numFiles = ReadNum(); @@ -888,7 +888,7 @@ internal partial class ArchiveReader { if (ReadByte() != 0) { - throw new InvalidOperationException(); + throw new ArchiveOperationException(); } } break; @@ -901,7 +901,7 @@ internal partial class ArchiveReader var checkRecordsSize = (db._majorVersion > 0 || db._minorVersion > 2); if (checkRecordsSize && _currentReader.Offset - oldPos != size) { - throw new InvalidOperationException(); + throw new ArchiveOperationException(); } } @@ -953,7 +953,7 @@ internal partial class ArchiveReader var delta = stream.Read(_header, offset, 0x20 - offset); if (delta == 0) { - throw new EndOfStreamException(); + throw new IncompleteArchiveException("Unexpected end of stream."); } offset += delta; @@ -1005,7 +1005,7 @@ internal partial class ArchiveReader if (db._majorVersion != 0) { - throw new InvalidOperationException(); + throw new ArchiveOperationException(); } var crcFromArchive = DataReader.Get32(_header, 8); @@ -1021,7 +1021,7 @@ internal partial class ArchiveReader if (crc != crcFromArchive) { - throw new InvalidOperationException(); + throw new ArchiveOperationException(); } db._startPositionAfterHeader = _streamOrigin + 0x20; @@ -1035,12 +1035,12 @@ internal partial class ArchiveReader if (nextHeaderOffset < 0 || nextHeaderSize < 0 || nextHeaderSize > int.MaxValue) { - throw new InvalidOperationException(); + throw new ArchiveOperationException(); } if (nextHeaderOffset > _streamEnding - db._startPositionAfterHeader) { - throw new InvalidOperationException("nextHeaderOffset is invalid"); + throw new ArchiveOperationException("nextHeaderOffset is invalid"); } _stream.Seek(nextHeaderOffset, SeekOrigin.Current); @@ -1050,7 +1050,7 @@ internal partial class ArchiveReader if (Crc.Finish(Crc.Update(Crc.INIT_CRC, header, 0, header.Length)) != nextHeaderCrc) { - throw new InvalidOperationException(); + throw new ArchiveOperationException(); } using (var streamSwitch = new CStreamSwitch()) @@ -1062,7 +1062,7 @@ internal partial class ArchiveReader { if (type != BlockType.EncodedHeader) { - throw new InvalidOperationException(); + throw new ArchiveOperationException(); } var dataVector = ReadAndDecodePackedStreams( @@ -1079,14 +1079,14 @@ internal partial class ArchiveReader if (dataVector.Count != 1) { - throw new InvalidOperationException(); + throw new ArchiveOperationException(); } streamSwitch.Set(this, dataVector[0]); if (ReadId() != BlockType.Header) { - throw new InvalidOperationException(); + throw new ArchiveOperationException(); } } @@ -1260,7 +1260,7 @@ internal partial class ArchiveReader var firstFileIndex = db._folderStartFileIndex[folderIndex]; if (firstFileIndex > fileIndex || fileIndex - firstFileIndex >= numFilesInFolder) { - throw new InvalidOperationException(); + throw new ArchiveOperationException(); } var skipCount = fileIndex - firstFileIndex; diff --git a/src/SharpCompress/Common/SevenZip/CFolder.cs b/src/SharpCompress/Common/SevenZip/CFolder.cs index 8b0123da..683c27ca 100644 --- a/src/SharpCompress/Common/SevenZip/CFolder.cs +++ b/src/SharpCompress/Common/SevenZip/CFolder.cs @@ -30,7 +30,7 @@ internal class CFolder } } - throw new InvalidOperationException(); + throw new ArchiveOperationException(); } public int GetNumOutStreams() diff --git a/src/SharpCompress/Common/SevenZip/CStreamSwitch.cs b/src/SharpCompress/Common/SevenZip/CStreamSwitch.cs index 8b813918..8b429b19 100644 --- a/src/SharpCompress/Common/SevenZip/CStreamSwitch.cs +++ b/src/SharpCompress/Common/SevenZip/CStreamSwitch.cs @@ -44,7 +44,7 @@ internal struct CStreamSwitch : IDisposable var dataIndex = archive.ReadNum(); if (dataIndex < 0 || dataIndex >= dataVector.Count) { - throw new InvalidOperationException(); + throw new ArchiveOperationException(); } _archive = archive; diff --git a/src/SharpCompress/Common/SevenZip/DataReader.cs b/src/SharpCompress/Common/SevenZip/DataReader.cs index e04d2950..8788192b 100644 --- a/src/SharpCompress/Common/SevenZip/DataReader.cs +++ b/src/SharpCompress/Common/SevenZip/DataReader.cs @@ -49,7 +49,7 @@ internal class DataReader { if (Offset >= _ending) { - throw new EndOfStreamException(); + throw new IncompleteArchiveException("Unexpected end of stream."); } return _buffer[Offset++]; @@ -59,7 +59,7 @@ internal class DataReader { if (length > _ending - Offset) { - throw new EndOfStreamException(); + throw new IncompleteArchiveException("Unexpected end of stream."); } while (length-- > 0) @@ -72,7 +72,7 @@ internal class DataReader { if (size > _ending - Offset) { - throw new EndOfStreamException(); + throw new IncompleteArchiveException("Unexpected end of stream."); } Offset += (int)size; @@ -84,7 +84,7 @@ internal class DataReader { if (Offset >= _ending) { - throw new EndOfStreamException(); + throw new IncompleteArchiveException("Unexpected end of stream."); } var firstByte = _buffer[Offset++]; @@ -102,7 +102,7 @@ internal class DataReader if (Offset >= _ending) { - throw new EndOfStreamException(); + throw new IncompleteArchiveException("Unexpected end of stream."); } value |= (ulong)_buffer[Offset++] << (8 * i); @@ -127,7 +127,7 @@ internal class DataReader { if (Offset + 4 > _ending) { - throw new EndOfStreamException(); + throw new IncompleteArchiveException("Unexpected end of stream."); } var res = Get32(_buffer, Offset); @@ -139,7 +139,7 @@ internal class DataReader { if (Offset + 8 > _ending) { - throw new EndOfStreamException(); + throw new IncompleteArchiveException("Unexpected end of stream."); } var res = Get64(_buffer, Offset); @@ -155,7 +155,7 @@ internal class DataReader { if (ending + 2 > _ending) { - throw new EndOfStreamException(); + throw new IncompleteArchiveException("Unexpected end of stream."); } if (_buffer[ending] == 0 && _buffer[ending + 1] == 0) diff --git a/src/SharpCompress/Common/SharpCompressException.cs b/src/SharpCompress/Common/SharpCompressException.cs index ee0ef2a5..7dc7cdda 100644 --- a/src/SharpCompress/Common/SharpCompressException.cs +++ b/src/SharpCompress/Common/SharpCompressException.cs @@ -15,6 +15,17 @@ public class SharpCompressException : Exception public class ArchiveException(string message) : SharpCompressException(message); +public class ArchiveOperationException : SharpCompressException +{ + public ArchiveOperationException() { } + + public ArchiveOperationException(string message) + : base(message) { } + + public ArchiveOperationException(string message, Exception inner) + : base(message, inner) { } +} + public class IncompleteArchiveException(string message) : ArchiveException(message); public class CryptographicException(string message) : SharpCompressException(message); diff --git a/src/SharpCompress/Common/Tar/Headers/TarHeader.Async.cs b/src/SharpCompress/Common/Tar/Headers/TarHeader.Async.cs index 9b3bf22f..aa6fa0f8 100644 --- a/src/SharpCompress/Common/Tar/Headers/TarHeader.Async.cs +++ b/src/SharpCompress/Common/Tar/Headers/TarHeader.Async.cs @@ -27,7 +27,7 @@ internal sealed partial class TarHeader await WriteUstarAsync(output, cancellationToken).ConfigureAwait(false); break; default: - throw new InvalidOperationException("This should be impossible..."); + throw new ArchiveOperationException("This should be impossible..."); } } @@ -80,7 +80,7 @@ internal sealed partial class TarHeader if (splitIndex == -1) { - throw new InvalidDataException( + throw new InvalidFormatException( $"Tar header USTAR format can not fit file name \"{fullName}\" of length {nameByteCount}! Directory separator not found! Try using GNU Tar format instead!" ); } @@ -90,14 +90,14 @@ internal sealed partial class TarHeader if (this.ArchiveEncoding.GetEncoding().GetByteCount(namePrefix) >= 155) { - throw new InvalidDataException( + throw new InvalidFormatException( $"Tar header USTAR format can not fit file name \"{fullName}\" of length {nameByteCount}! Try using GNU Tar format instead!" ); } if (this.ArchiveEncoding.GetEncoding().GetByteCount(name) >= 100) { - throw new InvalidDataException( + throw new InvalidFormatException( $"Tar header USTAR format can not fit file name \"{fullName}\" of length {nameByteCount}! Try using GNU Tar format instead!" ); } diff --git a/src/SharpCompress/Common/Tar/Headers/TarHeader.cs b/src/SharpCompress/Common/Tar/Headers/TarHeader.cs index 83018a3f..720acc37 100644 --- a/src/SharpCompress/Common/Tar/Headers/TarHeader.cs +++ b/src/SharpCompress/Common/Tar/Headers/TarHeader.cs @@ -52,7 +52,7 @@ internal sealed partial class TarHeader WriteUstar(output); break; default: - throw new InvalidOperationException("This should be impossible..."); + throw new ArchiveOperationException("This should be impossible..."); } } @@ -110,7 +110,7 @@ internal sealed partial class TarHeader if (splitIndex == -1) { - throw new InvalidDataException( + throw new InvalidFormatException( $"Tar header USTAR format can not fit file name \"{fullName}\" of length {nameByteCount}! Directory separator not found! Try using GNU Tar format instead!" ); } @@ -120,14 +120,14 @@ internal sealed partial class TarHeader if (this.ArchiveEncoding.GetEncoding().GetByteCount(namePrefix) >= 155) { - throw new InvalidDataException( + throw new InvalidFormatException( $"Tar header USTAR format can not fit file name \"{fullName}\" of length {nameByteCount}! Try using GNU Tar format instead!" ); } if (this.ArchiveEncoding.GetEncoding().GetByteCount(name) >= 100) { - throw new InvalidDataException( + throw new InvalidFormatException( $"Tar header USTAR format can not fit file name \"{fullName}\" of length {nameByteCount}! Try using GNU Tar format instead!" ); } diff --git a/src/SharpCompress/Common/Zip/SeekableZipHeaderFactory.Async.cs b/src/SharpCompress/Common/Zip/SeekableZipHeaderFactory.Async.cs index d7372b00..11e7eec4 100644 --- a/src/SharpCompress/Common/Zip/SeekableZipHeaderFactory.Async.cs +++ b/src/SharpCompress/Common/Zip/SeekableZipHeaderFactory.Async.cs @@ -143,7 +143,7 @@ internal sealed partial class SeekableZipHeaderFactory is not LocalEntryHeader localEntryHeader ) { - throw new InvalidOperationException(); + throw new ArchiveOperationException(); } // populate fields only known from the DirectoryEntryHeader diff --git a/src/SharpCompress/Common/Zip/SeekableZipHeaderFactory.cs b/src/SharpCompress/Common/Zip/SeekableZipHeaderFactory.cs index ccbf2082..22304b9d 100644 --- a/src/SharpCompress/Common/Zip/SeekableZipHeaderFactory.cs +++ b/src/SharpCompress/Common/Zip/SeekableZipHeaderFactory.cs @@ -148,7 +148,7 @@ internal sealed partial class SeekableZipHeaderFactory : ZipHeaderFactory var signature = reader.ReadUInt32(); if (ReadHeader(signature, reader, _zip64) is not LocalEntryHeader localEntryHeader) { - throw new InvalidOperationException(); + throw new ArchiveOperationException(); } // populate fields only known from the DirectoryEntryHeader diff --git a/src/SharpCompress/Common/Zip/StreamingZipHeaderFactory.Async.cs b/src/SharpCompress/Common/Zip/StreamingZipHeaderFactory.Async.cs index febd59b5..80ff16b4 100644 --- a/src/SharpCompress/Common/Zip/StreamingZipHeaderFactory.Async.cs +++ b/src/SharpCompress/Common/Zip/StreamingZipHeaderFactory.Async.cs @@ -82,7 +82,7 @@ internal sealed partial class StreamingZipHeaderFactory private ZipHeader? _current; public ZipHeader Current => - _current ?? throw new InvalidOperationException("No current header is available."); + _current ?? throw new ArchiveOperationException("No current header is available."); /// /// Advances to the next ZIP header in the stream, honoring streaming data descriptors where applicable. diff --git a/src/SharpCompress/Common/Zip/WinzipAesCryptoStream.Async.cs b/src/SharpCompress/Common/Zip/WinzipAesCryptoStream.Async.cs index 766f4ecb..74be64fc 100644 --- a/src/SharpCompress/Common/Zip/WinzipAesCryptoStream.Async.cs +++ b/src/SharpCompress/Common/Zip/WinzipAesCryptoStream.Async.cs @@ -99,7 +99,7 @@ internal partial class WinzipAesCryptoStream { if (_isFinalBlock) { - throw new InvalidOperationException(); + throw new ArchiveOperationException(); } var bytesRemaining = last - offset; diff --git a/src/SharpCompress/Common/Zip/WinzipAesCryptoStream.cs b/src/SharpCompress/Common/Zip/WinzipAesCryptoStream.cs index bb7cff31..2ea1d2d7 100644 --- a/src/SharpCompress/Common/Zip/WinzipAesCryptoStream.cs +++ b/src/SharpCompress/Common/Zip/WinzipAesCryptoStream.cs @@ -127,7 +127,7 @@ internal partial class WinzipAesCryptoStream : Stream { if (_isFinalBlock) { - throw new InvalidOperationException(); + throw new ArchiveOperationException(); } var bytesRemaining = last - offset; diff --git a/src/SharpCompress/Common/Zip/WinzipAesEncryptionData.cs b/src/SharpCompress/Common/Zip/WinzipAesEncryptionData.cs index 2da74a62..40309c3d 100644 --- a/src/SharpCompress/Common/Zip/WinzipAesEncryptionData.cs +++ b/src/SharpCompress/Common/Zip/WinzipAesEncryptionData.cs @@ -76,6 +76,6 @@ internal class WinzipAesEncryptionData WinzipAesKeySize.KeySize128 => 16, WinzipAesKeySize.KeySize192 => 24, WinzipAesKeySize.KeySize256 => 32, - _ => throw new InvalidOperationException(), + _ => throw new ArchiveOperationException(), }; } diff --git a/src/SharpCompress/Common/Zip/ZipFilePart.Async.cs b/src/SharpCompress/Common/Zip/ZipFilePart.Async.cs index 8b06727d..fb2caf11 100644 --- a/src/SharpCompress/Common/Zip/ZipFilePart.Async.cs +++ b/src/SharpCompress/Common/Zip/ZipFilePart.Async.cs @@ -101,7 +101,7 @@ internal abstract partial class ZipFilePart default: { - throw new InvalidOperationException("Header.CompressionMethod is invalid"); + throw new ArchiveOperationException("Header.CompressionMethod is invalid"); } } } diff --git a/src/SharpCompress/Common/Zip/ZipFilePart.cs b/src/SharpCompress/Common/Zip/ZipFilePart.cs index b45ef1fc..3f11df6d 100644 --- a/src/SharpCompress/Common/Zip/ZipFilePart.cs +++ b/src/SharpCompress/Common/Zip/ZipFilePart.cs @@ -270,7 +270,7 @@ internal abstract partial class ZipFilePart : FilePart default: { - throw new InvalidOperationException("Header.CompressionMethod is invalid"); + throw new ArchiveOperationException("Header.CompressionMethod is invalid"); } } } diff --git a/src/SharpCompress/Compressors/ArcLzw/ArcLzwStream.cs b/src/SharpCompress/Compressors/ArcLzw/ArcLzwStream.cs index 29373659..822b2d64 100644 --- a/src/SharpCompress/Compressors/ArcLzw/ArcLzwStream.cs +++ b/src/SharpCompress/Compressors/ArcLzw/ArcLzwStream.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.IO; using System.Linq; +using SharpCompress.Common; using SharpCompress.Compressors.RLE90; namespace SharpCompress.Compressors.ArcLzw; @@ -74,7 +75,7 @@ public partial class ArcLzwStream : Stream { if (input[0] != BITS) { - throw new InvalidDataException($"File packed with {input[0]}, expected {BITS}."); + throw new InvalidFormatException($"File packed with {input[0]}, expected {BITS}."); } input = input.Skip(1).ToArray(); diff --git a/src/SharpCompress/Compressors/Arj/BitReader.Async.cs b/src/SharpCompress/Compressors/Arj/BitReader.Async.cs index 612c9143..b999327f 100644 --- a/src/SharpCompress/Compressors/Arj/BitReader.Async.cs +++ b/src/SharpCompress/Compressors/Arj/BitReader.Async.cs @@ -2,6 +2,7 @@ using System; using System.IO; using System.Threading; using System.Threading.Tasks; +using SharpCompress.Common; namespace SharpCompress.Compressors.Arj; @@ -20,7 +21,7 @@ public partial class BitReader .ConfigureAwait(false); if (bytesRead < 1) { - throw new EndOfStreamException("No more data available in BitReader."); + throw new IncompleteArchiveException("No more data available in BitReader."); } _bitBuffer = buffer[0]; diff --git a/src/SharpCompress/Compressors/Arj/BitReader.cs b/src/SharpCompress/Compressors/Arj/BitReader.cs index efe5bf47..640fc060 100644 --- a/src/SharpCompress/Compressors/Arj/BitReader.cs +++ b/src/SharpCompress/Compressors/Arj/BitReader.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using SharpCompress.Common; namespace SharpCompress.Compressors.Arj; @@ -27,7 +28,7 @@ public partial class BitReader int nextByte = _input.ReadByte(); if (nextByte < 0) { - throw new EndOfStreamException("No more data available in BitReader."); + throw new IncompleteArchiveException("No more data available in BitReader."); } _bitBuffer = nextByte; diff --git a/src/SharpCompress/Compressors/Arj/HuffmanTree.Async.cs b/src/SharpCompress/Compressors/Arj/HuffmanTree.Async.cs index 9f7cb407..9f0c16e4 100644 --- a/src/SharpCompress/Compressors/Arj/HuffmanTree.Async.cs +++ b/src/SharpCompress/Compressors/Arj/HuffmanTree.Async.cs @@ -1,6 +1,7 @@ using System; using System.Threading; using System.Threading.Tasks; +using SharpCompress.Common; namespace SharpCompress.Compressors.Arj; @@ -13,7 +14,7 @@ public sealed partial class HuffTree { if (_tree.Count == 0) { - throw new InvalidOperationException("Tree not initialized"); + throw new ArchiveOperationException("Tree not initialized"); } TreeEntry node = _tree[0]; @@ -29,7 +30,7 @@ public sealed partial class HuffTree if (index >= _tree.Count) { - throw new InvalidOperationException("Invalid branch index during read"); + throw new ArchiveOperationException("Invalid branch index during read"); } node = _tree[index]; diff --git a/src/SharpCompress/Compressors/Arj/HuffmanTree.cs b/src/SharpCompress/Compressors/Arj/HuffmanTree.cs index 2e4ddd57..c9f5a505 100644 --- a/src/SharpCompress/Compressors/Arj/HuffmanTree.cs +++ b/src/SharpCompress/Compressors/Arj/HuffmanTree.cs @@ -87,7 +87,7 @@ public sealed partial class HuffTree if (valueLengths.Length > TreeEntry.MAX_INDEX / 2) { - throw new InvalidOperationException("Too many code lengths"); + throw new ArchiveOperationException("Too many code lengths"); } _tree.Clear(); @@ -109,7 +109,7 @@ public sealed partial class HuffTree catch (ArgumentOutOfRangeException e) { _tree.Clear(); - throw new InvalidOperationException("Branch index exceeds limit", e); + throw new ArchiveOperationException("Branch index exceeds limit", e); } // each branch node allocates two children @@ -135,7 +135,7 @@ public sealed partial class HuffTree // sanity check (too many leaves) if (_tree.Count > maxAllocated) { - throw new InvalidOperationException("Too many leaves"); + throw new ArchiveOperationException("Too many leaves"); } // stop when no longer finding longer codes @@ -148,7 +148,7 @@ public sealed partial class HuffTree // ensure tree is complete if (_tree.Count != maxAllocated) { - throw new InvalidOperationException( + throw new ArchiveOperationException( $"Missing some leaves: tree count = {_tree.Count}, expected = {maxAllocated}" ); } @@ -158,7 +158,7 @@ public sealed partial class HuffTree { if (_tree.Count == 0) { - throw new InvalidOperationException("Tree not initialized"); + throw new ArchiveOperationException("Tree not initialized"); } TreeEntry node = _tree[0]; @@ -174,7 +174,7 @@ public sealed partial class HuffTree if (index >= _tree.Count) { - throw new InvalidOperationException("Invalid branch index during read"); + throw new ArchiveOperationException("Invalid branch index during read"); } node = _tree[index]; diff --git a/src/SharpCompress/Compressors/Arj/LHDecoderStream.Async.cs b/src/SharpCompress/Compressors/Arj/LHDecoderStream.Async.cs index f978b0a8..b231a368 100644 --- a/src/SharpCompress/Compressors/Arj/LHDecoderStream.Async.cs +++ b/src/SharpCompress/Compressors/Arj/LHDecoderStream.Async.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.IO; using System.Threading; using System.Threading.Tasks; +using SharpCompress.Common; namespace SharpCompress.Compressors.Arj; @@ -34,7 +35,7 @@ public sealed partial class LHDecoderStream if (backPtr >= _buffer.Count) { - throw new InvalidDataException("Invalid back_ptr in LH stream"); + throw new InvalidFormatException("Invalid back_ptr in LH stream"); } int srcIndex = _buffer.Count - 1 - backPtr; diff --git a/src/SharpCompress/Compressors/Arj/LHDecoderStream.cs b/src/SharpCompress/Compressors/Arj/LHDecoderStream.cs index c60ef871..210bf46e 100644 --- a/src/SharpCompress/Compressors/Arj/LHDecoderStream.cs +++ b/src/SharpCompress/Compressors/Arj/LHDecoderStream.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.IO; +using SharpCompress.Common; namespace SharpCompress.Compressors.Arj; @@ -72,7 +73,7 @@ public sealed partial class LHDecoderStream : Stream if (backPtr >= _buffer.Count) { - throw new InvalidDataException("Invalid back_ptr in LH stream"); + throw new InvalidFormatException("Invalid back_ptr in LH stream"); } int srcIndex = _buffer.Count - 1 - backPtr; diff --git a/src/SharpCompress/Compressors/Arj/LhaStream.Async.cs b/src/SharpCompress/Compressors/Arj/LhaStream.Async.cs index 0dfe1a88..0d092e3b 100644 --- a/src/SharpCompress/Compressors/Arj/LhaStream.Async.cs +++ b/src/SharpCompress/Compressors/Arj/LhaStream.Async.cs @@ -3,6 +3,7 @@ using System.IO; using System.IO.Compression; using System.Threading; using System.Threading.Tasks; +using SharpCompress.Common; namespace SharpCompress.Compressors.Arj; @@ -64,7 +65,7 @@ public sealed partial class LhaStream len++; if (len > 255) { - throw new InvalidOperationException("Code length overflow"); + throw new ArchiveOperationException("Code length overflow"); } } } @@ -114,7 +115,7 @@ public sealed partial class LhaStream if (numCodes > NUM_TEMP_CODELEN) { - throw new InvalidDataException("temporary codelen table has invalid size"); + throw new InvalidFormatException("temporary codelen table has invalid size"); } // read actual lengths @@ -130,7 +131,7 @@ public sealed partial class LhaStream if (3 + skip > numCodes) { - throw new InvalidDataException("temporary codelen table has invalid size"); + throw new InvalidFormatException("temporary codelen table has invalid size"); } for (int i = 3 + skip; i < numCodes; i++) @@ -159,7 +160,7 @@ public sealed partial class LhaStream if (numCodes > NUM_COMMANDS) { - throw new InvalidDataException("commands codelen table has invalid size"); + throw new InvalidFormatException("commands codelen table has invalid size"); } int index = 0; @@ -208,7 +209,7 @@ public sealed partial class LhaStream if (numCodes > _config.HistoryBits) { - throw new InvalidDataException("Offset code table too large"); + throw new InvalidFormatException("Offset code table too large"); } byte[] codeLengths = new byte[NUM_TEMP_CODELEN]; diff --git a/src/SharpCompress/Compressors/Arj/LhaStream.cs b/src/SharpCompress/Compressors/Arj/LhaStream.cs index 65195405..f7c384ac 100644 --- a/src/SharpCompress/Compressors/Arj/LhaStream.cs +++ b/src/SharpCompress/Compressors/Arj/LhaStream.cs @@ -3,6 +3,7 @@ using System.Data; using System.IO; using System.IO.Compression; using System.Linq; +using SharpCompress.Common; namespace SharpCompress.Compressors.Arj; @@ -86,7 +87,7 @@ public sealed partial class LhaStream : Stream len++; if (len > 255) { - throw new InvalidOperationException("Code length overflow"); + throw new ArchiveOperationException("Code length overflow"); } } } @@ -133,7 +134,7 @@ public sealed partial class LhaStream : Stream if (numCodes > NUM_TEMP_CODELEN) { - throw new InvalidDataException("temporary codelen table has invalid size"); + throw new InvalidFormatException("temporary codelen table has invalid size"); } // read actual lengths @@ -148,7 +149,7 @@ public sealed partial class LhaStream : Stream if (3 + skip > numCodes) { - throw new InvalidDataException("temporary codelen table has invalid size"); + throw new InvalidFormatException("temporary codelen table has invalid size"); } for (int i = 3 + skip; i < numCodes; i++) @@ -176,7 +177,7 @@ public sealed partial class LhaStream : Stream if (numCodes > NUM_COMMANDS) { - throw new InvalidDataException("commands codelen table has invalid size"); + throw new InvalidFormatException("commands codelen table has invalid size"); } int index = 0; @@ -218,7 +219,7 @@ public sealed partial class LhaStream : Stream if (numCodes > _config.HistoryBits) { - throw new InvalidDataException("Offset code table too large"); + throw new InvalidFormatException("Offset code table too large"); } byte[] codeLengths = new byte[NUM_TEMP_CODELEN]; diff --git a/src/SharpCompress/Compressors/BZip2/CBZip2InputStream.Async.cs b/src/SharpCompress/Compressors/BZip2/CBZip2InputStream.Async.cs index 8b2c45af..75f50896 100644 --- a/src/SharpCompress/Compressors/BZip2/CBZip2InputStream.Async.cs +++ b/src/SharpCompress/Compressors/BZip2/CBZip2InputStream.Async.cs @@ -5,6 +5,7 @@ using System.Buffers; using System.IO; using System.Threading; using System.Threading.Tasks; +using SharpCompress.Common; using SharpCompress.IO; namespace SharpCompress.Compressors.BZip2; @@ -68,7 +69,7 @@ internal partial class CBZip2InputStream } if (magic0 != 'B' || magic1 != 'Z' || magic2 != 'h') { - throw new IOException("Not a BZIP2 marked stream"); + throw new InvalidFormatException("Not a BZIP2 marked stream"); } var read3 = await bsStream .ReadAsync(singleByte, 0, 1, cancellationToken) diff --git a/src/SharpCompress/Compressors/BZip2/CBZip2InputStream.cs b/src/SharpCompress/Compressors/BZip2/CBZip2InputStream.cs index 2bd0d94a..2fb58d21 100644 --- a/src/SharpCompress/Compressors/BZip2/CBZip2InputStream.cs +++ b/src/SharpCompress/Compressors/BZip2/CBZip2InputStream.cs @@ -5,6 +5,7 @@ using System.Buffers; using System.IO; using System.Threading; using System.Threading.Tasks; +using SharpCompress.Common; /* * Copyright 2001,2004-2005 The Apache Software Foundation @@ -45,7 +46,7 @@ internal partial class CBZip2InputStream : Stream private static void Cadvise() { //System.out.Println("CRC Error"); - throw new InvalidOperationException("BZip2 error"); + throw new ArchiveOperationException("BZip2 error"); } private static void BadBGLengths() => Cadvise(); @@ -54,7 +55,7 @@ internal partial class CBZip2InputStream : Stream private static void CompressedStreamEOF() { - throw new InvalidOperationException("BZip2 compressed file ends unexpectedly"); + throw new ArchiveOperationException("BZip2 compressed file ends unexpectedly"); } private void MakeMaps() @@ -259,7 +260,7 @@ internal partial class CBZip2InputStream : Stream } if (magic0 != 'B' || magic1 != 'Z' || magic2 != 'h') { - throw new IOException("Not a BZIP2 marked stream"); + throw new InvalidFormatException("Not a BZIP2 marked stream"); } var magic3 = bsStream.ReadByte(); if (magic3 < '1' || magic3 > '9') @@ -1084,7 +1085,7 @@ internal partial class CBZip2InputStream : Stream { if (!(0 <= newSize100k && newSize100k <= 9 && 0 <= blockSize100k && blockSize100k <= 9)) { - // throw new IOException("Invalid block size"); + // throw new InvalidFormatException("Invalid block size"); } blockSize100k = newSize100k; diff --git a/src/SharpCompress/Compressors/Deflate/DeflateManager.cs b/src/SharpCompress/Compressors/Deflate/DeflateManager.cs index a7b643cb..459ac5d9 100644 --- a/src/SharpCompress/Compressors/Deflate/DeflateManager.cs +++ b/src/SharpCompress/Compressors/Deflate/DeflateManager.cs @@ -70,6 +70,7 @@ using System; using SharpCompress.Algorithms; +using SharpCompress.Common; namespace SharpCompress.Compressors.Deflate; @@ -1707,7 +1708,7 @@ internal sealed partial class DeflateManager { throw new ZlibException( string.Format( - global::SharpCompress.Common.Constants.DefaultCultureInfo, + Constants.DefaultCultureInfo, "memLevel must be in the range 1.. {0}", MEM_LEVEL_MAX ) @@ -1882,7 +1883,7 @@ internal sealed partial class DeflateManager ]; throw new ZlibException( string.Format( - global::SharpCompress.Common.Constants.DefaultCultureInfo, + Constants.DefaultCultureInfo, "Something is fishy. [{0}]", _codec.Message ) diff --git a/src/SharpCompress/Compressors/Deflate/DeflateStream.cs b/src/SharpCompress/Compressors/Deflate/DeflateStream.cs index 6e212db6..774770ad 100644 --- a/src/SharpCompress/Compressors/Deflate/DeflateStream.cs +++ b/src/SharpCompress/Compressors/Deflate/DeflateStream.cs @@ -29,6 +29,7 @@ using System.IO; using System.Text; using System.Threading; using System.Threading.Tasks; +using SharpCompress.Common; using SharpCompress.IO; namespace SharpCompress.Compressors.Deflate; @@ -120,7 +121,7 @@ public partial class DeflateStream : Stream, IStreamStack { throw new ZlibException( string.Format( - global::SharpCompress.Common.Constants.DefaultCultureInfo, + Constants.DefaultCultureInfo, "Don't be silly. {0} bytes?? Use a bigger buffer, at least {1}.", value, ZlibConstants.WorkingBufferSizeMin diff --git a/src/SharpCompress/Compressors/Deflate/GZipStream.Async.cs b/src/SharpCompress/Compressors/Deflate/GZipStream.Async.cs index 270421bf..615f99fb 100644 --- a/src/SharpCompress/Compressors/Deflate/GZipStream.Async.cs +++ b/src/SharpCompress/Compressors/Deflate/GZipStream.Async.cs @@ -2,6 +2,7 @@ using System; using System.IO; using System.Threading; using System.Threading.Tasks; +using SharpCompress.Common; using SharpCompress.IO; namespace SharpCompress.Compressors.Deflate; @@ -85,7 +86,7 @@ public partial class GZipStream } else { - throw new InvalidOperationException(); + throw new ArchiveOperationException(); } } @@ -111,7 +112,7 @@ public partial class GZipStream } else { - throw new InvalidOperationException(); + throw new ArchiveOperationException(); } } diff --git a/src/SharpCompress/Compressors/Deflate/GZipStream.cs b/src/SharpCompress/Compressors/Deflate/GZipStream.cs index 04c7a5d9..5085cd14 100644 --- a/src/SharpCompress/Compressors/Deflate/GZipStream.cs +++ b/src/SharpCompress/Compressors/Deflate/GZipStream.cs @@ -116,7 +116,7 @@ public partial class GZipStream : Stream { throw new ZlibException( string.Format( - global::SharpCompress.Common.Constants.DefaultCultureInfo, + Constants.DefaultCultureInfo, "Don't be silly. {0} bytes?? Use a bigger buffer, at least {1}.", value, ZlibConstants.WorkingBufferSizeMin @@ -354,7 +354,7 @@ public partial class GZipStream : Stream } else { - throw new InvalidOperationException(); + throw new ArchiveOperationException(); } } @@ -413,7 +413,7 @@ public partial class GZipStream : Stream } if (_fileName.EndsWith('\\')) { - throw new InvalidOperationException("Illegal filename"); + throw new ArchiveOperationException("Illegal filename"); } #if LEGACY_DOTNET diff --git a/src/SharpCompress/Compressors/Deflate/Inflate.cs b/src/SharpCompress/Compressors/Deflate/Inflate.cs index 664f1bf4..0dc9fadc 100644 --- a/src/SharpCompress/Compressors/Deflate/Inflate.cs +++ b/src/SharpCompress/Compressors/Deflate/Inflate.cs @@ -66,6 +66,7 @@ using System; using System.Buffers; using SharpCompress.Algorithms; +using SharpCompress.Common; namespace SharpCompress.Compressors.Deflate; @@ -1746,7 +1747,7 @@ internal sealed class InflateManager { mode = InflateManagerMode.BAD; _codec.Message = string.Format( - global::SharpCompress.Common.Constants.DefaultCultureInfo, + Constants.DefaultCultureInfo, "unknown compression method (0x{0:X2})", method ); @@ -1757,7 +1758,7 @@ internal sealed class InflateManager { mode = InflateManagerMode.BAD; _codec.Message = string.Format( - global::SharpCompress.Common.Constants.DefaultCultureInfo, + Constants.DefaultCultureInfo, "invalid window size ({0})", (method >> 4) + 8 ); @@ -1949,7 +1950,7 @@ internal sealed class InflateManager case InflateManagerMode.BAD: throw new ZlibException( string.Format( - global::SharpCompress.Common.Constants.DefaultCultureInfo, + Constants.DefaultCultureInfo, "Bad state ({0})", _codec.Message ) diff --git a/src/SharpCompress/Compressors/Deflate/ZlibBaseStream.cs b/src/SharpCompress/Compressors/Deflate/ZlibBaseStream.cs index 46778000..ea96f065 100644 --- a/src/SharpCompress/Compressors/Deflate/ZlibBaseStream.cs +++ b/src/SharpCompress/Compressors/Deflate/ZlibBaseStream.cs @@ -33,6 +33,7 @@ using System.IO; using System.Text; using System.Threading; using System.Threading.Tasks; +using SharpCompress.Common; using SharpCompress.Common.Tar.Headers; using SharpCompress.IO; @@ -276,12 +277,7 @@ internal class ZlibBaseStream : Stream, IStreamStack if (_z.Message is null) { throw new ZlibException( - String.Format( - global::SharpCompress.Common.Constants.DefaultCultureInfo, - "{0}: (rc = {1})", - verb, - rc - ) + String.Format(Constants.DefaultCultureInfo, "{0}: (rc = {1})", verb, rc) ); } throw new ZlibException(verb + ": " + _z.Message); @@ -351,7 +347,7 @@ internal class ZlibBaseStream : Stream, IStreamStack { throw new ZlibException( String.Format( - global::SharpCompress.Common.Constants.DefaultCultureInfo, + Constants.DefaultCultureInfo, "Protocol error. AvailableBytesIn={0}, expected 8", _z.AvailableBytesIn + bytesRead ) @@ -372,7 +368,7 @@ internal class ZlibBaseStream : Stream, IStreamStack { throw new ZlibException( String.Format( - global::SharpCompress.Common.Constants.DefaultCultureInfo, + Constants.DefaultCultureInfo, "Bad CRC32 in GZIP stream. (actual({0:X8})!=expected({1:X8}))", crc32_actual, crc32_expected @@ -384,7 +380,7 @@ internal class ZlibBaseStream : Stream, IStreamStack { throw new ZlibException( String.Format( - global::SharpCompress.Common.Constants.DefaultCultureInfo, + Constants.DefaultCultureInfo, "Bad size in GZIP stream. (actual({0})!=expected({1}))", isize_actual, isize_expected @@ -424,12 +420,7 @@ internal class ZlibBaseStream : Stream, IStreamStack if (_z.Message is null) { throw new ZlibException( - String.Format( - global::SharpCompress.Common.Constants.DefaultCultureInfo, - "{0}: (rc = {1})", - verb, - rc - ) + String.Format(Constants.DefaultCultureInfo, "{0}: (rc = {1})", verb, rc) ); } throw new ZlibException(verb + ": " + _z.Message); @@ -887,7 +878,7 @@ internal class ZlibBaseStream : Stream, IStreamStack { throw new ZlibException( String.Format( - global::SharpCompress.Common.Constants.DefaultCultureInfo, + Constants.DefaultCultureInfo, "Deflating: rc={0} msg={1}", rc, _z.Message @@ -944,7 +935,7 @@ internal class ZlibBaseStream : Stream, IStreamStack { throw new ZlibException( String.Format( - global::SharpCompress.Common.Constants.DefaultCultureInfo, + Constants.DefaultCultureInfo, "{0}flating: rc={1} msg={2}", (_wantCompress ? "de" : "in"), rc, @@ -985,7 +976,7 @@ internal class ZlibBaseStream : Stream, IStreamStack { throw new ZlibException( String.Format( - global::SharpCompress.Common.Constants.DefaultCultureInfo, + Constants.DefaultCultureInfo, "Deflating: rc={0} msg={1}", rc, _z.Message @@ -1079,7 +1070,7 @@ internal class ZlibBaseStream : Stream, IStreamStack { throw new ZlibException( String.Format( - global::SharpCompress.Common.Constants.DefaultCultureInfo, + Constants.DefaultCultureInfo, "Deflating: rc={0} msg={1}", rc, _z.Message @@ -1138,7 +1129,7 @@ internal class ZlibBaseStream : Stream, IStreamStack { throw new ZlibException( String.Format( - global::SharpCompress.Common.Constants.DefaultCultureInfo, + Constants.DefaultCultureInfo, "{0}flating: rc={1} msg={2}", (_wantCompress ? "de" : "in"), rc, @@ -1179,7 +1170,7 @@ internal class ZlibBaseStream : Stream, IStreamStack { throw new ZlibException( String.Format( - global::SharpCompress.Common.Constants.DefaultCultureInfo, + Constants.DefaultCultureInfo, "Deflating: rc={0} msg={1}", rc, _z.Message diff --git a/src/SharpCompress/Compressors/Deflate/ZlibCodec.cs b/src/SharpCompress/Compressors/Deflate/ZlibCodec.cs index b00f93b8..73edf53c 100644 --- a/src/SharpCompress/Compressors/Deflate/ZlibCodec.cs +++ b/src/SharpCompress/Compressors/Deflate/ZlibCodec.cs @@ -66,6 +66,7 @@ // ----------------------------------------------------------------------- using System; +using SharpCompress.Common; namespace SharpCompress.Compressors.Deflate; @@ -696,7 +697,7 @@ internal sealed class ZlibCodec { throw new ZlibException( string.Format( - global::SharpCompress.Common.Constants.DefaultCultureInfo, + Constants.DefaultCultureInfo, "Invalid State. (pending.Length={0}, pendingCount={1})", dstate.pending.Length, dstate.pendingCount diff --git a/src/SharpCompress/Compressors/Deflate/ZlibStream.cs b/src/SharpCompress/Compressors/Deflate/ZlibStream.cs index bfdd82cd..c4652d44 100644 --- a/src/SharpCompress/Compressors/Deflate/ZlibStream.cs +++ b/src/SharpCompress/Compressors/Deflate/ZlibStream.cs @@ -30,6 +30,7 @@ using System.IO; using System.Text; using System.Threading; using System.Threading.Tasks; +using SharpCompress.Common; namespace SharpCompress.Compressors.Deflate; @@ -107,7 +108,7 @@ public partial class ZlibStream : Stream { throw new ZlibException( string.Format( - global::SharpCompress.Common.Constants.DefaultCultureInfo, + Constants.DefaultCultureInfo, "Don't be silly. {0} bytes?? Use a bigger buffer, at least {1}.", value, ZlibConstants.WorkingBufferSizeMin diff --git a/src/SharpCompress/Compressors/Deflate64/Deflate64Stream.cs b/src/SharpCompress/Compressors/Deflate64/Deflate64Stream.cs index 59d40dc7..9e9ff7db 100644 --- a/src/SharpCompress/Compressors/Deflate64/Deflate64Stream.cs +++ b/src/SharpCompress/Compressors/Deflate64/Deflate64Stream.cs @@ -141,7 +141,7 @@ public sealed partial class Deflate64Stream : Stream [MethodImpl(MethodImplOptions.NoInlining)] private static void ThrowCannotWriteToDeflateManagedStreamException() => - throw new InvalidOperationException("Deflate64: cannot write to this stream"); + throw new ArchiveOperationException("Deflate64: cannot write to this stream"); public override void Write(byte[] buffer, int offset, int count) => ThrowCannotWriteToDeflateManagedStreamException(); diff --git a/src/SharpCompress/Compressors/Explode/ExplodeStream.Async.cs b/src/SharpCompress/Compressors/Explode/ExplodeStream.Async.cs index 16dbfdd9..bd058dd4 100644 --- a/src/SharpCompress/Compressors/Explode/ExplodeStream.Async.cs +++ b/src/SharpCompress/Compressors/Explode/ExplodeStream.Async.cs @@ -3,6 +3,7 @@ using System.IO; using System.IO.Compression; using System.Threading; using System.Threading.Tasks; +using SharpCompress.Common; using SharpCompress.Common.Zip.Headers; using SharpCompress.IO; @@ -279,7 +280,7 @@ public partial class ExplodeStream if (literalResult.returnCode != 0) { - throw new InvalidDataException("Error decoding literal value"); + throw new InvalidFormatException("Error decoding literal value"); } huftPointer = literalResult.huftPointer; @@ -319,7 +320,7 @@ public partial class ExplodeStream if (distanceResult.returnCode != 0) { - throw new InvalidDataException("Error decoding distance high bits"); + throw new InvalidFormatException("Error decoding distance high bits"); } huftPointer = distanceResult.huftPointer; @@ -335,7 +336,7 @@ public partial class ExplodeStream if (lengthResult.returnCode != 0) { - throw new InvalidDataException("Error decoding coded length"); + throw new InvalidFormatException("Error decoding coded length"); } huftPointer = lengthResult.huftPointer; diff --git a/src/SharpCompress/Compressors/Explode/ExplodeStream.cs b/src/SharpCompress/Compressors/Explode/ExplodeStream.cs index 977cd629..d161f6a8 100644 --- a/src/SharpCompress/Compressors/Explode/ExplodeStream.cs +++ b/src/SharpCompress/Compressors/Explode/ExplodeStream.cs @@ -1,6 +1,7 @@ using System; using System.IO; using System.IO.Compression; +using SharpCompress.Common; using SharpCompress.Common.Zip.Headers; namespace SharpCompress.Compressors.Explode; @@ -697,7 +698,7 @@ public partial class ExplodeStream : Stream ) != 0 ) { - throw new InvalidDataException("Error decoding literal value"); + throw new InvalidFormatException("Error decoding literal value"); } nextByte = (byte)huftPointer.Value; @@ -736,7 +737,7 @@ public partial class ExplodeStream : Stream ) != 0 ) { - throw new InvalidDataException("Error decoding distance high bits"); + throw new InvalidFormatException("Error decoding distance high bits"); } distance = windowIndex - (distance + huftPointer.Value); /* construct offset */ @@ -752,7 +753,7 @@ public partial class ExplodeStream : Stream ) != 0 ) { - throw new InvalidDataException("Error decoding coded length"); + throw new InvalidFormatException("Error decoding coded length"); } length = huftPointer.Value; diff --git a/src/SharpCompress/Compressors/LZMA/AesDecoderStream.Async.cs b/src/SharpCompress/Compressors/LZMA/AesDecoderStream.Async.cs index 326eec1a..c76f726f 100644 --- a/src/SharpCompress/Compressors/LZMA/AesDecoderStream.Async.cs +++ b/src/SharpCompress/Compressors/LZMA/AesDecoderStream.Async.cs @@ -3,6 +3,7 @@ using System.IO; using System.Security.Cryptography; using System.Threading; using System.Threading.Tasks; +using SharpCompress.Common; namespace SharpCompress.Compressors.LZMA; @@ -41,7 +42,7 @@ internal sealed partial class AesDecoderStream if (read == 0) { // We are not done decoding and have less than 16 bytes. - throw new EndOfStreamException(); + throw new IncompleteArchiveException("Unexpected end of stream."); } mEnding += read; diff --git a/src/SharpCompress/Compressors/LZMA/AesDecoderStream.cs b/src/SharpCompress/Compressors/LZMA/AesDecoderStream.cs index 557456c8..52793292 100644 --- a/src/SharpCompress/Compressors/LZMA/AesDecoderStream.cs +++ b/src/SharpCompress/Compressors/LZMA/AesDecoderStream.cs @@ -4,6 +4,7 @@ using System.Security.Cryptography; using System.Text; using System.Threading; using System.Threading.Tasks; +using SharpCompress.Common; using SharpCompress.Compressors.LZMA.Utilities; namespace SharpCompress.Compressors.LZMA; @@ -44,7 +45,7 @@ internal sealed partial class AesDecoderStream : DecoderStream2 var key = InitKey(numCyclesPower, salt, passwordBytes); if (key == null) { - throw new InvalidOperationException("Initialized with null key"); + throw new ArchiveOperationException("Initialized with null key"); } using (var aes = Aes.Create()) @@ -107,7 +108,7 @@ internal sealed partial class AesDecoderStream : DecoderStream2 if (read == 0) { // We are not done decoding and have less than 16 bytes. - throw new EndOfStreamException(); + throw new IncompleteArchiveException("Unexpected end of stream."); } mEnding += read; @@ -160,7 +161,7 @@ internal sealed partial class AesDecoderStream : DecoderStream2 var ivSize = (bt >> 6) & 1; if (info.Length == 1) { - throw new InvalidOperationException(); + throw new ArchiveOperationException(); } var bt2 = info[1]; @@ -168,7 +169,7 @@ internal sealed partial class AesDecoderStream : DecoderStream2 ivSize += (bt2 & 15); if (info.Length < 2 + saltSize + ivSize) { - throw new InvalidOperationException(); + throw new ArchiveOperationException(); } salt = new byte[saltSize]; diff --git a/src/SharpCompress/Compressors/LZMA/Bcj2DecoderStream.cs b/src/SharpCompress/Compressors/LZMA/Bcj2DecoderStream.cs index 9f2c1011..a73da73b 100644 --- a/src/SharpCompress/Compressors/LZMA/Bcj2DecoderStream.cs +++ b/src/SharpCompress/Compressors/LZMA/Bcj2DecoderStream.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.IO; using System.Threading; using System.Threading.Tasks; +using SharpCompress.Common; namespace SharpCompress.Compressors.LZMA; @@ -32,7 +33,7 @@ internal class Bcj2DecoderStream : DecoderStream2 var bt = _mStream.ReadByte(); if (bt < 0) { - throw new EndOfStreamException(); + throw new IncompleteArchiveException("Unexpected end of stream."); } return (byte)bt; @@ -233,7 +234,7 @@ internal class Bcj2DecoderStream : DecoderStream2 var b0 = s.ReadByte(); if (b0 < 0) { - throw new EndOfStreamException(); + throw new IncompleteArchiveException("Unexpected end of stream."); } src <<= 8; diff --git a/src/SharpCompress/Compressors/LZMA/CRC.cs b/src/SharpCompress/Compressors/LZMA/CRC.cs index ea2969bd..6a237df2 100644 --- a/src/SharpCompress/Compressors/LZMA/CRC.cs +++ b/src/SharpCompress/Compressors/LZMA/CRC.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using SharpCompress.Common; namespace SharpCompress.Compressors.LZMA; @@ -39,7 +40,7 @@ internal static class Crc var delta = stream.Read(buffer, 0, (int)Math.Min(length, buffer.Length)); if (delta == 0) { - throw new EndOfStreamException(); + throw new IncompleteArchiveException("Unexpected end of stream."); } crc = Update(crc, buffer, 0, delta); length -= delta; diff --git a/src/SharpCompress/Compressors/LZMA/Log.cs b/src/SharpCompress/Compressors/LZMA/Log.cs index 204831aa..e0c9bcd9 100644 --- a/src/SharpCompress/Compressors/LZMA/Log.cs +++ b/src/SharpCompress/Compressors/LZMA/Log.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using SharpCompress.Common; namespace SharpCompress.Compressors.LZMA; @@ -16,7 +17,7 @@ internal static class Log { if (INDENT.Count == 1) { - throw new InvalidOperationException(); + throw new ArchiveOperationException(); } INDENT.Pop(); diff --git a/src/SharpCompress/Compressors/LZMA/RangeCoder/RangeCoder.Async.cs b/src/SharpCompress/Compressors/LZMA/RangeCoder/RangeCoder.Async.cs index e7d1706d..cd3fc6a5 100644 --- a/src/SharpCompress/Compressors/LZMA/RangeCoder/RangeCoder.Async.cs +++ b/src/SharpCompress/Compressors/LZMA/RangeCoder/RangeCoder.Async.cs @@ -3,6 +3,7 @@ using System.IO; using System.Threading; using System.Threading.Tasks; +using SharpCompress.Common; namespace SharpCompress.Compressors.LZMA.RangeCoder; @@ -91,7 +92,7 @@ internal partial class Decoder .ConfigureAwait(false); if (read == 0) { - throw new EndOfStreamException(); + throw new IncompleteArchiveException("Unexpected end of stream."); } _code = (_code << 8) | buffer[0]; } @@ -108,7 +109,7 @@ internal partial class Decoder .ConfigureAwait(false); if (read == 0) { - throw new EndOfStreamException(); + throw new IncompleteArchiveException("Unexpected end of stream."); } _code = (_code << 8) | buffer[0]; _range <<= 8; @@ -126,7 +127,7 @@ internal partial class Decoder .ConfigureAwait(false); if (read == 0) { - throw new EndOfStreamException(); + throw new IncompleteArchiveException("Unexpected end of stream."); } _code = (_code << 8) | buffer[0]; _range <<= 8; @@ -157,7 +158,7 @@ internal partial class Decoder .ConfigureAwait(false); if (read == 0) { - throw new EndOfStreamException(); + throw new IncompleteArchiveException("Unexpected end of stream."); } code = (code << 8) | buffer[0]; range <<= 8; diff --git a/src/SharpCompress/Compressors/LZMA/Utilities/CrcBuilderStream.Async.cs b/src/SharpCompress/Compressors/LZMA/Utilities/CrcBuilderStream.Async.cs index 8ff18ef7..e3fa5332 100644 --- a/src/SharpCompress/Compressors/LZMA/Utilities/CrcBuilderStream.Async.cs +++ b/src/SharpCompress/Compressors/LZMA/Utilities/CrcBuilderStream.Async.cs @@ -2,6 +2,7 @@ using System; using System.IO; using System.Threading; using System.Threading.Tasks; +using SharpCompress.Common; namespace SharpCompress.Compressors.LZMA.Utilities; @@ -17,7 +18,7 @@ internal partial class CrcBuilderStream : Stream cancellationToken.ThrowIfCancellationRequested(); if (_mFinished) { - throw new InvalidOperationException("CRC calculation has been finished."); + throw new ArchiveOperationException("CRC calculation has been finished."); } Processed += count; diff --git a/src/SharpCompress/Compressors/LZMA/Utilities/CrcBuilderStream.cs b/src/SharpCompress/Compressors/LZMA/Utilities/CrcBuilderStream.cs index 1c5acb63..f694d749 100644 --- a/src/SharpCompress/Compressors/LZMA/Utilities/CrcBuilderStream.cs +++ b/src/SharpCompress/Compressors/LZMA/Utilities/CrcBuilderStream.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using SharpCompress.Common; namespace SharpCompress.Compressors.LZMA.Utilities; @@ -57,7 +58,7 @@ internal partial class CrcBuilderStream : Stream } public override int Read(byte[] buffer, int offset, int count) => - throw new InvalidOperationException(); + throw new ArchiveOperationException(); public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException(); @@ -67,7 +68,7 @@ internal partial class CrcBuilderStream : Stream { if (_mFinished) { - throw new InvalidOperationException("CRC calculation has been finished."); + throw new ArchiveOperationException("CRC calculation has been finished."); } Processed += count; diff --git a/src/SharpCompress/Compressors/LZMA/Utilities/CrcCheckStream.cs b/src/SharpCompress/Compressors/LZMA/Utilities/CrcCheckStream.cs index 2c657519..47ef7342 100644 --- a/src/SharpCompress/Compressors/LZMA/Utilities/CrcCheckStream.cs +++ b/src/SharpCompress/Compressors/LZMA/Utilities/CrcCheckStream.cs @@ -3,6 +3,7 @@ using System.Buffers; using System.IO; using System.Threading; using System.Threading.Tasks; +using SharpCompress.Common; namespace SharpCompress.Compressors.LZMA.Utilities; @@ -25,7 +26,7 @@ public class CrcCheckStream(uint crc) : Stream if (_mCurrentCrc != crc) //moved test to here { - throw new InvalidOperationException(); + throw new ArchiveOperationException(); } } } @@ -53,7 +54,7 @@ public class CrcCheckStream(uint crc) : Stream } public override int Read(byte[] buffer, int offset, int count) => - throw new InvalidOperationException(); + throw new ArchiveOperationException(); public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException(); diff --git a/src/SharpCompress/Compressors/Lzw/LzwStream.Async.cs b/src/SharpCompress/Compressors/Lzw/LzwStream.Async.cs index 3303bbd9..5a8ac76b 100644 --- a/src/SharpCompress/Compressors/Lzw/LzwStream.Async.cs +++ b/src/SharpCompress/Compressors/Lzw/LzwStream.Async.cs @@ -38,7 +38,7 @@ public partial class LzwStream { throw new IncompleteArchiveException( String.Format( - global::SharpCompress.Common.Constants.DefaultCultureInfo, + Constants.DefaultCultureInfo, "Wrong LZW header. Magic bytes don't match. 0x{0:x2} 0x{1:x2}", hdr[0], hdr[1] @@ -326,7 +326,7 @@ public partial class LzwStream { throw new IncompleteArchiveException( String.Format( - global::SharpCompress.Common.Constants.DefaultCultureInfo, + Constants.DefaultCultureInfo, "Wrong LZW header. Magic bytes don't match. 0x{0:x2} 0x{1:x2}", hdr[0], hdr[1] diff --git a/src/SharpCompress/Compressors/Lzw/LzwStream.cs b/src/SharpCompress/Compressors/Lzw/LzwStream.cs index e09e4397..277dfb77 100644 --- a/src/SharpCompress/Compressors/Lzw/LzwStream.cs +++ b/src/SharpCompress/Compressors/Lzw/LzwStream.cs @@ -64,7 +64,7 @@ public partial class LzwStream : Stream { throw new IncompleteArchiveException( String.Format( - global::SharpCompress.Common.Constants.DefaultCultureInfo, + Constants.DefaultCultureInfo, "Wrong LZW header. Magic bytes don't match. 0x{0:x2} 0x{1:x2}", hdr[0], hdr[1] @@ -398,7 +398,7 @@ public partial class LzwStream : Stream { throw new IncompleteArchiveException( String.Format( - global::SharpCompress.Common.Constants.DefaultCultureInfo, + Constants.DefaultCultureInfo, "Wrong LZW header. Magic bytes don't match. 0x{0:x2} 0x{1:x2}", hdr[0], hdr[1] diff --git a/src/SharpCompress/Compressors/Rar/MultiVolumeReadOnlyAsyncStream.Async.cs b/src/SharpCompress/Compressors/Rar/MultiVolumeReadOnlyAsyncStream.Async.cs index 09d90e06..80a93308 100644 --- a/src/SharpCompress/Compressors/Rar/MultiVolumeReadOnlyAsyncStream.Async.cs +++ b/src/SharpCompress/Compressors/Rar/MultiVolumeReadOnlyAsyncStream.Async.cs @@ -67,7 +67,7 @@ internal sealed partial class MultiVolumeReadOnlyAsyncStream : MultiVolumeReadOn .ConfigureAwait(false); if (read < 0) { - throw new EndOfStreamException(); + throw new IncompleteArchiveException("Unexpected end of stream."); } currentPosition += read; @@ -129,7 +129,7 @@ internal sealed partial class MultiVolumeReadOnlyAsyncStream : MultiVolumeReadOn .ConfigureAwait(false); if (read < 0) { - throw new EndOfStreamException(); + throw new IncompleteArchiveException("Unexpected end of stream."); } currentPosition += read; diff --git a/src/SharpCompress/Compressors/Rar/MultiVolumeReadOnlyStream.Async.cs b/src/SharpCompress/Compressors/Rar/MultiVolumeReadOnlyStream.Async.cs index 15699db7..51bd2032 100644 --- a/src/SharpCompress/Compressors/Rar/MultiVolumeReadOnlyStream.Async.cs +++ b/src/SharpCompress/Compressors/Rar/MultiVolumeReadOnlyStream.Async.cs @@ -34,7 +34,7 @@ internal sealed partial class MultiVolumeReadOnlyStream : MultiVolumeReadOnlyStr .ConfigureAwait(false); if (read < 0) { - throw new EndOfStreamException(); + throw new IncompleteArchiveException("Unexpected end of stream."); } currentPosition += read; @@ -96,7 +96,7 @@ internal sealed partial class MultiVolumeReadOnlyStream : MultiVolumeReadOnlyStr .ConfigureAwait(false); if (read < 0) { - throw new EndOfStreamException(); + throw new IncompleteArchiveException("Unexpected end of stream."); } currentPosition += read; diff --git a/src/SharpCompress/Compressors/Rar/MultiVolumeReadOnlyStream.cs b/src/SharpCompress/Compressors/Rar/MultiVolumeReadOnlyStream.cs index c8235e3f..6e8f732c 100644 --- a/src/SharpCompress/Compressors/Rar/MultiVolumeReadOnlyStream.cs +++ b/src/SharpCompress/Compressors/Rar/MultiVolumeReadOnlyStream.cs @@ -57,7 +57,7 @@ internal sealed partial class MultiVolumeReadOnlyStream : MultiVolumeReadOnlyStr var read = currentStream.NotNull().Read(buffer, currentOffset, readSize); if (read < 0) { - throw new EndOfStreamException(); + throw new IncompleteArchiveException("Unexpected end of stream."); } currentPosition += read; diff --git a/src/SharpCompress/Compressors/Rar/RarStream.Async.cs b/src/SharpCompress/Compressors/Rar/RarStream.Async.cs index 444da3e7..244bc99f 100644 --- a/src/SharpCompress/Compressors/Rar/RarStream.Async.cs +++ b/src/SharpCompress/Compressors/Rar/RarStream.Async.cs @@ -5,6 +5,7 @@ using System.Buffers; using System.IO; using System.Threading; using System.Threading.Tasks; +using SharpCompress.Common; using SharpCompress.Common.Rar.Headers; using SharpCompress.IO; @@ -69,7 +70,7 @@ internal partial class RarStream if (count > 0 && outTotal == 0 && _position < Length) { // sanity check, eg if we try to decompress a redir entry - throw new InvalidOperationException( + throw new ArchiveOperationException( $"unpacked file size does not match header: expected {Length} found {_position}" ); } diff --git a/src/SharpCompress/Compressors/Rar/RarStream.cs b/src/SharpCompress/Compressors/Rar/RarStream.cs index d4d9b8e2..e78faa7a 100644 --- a/src/SharpCompress/Compressors/Rar/RarStream.cs +++ b/src/SharpCompress/Compressors/Rar/RarStream.cs @@ -3,6 +3,7 @@ using System; using System.Buffers; using System.IO; +using SharpCompress.Common; using SharpCompress.Common.Rar.Headers; namespace SharpCompress.Compressors.Rar; @@ -99,7 +100,7 @@ internal partial class RarStream : Stream if (count > 0 && outTotal == 0 && _position < Length) { // sanity check, eg if we try to decompress a redir entry - throw new InvalidOperationException( + throw new ArchiveOperationException( $"unpacked file size does not match header: expected {Length} found {_position}" ); } diff --git a/src/SharpCompress/Compressors/Rar/UnpackV2017/FragmentedWindow.unpack50frag_cpp.cs b/src/SharpCompress/Compressors/Rar/UnpackV2017/FragmentedWindow.unpack50frag_cpp.cs index 97d513c3..f269fb5f 100644 --- a/src/SharpCompress/Compressors/Rar/UnpackV2017/FragmentedWindow.unpack50frag_cpp.cs +++ b/src/SharpCompress/Compressors/Rar/UnpackV2017/FragmentedWindow.unpack50frag_cpp.cs @@ -1,6 +1,7 @@ #nullable disable using System; +using SharpCompress.Common; using size_t = System.UInt32; namespace SharpCompress.Compressors.Rar.UnpackV2017; @@ -63,7 +64,7 @@ internal partial class FragmentedWindow // sharpcompress: don't need this, freshly allocated above //Utility.Memset(NewMem,0,Size); - Mem[BlockNum] = NewMem ?? throw new InvalidOperationException(); + Mem[BlockNum] = NewMem ?? throw new ArchiveOperationException(); TotalSize += Size; MemSize[BlockNum] = TotalSize; BlockNum++; @@ -71,7 +72,7 @@ internal partial class FragmentedWindow if (TotalSize < WinSize) // Not found enough free blocks. //throw std::bad_alloc(); { - throw new InvalidOperationException(); + throw new ArchiveOperationException(); } } diff --git a/src/SharpCompress/Compressors/Shrink/ShrinkStream.Async.cs b/src/SharpCompress/Compressors/Shrink/ShrinkStream.Async.cs index 437a55f0..73ed2125 100644 --- a/src/SharpCompress/Compressors/Shrink/ShrinkStream.Async.cs +++ b/src/SharpCompress/Compressors/Shrink/ShrinkStream.Async.cs @@ -2,6 +2,7 @@ using System; using System.IO; using System.Threading; using System.Threading.Tasks; +using SharpCompress.Common; namespace SharpCompress.Compressors.Shrink; @@ -49,7 +50,7 @@ internal partial class ShrinkStream : Stream .ConfigureAwait(false); if (bytesRead == 0) { - throw new EndOfStreamException( + throw new IncompleteArchiveException( "Unexpected end of stream while reading compressed data" ); } diff --git a/src/SharpCompress/Compressors/Squeezed/BitReader.Async.cs b/src/SharpCompress/Compressors/Squeezed/BitReader.Async.cs index 6522a923..3a4597bd 100644 --- a/src/SharpCompress/Compressors/Squeezed/BitReader.Async.cs +++ b/src/SharpCompress/Compressors/Squeezed/BitReader.Async.cs @@ -2,6 +2,7 @@ using System; using System.IO; using System.Threading; using System.Threading.Tasks; +using SharpCompress.Common; namespace SharpCompress.Compressors.Squeezed; @@ -17,7 +18,7 @@ public partial class BitReader .ConfigureAwait(false); if (bytesRead == 0) { - throw new EndOfStreamException(); + throw new IncompleteArchiveException("Unexpected end of stream."); } _bitBuffer = buffer[0]; diff --git a/src/SharpCompress/Compressors/Squeezed/BitReader.cs b/src/SharpCompress/Compressors/Squeezed/BitReader.cs index 25c0601d..2d8a9563 100644 --- a/src/SharpCompress/Compressors/Squeezed/BitReader.cs +++ b/src/SharpCompress/Compressors/Squeezed/BitReader.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using SharpCompress.Common; namespace SharpCompress.Compressors.Squeezed; @@ -23,7 +24,7 @@ public partial class BitReader int nextByte = _stream.ReadByte(); if (nextByte == -1) { - throw new EndOfStreamException(); + throw new IncompleteArchiveException("Unexpected end of stream."); } _bitBuffer = nextByte; diff --git a/src/SharpCompress/Compressors/Squeezed/SqueezedStream.Async.cs b/src/SharpCompress/Compressors/Squeezed/SqueezedStream.Async.cs index 1b416af5..11610d5e 100644 --- a/src/SharpCompress/Compressors/Squeezed/SqueezedStream.Async.cs +++ b/src/SharpCompress/Compressors/Squeezed/SqueezedStream.Async.cs @@ -2,6 +2,7 @@ using System; using System.IO; using System.Threading; using System.Threading.Tasks; +using SharpCompress.Common; using SharpCompress.Compressors.RLE90; namespace SharpCompress.Compressors.Squeezed; @@ -73,7 +74,7 @@ public partial class SqueezeStream if (bytesRead != 4) { - throw new EndOfStreamException(); + throw new IncompleteArchiveException("Unexpected end of stream."); } dnode[j, 0] = (short)(nodeBytes[0] | (nodeBytes[1] << 8)); diff --git a/src/SharpCompress/Compressors/Xz/BinaryUtils.Async.cs b/src/SharpCompress/Compressors/Xz/BinaryUtils.Async.cs index 25e1182d..5b6796dc 100644 --- a/src/SharpCompress/Compressors/Xz/BinaryUtils.Async.cs +++ b/src/SharpCompress/Compressors/Xz/BinaryUtils.Async.cs @@ -3,6 +3,7 @@ using System.Buffers.Binary; using System.IO; using System.Threading; using System.Threading.Tasks; +using SharpCompress.Common; namespace SharpCompress.Compressors.Xz; @@ -17,7 +18,7 @@ public static partial class BinaryUtils var read = await stream.ReadFullyAsync(bytes, cancellationToken).ConfigureAwait(false); if (!read) { - throw new EndOfStreamException(); + throw new IncompleteArchiveException("Unexpected end of stream."); } return BinaryPrimitives.ReadInt32LittleEndian(bytes); } diff --git a/src/SharpCompress/Compressors/Xz/BinaryUtils.cs b/src/SharpCompress/Compressors/Xz/BinaryUtils.cs index f9f7af31..bf20c915 100644 --- a/src/SharpCompress/Compressors/Xz/BinaryUtils.cs +++ b/src/SharpCompress/Compressors/Xz/BinaryUtils.cs @@ -3,6 +3,7 @@ using System.Buffers.Binary; using System.IO; using System.Threading; using System.Threading.Tasks; +using SharpCompress.Common; namespace SharpCompress.Compressors.Xz; @@ -24,7 +25,7 @@ public static partial class BinaryUtils var read = stream.ReadFully(bytes); if (!read) { - throw new EndOfStreamException(); + throw new IncompleteArchiveException("Unexpected end of stream."); } return BinaryPrimitives.ReadInt32LittleEndian(bytes); } diff --git a/src/SharpCompress/Compressors/Xz/Filters/Lzma2Filter.cs b/src/SharpCompress/Compressors/Xz/Filters/Lzma2Filter.cs index ab8904b0..fef8017b 100644 --- a/src/SharpCompress/Compressors/Xz/Filters/Lzma2Filter.cs +++ b/src/SharpCompress/Compressors/Xz/Filters/Lzma2Filter.cs @@ -19,7 +19,7 @@ public partial class Lzma2Filter : BlockFilter { if (_dictionarySize > 40) { - throw new OverflowException("Dictionary size greater than UInt32.Max"); + throw new InvalidFormatException("Dictionary size greater than UInt32.Max"); } if (_dictionarySize == 40) diff --git a/src/SharpCompress/Compressors/Xz/XZBlock.Async.cs b/src/SharpCompress/Compressors/Xz/XZBlock.Async.cs index 08c0da70..7c6ab8ed 100644 --- a/src/SharpCompress/Compressors/Xz/XZBlock.Async.cs +++ b/src/SharpCompress/Compressors/Xz/XZBlock.Async.cs @@ -115,7 +115,7 @@ public sealed partial class XZBlock .ConfigureAwait(false); if (read != BlockHeaderSize - 5) { - throw new EndOfStreamException("Reached end of stream unexpectedly"); + throw new IncompleteArchiveException("Reached end of stream unexpectedly"); } var crc = await BaseStream diff --git a/src/SharpCompress/Compressors/Xz/XZBlock.cs b/src/SharpCompress/Compressors/Xz/XZBlock.cs index 9642cd51..00c967f7 100644 --- a/src/SharpCompress/Compressors/Xz/XZBlock.cs +++ b/src/SharpCompress/Compressors/Xz/XZBlock.cs @@ -139,7 +139,7 @@ public sealed partial class XZBlock : XZReadOnlyStream var read = BaseStream.Read(blockHeaderWithoutCrc, 1, BlockHeaderSize - 5); if (read != BlockHeaderSize - 5) { - throw new EndOfStreamException("Reached end of stream unexpectedly"); + throw new IncompleteArchiveException("Reached end of stream unexpectedly"); } var crc = BaseStream.ReadLittleEndianUInt32(); diff --git a/src/SharpCompress/Compressors/ZStandard/DecompressionStream.Async.cs b/src/SharpCompress/Compressors/ZStandard/DecompressionStream.Async.cs index 5cbdada2..eb2df104 100644 --- a/src/SharpCompress/Compressors/ZStandard/DecompressionStream.Async.cs +++ b/src/SharpCompress/Compressors/ZStandard/DecompressionStream.Async.cs @@ -3,6 +3,7 @@ using System.Buffers; using System.IO; using System.Threading; using System.Threading.Tasks; +using SharpCompress.Common; using SharpCompress.Compressors.ZStandard.Unsafe; namespace SharpCompress.Compressors.ZStandard; @@ -78,7 +79,7 @@ public partial class DecompressionStream { if (checkEndOfStream && lastDecompressResult != 0) { - throw new EndOfStreamException("Premature end of stream"); + throw new IncompleteArchiveException("Premature end of stream"); } return 0; diff --git a/src/SharpCompress/Compressors/ZStandard/DecompressionStream.cs b/src/SharpCompress/Compressors/ZStandard/DecompressionStream.cs index abb55e09..54b8064f 100644 --- a/src/SharpCompress/Compressors/ZStandard/DecompressionStream.cs +++ b/src/SharpCompress/Compressors/ZStandard/DecompressionStream.cs @@ -3,6 +3,7 @@ using System.Buffers; using System.IO; using System.Threading; using System.Threading.Tasks; +using SharpCompress.Common; using SharpCompress.Compressors.ZStandard.Unsafe; namespace SharpCompress.Compressors.ZStandard; @@ -151,7 +152,7 @@ public partial class DecompressionStream : Stream { if (checkEndOfStream && lastDecompressResult != 0) { - throw new EndOfStreamException("Premature end of stream"); + throw new IncompleteArchiveException("Premature end of stream"); } return 0; diff --git a/src/SharpCompress/IO/MarkingBinaryReader.cs b/src/SharpCompress/IO/MarkingBinaryReader.cs index 85902263..bbe17f04 100644 --- a/src/SharpCompress/IO/MarkingBinaryReader.cs +++ b/src/SharpCompress/IO/MarkingBinaryReader.cs @@ -46,7 +46,7 @@ internal class MarkingBinaryReader(Stream stream) { throw new InvalidFormatException( string.Format( - global::SharpCompress.Common.Constants.DefaultCultureInfo, + Constants.DefaultCultureInfo, "Could not read the requested amount of bytes. End of stream reached. Requested: {0} Read: {1}", count, bytes.Length @@ -115,7 +115,7 @@ internal class MarkingBinaryReader(Stream stream) shift += 7; } while (shift <= maxShift); - throw new FormatException("malformed vint"); + throw new InvalidFormatException("malformed vint"); } public uint ReadRarVIntUInt32(int maxBytes = 5) => @@ -153,6 +153,6 @@ internal class MarkingBinaryReader(Stream stream) shift += 7; } while (shift <= maxShift); - throw new FormatException("malformed vint"); + throw new InvalidFormatException("malformed vint"); } } diff --git a/src/SharpCompress/IO/SeekableSharpCompressStream.Async.cs b/src/SharpCompress/IO/SeekableSharpCompressStream.Async.cs index f0e4f1df..d9b727a9 100644 --- a/src/SharpCompress/IO/SeekableSharpCompressStream.Async.cs +++ b/src/SharpCompress/IO/SeekableSharpCompressStream.Async.cs @@ -2,6 +2,7 @@ using System; using System.IO; using System.Threading; using System.Threading.Tasks; +using SharpCompress.Common; namespace SharpCompress.IO; @@ -33,7 +34,7 @@ internal sealed partial class SeekableSharpCompressStream } if (ThrowOnDispose) { - throw new InvalidOperationException( + throw new ArchiveOperationException( $"Attempt to dispose of a {nameof(SeekableSharpCompressStream)} when {nameof(ThrowOnDispose)} is true" ); } diff --git a/src/SharpCompress/IO/SeekableSharpCompressStream.cs b/src/SharpCompress/IO/SeekableSharpCompressStream.cs index a2602a95..25675875 100644 --- a/src/SharpCompress/IO/SeekableSharpCompressStream.cs +++ b/src/SharpCompress/IO/SeekableSharpCompressStream.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using SharpCompress.Common; namespace SharpCompress.IO; @@ -97,7 +98,7 @@ internal sealed partial class SeekableSharpCompressStream : SharpCompressStream } if (ThrowOnDispose) { - throw new InvalidOperationException( + throw new ArchiveOperationException( $"Attempt to dispose of a {nameof(SeekableSharpCompressStream)} when {nameof(ThrowOnDispose)} is true" ); } diff --git a/src/SharpCompress/IO/SharpCompressStream.Async.cs b/src/SharpCompress/IO/SharpCompressStream.Async.cs index 159238aa..d1e12673 100644 --- a/src/SharpCompress/IO/SharpCompressStream.Async.cs +++ b/src/SharpCompress/IO/SharpCompressStream.Async.cs @@ -2,6 +2,7 @@ using System; using System.IO; using System.Threading; using System.Threading.Tasks; +using SharpCompress.Common; namespace SharpCompress.IO; @@ -71,7 +72,7 @@ public partial class SharpCompressStream // Verify data is available in ring buffer if (!_ringBuffer!.CanReadFromEnd(bytesFromEnd)) { - throw new InvalidOperationException( + throw new ArchiveOperationException( $"Ring buffer underflow: trying to read {bytesFromEnd} bytes back, " + $"but buffer only holds {_ringBuffer.Length} bytes." ); @@ -161,7 +162,7 @@ public partial class SharpCompressStream // Verify data is available in ring buffer if (!_ringBuffer!.CanReadFromEnd(bytesFromEnd)) { - throw new InvalidOperationException( + throw new ArchiveOperationException( $"Ring buffer underflow: trying to read {bytesFromEnd} bytes back, " + $"but buffer only holds {_ringBuffer.Length} bytes." ); @@ -263,7 +264,7 @@ public partial class SharpCompressStream { if (ThrowOnDispose) { - throw new InvalidOperationException( + throw new ArchiveOperationException( $"Attempt to dispose of a {nameof(SharpCompressStream)} when {nameof(ThrowOnDispose)} is true" ); } diff --git a/src/SharpCompress/IO/SharpCompressStream.cs b/src/SharpCompress/IO/SharpCompressStream.cs index 2163e5f8..770379fc 100644 --- a/src/SharpCompress/IO/SharpCompressStream.cs +++ b/src/SharpCompress/IO/SharpCompressStream.cs @@ -92,7 +92,7 @@ public partial class SharpCompressStream : Stream, IStreamStack } if (ThrowOnDispose) { - throw new InvalidOperationException( + throw new ArchiveOperationException( $"Attempt to dispose of a {nameof(SharpCompressStream)} when {nameof(ThrowOnDispose)} is true" ); } @@ -115,14 +115,14 @@ public partial class SharpCompressStream : Stream, IStreamStack { if (_isPassthrough) { - throw new InvalidOperationException( + throw new ArchiveOperationException( "Rewind cannot be called on a passthrough stream. Use Create() first." ); } if (_recordingStartPosition is null) { - throw new InvalidOperationException( + throw new ArchiveOperationException( "Rewind can only be called after StartRecording() has been called." ); } @@ -131,7 +131,7 @@ public partial class SharpCompressStream : Stream, IStreamStack long anchorAge = streamPosition - _recordingStartPosition.Value; if (anchorAge > _ringBuffer!.Length) { - throw new InvalidOperationException( + throw new ArchiveOperationException( $"Cannot rewind: recording anchor is {anchorAge} bytes behind current position, " + $"but ring buffer only holds {_ringBuffer.Length} bytes. " + $"Recording buffer overflow - increase DefaultRollingBufferSize or reduce format detection reads." @@ -154,13 +154,13 @@ public partial class SharpCompressStream : Stream, IStreamStack { if (_isPassthrough) { - throw new InvalidOperationException( + throw new ArchiveOperationException( "StopRecording cannot be called on a passthrough stream. Use Create() first." ); } if (!IsRecording) { - throw new InvalidOperationException( + throw new ArchiveOperationException( "StopRecording can only be called when recording is active." ); } @@ -179,13 +179,13 @@ public partial class SharpCompressStream : Stream, IStreamStack { if (_isPassthrough) { - throw new InvalidOperationException( + throw new ArchiveOperationException( "StartRecording cannot be called on a passthrough stream. Use Create() first." ); } if (IsRecording) { - throw new InvalidOperationException( + throw new ArchiveOperationException( "StartRecording can only be called when not already recording." ); } @@ -337,7 +337,7 @@ public partial class SharpCompressStream : Stream, IStreamStack // Verify data is available in ring buffer if (!_ringBuffer!.CanReadFromEnd(bytesFromEnd)) { - throw new InvalidOperationException( + throw new ArchiveOperationException( $"Ring buffer underflow: trying to read {bytesFromEnd} bytes back, " + $"but buffer only holds {_ringBuffer.Length} bytes." ); diff --git a/src/SharpCompress/IO/SourceStream.cs b/src/SharpCompress/IO/SourceStream.cs index 35ec3b9a..4a837713 100644 --- a/src/SharpCompress/IO/SourceStream.cs +++ b/src/SharpCompress/IO/SourceStream.cs @@ -4,6 +4,7 @@ using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; +using SharpCompress.Common; using SharpCompress.Readers; namespace SharpCompress.IO; @@ -206,7 +207,7 @@ public partial class SourceStream : Stream, IStreamStack if (!SetStream(_stream + 1)) { // No more streams available, cannot seek to requested position - throw new InvalidOperationException( + throw new ArchiveOperationException( $"Cannot seek to position {pos}. End of stream reached at position {_prevSize}." ); } @@ -216,7 +217,7 @@ public partial class SourceStream : Stream, IStreamStack if (currentLength <= 0 && Current.Length <= 0) { // Both old and new stream have zero length - cannot make progress - throw new InvalidOperationException( + throw new ArchiveOperationException( $"Cannot seek to position {pos}. Encountered zero-length streams at position {_prevSize}." ); } diff --git a/src/SharpCompress/Polyfills/AsyncEnumerableExtensions.cs b/src/SharpCompress/Polyfills/AsyncEnumerableExtensions.cs index 6df982a0..83636ea2 100644 --- a/src/SharpCompress/Polyfills/AsyncEnumerableExtensions.cs +++ b/src/SharpCompress/Polyfills/AsyncEnumerableExtensions.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; +using SharpCompress.Common; namespace SharpCompress; @@ -117,12 +118,12 @@ public static class AsyncEnumerableExtensions if (!await enumerator.MoveNextAsync().ConfigureAwait(false)) { - throw new InvalidOperationException("The source sequence is empty."); + throw new ArchiveOperationException("The source sequence is empty."); } var value = enumerator.Current; if (await enumerator.MoveNextAsync().ConfigureAwait(false)) { - throw new InvalidOperationException( + throw new ArchiveOperationException( "The source sequence contains more than one element." ); } @@ -135,7 +136,7 @@ public static class AsyncEnumerableExtensions { return item; } - throw new InvalidOperationException("The source sequence is empty."); + throw new ArchiveOperationException("The source sequence is empty."); } public async ValueTask FirstOrDefaultAsync( diff --git a/src/SharpCompress/Providers/CompressionProviderRegistry.cs b/src/SharpCompress/Providers/CompressionProviderRegistry.cs index c061dc26..ea0e217a 100644 --- a/src/SharpCompress/Providers/CompressionProviderRegistry.cs +++ b/src/SharpCompress/Providers/CompressionProviderRegistry.cs @@ -62,14 +62,14 @@ public sealed class CompressionProviderRegistry /// The destination stream. /// The compression level. /// A compression stream. - /// If no provider is registered for the type. + /// If no provider is registered for the type. /// If the provider does not support compression. public Stream CreateCompressStream(CompressionType type, Stream destination, int level) { var provider = GetProvider(type); if (provider is null) { - throw new InvalidOperationException( + throw new ArchiveOperationException( $"No compression provider registered for type: {type}" ); } @@ -82,14 +82,14 @@ public sealed class CompressionProviderRegistry /// The compression type. /// The source stream. /// A decompression stream. - /// If no provider is registered for the type. + /// If no provider is registered for the type. /// If the provider does not support decompression. public Stream CreateDecompressStream(CompressionType type, Stream source) { var provider = GetProvider(type); if (provider is null) { - throw new InvalidOperationException( + throw new ArchiveOperationException( $"No compression provider registered for type: {type}" ); } @@ -104,7 +104,7 @@ public sealed class CompressionProviderRegistry /// The compression level. /// Context information for the compression. /// A compression stream. - /// If no provider is registered for the type. + /// If no provider is registered for the type. /// If the provider does not support compression. public Stream CreateCompressStream( CompressionType type, @@ -116,7 +116,7 @@ public sealed class CompressionProviderRegistry var provider = GetProvider(type); if (provider is null) { - throw new InvalidOperationException( + throw new ArchiveOperationException( $"No compression provider registered for type: {type}" ); } @@ -130,7 +130,7 @@ public sealed class CompressionProviderRegistry /// The source stream. /// Context information for the decompression. /// A decompression stream. - /// If no provider is registered for the type. + /// If no provider is registered for the type. /// If the provider does not support decompression. public Stream CreateDecompressStream( CompressionType type, @@ -141,7 +141,7 @@ public sealed class CompressionProviderRegistry var provider = GetProvider(type); if (provider is null) { - throw new InvalidOperationException( + throw new ArchiveOperationException( $"No compression provider registered for type: {type}" ); } @@ -156,7 +156,7 @@ public sealed class CompressionProviderRegistry /// The compression level. /// Cancellation token. /// A task containing the compression stream. - /// If no provider is registered for the type. + /// If no provider is registered for the type. /// If the provider does not support compression. public ValueTask CreateCompressStreamAsync( CompressionType type, @@ -168,7 +168,7 @@ public sealed class CompressionProviderRegistry var provider = GetProvider(type); if (provider is null) { - throw new InvalidOperationException( + throw new ArchiveOperationException( $"No compression provider registered for type: {type}" ); } @@ -182,7 +182,7 @@ public sealed class CompressionProviderRegistry /// The source stream. /// Cancellation token. /// A task containing the decompression stream. - /// If no provider is registered for the type. + /// If no provider is registered for the type. /// If the provider does not support decompression. public ValueTask CreateDecompressStreamAsync( CompressionType type, @@ -193,7 +193,7 @@ public sealed class CompressionProviderRegistry var provider = GetProvider(type); if (provider is null) { - throw new InvalidOperationException( + throw new ArchiveOperationException( $"No compression provider registered for type: {type}" ); } @@ -209,7 +209,7 @@ public sealed class CompressionProviderRegistry /// Context information for the compression. /// Cancellation token. /// A task containing the compression stream. - /// If no provider is registered for the type. + /// If no provider is registered for the type. /// If the provider does not support compression. public ValueTask CreateCompressStreamAsync( CompressionType type, @@ -222,7 +222,7 @@ public sealed class CompressionProviderRegistry var provider = GetProvider(type); if (provider is null) { - throw new InvalidOperationException( + throw new ArchiveOperationException( $"No compression provider registered for type: {type}" ); } @@ -237,7 +237,7 @@ public sealed class CompressionProviderRegistry /// Context information for the decompression. /// Cancellation token. /// A task containing the decompression stream. - /// If no provider is registered for the type. + /// If no provider is registered for the type. /// If the provider does not support decompression. public ValueTask CreateDecompressStreamAsync( CompressionType type, @@ -249,7 +249,7 @@ public sealed class CompressionProviderRegistry var provider = GetProvider(type); if (provider is null) { - throw new InvalidOperationException( + throw new ArchiveOperationException( $"No compression provider registered for type: {type}" ); } diff --git a/src/SharpCompress/Providers/ContextRequiredDecompressionProviderBase.cs b/src/SharpCompress/Providers/ContextRequiredDecompressionProviderBase.cs index fac51678..1e7ac74f 100644 --- a/src/SharpCompress/Providers/ContextRequiredDecompressionProviderBase.cs +++ b/src/SharpCompress/Providers/ContextRequiredDecompressionProviderBase.cs @@ -2,6 +2,7 @@ using System; using System.IO; using System.Threading; using System.Threading.Tasks; +using SharpCompress.Common; namespace SharpCompress.Providers; @@ -12,7 +13,7 @@ public abstract class ContextRequiredDecompressionProviderBase : DecompressionOn protected virtual string DecompressionContextRequirementSuffix => string.Empty; public sealed override Stream CreateDecompressStream(Stream source) => - throw new InvalidOperationException( + throw new ArchiveOperationException( $"{DecompressionContextRequirementDescription}. " + $"Use CreateDecompressStream(Stream, CompressionContext) overload{DecompressionContextRequirementSuffix}." ); @@ -21,7 +22,7 @@ public abstract class ContextRequiredDecompressionProviderBase : DecompressionOn Stream source, CancellationToken cancellationToken = default ) => - throw new InvalidOperationException( + throw new ArchiveOperationException( $"{DecompressionContextRequirementDescription}. " + "Use CreateDecompressStreamAsync(Stream, CompressionContext, CancellationToken) " + $"overload{DecompressionContextRequirementSuffix}." diff --git a/src/SharpCompress/Providers/Default/LzmaCompressingProvider.cs b/src/SharpCompress/Providers/Default/LzmaCompressingProvider.cs index c2ac5e36..ce2e6c53 100644 --- a/src/SharpCompress/Providers/Default/LzmaCompressingProvider.cs +++ b/src/SharpCompress/Providers/Default/LzmaCompressingProvider.cs @@ -19,7 +19,7 @@ public sealed class LzmaCompressingProvider : CompressionProviderBase, ICompress public override Stream CreateCompressStream(Stream destination, int compressionLevel) { - throw new InvalidOperationException( + throw new ArchiveOperationException( "LZMA compression requires context with CanSeek information. " + "Use CreateCompressStream(Stream, int, CompressionContext) overload." ); @@ -39,7 +39,7 @@ public sealed class LzmaCompressingProvider : CompressionProviderBase, ICompress public override Stream CreateDecompressStream(Stream source) { - throw new InvalidOperationException( + throw new ArchiveOperationException( "LZMA decompression requires properties. " + "Use CreateDecompressStream(Stream, CompressionContext) overload with Properties." ); @@ -62,7 +62,7 @@ public sealed class LzmaCompressingProvider : CompressionProviderBase, ICompress Stream source, CancellationToken cancellationToken = default ) => - throw new InvalidOperationException( + throw new ArchiveOperationException( "LZMA decompression requires properties. " + "Use CreateDecompressStreamAsync(Stream, CompressionContext, CancellationToken) overload with Properties." ); diff --git a/src/SharpCompress/Providers/Default/PpmdCompressingProvider.cs b/src/SharpCompress/Providers/Default/PpmdCompressingProvider.cs index 8253ceeb..818976bd 100644 --- a/src/SharpCompress/Providers/Default/PpmdCompressingProvider.cs +++ b/src/SharpCompress/Providers/Default/PpmdCompressingProvider.cs @@ -41,7 +41,7 @@ public sealed class PpmdCompressingProvider : CompressionProviderBase, ICompress public override Stream CreateDecompressStream(Stream source) { - throw new InvalidOperationException( + throw new ArchiveOperationException( "PPMd decompression requires properties. " + "Use CreateDecompressStream(Stream, CompressionContext) overload with Properties." ); @@ -65,7 +65,7 @@ public sealed class PpmdCompressingProvider : CompressionProviderBase, ICompress Stream source, CancellationToken cancellationToken = default ) => - throw new InvalidOperationException( + throw new ArchiveOperationException( "PPMd decompression requires properties. " + "Use CreateDecompressStreamAsync(Stream, CompressionContext, CancellationToken) overload with Properties." ); diff --git a/src/SharpCompress/Readers/AbstractReader.cs b/src/SharpCompress/Readers/AbstractReader.cs index 73a282d4..e6d0966d 100644 --- a/src/SharpCompress/Readers/AbstractReader.cs +++ b/src/SharpCompress/Readers/AbstractReader.cs @@ -89,7 +89,7 @@ public abstract partial class AbstractReader : IReader, IAsyncR { if (_entriesForCurrentReadStreamAsync is not null) { - throw new InvalidOperationException( + throw new ArchiveOperationException( $"{nameof(MoveToNextEntry)} cannot be used after {nameof(MoveToNextEntryAsync)} has been used." ); } @@ -122,7 +122,7 @@ public abstract partial class AbstractReader : IReader, IAsyncR { if (_entriesForCurrentReadStreamAsync is not null) { - throw new InvalidOperationException( + throw new ArchiveOperationException( $"{nameof(LoadStreamForReading)} cannot be used after {nameof(LoadStreamForReadingAsync)} has been used." ); } diff --git a/src/SharpCompress/Readers/Rar/RarReader.Async.cs b/src/SharpCompress/Readers/Rar/RarReader.Async.cs index 9ccc5231..d8cd74e4 100644 --- a/src/SharpCompress/Readers/Rar/RarReader.Async.cs +++ b/src/SharpCompress/Readers/Rar/RarReader.Async.cs @@ -29,7 +29,7 @@ public abstract partial class RarReader { if (Entry.IsRedir) { - throw new InvalidOperationException("no stream for redirect entry"); + throw new ArchiveOperationException("no stream for redirect entry"); } var stream = await MultiVolumeReadOnlyAsyncStream diff --git a/src/SharpCompress/Readers/Rar/RarReader.cs b/src/SharpCompress/Readers/Rar/RarReader.cs index df006362..24235308 100644 --- a/src/SharpCompress/Readers/Rar/RarReader.cs +++ b/src/SharpCompress/Readers/Rar/RarReader.cs @@ -114,7 +114,7 @@ public abstract partial class RarReader : AbstractReader - _current ?? throw new InvalidOperationException("No current entry is available."); + _current ?? throw new ArchiveOperationException("No current entry is available."); /// /// Advances to the next non-directory entry-relevant header and materializes a , diff --git a/src/SharpCompress/Utility.Async.cs b/src/SharpCompress/Utility.Async.cs index 79df1f9c..60f52068 100644 --- a/src/SharpCompress/Utility.Async.cs +++ b/src/SharpCompress/Utility.Async.cs @@ -48,7 +48,7 @@ internal static partial class Utility .ConfigureAwait(false); if (fetched <= 0) { - throw new EndOfStreamException(); + throw new IncompleteArchiveException("Unexpected end of stream."); } offset += fetched; diff --git a/src/SharpCompress/Utility.cs b/src/SharpCompress/Utility.cs index cdc78549..af8572d7 100644 --- a/src/SharpCompress/Utility.cs +++ b/src/SharpCompress/Utility.cs @@ -273,7 +273,7 @@ internal static partial class Utility var fetched = source.Read(buffer, offset, length); if (fetched <= 0) { - throw new EndOfStreamException(); + throw new IncompleteArchiveException("Unexpected end of stream."); } offset += fetched; diff --git a/src/SharpCompress/Writers/Zip/ZipWriter.cs b/src/SharpCompress/Writers/Zip/ZipWriter.cs index 26ee920d..6056e190 100644 --- a/src/SharpCompress/Writers/Zip/ZipWriter.cs +++ b/src/SharpCompress/Writers/Zip/ZipWriter.cs @@ -460,7 +460,7 @@ public partial class ZipWriter : AbstractWriter ); if (compressingProvider is null) { - throw new InvalidOperationException("LZMA compression provider not found."); + throw new ArchiveOperationException("LZMA compression provider not found."); } var context = new CompressionContext { CanSeek = originalStream.CanSeek }; @@ -498,7 +498,7 @@ public partial class ZipWriter : AbstractWriter ); if (compressingProvider is null) { - throw new InvalidOperationException("PPMd compression provider not found."); + throw new ArchiveOperationException("PPMd compression provider not found."); } var context = new CompressionContext diff --git a/tests/SharpCompress.Test/BZip2/BZip2ReaderTests.cs b/tests/SharpCompress.Test/BZip2/BZip2ReaderTests.cs index d5ec94c4..70cea8c6 100644 --- a/tests/SharpCompress.Test/BZip2/BZip2ReaderTests.cs +++ b/tests/SharpCompress.Test/BZip2/BZip2ReaderTests.cs @@ -16,6 +16,6 @@ public class BZip2ReaderTests : ReaderTests Stream stream = new MemoryStream( new byte[] { 0x42, 0x5a, 0x68, 0x34, 0x31, 0x41, 0x59, 0x26, 0x53, 0x59, 0x35 } ); - Assert.Throws(() => ReaderFactory.OpenReader(stream)); + Assert.Throws(() => ReaderFactory.OpenReader(stream)); } } diff --git a/tests/SharpCompress.Test/ExceptionHierarchyTests.cs b/tests/SharpCompress.Test/ExceptionHierarchyTests.cs index b543d179..4fd7f25b 100644 --- a/tests/SharpCompress.Test/ExceptionHierarchyTests.cs +++ b/tests/SharpCompress.Test/ExceptionHierarchyTests.cs @@ -15,6 +15,11 @@ public class ExceptionHierarchyTests // Verify that ArchiveException inherits from SharpCompressException Assert.True(typeof(SharpCompressException).IsAssignableFrom(typeof(ArchiveException))); + // Verify that ArchiveOperationException inherits from SharpCompressException + Assert.True( + typeof(SharpCompressException).IsAssignableFrom(typeof(ArchiveOperationException)) + ); + // Verify that ExtractionException inherits from SharpCompressException Assert.True(typeof(SharpCompressException).IsAssignableFrom(typeof(ExtractionException))); diff --git a/tests/SharpCompress.Test/Rar/RarArchiveTests.cs b/tests/SharpCompress.Test/Rar/RarArchiveTests.cs index f2bdcb00..e4fef73f 100644 --- a/tests/SharpCompress.Test/Rar/RarArchiveTests.cs +++ b/tests/SharpCompress.Test/Rar/RarArchiveTests.cs @@ -681,7 +681,7 @@ public class RarArchiveTests : ArchiveTests // Opening the archive should work, but extracting should throw // when we try to read beyond the truncated data - var exception = Assert.Throws(() => + var exception = Assert.Throws(() => { using var archive = RarArchive.OpenArchive(truncatedStream); foreach (var entry in archive.Entries.Where(e => !e.IsDirectory)) @@ -751,7 +751,7 @@ public class RarArchiveTests : ArchiveTests var readerOptions = new ReaderOptions { LookForHeader = true }; // This should throw InvalidOperationException, not hang in an infinite loop - var exception = Assert.Throws(() => + var exception = Assert.Throws(() => { using var fileStream = File.Open( Path.Combine(TEST_ARCHIVES_PATH, testFile), diff --git a/tests/SharpCompress.Test/Rar/RarReaderAsyncTests.cs b/tests/SharpCompress.Test/Rar/RarReaderAsyncTests.cs index f74f2be9..d6d82e26 100644 --- a/tests/SharpCompress.Test/Rar/RarReaderAsyncTests.cs +++ b/tests/SharpCompress.Test/Rar/RarReaderAsyncTests.cs @@ -57,7 +57,7 @@ public class RarReaderAsyncTests : ReaderTests [Fact] public async ValueTask Rar_Multi_Reader_Encrypted_Async() => - await Assert.ThrowsAsync(async () => + await Assert.ThrowsAsync(async () => { string[] archives = [ diff --git a/tests/SharpCompress.Test/SevenZip/SevenZipArchiveTests.cs b/tests/SharpCompress.Test/SevenZip/SevenZipArchiveTests.cs index 6f5b6b84..ccb3596e 100644 --- a/tests/SharpCompress.Test/SevenZip/SevenZipArchiveTests.cs +++ b/tests/SharpCompress.Test/SevenZip/SevenZipArchiveTests.cs @@ -83,7 +83,7 @@ public class SevenZipArchiveTests : ArchiveTests [Fact] public void SevenZipArchive_BZip2_Split() => - Assert.Throws(() => + Assert.Throws(() => ArchiveStreamRead( ".001", null, @@ -100,7 +100,7 @@ public class SevenZipArchiveTests : ArchiveTests //Same as archive as Original.7z.001 ... 007 files without the root directory 'Original\' in the archive - this caused the verify to fail [Fact] public void SevenZipArchive_BZip2_Split_Working() => - Assert.Throws(() => + Assert.Throws(() => ArchiveStreamRead( ".001", null, diff --git a/tests/SharpCompress.Test/Streams/SharpCompressStreamEdgeTest.cs b/tests/SharpCompress.Test/Streams/SharpCompressStreamEdgeTest.cs index c4409af8..fa53e8e3 100644 --- a/tests/SharpCompress.Test/Streams/SharpCompressStreamEdgeTest.cs +++ b/tests/SharpCompress.Test/Streams/SharpCompressStreamEdgeTest.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using SharpCompress.Common; using SharpCompress.IO; using SharpCompress.Test.Mocks; using Xunit; @@ -34,7 +35,7 @@ public class SharpCompressStreamEdgeTest var ms = new MemoryStream(new byte[] { 1, 2, 3, 4, 5 }); var stream = SharpCompressStream.CreateNonDisposing(ms); stream.ThrowOnDispose = true; - Assert.Throws(() => stream.Dispose()); + Assert.Throws(() => stream.Dispose()); } [Fact] diff --git a/tests/SharpCompress.Test/Streams/SharpCompressStreamErrorAsyncTest.cs b/tests/SharpCompress.Test/Streams/SharpCompressStreamErrorAsyncTest.cs index 8d59ddaa..cf90a566 100644 --- a/tests/SharpCompress.Test/Streams/SharpCompressStreamErrorAsyncTest.cs +++ b/tests/SharpCompress.Test/Streams/SharpCompressStreamErrorAsyncTest.cs @@ -1,6 +1,7 @@ using System; using System.IO; using System.Threading.Tasks; +using SharpCompress.Common; using SharpCompress.IO; using Xunit; @@ -56,7 +57,7 @@ public class SharpCompressStreamErrorAsyncTest var stream = SharpCompressStream.CreateNonDisposing(ms); stream.ThrowOnDispose = true; await Assert - .ThrowsAsync(async () => + .ThrowsAsync(async () => await stream.DisposeAsync().ConfigureAwait(false) ) .ConfigureAwait(false); @@ -108,7 +109,7 @@ public class SharpCompressStreamErrorAsyncTest { await stream.ReadExactAsync(buffer, 0, 32).ConfigureAwait(false); } - Assert.Throws(() => stream.Rewind()); + Assert.Throws(() => stream.Rewind()); } [Fact] diff --git a/tests/SharpCompress.Test/Streams/SharpCompressStreamErrorTest.cs b/tests/SharpCompress.Test/Streams/SharpCompressStreamErrorTest.cs index 6ac0ee38..7b844ff4 100644 --- a/tests/SharpCompress.Test/Streams/SharpCompressStreamErrorTest.cs +++ b/tests/SharpCompress.Test/Streams/SharpCompressStreamErrorTest.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using SharpCompress.Common; using SharpCompress.IO; using SharpCompress.Test.Mocks; using Xunit; @@ -60,7 +61,7 @@ public class SharpCompressStreamErrorTest var ms = new MemoryStream(new byte[] { 1, 2, 3, 4, 5 }); var nonSeekableMs = new NonSeekableStreamWrapper(ms); var stream = SharpCompressStream.Create(nonSeekableMs, 128); - Assert.Throws(() => stream.Rewind()); + Assert.Throws(() => stream.Rewind()); } [Fact] @@ -68,7 +69,7 @@ public class SharpCompressStreamErrorTest { var ms = new MemoryStream(new byte[] { 1, 2, 3, 4, 5 }); var stream = SharpCompressStream.CreateNonDisposing(ms); - Assert.Throws(() => stream.Rewind()); + Assert.Throws(() => stream.Rewind()); } [Fact] @@ -78,7 +79,7 @@ public class SharpCompressStreamErrorTest var nonSeekableMs = new NonSeekableStreamWrapper(ms); var stream = SharpCompressStream.Create(nonSeekableMs, 128); stream.StartRecording(); - Assert.Throws(() => stream.StartRecording()); + Assert.Throws(() => stream.StartRecording()); } [Fact] @@ -86,7 +87,7 @@ public class SharpCompressStreamErrorTest { var ms = new MemoryStream(new byte[] { 1, 2, 3, 4, 5 }); var stream = SharpCompressStream.CreateNonDisposing(ms); - Assert.Throws(() => stream.StartRecording()); + Assert.Throws(() => stream.StartRecording()); } [Fact] @@ -95,7 +96,7 @@ public class SharpCompressStreamErrorTest var ms = new MemoryStream(new byte[] { 1, 2, 3, 4, 5 }); var nonSeekableMs = new NonSeekableStreamWrapper(ms); var stream = SharpCompressStream.Create(nonSeekableMs, 128); - Assert.Throws(() => stream.StopRecording()); + Assert.Throws(() => stream.StopRecording()); } [Fact] @@ -103,7 +104,7 @@ public class SharpCompressStreamErrorTest { var ms = new MemoryStream(new byte[] { 1, 2, 3, 4, 5 }); var stream = SharpCompressStream.CreateNonDisposing(ms); - Assert.Throws(() => stream.StopRecording()); + Assert.Throws(() => stream.StopRecording()); } [Fact] @@ -115,7 +116,7 @@ public class SharpCompressStreamErrorTest stream.StartRecording(); stream.Read(new byte[4], 0, 4); stream.StopRecording(); - Assert.Throws(() => stream.StopRecording()); + Assert.Throws(() => stream.StopRecording()); } [Fact] diff --git a/tests/SharpCompress.Test/Streams/SharpCompressStreamPassthroughAsyncTest.cs b/tests/SharpCompress.Test/Streams/SharpCompressStreamPassthroughAsyncTest.cs index a3972f33..521bac6a 100644 --- a/tests/SharpCompress.Test/Streams/SharpCompressStreamPassthroughAsyncTest.cs +++ b/tests/SharpCompress.Test/Streams/SharpCompressStreamPassthroughAsyncTest.cs @@ -1,6 +1,7 @@ using System; using System.IO; using System.Threading.Tasks; +using SharpCompress.Common; using SharpCompress.IO; using SharpCompress.Test.Mocks; using Xunit; @@ -101,7 +102,7 @@ public class SharpCompressStreamPassthroughAsyncTest var stream = SharpCompressStream.CreateNonDisposing(ms); stream.ThrowOnDispose = true; await Assert - .ThrowsAsync(async () => + .ThrowsAsync(async () => await stream.DisposeAsync().ConfigureAwait(false) ) .ConfigureAwait(false); diff --git a/tests/SharpCompress.Test/Streams/SharpCompressStreamPassthroughTest.cs b/tests/SharpCompress.Test/Streams/SharpCompressStreamPassthroughTest.cs index 154043e0..6e346be1 100644 --- a/tests/SharpCompress.Test/Streams/SharpCompressStreamPassthroughTest.cs +++ b/tests/SharpCompress.Test/Streams/SharpCompressStreamPassthroughTest.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using SharpCompress.Common; using SharpCompress.IO; using SharpCompress.Test.Mocks; using Xunit; @@ -156,7 +157,7 @@ public class SharpCompressStreamPassthroughTest { var ms = new MemoryStream(new byte[] { 1, 2, 3, 4, 5 }); var stream = SharpCompressStream.CreateNonDisposing(ms); - Assert.Throws(() => stream.StartRecording()); + Assert.Throws(() => stream.StartRecording()); } [Fact] @@ -164,7 +165,7 @@ public class SharpCompressStreamPassthroughTest { var ms = new MemoryStream(new byte[] { 1, 2, 3, 4, 5 }); var stream = SharpCompressStream.CreateNonDisposing(ms); - Assert.Throws(() => stream.Rewind()); + Assert.Throws(() => stream.Rewind()); } [Fact] @@ -172,7 +173,7 @@ public class SharpCompressStreamPassthroughTest { var ms = new MemoryStream(new byte[] { 1, 2, 3, 4, 5 }); var stream = SharpCompressStream.CreateNonDisposing(ms); - Assert.Throws(() => stream.StopRecording()); + Assert.Throws(() => stream.StopRecording()); } [Fact] diff --git a/tests/SharpCompress.Test/UtilityTests.cs b/tests/SharpCompress.Test/UtilityTests.cs index ea3e902c..20d46e6b 100644 --- a/tests/SharpCompress.Test/UtilityTests.cs +++ b/tests/SharpCompress.Test/UtilityTests.cs @@ -5,6 +5,7 @@ using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; +using SharpCompress.Common; using Xunit; namespace SharpCompress.Test; @@ -174,12 +175,14 @@ public class UtilityTests } [Fact] - public async ValueTask ReadByteAsync_EmptyStream_ThrowsEndOfStreamException() + public async ValueTask ReadByteAsync_EmptyStream_ThrowsIncompleteArchiveException() { using var stream = new MemoryStream(); using var reader = new BinaryReader(stream); - await Assert.ThrowsAsync(async () => await reader.ReadByteAsync()); + await Assert.ThrowsAsync(async () => + await reader.ReadByteAsync() + ); } [Fact] @@ -216,22 +219,26 @@ public class UtilityTests } [Fact] - public async ValueTask ReadBytesAsync_NotEnoughData_ThrowsEndOfStreamException() + public async ValueTask ReadBytesAsync_NotEnoughData_ThrowsIncompleteArchiveException() { var data = new byte[] { 1, 2, 3 }; using var stream = new MemoryStream(data); using var reader = new BinaryReader(stream); - await Assert.ThrowsAsync(async () => await reader.ReadBytesAsync(5)); + await Assert.ThrowsAsync(async () => + await reader.ReadBytesAsync(5) + ); } [Fact] - public async ValueTask ReadBytesAsync_EmptyStream_ThrowsEndOfStreamException() + public async ValueTask ReadBytesAsync_EmptyStream_ThrowsIncompleteArchiveException() { using var stream = new MemoryStream(); using var reader = new BinaryReader(stream); - await Assert.ThrowsAsync(async () => await reader.ReadBytesAsync(1)); + await Assert.ThrowsAsync(async () => + await reader.ReadBytesAsync(1) + ); } [Fact] diff --git a/tests/SharpCompress.Test/Xz/Filters/Lzma2Tests.cs b/tests/SharpCompress.Test/Xz/Filters/Lzma2Tests.cs index 12db0746..e6ad48d6 100644 --- a/tests/SharpCompress.Test/Xz/Filters/Lzma2Tests.cs +++ b/tests/SharpCompress.Test/Xz/Filters/Lzma2Tests.cs @@ -40,7 +40,7 @@ public class Lzma2Tests : XzTestsBase { uint temp; _filter.Init([41]); - var ex = Assert.Throws(() => + var ex = Assert.Throws(() => { temp = _filter.DictionarySize; });