Merge pull request #834 from adamhathcock/exception-normalization

Add SharpCompressException and use it or children in most places
This commit is contained in:
Adam Hathcock
2025-04-28 16:12:25 +01:00
committed by GitHub
70 changed files with 234 additions and 258 deletions

View File

@@ -53,7 +53,7 @@ public abstract class AbstractArchive<TEntry, TVolume> : IArchive, IArchiveExtra
{
if (!stream.CanSeek || !stream.CanRead)
{
throw new ArgumentException("Archive streams must be Readable and Seekable");
throw new ArchiveException("Archive streams must be Readable and Seekable");
}
return stream;
}

View File

@@ -151,7 +151,7 @@ public abstract class AbstractWritableArchive<TEntry, TVolume>
{
if (!source.CanRead || !source.CanSeek)
{
throw new ArgumentException(
throw new ArchiveException(
"Streams must be readable and seekable to use the Writing Archive API"
);
}

View File

@@ -162,7 +162,7 @@ public class GZipArchive : AbstractWritableArchive<GZipArchiveEntry, GZipVolume>
{
if (Entries.Any())
{
throw new InvalidOperationException("Only one entry is allowed in a GZip Archive");
throw new InvalidFormatException("Only one entry is allowed in a GZip Archive");
}
return new GZipWritableArchiveEntry(this, source, filePath, size, modified, closeStream);
}
@@ -176,7 +176,7 @@ public class GZipArchive : AbstractWritableArchive<GZipArchiveEntry, GZipVolume>
{
if (Entries.Count > 1)
{
throw new InvalidOperationException("Only one entry is allowed in a GZip Archive");
throw new InvalidFormatException("Only one entry is allowed in a GZip Archive");
}
using var writer = new GZipWriter(stream, new GZipWriterOptions(options));
foreach (var entry in oldEntries.Concat(newEntries).Where(x => !x.IsDirectory))

View File

@@ -17,7 +17,7 @@ public class GZipArchiveEntry : GZipEntry, IArchiveEntry
{
part.GetRawStream().Position = part.EntryStartPosition;
}
return Parts.Single().GetCompressedStream();
return Parts.Single().GetCompressedStream().NotNull();
}
#region IArchiveEntry Members

View File

@@ -42,7 +42,7 @@ public class RarArchiveEntry : RarEntry, IArchiveEntry
{
CheckIncomplete();
return BitConverter.ToUInt32(
parts.Select(fp => fp.FileHeader).Single(fh => !fh.IsSplitAfter).FileCrc,
parts.Select(fp => fp.FileHeader).Single(fh => !fh.IsSplitAfter).FileCrc.NotNull(),
0
);
}

View File

@@ -10,7 +10,7 @@ public class TarArchiveEntry : TarEntry, IArchiveEntry
internal TarArchiveEntry(TarArchive archive, TarFilePart? part, CompressionType compressionType)
: base(part, compressionType) => Archive = archive;
public virtual Stream OpenEntryStream() => Parts.Single().GetCompressedStream();
public virtual Stream OpenEntryStream() => Parts.Single().GetCompressedStream().NotNull();
#region IArchiveEntry Members

View File

@@ -9,7 +9,7 @@ public class ZipArchiveEntry : ZipEntry, IArchiveEntry
internal ZipArchiveEntry(ZipArchive archive, SeekableZipFilePart? part)
: base(part) => Archive = archive;
public virtual Stream OpenEntryStream() => Parts.Single().GetCompressedStream();
public virtual Stream OpenEntryStream() => Parts.Single().GetCompressedStream().NotNull();
#region IArchiveEntry Members

View File

@@ -1,9 +0,0 @@
using System;
namespace SharpCompress.Common;
public class ArchiveException : Exception
{
public ArchiveException(string message)
: base(message) { }
}

View File

@@ -1,9 +0,0 @@
using System;
namespace SharpCompress.Common;
public class CryptographicException : Exception
{
public CryptographicException(string message)
: base(message) { }
}

View File

@@ -1,12 +0,0 @@
using System;
namespace SharpCompress.Common;
public class ExtractionException : Exception
{
public ExtractionException(string message)
: base(message) { }
public ExtractionException(string message, Exception inner)
: base(message, inner) { }
}

View File

@@ -11,7 +11,7 @@ public abstract class FilePart
internal abstract string? FilePartName { get; }
public int Index { get; set; }
internal abstract Stream GetCompressedStream();
internal abstract Stream? GetCompressedStream();
internal abstract Stream? GetRawStream();
internal bool Skipped { get; set; }
}

View File

@@ -1,7 +0,0 @@
namespace SharpCompress.Common;
public class IncompleteArchiveException : ArchiveException
{
public IncompleteArchiveException(string message)
: base(message) { }
}

View File

@@ -1,12 +0,0 @@
using System;
namespace SharpCompress.Common;
public class InvalidFormatException : ExtractionException
{
public InvalidFormatException(string message)
: base(message) { }
public InvalidFormatException(string message, Exception inner)
: base(message, inner) { }
}

View File

@@ -1,12 +0,0 @@
using System;
namespace SharpCompress.Common;
public class MultiVolumeExtractionException : ExtractionException
{
public MultiVolumeExtractionException(string message)
: base(message) { }
public MultiVolumeExtractionException(string message, Exception inner)
: base(message, inner) { }
}

View File

@@ -1,7 +0,0 @@
namespace SharpCompress.Common;
public class MultipartStreamRequiredException : ExtractionException
{
public MultipartStreamRequiredException(string message)
: base(message) { }
}

View File

@@ -17,7 +17,7 @@ internal class CryptKey5 : ICryptKey
private byte[] _pswCheck = { };
private byte[] _hashKey = { };
public CryptKey5(string password, Rar5CryptoInfo rar5CryptoInfo)
public CryptKey5(string? password, Rar5CryptoInfo rar5CryptoInfo)
{
_password = password ?? "";
_cryptoInfo = rar5CryptoInfo;

View File

@@ -1,8 +1,5 @@
#nullable disable
using System;
using System.Security.Cryptography;
using SharpCompress.Common.Rar.Headers;
using SharpCompress.IO;
namespace SharpCompress.Common.Rar.Headers;

View File

@@ -1,9 +1,6 @@
#nullable disable
using System;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using SharpCompress.IO;
#if !Rar2017_64bit
@@ -18,7 +15,7 @@ namespace SharpCompress.Common.Rar.Headers;
internal class FileHeader : RarHeader
{
private byte[] _hash;
private byte[]? _hash;
public FileHeader(RarHeader header, RarCrcBinaryReader reader, HeaderType headerType)
: base(header, reader, headerType) { }
@@ -319,6 +316,10 @@ internal class FileHeader : RarHeader
if (NewSubHeaderType.SUBHEAD_TYPE_RR.Equals(fileNameBytes))
{
if (SubData is null)
{
throw new InvalidFormatException();
}
RecoverySectors =
SubData[8]
+ (SubData[9] << 8)
@@ -340,12 +341,16 @@ internal class FileHeader : RarHeader
if (RemainingHeaderBytes(reader) >= 2)
{
var extendedFlags = reader.ReadUInt16();
FileLastModifiedTime = ProcessExtendedTimeV4(
extendedFlags,
FileLastModifiedTime,
reader,
0
);
if (FileLastModifiedTime is not null)
{
FileLastModifiedTime = ProcessExtendedTimeV4(
extendedFlags,
FileLastModifiedTime,
reader,
0
);
}
FileCreatedTime = ProcessExtendedTimeV4(extendedFlags, null, reader, 1);
FileLastAccessedTime = ProcessExtendedTimeV4(extendedFlags, null, reader, 2);
FileArchivedTime = ProcessExtendedTimeV4(extendedFlags, null, reader, 3);
@@ -377,7 +382,7 @@ internal class FileHeader : RarHeader
var dosTime = reader.ReadUInt32();
time = Utility.DosDateToDateTime(dosTime);
}
if ((rmode & 4) == 0)
if ((rmode & 4) == 0 && time is not null)
{
time = time.Value.AddSeconds(1);
}
@@ -390,7 +395,11 @@ internal class FileHeader : RarHeader
}
//10^-7 to 10^-3
return time.Value.AddMilliseconds(nanosecondHundreds * Math.Pow(10, -4));
if (time is not null)
{
return time.Value.AddMilliseconds(nanosecondHundreds * Math.Pow(10, -4));
}
return null;
}
private static string ConvertPathV4(string path)
@@ -406,13 +415,13 @@ internal class FileHeader : RarHeader
return path;
}
public override string ToString() => FileName;
public override string ToString() => FileName ?? "FileHeader";
private ushort Flags { get; set; }
private bool HasFlag(ushort flag) => (Flags & flag) == flag;
internal byte[] FileCrc
internal byte[]? FileCrc
{
get => _hash;
private set => _hash = value;
@@ -441,22 +450,22 @@ internal class FileHeader : RarHeader
public bool IsRedir => RedirType != 0;
public byte RedirFlags { get; private set; }
public bool IsRedirDirectory => (RedirFlags & RedirFlagV5.DIRECTORY) != 0;
public string RedirTargetName { get; private set; }
public string? RedirTargetName { get; private set; }
// unused for UnpackV1 implementation (limitation)
internal size_t WindowSize { get; private set; }
internal byte[] R4Salt { get; private set; }
internal Rar5CryptoInfo Rar5CryptoInfo { get; private set; }
internal byte[]? R4Salt { get; private set; }
internal Rar5CryptoInfo? Rar5CryptoInfo { get; private set; }
private byte HostOs { get; set; }
internal uint FileAttributes { get; private set; }
internal long CompressedSize { get; private set; }
internal long UncompressedSize { get; private set; }
internal string FileName { get; private set; }
internal byte[] SubData { get; private set; }
internal string? FileName { get; private set; }
internal byte[]? SubData { get; private set; }
internal int RecoverySectors { get; private set; }
internal long DataStartPosition { get; set; }
public Stream PackedStream { get; set; }
public Stream? PackedStream { get; set; }
public bool IsSplitBefore =>
IsRar5 ? HasHeaderFlag(HeaderFlagsV5.SPLIT_BEFORE) : HasFlag(FileFlagsV4.SPLIT_BEFORE);

View File

@@ -1,5 +1,4 @@
using System;
using System.IO;
using SharpCompress.IO;
namespace SharpCompress.Common.Rar.Headers;
@@ -21,7 +20,7 @@ internal class RarHeader : IRarHeader
{
return new RarHeader(reader, isRar5, archiveEncoding);
}
catch (EndOfStreamException)
catch (InvalidFormatException)
{
return null;
}

View File

@@ -1,7 +1,5 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using SharpCompress.Common.Rar;
using SharpCompress.IO;
using SharpCompress.Readers;
@@ -160,10 +158,15 @@ public class RarHeaderFactory
{
fh.PackedStream = new RarCryptoWrapper(
ms,
fh.R4Salt is null ? fh.Rar5CryptoInfo.Salt : fh.R4Salt,
fh.R4Salt is null
? new CryptKey5(Options.Password!, fh.Rar5CryptoInfo)
: new CryptKey3(Options.Password!)
? fh.Rar5CryptoInfo.NotNull().Salt
: fh.R4Salt,
fh.R4Salt is null
? new CryptKey5(
Options.Password,
fh.Rar5CryptoInfo.NotNull()
)
: new CryptKey3(Options.Password)
);
}
}

View File

@@ -20,7 +20,7 @@ public abstract class RarEntry : Entry
/// <summary>
/// The File's 32 bit CRC Hash
/// </summary>
public override long Crc => BitConverter.ToUInt32(FileHeader.FileCrc, 0);
public override long Crc => BitConverter.ToUInt32(FileHeader.FileCrc.NotNull(), 0);
/// <summary>
/// The path of the file internal to the Rar Archive.
@@ -68,7 +68,7 @@ public abstract class RarEntry : Entry
public bool IsRedir => FileHeader.IsRedir;
public string RedirTargetName => FileHeader.RedirTargetName;
public string? RedirTargetName => FileHeader.RedirTargetName;
public override string ToString() =>
string.Format(

View File

@@ -62,7 +62,7 @@ public abstract class RarVolume : Volume
if (fh.FileName == "CMT")
{
var buffer = new byte[fh.CompressedSize];
fh.PackedStream.Read(buffer, 0, buffer.Length);
fh.PackedStream.NotNull().ReadFully(buffer);
Comment = Encoding.UTF8.GetString(buffer, 0, buffer.Length - 1);
}
}

View File

@@ -1,12 +0,0 @@
using System;
namespace SharpCompress.Common;
public class ReaderCancelledException : Exception
{
public ReaderCancelledException(string message)
: base(message) { }
public ReaderCancelledException(string message, Exception inner)
: base(message, inner) { }
}

View File

@@ -784,7 +784,7 @@ internal class ArchiveReader
);
break;
default:
throw new InvalidOperationException();
throw new InvalidFormatException();
}
}
}
@@ -843,7 +843,7 @@ internal class ArchiveReader
outStream.ReadExact(data, 0, data.Length);
if (outStream.ReadByte() >= 0)
{
throw new InvalidOperationException("Decoded stream is longer than expected.");
throw new InvalidFormatException("Decoded stream is longer than expected.");
}
dataVector.Add(data);
@@ -854,7 +854,7 @@ internal class ArchiveReader
!= folder._unpackCrc
)
{
throw new InvalidOperationException(
throw new InvalidFormatException(
"Decoded stream does not match expected CRC."
);
}

View File

@@ -1,4 +1,3 @@
using System;
using System.IO;
using System.Linq;
using SharpCompress.IO;
@@ -41,7 +40,7 @@ internal class SevenZipFilePart : FilePart
{
if (!Header.HasStream)
{
throw new InvalidOperationException("File does not have a stream.");
throw new InvalidFormatException("File does not have a stream.");
}
var folderStream = _database.GetFolderStream(_stream, Folder!, _database.PasswordProvider);
@@ -86,7 +85,7 @@ internal class SevenZipFilePart : FilePart
K_LZMA or K_LZMA2 => CompressionType.LZMA,
K_PPMD => CompressionType.PPMd,
K_B_ZIP2 => CompressionType.BZip2,
_ => throw new NotImplementedException(),
_ => throw new InvalidFormatException(),
};
}

View File

@@ -0,0 +1,48 @@
using System;
namespace SharpCompress.Common;
public class SharpCompressException : Exception
{
public SharpCompressException() { }
public SharpCompressException(string message)
: base(message) { }
public SharpCompressException(string message, Exception inner)
: base(message, inner) { }
}
public class ArchiveException(string message) : SharpCompressException(message);
public class IncompleteArchiveException(string message) : ArchiveException(message);
public class CryptographicException(string message) : SharpCompressException(message);
public class ReaderCancelledException(string message) : SharpCompressException(message);
public class ExtractionException : SharpCompressException
{
public ExtractionException() { }
public ExtractionException(string message)
: base(message) { }
public ExtractionException(string message, Exception inner)
: base(message, inner) { }
}
public class MultipartStreamRequiredException(string message) : ExtractionException(message);
public class MultiVolumeExtractionException(string message) : ExtractionException(message);
public class InvalidFormatException : ExtractionException
{
public InvalidFormatException() { }
public InvalidFormatException(string message)
: base(message) { }
public InvalidFormatException(string message, Exception inner)
: base(message, inner) { }
}

View File

@@ -216,7 +216,7 @@ internal sealed class TarHeader
if (buffer.Length != 0 && buffer.Length < BLOCK_SIZE)
{
throw new InvalidOperationException("Buffer is invalid size");
throw new InvalidFormatException("Buffer is invalid size");
}
return buffer;
}

View File

@@ -1,5 +1,4 @@
using System.IO;
using System.Net.Sockets;
using SharpCompress.Common.Zip.Headers;
using SharpCompress.Compressors.Deflate;
using SharpCompress.IO;

View File

@@ -62,8 +62,8 @@
//
// -----------------------------------------------------------------------
using System;
using System.IO;
using SharpCompress.Common;
namespace SharpCompress.Compressors.Deflate;
@@ -177,7 +177,7 @@ public enum CompressionStrategy
/// <summary>
/// A general purpose exception class for exceptions in the Zlib library.
/// </summary>
public class ZlibException : Exception
public class ZlibException : SharpCompressException
{
/// <summary>
/// The ZlibException class captures exception information generated

View File

@@ -8,6 +8,7 @@ using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.CompilerServices;
using SharpCompress.Common;
using SharpCompress.Common.Zip;
namespace SharpCompress.Compressors.Deflate64;
@@ -151,7 +152,7 @@ public sealed class Deflate64Stream : Stream
{
// The stream is either malicious or poorly implemented and returned a number of
// bytes larger than the buffer supplied to it.
throw new InvalidDataException("Deflate64: invalid data");
throw new InvalidFormatException("Deflate64: invalid data");
}
_inflater.SetInput(_buffer, 0, bytes);

View File

@@ -4,7 +4,7 @@
using System;
using System.Diagnostics;
using System.IO;
using SharpCompress.Common;
namespace SharpCompress.Compressors.Deflate64;
@@ -192,7 +192,7 @@ internal sealed class HuffmanTree
var increment = 1 << len;
if (start >= increment)
{
throw new InvalidDataException("Deflate64: invalid Huffman data");
throw new InvalidFormatException("Deflate64: invalid Huffman data");
}
// Note the bits in the table are reverted.
@@ -234,7 +234,7 @@ internal sealed class HuffmanTree
if (value > 0)
{
// prevent an IndexOutOfRangeException from array[index]
throw new InvalidDataException("Deflate64: invalid Huffman data");
throw new InvalidFormatException("Deflate64: invalid Huffman data");
}
Debug.Assert(
@@ -307,7 +307,7 @@ internal sealed class HuffmanTree
// huffman code lengths must be at least 1 bit long
if (codeLength <= 0)
{
throw new InvalidDataException("Deflate64: invalid Huffman data");
throw new InvalidFormatException("Deflate64: invalid Huffman data");
}
//

View File

@@ -30,7 +30,7 @@
using System;
using System.Diagnostics;
using System.IO;
using SharpCompress.Compressors.Deflate;
namespace SharpCompress.Compressors.Deflate64;
@@ -385,7 +385,7 @@ internal sealed class InflaterManaged
}
else
{
throw new InvalidDataException("Deflate64: unknown block type");
throw new ZlibException("Deflate64: unknown block type");
}
}
@@ -411,7 +411,7 @@ internal sealed class InflaterManaged
}
else
{
throw new InvalidDataException("Deflate64: unknown block type");
throw new ZlibException("Deflate64: unknown block type");
}
//
@@ -473,7 +473,7 @@ internal sealed class InflaterManaged
// make sure complement matches
if ((ushort)_blockLength != (ushort)(~blockLengthComplement))
{
throw new InvalidDataException("Deflate64: invalid block length");
throw new ZlibException("Deflate64: invalid block length");
}
}
@@ -507,7 +507,7 @@ internal sealed class InflaterManaged
default:
Debug. /*Fail*/
Assert(false, "check why we are here!");
throw new InvalidDataException("Deflate64: unknown state");
throw new ZlibException("Deflate64: unknown state");
}
}
}
@@ -569,7 +569,7 @@ internal sealed class InflaterManaged
{
if (symbol < 0 || symbol >= S_EXTRA_LENGTH_BITS.Length)
{
throw new InvalidDataException("Deflate64: invalid data");
throw new ZlibException("Deflate64: invalid data");
}
_extraBits = S_EXTRA_LENGTH_BITS[symbol];
Debug.Assert(_extraBits != 0, "We handle other cases separately!");
@@ -591,7 +591,7 @@ internal sealed class InflaterManaged
if (_length < 0 || _length >= S_LENGTH_BASE.Length)
{
throw new InvalidDataException("Deflate64: invalid data");
throw new ZlibException("Deflate64: invalid data");
}
_length = S_LENGTH_BASE[_length] + bits;
}
@@ -649,7 +649,7 @@ internal sealed class InflaterManaged
default:
Debug. /*Fail*/
Assert(false, "check why we are here!");
throw new InvalidDataException("Deflate64: unknown state");
throw new ZlibException("Deflate64: unknown state");
}
}
@@ -781,7 +781,7 @@ internal sealed class InflaterManaged
if (_loopCounter == 0)
{
// can't have "prev code" on first code
throw new InvalidDataException();
throw new ZlibException();
}
var previousCode = _codeList[_loopCounter - 1];
@@ -789,7 +789,7 @@ internal sealed class InflaterManaged
if (_loopCounter + repeatCount > _codeArraySize)
{
throw new InvalidDataException();
throw new ZlibException();
}
for (var j = 0; j < repeatCount; j++)
@@ -809,7 +809,7 @@ internal sealed class InflaterManaged
if (_loopCounter + repeatCount > _codeArraySize)
{
throw new InvalidDataException();
throw new ZlibException();
}
for (var j = 0; j < repeatCount; j++)
@@ -830,7 +830,7 @@ internal sealed class InflaterManaged
if (_loopCounter + repeatCount > _codeArraySize)
{
throw new InvalidDataException();
throw new ZlibException();
}
for (var j = 0; j < repeatCount; j++)
@@ -846,7 +846,7 @@ internal sealed class InflaterManaged
default:
Debug. /*Fail*/
Assert(false, "check why we are here!");
throw new InvalidDataException("Deflate64: unknown state");
throw new ZlibException("Deflate64: unknown state");
}
var literalTreeCodeLength = new byte[HuffmanTree.MAX_LITERAL_TREE_ELEMENTS];
@@ -865,7 +865,7 @@ internal sealed class InflaterManaged
// Make sure there is an end-of-block code, otherwise how could we ever end?
if (literalTreeCodeLength[HuffmanTree.END_OF_BLOCK_CODE] == 0)
{
throw new InvalidDataException();
throw new ZlibException();
}
_literalLengthTree = new HuffmanTree(literalTreeCodeLength);

View File

@@ -5,8 +5,8 @@
*/
using System;
using System.IO;
using System.Runtime.CompilerServices;
using SharpCompress.Common;
namespace SharpCompress.Compressors.Filters;
@@ -244,7 +244,7 @@ public sealed class BranchExecFilter
long size = data.Length;
if (size < 16)
{
throw new InvalidDataException("Unexpected data size");
throw new InvalidFormatException("Unexpected data size");
}
size -= 16;

View File

@@ -1,4 +1,3 @@
using System;
using System.IO;
namespace SharpCompress.Compressors.Filters

View File

@@ -1,5 +1,6 @@
using System;
using System.IO;
using SharpCompress.Common;
using SharpCompress.Common.SevenZip;
using SharpCompress.Compressors.LZMA.Utilites;
using SharpCompress.IO;
@@ -46,7 +47,7 @@ internal static class DecoderStreamHelper
}
}
throw new InvalidOperationException("Could not link output stream to coder.");
throw new InvalidFormatException("Could not link output stream to coder.");
}
private static void FindPrimaryOutStreamIndex(
@@ -75,7 +76,7 @@ internal static class DecoderStreamHelper
{
if (foundPrimaryOutStream)
{
throw new NotSupportedException("Multiple output streams.");
throw new InvalidFormatException("Multiple output streams.");
}
foundPrimaryOutStream = true;
@@ -87,7 +88,7 @@ internal static class DecoderStreamHelper
if (!foundPrimaryOutStream)
{
throw new NotSupportedException("No output stream.");
throw new InvalidFormatException("No output stream.");
}
}

View File

@@ -1,6 +1,7 @@
using System;
using System.Buffers.Binary;
using System.IO;
using SharpCompress.Common;
using SharpCompress.Crypto;
using SharpCompress.IO;
@@ -32,7 +33,7 @@ public sealed class LZipStream : Stream
var dSize = ValidateAndReadSize(stream);
if (dSize == 0)
{
throw new IOException("Not an LZip stream");
throw new InvalidFormatException("Not an LZip stream");
}
var properties = GetProperties(dSize);
_stream = new LzmaStream(properties, stream);
@@ -167,11 +168,6 @@ public sealed class LZipStream : Stream
/// </summary>
public static int ValidateAndReadSize(Stream stream)
{
if (stream is null)
{
throw new ArgumentNullException(nameof(stream));
}
// Read the header
Span<byte> header = stackalloc byte[6];
var n = stream.Read(header);
@@ -198,33 +194,25 @@ public sealed class LZipStream : Stream
return (1 << basePower) - (subtractionNumerator * (1 << (basePower - 4)));
}
private static readonly byte[] headerBytes = new byte[6]
{
private static readonly byte[] headerBytes =
[
(byte)'L',
(byte)'Z',
(byte)'I',
(byte)'P',
1,
113,
};
public static void WriteHeaderSize(Stream stream)
{
if (stream is null)
{
throw new ArgumentNullException(nameof(stream));
}
];
public static void WriteHeaderSize(Stream stream) =>
// hard coding the dictionary size encoding
stream.Write(headerBytes, 0, 6);
}
/// <summary>
/// Creates a byte array to communicate the parameters and dictionary size to LzmaStream.
/// </summary>
private static byte[] GetProperties(int dictionarySize) =>
new byte[]
{
[
// Parameters as per http://www.nongnu.org/lzip/manual/lzip_manual.html#Stream-format
// but encoded as a single byte in the format LzmaStream expects.
// literal_context_bits = 3
@@ -236,5 +224,5 @@ public sealed class LZipStream : Stream
(byte)((dictionarySize >> 8) & 0xff),
(byte)((dictionarySize >> 16) & 0xff),
(byte)((dictionarySize >> 24) & 0xff),
};
];
}

View File

@@ -2,6 +2,7 @@
using System;
using System.IO;
using SharpCompress.Common;
using SharpCompress.Compressors.LZMA.LZ;
using SharpCompress.Compressors.LZMA.RangeCoder;
@@ -1611,7 +1612,7 @@ internal class Encoder : ICoder, ISetCoderProperties, IWriteCoderProperties
{
if (_nowPos64 > 0)
{
throw new InvalidOperationException();
throw new InvalidFormatException();
}
_trainSize = (uint)trainStream.Length;
if (_trainSize > 0)

View File

@@ -1,7 +1,6 @@
using System;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using SharpCompress.Common;
using SharpCompress.Common.Rar.Headers;
@@ -93,7 +92,7 @@ internal class RarBLAKE2spStream : RarStream
{
this.readStream = readStream;
disableCRCCheck = fileHeader.IsEncrypted;
_hash = fileHeader.FileCrc;
_hash = fileHeader.FileCrc.NotNull();
_blake2sp = new BLAKE2SP();
ResetCrc();
}

View File

@@ -3,7 +3,6 @@
using System;
using static SharpCompress.Compressors.Rar.UnpackV2017.PackDef;
using static SharpCompress.Compressors.Rar.UnpackV2017.UnpackGlobal;
using int64 = System.Int64;
#if !Rar2017_64bit
using size_t = System.UInt32;
#else

View File

@@ -1,9 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SharpCompress.Compressors.Shrink
{
internal class BitStream

View File

@@ -1,9 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SharpCompress.Compressors.Shrink;

View File

@@ -5,6 +5,7 @@
*/
using System.IO;
using SharpCompress.Common;
using SharpCompress.Compressors.Filters;
namespace SharpCompress.Compressors.Xz.Filters;
@@ -25,19 +26,19 @@ public class ArmFilter : BlockFilter
{
if (properties.Length != 0 && properties.Length != 4)
{
throw new InvalidDataException("ARM properties unexpected length");
throw new InvalidFormatException("ARM properties unexpected length");
}
if (properties.Length == 4)
{
// Even XZ doesn't support it.
throw new InvalidDataException("ARM properties offset is not supported");
throw new InvalidFormatException("ARM properties offset is not supported");
//_offset = BitConverter.ToUInt32(properties, 0);
//
//if (_offset % (UInt32)BranchExec.Alignment.ARCH_ARM_ALIGNMENT != 0)
//{
// throw new InvalidDataException("Filter offset does not match alignment");
// throw new InvalidFormatException("Filter offset does not match alignment");
//}
}
}

View File

@@ -5,6 +5,7 @@
*/
using System.IO;
using SharpCompress.Common;
using SharpCompress.Compressors.Filters;
namespace SharpCompress.Compressors.Xz.Filters;
@@ -25,19 +26,19 @@ public class ArmThumbFilter : BlockFilter
{
if (properties.Length != 0 && properties.Length != 4)
{
throw new InvalidDataException("ARM Thumb properties unexpected length");
throw new InvalidFormatException("ARM Thumb properties unexpected length");
}
if (properties.Length == 4)
{
// Even XZ doesn't support it.
throw new InvalidDataException("ARM Thumb properties offset is not supported");
throw new InvalidFormatException("ARM Thumb properties offset is not supported");
//_offset = BitConverter.ToUInt32(properties, 0);
//
//if (_offset % (UInt32)BranchExec.Alignment.ARCH_ARMTHUMB_ALIGNMENT != 0)
//{
// throw new InvalidDataException("Filter offset does not match alignment");
// throw new InvalidFormatException("Filter offset does not match alignment");
//}
}
}

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using SharpCompress.Common;
namespace SharpCompress.Compressors.Xz.Filters;
@@ -49,7 +50,7 @@ public abstract class BlockFilter : ReadOnlyStream
var sizeOfProperties = reader.ReadXZInteger();
if (sizeOfProperties > int.MaxValue)
{
throw new InvalidDataException("Block filter information too large");
throw new InvalidFormatException("Block filter information too large");
}
var properties = reader.ReadBytes((int)sizeOfProperties);

View File

@@ -5,6 +5,7 @@
*/
using System.IO;
using SharpCompress.Common;
using SharpCompress.Compressors.Filters;
namespace SharpCompress.Compressors.Xz.Filters;
@@ -25,19 +26,19 @@ public class IA64Filter : BlockFilter
{
if (properties.Length != 0 && properties.Length != 4)
{
throw new InvalidDataException("IA64 properties unexpected length");
throw new InvalidFormatException("IA64 properties unexpected length");
}
if (properties.Length == 4)
{
// Even XZ doesn't support it.
throw new InvalidDataException("IA64 properties offset is not supported");
throw new InvalidFormatException("IA64 properties offset is not supported");
//_offset = BitConverter.ToUInt32(properties, 0);
//
//if (_offset % (UInt32)BranchExec.Alignment.ARCH_IA64_ALIGNMENT != 0)
//{
// throw new InvalidDataException("Filter offset does not match alignment");
// throw new InvalidFormatException("Filter offset does not match alignment");
//}
}
}

View File

@@ -1,5 +1,6 @@
using System;
using System.IO;
using SharpCompress.Common;
using SharpCompress.Compressors.LZMA;
namespace SharpCompress.Compressors.Xz.Filters;
@@ -35,14 +36,14 @@ public class Lzma2Filter : BlockFilter
{
if (properties.Length != 1)
{
throw new InvalidDataException("LZMA properties unexpected length");
throw new InvalidFormatException("LZMA properties unexpected length");
}
_dictionarySize = (byte)(properties[0] & 0x3F);
var reserved = properties[0] & 0xC0;
if (reserved != 0)
{
throw new InvalidDataException("Reserved bits used in LZMA properties");
throw new InvalidFormatException("Reserved bits used in LZMA properties");
}
}

View File

@@ -5,6 +5,7 @@
*/
using System.IO;
using SharpCompress.Common;
using SharpCompress.Compressors.Filters;
namespace SharpCompress.Compressors.Xz.Filters;
@@ -25,19 +26,19 @@ public class PowerPCFilter : BlockFilter
{
if (properties.Length != 0 && properties.Length != 4)
{
throw new InvalidDataException("PPC properties unexpected length");
throw new InvalidFormatException("PPC properties unexpected length");
}
if (properties.Length == 4)
{
// Even XZ doesn't support it.
throw new InvalidDataException("PPC properties offset is not supported");
throw new InvalidFormatException("PPC properties offset is not supported");
//_offset = BitConverter.ToUInt32(properties, 0);
//
//if (_offset % (UInt32)BranchExec.Alignment.ARCH_PowerPC_ALIGNMENT != 0)
//{
// throw new InvalidDataException("Filter offset does not match alignment");
// throw new InvalidFormatException("Filter offset does not match alignment");
//}
}
}

View File

@@ -5,6 +5,7 @@
*/
using System.IO;
using SharpCompress.Common;
using SharpCompress.Compressors.Filters;
namespace SharpCompress.Compressors.Xz.Filters;
@@ -25,19 +26,19 @@ public class SparcFilter : BlockFilter
{
if (properties.Length != 0 && properties.Length != 4)
{
throw new InvalidDataException("SPARC properties unexpected length");
throw new InvalidFormatException("SPARC properties unexpected length");
}
if (properties.Length == 4)
{
// Even XZ doesn't support it.
throw new InvalidDataException("SPARC properties offset is not supported");
throw new InvalidFormatException("SPARC properties offset is not supported");
//_offset = BitConverter.ToUInt32(properties, 0);
//
//if (_offset % (UInt32)BranchExec.Alignment.ARCH_SPARC_ALIGNMENT != 0)
//{
// throw new InvalidDataException("Filter offset does not match alignment");
// throw new InvalidFormatException("Filter offset does not match alignment");
//}
}
}

View File

@@ -5,6 +5,7 @@
*/
using System.IO;
using SharpCompress.Common;
using SharpCompress.Compressors.Filters;
namespace SharpCompress.Compressors.Xz.Filters;
@@ -27,19 +28,19 @@ public class X86Filter : BlockFilter
{
if (properties.Length != 0 && properties.Length != 4)
{
throw new InvalidDataException("X86 properties unexpected length");
throw new InvalidFormatException("X86 properties unexpected length");
}
if (properties.Length == 4)
{
// Even XZ doesn't support it.
throw new InvalidDataException("X86 properties offset is not supported");
throw new InvalidFormatException("X86 properties offset is not supported");
//_offset = BitConverter.ToUInt32(properties, 0);
//
//if (_offset % (UInt32)BranchExec.Alignment.ARCH_x86_ALIGNMENT != 0)
//{
// throw new InvalidDataException("Filter offset does not match alignment");
// throw new InvalidFormatException("Filter offset does not match alignment");
//}
}
}

View File

@@ -1,5 +1,6 @@
using System;
using System.IO;
using SharpCompress.Common;
namespace SharpCompress.Compressors.Xz;
@@ -25,13 +26,13 @@ internal static class MultiByteIntegers
{
if (++i >= MaxBytes)
{
throw new InvalidDataException();
throw new InvalidFormatException();
}
LastByte = reader.ReadByte();
if (LastByte == 0)
{
throw new InvalidDataException();
throw new InvalidFormatException();
}
Output |= ((ulong)(LastByte & 0x7F)) << (i * 7);

View File

@@ -4,6 +4,7 @@ using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using SharpCompress.Common;
using SharpCompress.Compressors.Xz.Filters;
namespace SharpCompress.Compressors.Xz;
@@ -80,7 +81,7 @@ public sealed class XZBlock : XZReadOnlyStream
BaseStream.Read(paddingBytes, 0, paddingBytes.Length);
if (paddingBytes.Any(b => b != 0))
{
throw new InvalidDataException("Padding bytes were non-null");
throw new InvalidFormatException("Padding bytes were non-null");
}
}
_paddingSkipped = true;
@@ -145,7 +146,7 @@ public sealed class XZBlock : XZReadOnlyStream
var calcCrc = Crc32.Compute(blockHeaderWithoutCrc);
if (crc != calcCrc)
{
throw new InvalidDataException("Block header corrupt");
throw new InvalidFormatException("Block header corrupt");
}
return blockHeaderWithoutCrc;
@@ -159,7 +160,7 @@ public sealed class XZBlock : XZReadOnlyStream
if (reserved != 0)
{
throw new InvalidDataException(
throw new InvalidFormatException(
"Reserved bytes used, perhaps an unknown XZ implementation"
);
}
@@ -189,7 +190,7 @@ public sealed class XZBlock : XZReadOnlyStream
|| (i + 1 < _numFilters && !filter.AllowAsNonLast)
)
{
throw new InvalidDataException("Block Filters in bad order");
throw new InvalidFormatException("Block Filters in bad order");
}
if (filter.ChangesDataSize && i + 1 < _numFilters)
@@ -202,7 +203,7 @@ public sealed class XZBlock : XZReadOnlyStream
}
if (nonLastSizeChangers > 2)
{
throw new InvalidDataException(
throw new InvalidFormatException(
"More than two non-last block filters cannot change stream size"
);
}
@@ -212,7 +213,7 @@ public sealed class XZBlock : XZReadOnlyStream
var blockHeaderPadding = reader.ReadBytes(blockHeaderPaddingSize);
if (!blockHeaderPadding.All(b => b == 0))
{
throw new InvalidDataException("Block header contains unknown fields");
throw new InvalidFormatException("Block header contains unknown fields");
}
}
}

View File

@@ -1,6 +1,7 @@
using System;
using System.IO;
using System.Text;
using SharpCompress.Common;
using SharpCompress.IO;
namespace SharpCompress.Compressors.Xz;
@@ -35,7 +36,7 @@ public class XZFooter
var myCrc = Crc32.Compute(footerBytes);
if (crc != myCrc)
{
throw new InvalidDataException("Footer corrupt");
throw new InvalidFormatException("Footer corrupt");
}
using (var stream = new MemoryStream(footerBytes))
@@ -47,7 +48,7 @@ public class XZFooter
var magBy = _reader.ReadBytes(2);
if (!magBy.AsSpan().SequenceEqual(_magicBytes))
{
throw new InvalidDataException("Magic footer missing");
throw new InvalidFormatException("Magic footer missing");
}
}
}

View File

@@ -1,6 +1,7 @@
using System.IO;
using System.Linq;
using System.Text;
using SharpCompress.Common;
using SharpCompress.IO;
namespace SharpCompress.Compressors.Xz;
@@ -37,14 +38,14 @@ public class XZHeader
var calcCrc = Crc32.Compute(streamFlags);
if (crc != calcCrc)
{
throw new InvalidDataException("Stream header corrupt");
throw new InvalidFormatException("Stream header corrupt");
}
BlockCheckType = (CheckType)(streamFlags[1] & 0x0F);
var futureUse = (byte)(streamFlags[1] & 0xF0);
if (futureUse != 0 || streamFlags[0] != 0)
{
throw new InvalidDataException("Unknown XZ Stream Version");
throw new InvalidFormatException("Unknown XZ Stream Version");
}
}
@@ -52,7 +53,7 @@ public class XZHeader
{
if (!header.SequenceEqual(MagicHeader))
{
throw new InvalidDataException("Invalid XZ Stream");
throw new InvalidFormatException("Invalid XZ Stream");
}
}
}

View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using SharpCompress.Common;
using SharpCompress.IO;
namespace SharpCompress.Compressors.Xz;
@@ -59,7 +60,7 @@ public class XZIndex
var marker = _reader.ReadByte();
if (marker != 0)
{
throw new InvalidDataException("Not an index block");
throw new InvalidFormatException("Not an index block");
}
}
@@ -71,7 +72,7 @@ public class XZIndex
var paddingBytes = _reader.ReadBytes(4 - bytes);
if (paddingBytes.Any(b => b != 0))
{
throw new InvalidDataException("Padding bytes were non-null");
throw new InvalidFormatException("Padding bytes were non-null");
}
}
}

View File

@@ -1,4 +1,5 @@
using System.IO;
using SharpCompress.Common;
namespace SharpCompress.Compressors.Xz;
@@ -9,7 +10,7 @@ public abstract class XZReadOnlyStream : ReadOnlyStream
BaseStream = stream;
if (!BaseStream.CanRead)
{
throw new InvalidDataException("Must be able to read from stream");
throw new InvalidFormatException("Must be able to read from stream");
}
}
}

View File

@@ -2,6 +2,7 @@
using System;
using System.IO;
using SharpCompress.Common;
namespace SharpCompress.Compressors.Xz;
@@ -30,7 +31,7 @@ public sealed class XZStream : XZReadOnlyStream
case CheckType.SHA256:
break;
default:
throw new NotSupportedException("Check Type unknown to this version of decoder.");
throw new InvalidFormatException("Check Type unknown to this version of decoder.");
}
}

View File

@@ -1,6 +1,5 @@
using System;
using System.IO;
using System.Runtime.CompilerServices;
namespace SharpCompress.IO;

View File

@@ -1,6 +1,7 @@
using System;
using System.Buffers.Binary;
using System.IO;
using SharpCompress.Common;
namespace SharpCompress.IO;
@@ -44,7 +45,7 @@ internal class MarkingBinaryReader : BinaryReader
var bytes = base.ReadBytes(count);
if (bytes.Length != count)
{
throw new EndOfStreamException(
throw new InvalidFormatException(
string.Format(
"Could not read the requested amount of bytes. End of stream reached. Requested: {0} Read: {1}",
count,

View File

@@ -8,9 +8,11 @@ namespace SharpCompress.Helpers;
internal static class NotNullExtensions
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IEnumerable<T> Empty<T>(this IEnumerable<T>? source) =>
source ?? Enumerable.Empty<T>();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IEnumerable<T> Empty<T>(this T? source)
{
if (source is null)
@@ -21,6 +23,7 @@ internal static class NotNullExtensions
}
#if NETFRAMEWORK || NETSTANDARD
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T NotNull<T>(this T? obj, string? message = null)
where T : class
{
@@ -31,6 +34,7 @@ internal static class NotNullExtensions
return obj;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T NotNull<T>(this T? obj, string? message = null)
where T : struct
{
@@ -42,6 +46,7 @@ internal static class NotNullExtensions
}
#else
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T NotNull<T>(
[NotNull] this T? obj,
[CallerArgumentExpression(nameof(obj))] string? paramName = null
@@ -52,6 +57,7 @@ internal static class NotNullExtensions
return obj;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T NotNull<T>(
[NotNull] this T? obj,
[CallerArgumentExpression(nameof(obj))] string? paramName = null

View File

@@ -192,7 +192,8 @@ public abstract class AbstractReader<TEntry, TVolume> : IReader, IReaderExtracti
/// <summary>
/// Retains a reference to the entry stream, so we can check whether it completed later.
/// </summary>
protected EntryStream CreateEntryStream(Stream decompressed) => new(this, decompressed);
protected EntryStream CreateEntryStream(Stream? decompressed) =>
new(this, decompressed.NotNull());
protected virtual EntryStream GetEntryStream() =>
CreateEntryStream(Entry.Parts.First().GetCompressedStream());

View File

@@ -9,7 +9,7 @@ internal class NonSeekableStreamFilePart : RarFilePart
internal NonSeekableStreamFilePart(MarkHeader mh, FileHeader fh, int index = 0)
: base(mh, fh, index) { }
internal override Stream GetCompressedStream() => FileHeader.PackedStream;
internal override Stream? GetCompressedStream() => FileHeader.PackedStream;
internal override Stream? GetRawStream() => FileHeader.PackedStream;

View File

@@ -93,7 +93,7 @@ public abstract class RarReader : AbstractReader<RarReaderEntry, RarVolume>
return CreateEntryStream(new RarCrcStream(UnpackV1.Value, Entry.FileHeader, stream));
}
if (Entry.FileHeader.FileCrc.Length > 5)
if (Entry.FileHeader.FileCrc?.Length > 5)
{
return CreateEntryStream(
new RarBLAKE2spStream(UnpackV2017.Value, Entry.FileHeader, stream)

View File

@@ -1,6 +1,6 @@
using System;
using System.IO;
using System.Linq;
using SharpCompress.Common;
using SharpCompress.IO;
namespace SharpCompress.Readers;
@@ -29,7 +29,7 @@ public static class ReaderFactory
}
}
throw new InvalidOperationException(
throw new InvalidFormatException(
"Cannot determine compressed stream type. Supported Reader Formats: Arc, Zip, GZip, BZip2, Tar, Rar, LZip, XZ"
);
}

View File

@@ -307,9 +307,9 @@
"net8.0": {
"Microsoft.NET.ILLink.Tasks": {
"type": "Direct",
"requested": "[8.0.12, )",
"resolved": "8.0.12",
"contentHash": "FV4HnQ3JI15PHnJ5PGTbz+rYvrih42oLi/7UMIshNwCwUZhTq13UzrggtXk4ygrcMcN+4jsS6hhshx2p/Zd0ig=="
"requested": "[8.0.15, )",
"resolved": "8.0.15",
"contentHash": "s4eXlcRGyHeCgFUGQnhq0e/SCHBPp0jOHgMqZg3fQ2OCHJSm1aOUhI6RFWuVIcEb9ig2WgI2kWukk8wu72EbUQ=="
},
"Microsoft.SourceLink.GitHub": {
"type": "Direct",

View File

@@ -1,4 +1,3 @@
using System;
using System.IO;
using System.Linq;
using SharpCompress.Archives;
@@ -63,7 +62,7 @@ public class GZipArchiveTests : ArchiveTests
var jpg = Path.Combine(ORIGINAL_FILES_PATH, "jpg", "test.jpg");
using Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz"));
using var archive = GZipArchive.Open(stream);
Assert.Throws<InvalidOperationException>(() => archive.AddEntry("jpg\\test.jpg", jpg));
Assert.Throws<InvalidFormatException>(() => archive.AddEntry("jpg\\test.jpg", jpg));
archive.SaveTo(Path.Combine(SCRATCH_FILES_PATH, "Tar.tar.gz"));
}

View File

@@ -3,7 +3,7 @@
* <Contribution by Louis-Michel Bergeron, on behalf of aDolus Technolog Inc., 2022>
*/
using System.IO;
using SharpCompress.Common;
using SharpCompress.Compressors.Xz.Filters;
using Xunit;
@@ -66,23 +66,23 @@ public class BcjTests : XzTestsBase
[InlineData(new byte[] { 0, 0, 0, 0, 0 })]
public void OnlyAcceptsOneByte(byte[] bytes)
{
InvalidDataException ex;
ex = Assert.Throws<InvalidDataException>(() => _armFilter.Init(bytes));
InvalidFormatException ex;
ex = Assert.Throws<InvalidFormatException>(() => _armFilter.Init(bytes));
Assert.Equal("ARM properties unexpected length", ex.Message);
ex = Assert.Throws<InvalidDataException>(() => _armtFilter.Init(bytes));
ex = Assert.Throws<InvalidFormatException>(() => _armtFilter.Init(bytes));
Assert.Equal("ARM Thumb properties unexpected length", ex.Message);
ex = Assert.Throws<InvalidDataException>(() => _ia64Filter.Init(bytes));
ex = Assert.Throws<InvalidFormatException>(() => _ia64Filter.Init(bytes));
Assert.Equal("IA64 properties unexpected length", ex.Message);
ex = Assert.Throws<InvalidDataException>(() => _ppcFilter.Init(bytes));
ex = Assert.Throws<InvalidFormatException>(() => _ppcFilter.Init(bytes));
Assert.Equal("PPC properties unexpected length", ex.Message);
ex = Assert.Throws<InvalidDataException>(() => _sparcFilter.Init(bytes));
ex = Assert.Throws<InvalidFormatException>(() => _sparcFilter.Init(bytes));
Assert.Equal("SPARC properties unexpected length", ex.Message);
ex = Assert.Throws<InvalidDataException>(() => _x86Filter.Init(bytes));
ex = Assert.Throws<InvalidFormatException>(() => _x86Filter.Init(bytes));
Assert.Equal("X86 properties unexpected length", ex.Message);
}
}

View File

@@ -1,5 +1,5 @@
using System;
using System.IO;
using SharpCompress.Common;
using SharpCompress.Compressors.Xz.Filters;
using Xunit;
@@ -52,14 +52,14 @@ public class Lzma2Tests : XzTestsBase
[InlineData(new byte[] { 0, 0 })]
public void OnlyAcceptsOneByte(byte[] bytes)
{
var ex = Assert.Throws<InvalidDataException>(() => _filter.Init(bytes));
var ex = Assert.Throws<InvalidFormatException>(() => _filter.Init(bytes));
Assert.Equal("LZMA properties unexpected length", ex.Message);
}
[Fact]
public void ReservedBytesThrow()
{
var ex = Assert.Throws<InvalidDataException>(() => _filter.Init([0xC0]));
var ex = Assert.Throws<InvalidFormatException>(() => _filter.Init([0xC0]));
Assert.Equal("Reserved bits used in LZMA properties", ex.Message);
}
}

View File

@@ -1,5 +1,6 @@
using System.IO;
using System.Text;
using SharpCompress.Common;
using SharpCompress.Compressors.Xz;
using Xunit;
@@ -43,7 +44,7 @@ public class XzBlockTests : XzTestsBase
using Stream badCrcStream = new MemoryStream(bytes);
Rewind(badCrcStream);
var xzBlock = new XZBlock(badCrcStream, CheckType.CRC64, 8);
var ex = Assert.Throws<InvalidDataException>(() =>
var ex = Assert.Throws<InvalidFormatException>(() =>
{
ReadBytes(xzBlock, 1);
});

View File

@@ -1,4 +1,5 @@
using System.IO;
using SharpCompress.Common;
using SharpCompress.Compressors.Xz;
using Xunit;
@@ -14,7 +15,7 @@ public class XzHeaderTests : XzTestsBase
using Stream badMagicNumberStream = new MemoryStream(bytes);
var br = new BinaryReader(badMagicNumberStream);
var header = new XZHeader(br);
var ex = Assert.Throws<InvalidDataException>(() =>
var ex = Assert.Throws<InvalidFormatException>(() =>
{
header.Process();
});
@@ -29,7 +30,7 @@ public class XzHeaderTests : XzTestsBase
using Stream badCrcStream = new MemoryStream(bytes);
var br = new BinaryReader(badCrcStream);
var header = new XZHeader(br);
var ex = Assert.Throws<InvalidDataException>(() =>
var ex = Assert.Throws<InvalidFormatException>(() =>
{
header.Process();
});
@@ -47,7 +48,7 @@ public class XzHeaderTests : XzTestsBase
using Stream badFlagStream = new MemoryStream(bytes);
var br = new BinaryReader(badFlagStream);
var header = new XZHeader(br);
var ex = Assert.Throws<InvalidDataException>(() =>
var ex = Assert.Throws<InvalidFormatException>(() =>
{
header.Process();
});

View File

@@ -1,4 +1,5 @@
using System.IO;
using SharpCompress.Common;
using SharpCompress.Compressors.Xz;
using Xunit;
@@ -27,7 +28,7 @@ public class XzIndexTests : XzTestsBase
using Stream badStream = new MemoryStream([1, 2, 3, 4, 5]);
var br = new BinaryReader(badStream);
var index = new XZIndex(br, false);
Assert.Throws<InvalidDataException>(() => index.Process());
Assert.Throws<InvalidFormatException>(() => index.Process());
}
[Fact]