From 147be6e6e11c2eecdb96295ba4e162e13b7e3ce8 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Sat, 9 Jan 2021 10:34:49 +0000 Subject: [PATCH 1/2] Use Net5, NetCoreApp3.1, NetStandard2.1, NetStandard2.0 only --- .github/workflows/dotnetcore.yml | 2 +- build/Program.cs | 9 ++------- build/build.csproj | 6 +++--- global.json | 2 +- src/SharpCompress/Algorithms/Alder32.cs | 8 ++++---- src/SharpCompress/Archives/Zip/ZipArchive.cs | 3 +-- .../Common/Zip/StreamingZipHeaderFactory.cs | 5 ++++- src/SharpCompress/Common/Zip/ZipFilePart.cs | 2 +- .../Common/Zip/ZipHeaderFactory.cs | 2 +- .../Compressors/BZip2/CBZip2OutputStream.cs | 18 ++---------------- .../Compressors/LZMA/AesDecoderStream.cs | 8 ++++++-- src/SharpCompress/SharpCompress.csproj | 12 ++++-------- .../SharpCompress.Test.csproj | 4 ++-- 13 files changed, 32 insertions(+), 49 deletions(-) diff --git a/.github/workflows/dotnetcore.yml b/.github/workflows/dotnetcore.yml index 531454208..d36c81db9 100644 --- a/.github/workflows/dotnetcore.yml +++ b/.github/workflows/dotnetcore.yml @@ -12,7 +12,7 @@ jobs: - uses: actions/checkout@v1 - uses: actions/setup-dotnet@v1 with: - dotnet-version: 3.1.403 + dotnet-version: 5.0.101 - run: dotnet run -p build/build.csproj - uses: actions/upload-artifact@v2 with: diff --git a/build/Program.cs b/build/Program.cs index 1c55c3dc2..441688b27 100644 --- a/build/Program.cs +++ b/build/Program.cs @@ -39,17 +39,12 @@ void RemoveDirectory(string d) } }); - Target(Build, ForEach("net46", "netstandard2.0", "netstandard2.1"), - framework => + Target(Build, () => { - if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && framework == "net46") - { - return; - } Run("dotnet", "build src/SharpCompress/SharpCompress.csproj -c Release"); }); - Target(Test, DependsOn(Build), ForEach("netcoreapp3.1"), + Target(Test, DependsOn(Build), ForEach("net5.0"), framework => { IEnumerable GetFiles(string d) diff --git a/build/build.csproj b/build/build.csproj index 2600bddad..635e5b012 100644 --- a/build/build.csproj +++ b/build/build.csproj @@ -2,13 +2,13 @@ Exe - netcoreapp3.1 + net5.0 - + - + diff --git a/global.json b/global.json index 9731311d0..fbb332028 100644 --- a/global.json +++ b/global.json @@ -1,5 +1,5 @@ { "sdk": { - "version": "3.1.403" + "version": "5.0.101" } } \ No newline at end of file diff --git a/src/SharpCompress/Algorithms/Alder32.cs b/src/SharpCompress/Algorithms/Alder32.cs index d0b3279fd..06dfed524 100644 --- a/src/SharpCompress/Algorithms/Alder32.cs +++ b/src/SharpCompress/Algorithms/Alder32.cs @@ -4,7 +4,7 @@ using System; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -#if NETCOREAPP3_1 +#if !NETSTANDARD2_0 && !NETSTANDARD2_1 using System.Runtime.Intrinsics; using System.Runtime.Intrinsics.X86; #endif @@ -22,7 +22,7 @@ internal static class Adler32 /// public const uint SeedValue = 1U; -#if NETCOREAPP3_1 +#if !NETSTANDARD2_0 && !NETSTANDARD2_1 private const int MinBufferSize = 64; #endif @@ -56,7 +56,7 @@ public static uint Calculate(uint adler, ReadOnlySpan buffer) return SeedValue; } -#if NETCOREAPP3_1 +#if !NETSTANDARD2_0 && !NETSTANDARD2_1 if (Sse3.IsSupported && buffer.Length >= MinBufferSize) { return CalculateSse(adler, buffer); @@ -69,7 +69,7 @@ public static uint Calculate(uint adler, ReadOnlySpan buffer) } // Based on https://github.com/chromium/chromium/blob/master/third_party/zlib/adler32_simd.c -#if NETCOREAPP3_1 +#if !NETSTANDARD2_0 && !NETSTANDARD2_1 private static unsafe uint CalculateSse(uint adler, ReadOnlySpan buffer) { uint s1 = adler & 0xFFFF; diff --git a/src/SharpCompress/Archives/Zip/ZipArchive.cs b/src/SharpCompress/Archives/Zip/ZipArchive.cs index 88d4219bf..c9daef68e 100644 --- a/src/SharpCompress/Archives/Zip/ZipArchive.cs +++ b/src/SharpCompress/Archives/Zip/ZipArchive.cs @@ -80,8 +80,7 @@ public static bool IsZipFile(Stream stream, string? password = null) StreamingZipHeaderFactory headerFactory = new StreamingZipHeaderFactory(password, new ArchiveEncoding()); try { - ZipHeader header = - headerFactory.ReadStreamHeader(stream).FirstOrDefault(x => x.ZipHeaderType != ZipHeaderType.Split); + ZipHeader? header = headerFactory.ReadStreamHeader(stream).FirstOrDefault(x => x.ZipHeaderType != ZipHeaderType.Split); if (header is null) { return false; diff --git a/src/SharpCompress/Common/Zip/StreamingZipHeaderFactory.cs b/src/SharpCompress/Common/Zip/StreamingZipHeaderFactory.cs index e37da40a2..d3df8dc69 100644 --- a/src/SharpCompress/Common/Zip/StreamingZipHeaderFactory.cs +++ b/src/SharpCompress/Common/Zip/StreamingZipHeaderFactory.cs @@ -49,7 +49,10 @@ internal IEnumerable ReadStreamHeader(Stream stream) _lastEntryHeader = null; uint headerBytes = reader.ReadUInt32(); header = ReadHeader(headerBytes, reader); - if (header is null) { yield break; } + if (header is null) + { + yield break; + } //entry could be zero bytes so we need to know that. if (header.ZipHeaderType == ZipHeaderType.LocalEntry) diff --git a/src/SharpCompress/Common/Zip/ZipFilePart.cs b/src/SharpCompress/Common/Zip/ZipFilePart.cs index 0c5d78e6b..508672763 100644 --- a/src/SharpCompress/Common/Zip/ZipFilePart.cs +++ b/src/SharpCompress/Common/Zip/ZipFilePart.cs @@ -99,7 +99,7 @@ protected Stream CreateDecompressionStream(Stream stream, ZipCompressionMethod m } case ZipCompressionMethod.WinzipAes: { - ExtraData data = Header.Extra.Where(x => x.Type == ExtraDataType.WinZipAes).SingleOrDefault(); + ExtraData? data = Header.Extra.SingleOrDefault(x => x.Type == ExtraDataType.WinZipAes); if (data is null) { throw new InvalidFormatException("No Winzip AES extra data found."); diff --git a/src/SharpCompress/Common/Zip/ZipHeaderFactory.cs b/src/SharpCompress/Common/Zip/ZipHeaderFactory.cs index 37612e0fd..14e93d1c1 100644 --- a/src/SharpCompress/Common/Zip/ZipHeaderFactory.cs +++ b/src/SharpCompress/Common/Zip/ZipHeaderFactory.cs @@ -129,7 +129,7 @@ private void LoadHeader(ZipFileEntry entryHeader, Stream stream) if (entryHeader.CompressionMethod == ZipCompressionMethod.WinzipAes) { - ExtraData data = entryHeader.Extra.SingleOrDefault(x => x.Type == ExtraDataType.WinZipAes); + ExtraData? data = entryHeader.Extra.SingleOrDefault(x => x.Type == ExtraDataType.WinZipAes); if (data != null) { var keySize = (WinzipAesKeySize)data.DataBytes[4]; diff --git a/src/SharpCompress/Compressors/BZip2/CBZip2OutputStream.cs b/src/SharpCompress/Compressors/BZip2/CBZip2OutputStream.cs index a0e9705a0..b86695bdf 100644 --- a/src/SharpCompress/Compressors/BZip2/CBZip2OutputStream.cs +++ b/src/SharpCompress/Compressors/BZip2/CBZip2OutputStream.cs @@ -604,14 +604,7 @@ private void BsFinishedWithStream() while (bsLive > 0) { int ch = (bsBuff >> 24); - try - { - bsStream.WriteByte((byte)ch); // write 8-bit - } - catch (IOException e) - { - throw e; - } + bsStream.WriteByte((byte)ch); // write 8-bit bsBuff <<= 8; bsLive -= 8; bytesOut++; @@ -623,14 +616,7 @@ private void BsW(int n, int v) while (bsLive >= 8) { int ch = (bsBuff >> 24); - try - { - bsStream.WriteByte((byte)ch); // write 8-bit - } - catch (IOException e) - { - throw e; - } + bsStream.WriteByte((byte)ch); // write 8-bit bsBuff <<= 8; bsLive -= 8; bytesOut++; diff --git a/src/SharpCompress/Compressors/LZMA/AesDecoderStream.cs b/src/SharpCompress/Compressors/LZMA/AesDecoderStream.cs index 0a4be38b8..fbce497be 100644 --- a/src/SharpCompress/Compressors/LZMA/AesDecoderStream.cs +++ b/src/SharpCompress/Compressors/LZMA/AesDecoderStream.cs @@ -31,7 +31,11 @@ public AesDecoderStream(Stream input, byte[] info, IPasswordProvider pass, long Init(info, out int numCyclesPower, out byte[] salt, out byte[] seed); byte[] password = Encoding.Unicode.GetBytes(pass.CryptoGetTextPassword()); - byte[] key = InitKey(numCyclesPower, salt, password); + byte[]? key = InitKey(numCyclesPower, salt, password); + if (key == null) + { + throw new InvalidOperationException("Initialized with null key"); + } using (var aes = Aes.Create()) { @@ -177,7 +181,7 @@ private void Init(byte[] info, out int numCyclesPower, out byte[] salt, out byte } } - private byte[] InitKey(int mNumCyclesPower, byte[] salt, byte[] pass) + private byte[]? InitKey(int mNumCyclesPower, byte[] salt, byte[] pass) { if (mNumCyclesPower == 0x3F) { diff --git a/src/SharpCompress/SharpCompress.csproj b/src/SharpCompress/SharpCompress.csproj index de803800e..b4ced089b 100644 --- a/src/SharpCompress/SharpCompress.csproj +++ b/src/SharpCompress/SharpCompress.csproj @@ -6,14 +6,12 @@ 0.26.0 0.26.0 Adam Hathcock - netstandard2.0;netstandard2.1;netcoreapp3.1;net461 - netstandard2.0;netstandard2.1;netcoreapp3.1; + netstandard2.0;netstandard2.1;netcoreapp3.1;net5.0 true false SharpCompress ../../SharpCompress.snk true - true SharpCompress rar;unrar;zip;unzip;bzip2;gzip;tar;7zip;lzip;xz https://github.com/adamhathcock/sharpcompress @@ -31,14 +29,12 @@ + + + - - - - - diff --git a/tests/SharpCompress.Test/SharpCompress.Test.csproj b/tests/SharpCompress.Test/SharpCompress.Test.csproj index 0c3d5d6f3..fbb420d63 100644 --- a/tests/SharpCompress.Test/SharpCompress.Test.csproj +++ b/tests/SharpCompress.Test/SharpCompress.Test.csproj @@ -1,6 +1,6 @@  - net461;netcoreapp3.1 + net5.0 SharpCompress.Test ../../SharpCompress.snk true @@ -12,7 +12,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive From 7edc437df24217051f2ef822757da5c930cb8fb2 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Sat, 9 Jan 2021 13:40:57 +0000 Subject: [PATCH 2/2] formatting --- build/Program.cs | 2 +- src/SharpCompress/Common/Zip/ZipFilePart.cs | 20 ++++++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/build/Program.cs b/build/Program.cs index d4bf56d13..81ac2a91e 100644 --- a/build/Program.cs +++ b/build/Program.cs @@ -56,7 +56,7 @@ void RemoveDirectory(string d) Run("dotnet", "build src/SharpCompress/SharpCompress.csproj -c Release"); }); - Target(Test, DependsOn(Build), ForEach("net5.0"), + Target(Test, DependsOn(Build), ForEach("net5.0"), framework => { IEnumerable GetFiles(string d) diff --git a/src/SharpCompress/Common/Zip/ZipFilePart.cs b/src/SharpCompress/Common/Zip/ZipFilePart.cs index ebe2338a7..23fbaf24b 100644 --- a/src/SharpCompress/Common/Zip/ZipFilePart.cs +++ b/src/SharpCompress/Common/Zip/ZipFilePart.cs @@ -98,17 +98,17 @@ protected Stream CreateDecompressionStream(Stream stream, ZipCompressionMethod m return new PpmdStream(new PpmdProperties(props), stream, false); } case ZipCompressionMethod.WinzipAes: - { - ExtraData? data = Header.Extra.SingleOrDefault(x => x.Type == ExtraDataType.WinZipAes); - if (data is null) - { - throw new InvalidFormatException("No Winzip AES extra data found."); - } - if (data.Length != 7) { - throw new InvalidFormatException("Winzip data length is not 7."); - } - ushort compressedMethod = BinaryPrimitives.ReadUInt16LittleEndian(data.DataBytes); + ExtraData? data = Header.Extra.SingleOrDefault(x => x.Type == ExtraDataType.WinZipAes); + if (data is null) + { + throw new InvalidFormatException("No Winzip AES extra data found."); + } + if (data.Length != 7) + { + throw new InvalidFormatException("Winzip data length is not 7."); + } + ushort compressedMethod = BinaryPrimitives.ReadUInt16LittleEndian(data.DataBytes); if (compressedMethod != 0x01 && compressedMethod != 0x02) {