Add SharpCompressException and use it or children in most places

This commit is contained in:
Adam Hathcock
2024-04-23 14:47:40 +01:00
parent b203d165f5
commit 8cb621ebed
45 changed files with 180 additions and 191 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

@@ -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

@@ -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

@@ -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,9 +854,9 @@ internal class ArchiveReader
!= folder._unpackCrc
)
{
throw new InvalidOperationException(
"Decoded stream does not match expected CRC."
);
throw new InvalidFormatException(
"Decoded stream does not match expected CRC."
);
}
}
}

View File

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

View File

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

View File

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

View File

@@ -64,6 +64,7 @@
using System;
using System.IO;
using SharpCompress.Common;
namespace SharpCompress.Compressors.Deflate;
@@ -177,7 +178,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

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

View File

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

View File

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

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,34 +194,27 @@ public sealed class LZipStream : Stream
return (1 << basePower) - (subtractionNumerator * (1 << (basePower - 4)));
}
private static readonly byte[] headerBytes = new byte[6]
{
private static readonly byte[] headerBytes =
[
(byte)'L',
(byte)'Z',
(byte)'I',
(byte)'P',
1,
113
};
];
public static void WriteHeaderSize(Stream stream)
{
if (stream is null)
{
throw new ArgumentNullException(nameof(stream));
}
public static void WriteHeaderSize(Stream stream) =>
// hard coding the dictionary size encoding
stream.Write(headerBytes, 0, 6);
}
/// <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
[
// Parameters as per http://www.nongnu.org/lzip/manual/lzip_manual.html#Stream-format
// but encoded as a single byte in the format LzmaStream expects.
// literal_context_bits = 3
// literal_pos_state_bits = 0
@@ -236,5 +225,5 @@ public sealed class LZipStream : Stream
(byte)((dictionarySize >> 8) & 0xff),
(byte)((dictionarySize >> 16) & 0xff),
(byte)((dictionarySize >> 24) & 0xff)
};
];
}

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

@@ -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;
@@ -50,7 +51,7 @@ public abstract class BlockFilter : ReadOnlyStream
var sizeOfProperties = reader.ReadXZInteger();
if (sizeOfProperties > int.MaxValue)
{
throw new InvalidDataException("Block filter information too large");
throw new InvalidFormatException("Block filter information too large");
}
var properties = reader.ReadBytes((int)sizeOfProperties);

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;
@@ -33,7 +34,7 @@ public sealed class XZStream : XZReadOnlyStream
case CheckType.SHA256:
throw new NotImplementedException();
default:
throw new NotSupportedException("Check Type unknown to this version of decoder.");
throw new InvalidFormatException("Check Type unknown to this version of decoder.");
}
}

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

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

View File

@@ -4,6 +4,7 @@
*/
using System.IO;
using SharpCompress.Common;
using SharpCompress.Compressors.Xz.Filters;
using Xunit;
@@ -66,23 +67,23 @@ public class BcjTests : XzTestsBase
[InlineData(new byte[] { 0, 0, 0, 0, 0 })]
public void OnlyAcceptsOneByte(byte[] bytes)
{
InvalidDataException ex;
ex = Assert.Throws<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,6 @@
using System;
using System.IO;
using SharpCompress.Common;
using SharpCompress.Compressors.Xz.Filters;
using Xunit;
@@ -52,14 +53,14 @@ public class Lzma2Tests : XzTestsBase
[InlineData(new byte[] { 0, 0 })]
public void OnlyAcceptsOneByte(byte[] bytes)
{
var ex = Assert.Throws<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

@@ -43,7 +43,7 @@ public class XzBlockTests : XzTestsBase
using Stream badCrcStream = new MemoryStream(bytes);
Rewind(badCrcStream);
var xzBlock = new XZBlock(badCrcStream, CheckType.CRC64, 8);
var ex = Assert.Throws<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]