Skip to content

Commit

Permalink
Merge pull request #815 from adamhathcock/code-clean-up
Browse files Browse the repository at this point in the history
Code clean up
  • Loading branch information
adamhathcock committed Mar 14, 2024
2 parents 498d132 + 3fa85fc commit 6a52f90
Show file tree
Hide file tree
Showing 117 changed files with 1,222 additions and 1,751 deletions.
66 changes: 33 additions & 33 deletions src/SharpCompress/Algorithms/Adler32.cs
Expand Up @@ -62,7 +62,7 @@ private static class Numerics // From https://github.com/SixLabors/ImageSharp/bl
public static int ReduceSum(Vector256<int> accumulator)
{
// Add upper lane to lower lane.
Vector128<int> vsum = Sse2.Add(accumulator.GetLower(), accumulator.GetUpper());
var vsum = Sse2.Add(accumulator.GetLower(), accumulator.GetUpper());

// Add odd to even.
vsum = Sse2.Add(vsum, Sse2.Shuffle(vsum, 0b_11_11_01_01));
Expand All @@ -81,7 +81,7 @@ public static int ReduceSum(Vector256<int> accumulator)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int EvenReduceSum(Vector256<int> accumulator)
{
Vector128<int> vsum = Sse2.Add(accumulator.GetLower(), accumulator.GetUpper()); // add upper lane to lower lane
var vsum = Sse2.Add(accumulator.GetLower(), accumulator.GetUpper()); // add upper lane to lower lane
vsum = Sse2.Add(vsum, Sse2.Shuffle(vsum, 0b_11_10_11_10)); // add high to low

// Vector128<int>.ToScalar() isn't optimized pre-net5.0 https://github.com/dotnet/runtime/pull/37882
Expand Down Expand Up @@ -189,29 +189,29 @@ public static uint Calculate(uint adler, ReadOnlySpan<byte> buffer)
[MethodImpl(InliningOptions.HotPath | InliningOptions.ShortMethod)]
private static unsafe uint CalculateSse(uint adler, ReadOnlySpan<byte> buffer)
{
uint s1 = adler & 0xFFFF;
uint s2 = (adler >> 16) & 0xFFFF;
var s1 = adler & 0xFFFF;
var s2 = (adler >> 16) & 0xFFFF;

// Process the data in blocks.
uint length = (uint)buffer.Length;
uint blocks = length / BlockSize;
var length = (uint)buffer.Length;
var blocks = length / BlockSize;
length -= blocks * BlockSize;

fixed (byte* bufferPtr = &MemoryMarshal.GetReference(buffer))
{
fixed (byte* tapPtr = &MemoryMarshal.GetReference(Tap1Tap2))
{
byte* localBufferPtr = bufferPtr;
var localBufferPtr = bufferPtr;

// _mm_setr_epi8 on x86
Vector128<sbyte> tap1 = Sse2.LoadVector128((sbyte*)tapPtr);
Vector128<sbyte> tap2 = Sse2.LoadVector128((sbyte*)(tapPtr + 0x10));
Vector128<byte> zero = Vector128<byte>.Zero;
var tap1 = Sse2.LoadVector128((sbyte*)tapPtr);
var tap2 = Sse2.LoadVector128((sbyte*)(tapPtr + 0x10));
var zero = Vector128<byte>.Zero;
var ones = Vector128.Create((short)1);

while (blocks > 0)
{
uint n = NMAX / BlockSize; /* The NMAX constraint. */
var n = NMAX / BlockSize; /* The NMAX constraint. */
if (n > blocks)
{
n = blocks;
Expand All @@ -221,27 +221,27 @@ private static unsafe uint CalculateSse(uint adler, ReadOnlySpan<byte> buffer)

// Process n blocks of data. At most NMAX data bytes can be
// processed before s2 must be reduced modulo BASE.
Vector128<uint> v_ps = Vector128.CreateScalar(s1 * n);
Vector128<uint> v_s2 = Vector128.CreateScalar(s2);
Vector128<uint> v_s1 = Vector128<uint>.Zero;
var v_ps = Vector128.CreateScalar(s1 * n);
var v_s2 = Vector128.CreateScalar(s2);
var v_s1 = Vector128<uint>.Zero;

do
{
// Load 32 input bytes.
Vector128<byte> bytes1 = Sse3.LoadDquVector128(localBufferPtr);
Vector128<byte> bytes2 = Sse3.LoadDquVector128(localBufferPtr + 0x10);
var bytes1 = Sse3.LoadDquVector128(localBufferPtr);
var bytes2 = Sse3.LoadDquVector128(localBufferPtr + 0x10);

// Add previous block byte sum to v_ps.
v_ps = Sse2.Add(v_ps, v_s1);

// Horizontally add the bytes for s1, multiply-adds the
// bytes by [ 32, 31, 30, ... ] for s2.
v_s1 = Sse2.Add(v_s1, Sse2.SumAbsoluteDifferences(bytes1, zero).AsUInt32());
Vector128<short> mad1 = Ssse3.MultiplyAddAdjacent(bytes1, tap1);
var mad1 = Ssse3.MultiplyAddAdjacent(bytes1, tap1);
v_s2 = Sse2.Add(v_s2, Sse2.MultiplyAddAdjacent(mad1, ones).AsUInt32());

v_s1 = Sse2.Add(v_s1, Sse2.SumAbsoluteDifferences(bytes2, zero).AsUInt32());
Vector128<short> mad2 = Ssse3.MultiplyAddAdjacent(bytes2, tap2);
var mad2 = Ssse3.MultiplyAddAdjacent(bytes2, tap2);
v_s2 = Sse2.Add(v_s2, Sse2.MultiplyAddAdjacent(mad2, ones).AsUInt32());

localBufferPtr += BlockSize;
Expand Down Expand Up @@ -281,15 +281,15 @@ private static unsafe uint CalculateSse(uint adler, ReadOnlySpan<byte> buffer)
[MethodImpl(InliningOptions.HotPath | InliningOptions.ShortMethod)]
public static unsafe uint CalculateAvx2(uint adler, ReadOnlySpan<byte> buffer)
{
uint s1 = adler & 0xFFFF;
uint s2 = (adler >> 16) & 0xFFFF;
uint length = (uint)buffer.Length;
var s1 = adler & 0xFFFF;
var s2 = (adler >> 16) & 0xFFFF;
var length = (uint)buffer.Length;

fixed (byte* bufferPtr = &MemoryMarshal.GetReference(buffer))
{
byte* localBufferPtr = bufferPtr;
var localBufferPtr = bufferPtr;

Vector256<byte> zero = Vector256<byte>.Zero;
var zero = Vector256<byte>.Zero;
var dot3v = Vector256.Create((short)1);
var dot2v = Vector256.Create(
32,
Expand Down Expand Up @@ -333,29 +333,29 @@ public static unsafe uint CalculateAvx2(uint adler, ReadOnlySpan<byte> buffer)

while (length >= 32)
{
int k = length < NMAX ? (int)length : (int)NMAX;
var k = length < NMAX ? (int)length : (int)NMAX;
k -= k % 32;
length -= (uint)k;

Vector256<uint> vs10 = vs1;
Vector256<uint> vs3 = Vector256<uint>.Zero;
var vs10 = vs1;
var vs3 = Vector256<uint>.Zero;

while (k >= 32)
{
// Load 32 input bytes.
Vector256<byte> block = Avx.LoadVector256(localBufferPtr);
var block = Avx.LoadVector256(localBufferPtr);

// Sum of abs diff, resulting in 2 x int32's
Vector256<ushort> vs1sad = Avx2.SumAbsoluteDifferences(block, zero);
var vs1sad = Avx2.SumAbsoluteDifferences(block, zero);

vs1 = Avx2.Add(vs1, vs1sad.AsUInt32());
vs3 = Avx2.Add(vs3, vs10);

// sum 32 uint8s to 16 shorts.
Vector256<short> vshortsum2 = Avx2.MultiplyAddAdjacent(block, dot2v);
var vshortsum2 = Avx2.MultiplyAddAdjacent(block, dot2v);

// sum 16 shorts to 8 uint32s.
Vector256<int> vsum2 = Avx2.MultiplyAddAdjacent(vshortsum2, dot3v);
var vsum2 = Avx2.MultiplyAddAdjacent(vshortsum2, dot3v);

vs2 = Avx2.Add(vsum2.AsUInt32(), vs2);
vs10 = vs1;
Expand Down Expand Up @@ -434,14 +434,14 @@ public static unsafe uint CalculateAvx2(uint adler, ReadOnlySpan<byte> buffer)
[MethodImpl(InliningOptions.HotPath | InliningOptions.ShortMethod)]
private static unsafe uint CalculateScalar(uint adler, ReadOnlySpan<byte> buffer)
{
uint s1 = adler & 0xFFFF;
uint s2 = (adler >> 16) & 0xFFFF;
var s1 = adler & 0xFFFF;
var s2 = (adler >> 16) & 0xFFFF;
uint k;

fixed (byte* bufferPtr = buffer)
{
var localBufferPtr = bufferPtr;
uint length = (uint)buffer.Length;
var length = (uint)buffer.Length;

while (length > 0)
{
Expand Down
6 changes: 3 additions & 3 deletions src/SharpCompress/Archives/AbstractWritableArchive.cs
Expand Up @@ -31,10 +31,10 @@ public void Dispose()
}
}

private readonly List<TEntry> newEntries = new List<TEntry>();
private readonly List<TEntry> removedEntries = new List<TEntry>();
private readonly List<TEntry> newEntries = new();
private readonly List<TEntry> removedEntries = new();

private readonly List<TEntry> modifiedEntries = new List<TEntry>();
private readonly List<TEntry> modifiedEntries = new();
private bool hasModifications;
private bool pauseRebuilding;

Expand Down
2 changes: 1 addition & 1 deletion src/SharpCompress/Archives/ArchiveVolumeFactory.cs
Expand Up @@ -11,7 +11,7 @@ internal abstract class ArchiveVolumeFactory
FileInfo? item = null;

//split 001, 002 ...
Match m = Regex.Match(part1.Name, @"^(.*\.)([0-9]+)$", RegexOptions.IgnoreCase);
var m = Regex.Match(part1.Name, @"^(.*\.)([0-9]+)$", RegexOptions.IgnoreCase);
if (m.Success)
item = new FileInfo(
Path.Combine(
Expand Down
2 changes: 1 addition & 1 deletion src/SharpCompress/Archives/GZip/GZipArchive.cs
Expand Up @@ -94,7 +94,7 @@ public static GZipArchive Open(Stream stream, ReaderOptions? readerOptions = nul
);
}

public static GZipArchive Create() => new GZipArchive();
public static GZipArchive Create() => new();

/// <summary>
/// Constructor with a SourceStream able to handle FileInfo and Streams.
Expand Down
6 changes: 3 additions & 3 deletions src/SharpCompress/Archives/Rar/RarArchive.cs
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand All @@ -14,9 +15,8 @@ namespace SharpCompress.Archives.Rar;
public class RarArchive : AbstractArchive<RarArchiveEntry, RarVolume>
{
internal Lazy<IRarUnpack> UnpackV2017 { get; } =
new Lazy<IRarUnpack>(() => new Compressors.Rar.UnpackV2017.Unpack());
internal Lazy<IRarUnpack> UnpackV1 { get; } =
new Lazy<IRarUnpack>(() => new Compressors.Rar.UnpackV1.Unpack());
new(() => new Compressors.Rar.UnpackV2017.Unpack());
internal Lazy<IRarUnpack> UnpackV1 { get; } = new(() => new Compressors.Rar.UnpackV1.Unpack());

/// <summary>
/// Constructor with a SourceStream able to handle FileInfo and Streams.
Expand Down
2 changes: 1 addition & 1 deletion src/SharpCompress/Archives/Rar/RarArchiveVolumeFactory.cs
Expand Up @@ -11,7 +11,7 @@ internal static class RarArchiveVolumeFactory
FileInfo? item = null;

//new style rar - ..part1 | /part01 | part001 ....
Match m = Regex.Match(part1.Name, @"^(.*\.part)([0-9]+)(\.rar)$", RegexOptions.IgnoreCase);
var m = Regex.Match(part1.Name, @"^(.*\.part)([0-9]+)(\.rar)$", RegexOptions.IgnoreCase);
if (m.Success)
item = new FileInfo(
Path.Combine(
Expand Down
2 changes: 1 addition & 1 deletion src/SharpCompress/Archives/Tar/TarArchive.cs
Expand Up @@ -195,7 +195,7 @@ protected override IEnumerable<TarArchiveEntry> LoadEntries(IEnumerable<TarVolum
}
}

public static TarArchive Create() => new TarArchive();
public static TarArchive Create() => new();

protected override TarArchiveEntry CreateEntryInternal(
string filePath,
Expand Down
2 changes: 1 addition & 1 deletion src/SharpCompress/Archives/Zip/ZipArchive.cs
Expand Up @@ -294,7 +294,7 @@ IEnumerable<ZipArchiveEntry> newEntries
bool closeStream
) => new ZipWritableArchiveEntry(this, source, filePath, size, modified, closeStream);

public static ZipArchive Create() => new ZipArchive();
public static ZipArchive Create() => new();

protected override IReader CreateReaderForSolidExtraction()
{
Expand Down
2 changes: 1 addition & 1 deletion src/SharpCompress/Archives/Zip/ZipArchiveVolumeFactory.cs
Expand Up @@ -12,7 +12,7 @@ internal static class ZipArchiveVolumeFactory

//load files with zip/zipx first. Swapped to end once loaded in ZipArchive
//new style .zip, z01.. | .zipx, zx01 - if the numbers go beyond 99 then they use 100 ...1000 etc
Match m = Regex.Match(part1.Name, @"^(.*\.)(zipx?|zx?[0-9]+)$", RegexOptions.IgnoreCase);
var m = Regex.Match(part1.Name, @"^(.*\.)(zipx?|zx?[0-9]+)$", RegexOptions.IgnoreCase);
if (m.Success)
item = new FileInfo(
Path.Combine(
Expand Down
10 changes: 5 additions & 5 deletions src/SharpCompress/Common/ExtractionMethods.cs
Expand Up @@ -16,7 +16,7 @@ internal static class ExtractionMethods
)
{
string destinationFileName;
string fullDestinationDirectoryPath = Path.GetFullPath(destinationDirectory);
var fullDestinationDirectoryPath = Path.GetFullPath(destinationDirectory);

//check for trailing slash.
if (
Expand All @@ -36,11 +36,11 @@ internal static class ExtractionMethods

options ??= new ExtractionOptions() { Overwrite = true };

string file = Path.GetFileName(entry.Key);
var file = Path.GetFileName(entry.Key);
if (options.ExtractFullPath)
{
string folder = Path.GetDirectoryName(entry.Key)!;
string destdir = Path.GetFullPath(Path.Combine(fullDestinationDirectoryPath, folder));
var folder = Path.GetDirectoryName(entry.Key)!;
var destdir = Path.GetFullPath(Path.Combine(fullDestinationDirectoryPath, folder));

if (!Directory.Exists(destdir))
{
Expand Down Expand Up @@ -102,7 +102,7 @@ internal static class ExtractionMethods
}
else
{
FileMode fm = FileMode.Create;
var fm = FileMode.Create;
options ??= new ExtractionOptions() { Overwrite = true };

if (!options.Overwrite)
Expand Down
2 changes: 1 addition & 1 deletion src/SharpCompress/Common/OptionsBase.cs
Expand Up @@ -7,5 +7,5 @@ public class OptionsBase
/// </summary>
public bool LeaveStreamOpen { get; set; } = true;

public ArchiveEncoding ArchiveEncoding { get; set; } = new ArchiveEncoding();
public ArchiveEncoding ArchiveEncoding { get; set; } = new();
}
12 changes: 0 additions & 12 deletions src/SharpCompress/Common/PasswordProtectedException.cs

This file was deleted.

5 changes: 1 addition & 4 deletions src/SharpCompress/Common/Rar/CryptKey3.cs
Expand Up @@ -12,10 +12,7 @@ internal class CryptKey3 : ICryptKey

private string _password;

public CryptKey3(string password)
{
_password = password ?? "";
}
public CryptKey3(string password) => _password = password ?? "";

public ICryptoTransform Transformer(byte[] salt)
{
Expand Down
34 changes: 16 additions & 18 deletions src/SharpCompress/Common/Rar/CryptKey5.cs
Expand Up @@ -34,35 +34,33 @@ public CryptKey5(string password, Rar5CryptoInfo rar5CryptoInfo)
int keyLength
)
{
using (HMACSHA256 hmac = new HMACSHA256(Encoding.UTF8.GetBytes(password)))
{
byte[] block = hmac.ComputeHash(salt);
byte[] finalHash = (byte[])block.Clone();
using var hmac = new HMACSHA256(Encoding.UTF8.GetBytes(password));
var block = hmac.ComputeHash(salt);
var finalHash = (byte[])block.Clone();

var loop = new int[] { iterations, 17, 17 };
var res = new List<byte[]> { };
var loop = new int[] { iterations, 17, 17 };
var res = new List<byte[]> { };

for (int x = 0; x < 3; x++)
for (var x = 0; x < 3; x++)
{
for (var i = 1; i < loop[x]; i++)
{
for (int i = 1; i < loop[x]; i++)
block = hmac.ComputeHash(block);
for (var j = 0; j < finalHash.Length; j++)
{
block = hmac.ComputeHash(block);
for (int j = 0; j < finalHash.Length; j++)
{
finalHash[j] ^= block[j];
}
finalHash[j] ^= block[j];
}

res.Add((byte[])finalHash.Clone());
}

return res;
res.Add((byte[])finalHash.Clone());
}

return res;
}

public ICryptoTransform Transformer(byte[] salt)
{
int iterations = (1 << _cryptoInfo.LG2Count); // Adjust the number of iterations as needed
var iterations = (1 << _cryptoInfo.LG2Count); // Adjust the number of iterations as needed

var salt_rar5 = salt.Concat(new byte[] { 0, 0, 0, 1 });
var derivedKey = GenerateRarPBKDF2Key(
Expand All @@ -76,7 +74,7 @@ public ICryptoTransform Transformer(byte[] salt)

_pswCheck = new byte[EncryptionConstV5.SIZE_PSWCHECK];

for (int i = 0; i < SHA256_DIGEST_SIZE; i++)
for (var i = 0; i < SHA256_DIGEST_SIZE; i++)
{
_pswCheck[i % EncryptionConstV5.SIZE_PSWCHECK] ^= derivedKey[2][i];
}
Expand Down
6 changes: 2 additions & 4 deletions src/SharpCompress/Common/Rar/Headers/ArchiveCryptHeader.cs
Expand Up @@ -12,10 +12,8 @@ internal class ArchiveCryptHeader : RarHeader
public ArchiveCryptHeader(RarHeader header, RarCrcBinaryReader reader)
: base(header, reader, HeaderType.Crypt) { }

public Rar5CryptoInfo CryptInfo = new Rar5CryptoInfo();
public Rar5CryptoInfo CryptInfo = new();

protected override void ReadFinish(MarkingBinaryReader reader)
{
protected override void ReadFinish(MarkingBinaryReader reader) =>
CryptInfo = new Rar5CryptoInfo(reader, false);
}
}

0 comments on commit 6a52f90

Please sign in to comment.