Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SharpCompressException and use it or children in most places #834

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/SharpCompress/Archives/AbstractArchive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private static Stream CheckStreams(Stream stream)
{
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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/SharpCompress/Archives/AbstractWritableArchive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ bool closeStream
{
if (!source.CanRead || !source.CanSeek)
{
throw new ArgumentException(
throw new ArchiveException(
"Streams must be readable and seekable to use the Writing Archive API"
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/SharpCompress/Archives/GZip/GZipArchive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ bool closeStream
{
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);
}
Expand All @@ -176,7 +176,7 @@ IEnumerable<GZipArchiveEntry> newEntries
{
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))
Expand Down
2 changes: 1 addition & 1 deletion src/SharpCompress/Archives/GZip/GZipArchiveEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public virtual Stream OpenEntryStream()
{
part.GetRawStream().Position = part.EntryStartPosition;
}
return Parts.Single().GetCompressedStream();
return Parts.Single().GetCompressedStream().NotNull();
}

#region IArchiveEntry Members
Expand Down
2 changes: 1 addition & 1 deletion src/SharpCompress/Archives/Rar/RarArchiveEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public override long Crc
{
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
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/SharpCompress/Archives/Tar/TarArchiveEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/SharpCompress/Archives/Zip/ZipArchiveEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 0 additions & 9 deletions src/SharpCompress/Common/ArchiveException.cs

This file was deleted.

9 changes: 0 additions & 9 deletions src/SharpCompress/Common/CryptographicException.cs

This file was deleted.

12 changes: 0 additions & 12 deletions src/SharpCompress/Common/ExtractionException.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/SharpCompress/Common/FilePart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
7 changes: 0 additions & 7 deletions src/SharpCompress/Common/IncompleteArchiveException.cs

This file was deleted.

12 changes: 0 additions & 12 deletions src/SharpCompress/Common/InvalidFormatException.cs

This file was deleted.

12 changes: 0 additions & 12 deletions src/SharpCompress/Common/MultiVolumeExtractionException.cs

This file was deleted.

7 changes: 0 additions & 7 deletions src/SharpCompress/Common/MultipartStreamRequiredException.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/SharpCompress/Common/Rar/CryptKey5.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 0 additions & 3 deletions src/SharpCompress/Common/Rar/Headers/ArchiveCryptHeader.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
49 changes: 29 additions & 20 deletions src/SharpCompress/Common/Rar/Headers/FileHeader.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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) { }
Expand Down Expand Up @@ -319,6 +316,10 @@ private void ReadFromReaderV4(MarkingBinaryReader reader)

if (NewSubHeaderType.SUBHEAD_TYPE_RR.Equals(fileNameBytes))
{
if (SubData is null)
{
throw new InvalidFormatException();
}
RecoverySectors =
SubData[8]
+ (SubData[9] << 8)
Expand All @@ -340,12 +341,16 @@ private void ReadFromReaderV4(MarkingBinaryReader reader)
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);
Expand Down Expand Up @@ -377,7 +382,7 @@ int i
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);
}
Expand All @@ -390,7 +395,11 @@ int i
}

//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)
Expand All @@ -406,13 +415,13 @@ private static string ConvertPathV4(string path)
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;
Expand Down Expand Up @@ -441,22 +450,22 @@ internal byte[] FileCrc
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);
Expand Down
3 changes: 1 addition & 2 deletions src/SharpCompress/Common/Rar/Headers/RarHeader.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.IO;
using SharpCompress.IO;

namespace SharpCompress.Common.Rar.Headers;
Expand All @@ -21,7 +20,7 @@ ArchiveEncoding archiveEncoding
{
return new RarHeader(reader, isRar5, archiveEncoding);
}
catch (EndOfStreamException)
catch (InvalidFormatException)
{
return null;
}
Expand Down
13 changes: 8 additions & 5 deletions src/SharpCompress/Common/Rar/Headers/RarHeaderFactory.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using SharpCompress.Common.Rar;
using SharpCompress.IO;
using SharpCompress.Readers;

Expand Down Expand Up @@ -160,10 +158,15 @@ public IEnumerable<IRarHeader> ReadHeaders(Stream stream)
{
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)
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/SharpCompress/Common/Rar/RarEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion src/SharpCompress/Common/Rar/RarVolume.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ internal IEnumerable<RarFilePart> GetVolumeFileParts()
{
var part = CreateFilePart(lastMarkHeader!, fh);
var buffer = new byte[fh.CompressedSize];
part.GetCompressedStream().Read(buffer, 0, buffer.Length);
part.GetCompressedStream().NotNull().Read(buffer, 0, buffer.Length);
Comment = Encoding.UTF8.GetString(buffer, 0, buffer.Length - 1);
}
}
Expand Down
12 changes: 0 additions & 12 deletions src/SharpCompress/Common/ReaderCancelledException.cs

This file was deleted.

Loading