more nulls and a namespace folder fix

This commit is contained in:
Adam Hathcock
2026-02-13 15:01:12 +00:00
parent eb5c5faa99
commit 5263f43c29
19 changed files with 70 additions and 136 deletions

View File

@@ -6,7 +6,7 @@ using System.Threading;
using System.Threading.Tasks;
using SharpCompress.Common;
using SharpCompress.Common.SevenZip;
using SharpCompress.Compressors.LZMA.Utilites;
using SharpCompress.Compressors.LZMA.Utilities;
using SharpCompress.IO;
using SharpCompress.Readers;

View File

@@ -1,10 +1,8 @@
#nullable disable
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using SharpCompress.Compressors.LZMA;
using SharpCompress.Compressors.LZMA.Utilites;
using SharpCompress.Compressors.LZMA.Utilities;
namespace SharpCompress.Common.SevenZip;
@@ -13,6 +11,7 @@ internal sealed partial class ArchiveDatabase
internal async ValueTask<Stream> GetFolderStreamAsync(
Stream stream,
CFolder folder,
IPasswordProvider pw,
CancellationToken cancellationToken
)

View File

@@ -1,10 +1,8 @@
#nullable disable
using System;
using System.Collections.Generic;
using System.IO;
using SharpCompress.Compressors.LZMA;
using SharpCompress.Compressors.LZMA.Utilites;
using SharpCompress.Compressors.LZMA.Utilities;
namespace SharpCompress.Common.SevenZip;
@@ -18,7 +16,7 @@ internal partial class ArchiveDatabase
internal List<long> _packSizes = new();
internal List<uint?> _packCrCs = new();
internal List<CFolder> _folders = new();
internal List<int> _numUnpackStreamsVector;
internal List<int> _numUnpackStreamsVector = null!;
internal List<CFileItem> _files = new();
internal List<long> _packStreamStartPositions = new();
@@ -35,7 +33,7 @@ internal partial class ArchiveDatabase
_packSizes.Clear();
_packCrCs.Clear();
_folders.Clear();
_numUnpackStreamsVector = null;
_numUnpackStreamsVector = null!;
_files.Clear();
_packStreamStartPositions.Clear();

View File

@@ -1,13 +1,13 @@
#nullable disable
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using SharpCompress.Compressors.Deflate64;
using SharpCompress.Compressors.LZMA;
using SharpCompress.Compressors.LZMA.Utilites;
using SharpCompress.Compressors.LZMA.Utilities;
using SharpCompress.IO;
using BlockType = SharpCompress.Compressors.LZMA.Utilities.BlockType;
namespace SharpCompress.Common.SevenZip;
@@ -264,7 +264,7 @@ internal sealed partial class ArchiveReader
type = ReadId();
}
List<byte[]> dataVector = null;
List<byte[]>? dataVector = null;
if (type == BlockType.AdditionalStreamsInfo)
{
dataVector = await ReadAndDecodePackedStreamsAsync(
@@ -332,8 +332,8 @@ internal sealed partial class ArchiveReader
}
var emptyStreamVector = new BitVector(numFiles);
BitVector emptyFileVector = null;
BitVector antiFileVector = null;
BitVector emptyFileVector = null!;
BitVector antiFileVector = null!;
var numEmptyStreams = 0;
for (; ; )
@@ -351,7 +351,7 @@ internal sealed partial class ArchiveReader
case BlockType.Name:
using (var streamSwitch = new CStreamSwitch())
{
streamSwitch.Set(this, dataVector);
streamSwitch.Set(this, dataVector ?? []);
#if DEBUG
Log.Write("FileNames:");
#endif

View File

@@ -1,5 +1,3 @@
#nullable disable
using System;
using System.Collections.Generic;
using System.Diagnostics;
@@ -7,20 +5,22 @@ using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using SharpCompress.Compressors.Deflate64;
using SharpCompress.Compressors.LZMA;
using SharpCompress.Compressors.LZMA.Utilites;
using SharpCompress.Compressors.LZMA.Utilities;
using SharpCompress.IO;
using BlockType = SharpCompress.Compressors.LZMA.Utilities.BlockType;
namespace SharpCompress.Common.SevenZip;
internal partial class ArchiveReader
{
internal Stream _stream;
internal Stream _stream = null!;
internal Stack<DataReader> _readerStack = new();
internal DataReader _currentReader;
internal DataReader _currentReader = null!;
internal long _streamOrigin;
internal long _streamEnding;
internal byte[] _header;
internal byte[] _header = null!;
private readonly Dictionary<int, Stream> _cachedStreams = new();
@@ -54,9 +54,6 @@ internal partial class ArchiveReader
{
return null;
}
#if DEBUG
Log.WriteLine("ReadId: {0}", (BlockType)id);
#endif
return (BlockType)id;
}
@@ -130,12 +127,12 @@ internal partial class ArchiveReader
return ReadBitVector(length);
}
private void ReadNumberVector(List<byte[]> dataVector, int numFiles, Action<int, long?> action)
private void ReadNumberVector(List<byte[]>? dataVector, int numFiles, Action<int, long?> action)
{
var defined = ReadOptionalBitVector(numFiles);
using var streamSwitch = new CStreamSwitch();
streamSwitch.Set(this, dataVector);
streamSwitch.Set(this, dataVector ?? []);
for (var i = 0; i < numFiles; i++)
{
@@ -164,7 +161,7 @@ internal partial class ArchiveReader
}
private void ReadDateTimeVector(
List<byte[]> dataVector,
List<byte[]>? dataVector,
int numFiles,
Action<int, DateTime?> action
) =>
@@ -175,14 +172,14 @@ internal partial class ArchiveReader
);
private void ReadAttributeVector(
List<byte[]> dataVector,
List<byte[]>? dataVector,
int numFiles,
Action<int, uint?> action
)
{
var boolVector = ReadOptionalBitVector(numFiles);
using var streamSwitch = new CStreamSwitch();
streamSwitch.Set(this, dataVector);
streamSwitch.Set(this, dataVector ?? []);
for (var i = 0; i < numFiles; i++)
{
if (boolVector[i])
@@ -427,7 +424,7 @@ internal partial class ArchiveReader
#endif
try
{
packCrCs = null;
packCrCs = null!;
dataOffset = checked((long)ReadNumber());
#if DEBUG
@@ -489,7 +486,7 @@ internal partial class ArchiveReader
}
}
private void ReadUnpackInfo(List<byte[]> dataVector, out List<CFolder> folders)
private void ReadUnpackInfo(List<byte[]>? dataVector, out List<CFolder> folders)
{
#if DEBUG
Log.WriteLine("-- ReadUnpackInfo --");
@@ -505,7 +502,7 @@ internal partial class ArchiveReader
using (var streamSwitch = new CStreamSwitch())
{
streamSwitch.Set(this, dataVector);
streamSwitch.Set(this, dataVector ?? []);
//folders.Clear();
//folders.Reserve(numFolders);
@@ -586,7 +583,7 @@ internal partial class ArchiveReader
#endif
try
{
numUnpackStreamsInFolders = null;
numUnpackStreamsInFolders = null!;
BlockType? type;
for (; ; )
@@ -679,7 +676,7 @@ internal partial class ArchiveReader
numDigestsTotal += numSubstreams;
}
digests = null;
digests = null!;
for (; ; )
{
@@ -696,7 +693,7 @@ internal partial class ArchiveReader
var folder = folders[i];
if (numSubstreams == 1 && folder.UnpackCrcDefined)
{
digests.Add(folder._unpackCrc.Value);
digests.Add(folder._unpackCrc!.Value);
}
else
{
@@ -741,7 +738,7 @@ internal partial class ArchiveReader
}
private void ReadStreamsInfo(
List<byte[]> dataVector,
List<byte[]>? dataVector,
out long dataOffset,
out List<long> packSizes,
out List<uint?> packCrCs,
@@ -758,12 +755,12 @@ internal partial class ArchiveReader
try
{
dataOffset = long.MinValue;
packSizes = null;
packCrCs = null;
folders = null;
numUnpackStreamsInFolders = null;
unpackSizes = null;
digests = null;
packSizes = null!;
packCrCs = null!;
folders = null!;
numUnpackStreamsInFolders = null!;
unpackSizes = null!;
digests = null!;
for (; ; )
{
@@ -779,7 +776,7 @@ internal partial class ArchiveReader
break;
case BlockType.SubStreamsInfo:
ReadSubStreamsInfo(
folders,
folders!,
out numUnpackStreamsInFolders,
out unpackSizes,
out digests
@@ -888,7 +885,7 @@ internal partial class ArchiveReader
type = ReadId();
}
List<byte[]> dataVector = null;
List<byte[]>? dataVector = null;
if (type == BlockType.AdditionalStreamsInfo)
{
dataVector = ReadAndDecodePackedStreams(
@@ -954,8 +951,8 @@ internal partial class ArchiveReader
}
var emptyStreamVector = new BitVector(numFiles);
BitVector emptyFileVector = null;
BitVector antiFileVector = null;
BitVector emptyFileVector = null!;
BitVector antiFileVector = null!;
var numEmptyStreams = 0;
for (; ; )
@@ -973,7 +970,7 @@ internal partial class ArchiveReader
case BlockType.Name:
using (var streamSwitch = new CStreamSwitch())
{
streamSwitch.Set(this, dataVector);
streamSwitch.Set(this, dataVector ?? []);
#if DEBUG
Log.Write("FileNames:");
#endif
@@ -1445,7 +1442,7 @@ internal partial class ArchiveReader
public override void SetLength(long value) => throw new NotSupportedException();
private Stream _stream;
private Stream? _stream;
private long _rem;
private int _currentIndex;
@@ -1457,7 +1454,7 @@ internal partial class ArchiveReader
)
{
OpenFile();
_stream.Dispose();
_stream.NotNull().Dispose();
_stream = null;
_currentIndex++;
}
@@ -1469,9 +1466,10 @@ internal partial class ArchiveReader
#if DEBUG
Log.WriteLine(_db._files[index].Name);
#endif
if (_db._files[index].Crc.HasValue)
var crc = _db._files[index].Crc;
if (crc.HasValue)
{
_stream = new CrcCheckStream(_db._files[index].Crc.Value);
_stream = new CrcCheckStream(crc.Value);
}
else
{
@@ -1568,11 +1566,10 @@ internal partial class ArchiveReader
return new ReadOnlySubStream(s, db._files[fileIndex].Size);
}
public void Extract(ArchiveDatabase db, int[] indices)
public void Extract(ArchiveDatabase db, int[]? indices)
{
var allFilesMode = (indices is null);
var numItems = allFilesMode ? db._files.Count : indices.Length;
var allFilesMode = indices is null;
var numItems = allFilesMode ? db._files.Count : indices!.Length;
if (numItems == 0)
{
@@ -1582,7 +1579,7 @@ internal partial class ArchiveReader
var extractFolderInfoVector = new List<CExtractFolderInfo>();
for (var i = 0; i < numItems; i++)
{
var fileIndex = allFilesMode ? i : indices[i];
var fileIndex = allFilesMode ? i : indices![i];
var folderIndex = db._fileIndexToFolderIndexMap[fileIndex];
if (folderIndex == -1)
@@ -1608,7 +1605,7 @@ internal partial class ArchiveReader
}
}
byte[] buffer = null;
byte[] buffer = null!;
foreach (var efi in extractFolderInfoVector)
{
int startIndex;

View File

@@ -4,7 +4,7 @@ using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using SharpCompress.Compressors.LZMA.Utilites;
using SharpCompress.Compressors.LZMA.Utilities;
namespace SharpCompress.Compressors.LZMA;

View File

@@ -7,7 +7,7 @@ using SharpCompress.Common.SevenZip;
using SharpCompress.Compressors.BZip2;
using SharpCompress.Compressors.Deflate;
using SharpCompress.Compressors.Filters;
using SharpCompress.Compressors.LZMA.Utilites;
using SharpCompress.Compressors.LZMA.Utilities;
using SharpCompress.Compressors.PPMd;
using SharpCompress.Compressors.ZStandard;

View File

@@ -4,7 +4,7 @@ using System.Threading;
using System.Threading.Tasks;
using SharpCompress.Common;
using SharpCompress.Common.SevenZip;
using SharpCompress.Compressors.LZMA.Utilites;
using SharpCompress.Compressors.LZMA.Utilities;
using SharpCompress.IO;
namespace SharpCompress.Compressors.LZMA;

View File

@@ -5,7 +5,7 @@ using SharpCompress.Common.SevenZip;
using SharpCompress.Compressors.BZip2;
using SharpCompress.Compressors.Deflate;
using SharpCompress.Compressors.Filters;
using SharpCompress.Compressors.LZMA.Utilites;
using SharpCompress.Compressors.LZMA.Utilities;
using SharpCompress.Compressors.PPMd;
using SharpCompress.Compressors.ZStandard;

View File

@@ -1,8 +1,7 @@
using System;
using System.Diagnostics;
using System.IO;
namespace SharpCompress.Compressors.LZMA.Utilites;
namespace SharpCompress.Compressors.LZMA.Utilities;
internal enum BlockType : byte
{
@@ -38,19 +37,3 @@ internal enum BlockType : byte
#endregion
}
internal static class Utils
{
[Conditional("DEBUG")]
public static void Assert(bool expression)
{
if (!expression)
{
if (Debugger.IsAttached)
{
Debugger.Break();
}
throw new InvalidOperationException("Assertion failed.");
}
}
}

View File

@@ -3,7 +3,7 @@ using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace SharpCompress.Compressors.LZMA.Utilites;
namespace SharpCompress.Compressors.LZMA.Utilities;
internal partial class CrcBuilderStream : Stream
{

View File

@@ -1,9 +1,7 @@
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace SharpCompress.Compressors.LZMA.Utilites;
namespace SharpCompress.Compressors.LZMA.Utilities;
internal partial class CrcBuilderStream : Stream
{

View File

@@ -1,64 +1,29 @@
using System;
using System.Buffers;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace SharpCompress.Compressors.LZMA.Utilites;
namespace SharpCompress.Compressors.LZMA.Utilities;
[CLSCompliant(false)]
public class CrcCheckStream : Stream
public class CrcCheckStream(uint crc) : Stream
{
private readonly uint _mExpectedCrc;
private uint _mCurrentCrc;
private uint _mCurrentCrc = Crc.INIT_CRC;
private bool _mClosed;
private readonly long[] _mBytes = ArrayPool<long>.Shared.Rent(256);
public CrcCheckStream(uint crc)
{
_mExpectedCrc = crc;
_mCurrentCrc = Crc.INIT_CRC;
}
protected override void Dispose(bool disposing)
{
//Nanook - is not equal here - _mCurrentCrc is yet to be negated
//if (_mCurrentCrc != _mExpectedCrc)
//{
// throw new InvalidOperationException();
//}
try
{
if (disposing && !_mClosed)
{
_mClosed = true;
_mCurrentCrc = Crc.Finish(_mCurrentCrc); //now becomes equal
#if DEBUG
if (_mCurrentCrc == _mExpectedCrc)
{
Debug.WriteLine("CRC ok: " + _mExpectedCrc.ToString("x8"));
}
else
{
Debugger.Break();
Debug.WriteLine("bad CRC");
}
var lengthInv = 1.0 / _mLength;
double entropy = 0;
for (var i = 0; i < 256; i++)
{
if (_mBytes[i] != 0)
{
var p = lengthInv * _mBytes[i];
entropy -= p * Math.Log(p, 256);
}
}
Debug.WriteLine("entropy: " + (int)(entropy * 100) + "%");
#endif
if (_mCurrentCrc != _mExpectedCrc) //moved test to here
if (_mCurrentCrc != crc) //moved test to here
{
throw new InvalidOperationException();
}

View File

@@ -1,4 +1,4 @@
namespace SharpCompress.Compressors.LZMA.Utilites;
namespace SharpCompress.Compressors.LZMA.Utilities;
internal interface IPasswordProvider
{

View File

@@ -1,5 +1,3 @@
#nullable disable
using System;
namespace SharpCompress.Compressors.Xz;
@@ -10,7 +8,7 @@ public static class Crc32
public const uint DefaultPolynomial = 0xedb88320u;
public const uint DefaultSeed = 0xffffffffu;
private static uint[] defaultTable;
private static uint[]? defaultTable;
public static uint Compute(byte[] buffer) => Compute(DefaultSeed, buffer);

View File

@@ -1,5 +1,3 @@
#nullable disable
using System;
namespace SharpCompress.Compressors.Xz;
@@ -9,7 +7,7 @@ public static class Crc64
{
public const ulong DefaultSeed = 0x0;
internal static ulong[] Table;
internal static ulong[]? Table;
public const ulong Iso3309Polynomial = 0xD800000000000000;

View File

@@ -1,5 +1,3 @@
#nullable disable
using System;
using System.Collections.Generic;
using System.IO;

View File

@@ -4,7 +4,7 @@ using System.Threading.Tasks;
using SharpCompress.Archives;
using SharpCompress.Archives.Rar;
using SharpCompress.Common;
using SharpCompress.Compressors.LZMA.Utilites;
using SharpCompress.Compressors.LZMA.Utilities;
using SharpCompress.Readers;
using SharpCompress.Test.Mocks;
using Xunit;

View File

@@ -4,7 +4,7 @@ using System.Linq;
using SharpCompress.Archives;
using SharpCompress.Archives.Rar;
using SharpCompress.Common;
using SharpCompress.Compressors.LZMA.Utilites;
using SharpCompress.Compressors.LZMA.Utilities;
using SharpCompress.Readers;
using SharpCompress.Test.Mocks;
using Xunit;