From 632bae725d6b45e96e532eaa31c59b79b0530875 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Thu, 14 May 2020 13:47:21 +0100 Subject: [PATCH] Add braces for clarity --- .../Common/Rar/RarCryptoBinaryReader.cs | 2 ++ src/SharpCompress/Common/Rar/RarCryptoWrapper.cs | 5 ++++- .../Compressors/Deflate64/FastEncoderStatus.cs | 4 ++++ .../Compressors/Deflate64/HuffmanTree.cs | 8 ++++++++ .../Compressors/LZMA/AesDecoderStream.cs | 13 +++++++++++++ .../Compressors/Rar/UnpackV1/Unpack50.cs | 3 +++ .../FragmentedWindow.unpack50frag_cpp.cs | 13 ++++++++++++- .../Rar/UnpackV2017/Unpack.unpack15_cpp.cs | 15 +++++++++++++++ .../Rar/UnpackV2017/Unpack.unpack20_cpp.cs | 4 ++++ .../Rar/UnpackV2017/Unpack.unpack50_cpp.cs | 10 ++++++++++ .../Rar/UnpackV2017/Unpack.unpack_cpp.cs | 6 ++++++ .../Rar/UnpackV2017/Unpack.unpackinline_cpp.cs | 2 ++ src/SharpCompress/Compressors/Xz/Crc32.cs | 5 +++++ src/SharpCompress/Compressors/Xz/Crc64.cs | 4 ++++ src/SharpCompress/Crypto/Crc32Stream.cs | 2 ++ tests/SharpCompress.Test/Tar/TarArchiveTests.cs | 7 +++++++ tests/SharpCompress.Test/Zip/Zip64Tests.cs | 12 ++++++++---- tests/SharpCompress.Test/Zip/ZipArchiveTests.cs | 8 ++++++++ 18 files changed, 117 insertions(+), 6 deletions(-) diff --git a/src/SharpCompress/Common/Rar/RarCryptoBinaryReader.cs b/src/SharpCompress/Common/Rar/RarCryptoBinaryReader.cs index d1893fa42..6ccc3bb68 100644 --- a/src/SharpCompress/Common/Rar/RarCryptoBinaryReader.cs +++ b/src/SharpCompress/Common/Rar/RarCryptoBinaryReader.cs @@ -81,7 +81,9 @@ private byte[] ReadAndDecryptBytes(int count) byte[] cipherText = ReadBytesNoCrc(16); var readBytes = _rijndael.ProcessBlock(cipherText); foreach (var readByte in readBytes) + { _data.Enqueue(readByte); + } } } diff --git a/src/SharpCompress/Common/Rar/RarCryptoWrapper.cs b/src/SharpCompress/Common/Rar/RarCryptoWrapper.cs index 167b50601..06aac6bd7 100644 --- a/src/SharpCompress/Common/Rar/RarCryptoWrapper.cs +++ b/src/SharpCompress/Common/Rar/RarCryptoWrapper.cs @@ -58,12 +58,15 @@ public int ReadAndDecrypt(byte[] buffer, int offset, int count) var readBytes = _rijndael.ProcessBlock(cipherText); foreach (var readByte in readBytes) + { _data.Enqueue(readByte); - + } } for (int i = 0; i < count; i++) + { buffer[offset + i] = _data.Dequeue(); + } } return count; } diff --git a/src/SharpCompress/Compressors/Deflate64/FastEncoderStatus.cs b/src/SharpCompress/Compressors/Deflate64/FastEncoderStatus.cs index ecfae42b1..19c22c946 100644 --- a/src/SharpCompress/Compressors/Deflate64/FastEncoderStatus.cs +++ b/src/SharpCompress/Compressors/Deflate64/FastEncoderStatus.cs @@ -208,7 +208,9 @@ private static byte[] CreateDistanceLookup() for (code = 0; code < 16; code++) { for (int n = 0; n < (1 << EXTRA_DISTANCE_BITS[code]); n++) + { result[dist++] = (byte)code; + } } dist >>= 7; // from now on, all distances are divided by 128 @@ -216,7 +218,9 @@ private static byte[] CreateDistanceLookup() for (; code < NUM_DIST_BASE_CODES; code++) { for (int n = 0; n < (1 << (EXTRA_DISTANCE_BITS[code] - 7)); n++) + { result[256 + dist++] = (byte)code; + } } return result; diff --git a/src/SharpCompress/Compressors/Deflate64/HuffmanTree.cs b/src/SharpCompress/Compressors/Deflate64/HuffmanTree.cs index 21cb46cb0..d7963c3df 100644 --- a/src/SharpCompress/Compressors/Deflate64/HuffmanTree.cs +++ b/src/SharpCompress/Compressors/Deflate64/HuffmanTree.cs @@ -82,16 +82,24 @@ private static byte[] GetStaticLiteralTreeLength() { byte[] literalTreeLength = new byte[MAX_LITERAL_TREE_ELEMENTS]; for (int i = 0; i <= 143; i++) + { literalTreeLength[i] = 8; + } for (int i = 144; i <= 255; i++) + { literalTreeLength[i] = 9; + } for (int i = 256; i <= 279; i++) + { literalTreeLength[i] = 7; + } for (int i = 280; i <= 287; i++) + { literalTreeLength[i] = 8; + } return literalTreeLength; } diff --git a/src/SharpCompress/Compressors/LZMA/AesDecoderStream.cs b/src/SharpCompress/Compressors/LZMA/AesDecoderStream.cs index c2101f678..62c4bb612 100644 --- a/src/SharpCompress/Compressors/LZMA/AesDecoderStream.cs +++ b/src/SharpCompress/Compressors/LZMA/AesDecoderStream.cs @@ -183,11 +183,15 @@ private void Init(byte[] info, out int numCyclesPower, out byte[] salt, out byte salt = new byte[saltSize]; for (int i = 0; i < saltSize; i++) + { salt[i] = info[i + 2]; + } iv = new byte[16]; for (int i = 0; i < ivSize; i++) + { iv[i] = info[i + saltSize + 2]; + } if (numCyclesPower > 24) { @@ -203,9 +207,14 @@ private byte[] InitKey(int mNumCyclesPower, byte[] salt, byte[] pass) int pos; for (pos = 0; pos < salt.Length; pos++) + { key[pos] = salt[pos]; + } + for (int i = 0; i < pass.Length && pos < 32; i++) + { key[pos++] = pass[i]; + } return key; } @@ -225,10 +234,12 @@ private byte[] InitKey(int mNumCyclesPower, byte[] salt, byte[] pass) // This mirrors the counter so we don't have to convert long to byte[] each round. // (It also ensures the counter is little endian, which BitConverter does not.) for (int i = 0; i < 8; i++) + { if (++counter[i] != 0) { break; } + } } return sha.GetHashAndReset(); } @@ -246,10 +257,12 @@ private byte[] InitKey(int mNumCyclesPower, byte[] salt, byte[] pass) // This mirrors the counter so we don't have to convert long to byte[] each round. // (It also ensures the counter is little endian, which BitConverter does not.) for (int i = 0; i < 8; i++) + { if (++counter[i] != 0) { break; } + } } sha.TransformFinalBlock(counter, 0, 0); diff --git a/src/SharpCompress/Compressors/Rar/UnpackV1/Unpack50.cs b/src/SharpCompress/Compressors/Rar/UnpackV1/Unpack50.cs index 194fce273..81fe82082 100644 --- a/src/SharpCompress/Compressors/Rar/UnpackV1/Unpack50.cs +++ b/src/SharpCompress/Compressors/Rar/UnpackV1/Unpack50.cs @@ -298,7 +298,10 @@ public void Unpack5(bool Solid) { //for (uint I=DistNum;I>0;I--) for (int I=DistNum;I>0;I--) //OldDistN[I]=OldDistN(I-1); + { SetOldDistN(I, OldDistN(I-1)); + } + //OldDistN[0]=Distance; SetOldDistN(0, Distance); diff --git a/src/SharpCompress/Compressors/Rar/UnpackV2017/FragmentedWindow.unpack50frag_cpp.cs b/src/SharpCompress/Compressors/Rar/UnpackV2017/FragmentedWindow.unpack50frag_cpp.cs index dbedb0bcf..e33ded599 100644 --- a/src/SharpCompress/Compressors/Rar/UnpackV2017/FragmentedWindow.unpack50frag_cpp.cs +++ b/src/SharpCompress/Compressors/Rar/UnpackV2017/FragmentedWindow.unpack50frag_cpp.cs @@ -31,11 +31,13 @@ public FragmentedWindow() private void Reset() { for (uint I=0;I>(int)GetShortLen1(Length))))==0) { break; } + } Inp.faddbits(GetShortLen1(Length)); } else { for (Length=0;;Length++) + { if (((BitField^ShortXor2[Length]) & (~(0xff>>(int)GetShortLen2(Length))))==0) { break; } + } Inp.faddbits(GetShortLen2(Length)); } @@ -305,7 +309,10 @@ private void LongLZ() else { for (Length=0;((BitField<<(int)Length)&0x8000)==0;Length++) + { ; + } + Inp.faddbits(Length+1); } @@ -557,10 +564,15 @@ private void CorrHuff(ushort[] CharSet,byte[] NumToPlace) int I,J; for (I=7;I>=0;I--) for (J=0;J<32;J++) + { CharSet[J]=(ushort)((CharSet[J] & ~0xff) | I); + } + new Span(NumToPlace, 0, NToPl.Length).Clear(); for (I=6;I>=0;I--) + { NumToPlace[I]=(byte)((7-I)*32); + } } private void CopyString15(uint Distance,uint Length) @@ -577,7 +589,10 @@ private uint DecodeNum(uint Num,uint StartPos,uint[] DecTab,uint[] PosTab) { int I; for (Num&=0xfff0,I=0;DecTab[I]<=Num;I++) + { StartPos++; + } + Inp.faddbits(StartPos); return(((Num-(I != 0 ? DecTab[I-1]:0))>>(int)(16-StartPos))+PosTab[StartPos]); } diff --git a/src/SharpCompress/Compressors/Rar/UnpackV2017/Unpack.unpack20_cpp.cs b/src/SharpCompress/Compressors/Rar/UnpackV2017/Unpack.unpack20_cpp.cs index 05972f18f..56a65c5cd 100644 --- a/src/SharpCompress/Compressors/Rar/UnpackV2017/Unpack.unpack20_cpp.cs +++ b/src/SharpCompress/Compressors/Rar/UnpackV2017/Unpack.unpack20_cpp.cs @@ -307,7 +307,9 @@ private bool ReadTables20() Inp.addbits(7); } while (N-- > 0 && I0;I--) + { OldDist[I]=OldDist[I-1]; + } + OldDist[0]=Distance; uint LengthSlot=DecodeNumber(Inp,BlockTables.RD); @@ -586,7 +589,9 @@ private byte[] ApplyFilter(byte[] __d,uint DataSize,UnpackFilter Flt) { byte PrevByte=0; for (uint DestPos=CurChannel;DestPos 0 && I 0 && I=Dec.DecodeLen[CurBitLength]) + { CurBitLength++; + } // Translation of right aligned bit string to bit length. Dec.QuickLen[Code]=CurBitLength; diff --git a/src/SharpCompress/Compressors/Rar/UnpackV2017/Unpack.unpackinline_cpp.cs b/src/SharpCompress/Compressors/Rar/UnpackV2017/Unpack.unpackinline_cpp.cs index 53ef977da..faad7b510 100644 --- a/src/SharpCompress/Compressors/Rar/UnpackV2017/Unpack.unpackinline_cpp.cs +++ b/src/SharpCompress/Compressors/Rar/UnpackV2017/Unpack.unpackinline_cpp.cs @@ -120,11 +120,13 @@ private uint DecodeNumber(BitInput Inp,DecodeTable Dec) // Detect the real bit length for current code. uint Bits=15; for (uint I=Dec.QuickBits+1;I<15;I++) + { if (BitField> 1) ^ polynomial; @@ -45,6 +46,7 @@ private static UInt32[] InitializeTable(UInt32 polynomial) { entry = entry >> 1; } + } createTable[i] = entry; } @@ -61,7 +63,10 @@ private static UInt32 CalculateHash(UInt32[] table, UInt32 seed, IList buf { var crc = seed; for (var i = start; i < size - start; i++) + { crc = (crc >> 8) ^ table[buffer[i] ^ crc & 0xff]; + } + return crc; } diff --git a/src/SharpCompress/Compressors/Xz/Crc64.cs b/src/SharpCompress/Compressors/Xz/Crc64.cs index 639fcef08..ff7773dbd 100644 --- a/src/SharpCompress/Compressors/Xz/Crc64.cs +++ b/src/SharpCompress/Compressors/Xz/Crc64.cs @@ -31,10 +31,12 @@ public static UInt64 CalculateHash(UInt64 seed, UInt64[] table, IList buff var crc = seed; for (var i = start; i < size; i++) + { unchecked { crc = (crc >> 8) ^ table[(buffer[i] ^ crc) & 0xff]; } + } return crc; } @@ -46,6 +48,7 @@ public static ulong[] CreateTable(ulong polynomial) { var entry = (UInt64)i; for (var j = 0; j < 8; ++j) + { if ((entry & 1) == 1) { entry = (entry >> 1) ^ polynomial; @@ -54,6 +57,7 @@ public static ulong[] CreateTable(ulong polynomial) { entry = entry >> 1; } + } createTable[i] = entry; } diff --git a/src/SharpCompress/Crypto/Crc32Stream.cs b/src/SharpCompress/Crypto/Crc32Stream.cs index 09bd100ca..f5a342e09 100644 --- a/src/SharpCompress/Crypto/Crc32Stream.cs +++ b/src/SharpCompress/Crypto/Crc32Stream.cs @@ -87,6 +87,7 @@ private static uint[] InitializeTable(uint polynomial) { var entry = (uint)i; for (var j = 0; j < 8; j++) + { if ((entry & 1) == 1) { entry = (entry >> 1) ^ polynomial; @@ -95,6 +96,7 @@ private static uint[] InitializeTable(uint polynomial) { entry = entry >> 1; } + } createTable[i] = entry; } diff --git a/tests/SharpCompress.Test/Tar/TarArchiveTests.cs b/tests/SharpCompress.Test/Tar/TarArchiveTests.cs index b67a046bd..a03bb64e9 100644 --- a/tests/SharpCompress.Test/Tar/TarArchiveTests.cs +++ b/tests/SharpCompress.Test/Tar/TarArchiveTests.cs @@ -61,7 +61,9 @@ public void Tar_FileName_Exactly_100_Characters() Assert.Contains(filename, archive2.Entries.Select(entry => entry.Key)); foreach (var entry in archive2.Entries) + { Assert.Equal("dummy filecontent", new StreamReader(entry.OpenEntryStream()).ReadLine()); + } } } @@ -89,7 +91,10 @@ public void Tar_VeryLongFilepathReadback() // create a very long filename string longFilename = ""; for (int i = 0; i < 600; i = longFilename.Length) + { longFilename += i.ToString("D10") + "-"; + } + longFilename += ".txt"; // Step 1: create a tar file containing a file with a long name @@ -113,7 +118,9 @@ public void Tar_VeryLongFilepathReadback() Assert.Contains(longFilename, archive2.Entries.Select(entry => entry.Key)); foreach (var entry in archive2.Entries) + { Assert.Equal("dummy filecontent", new StreamReader(entry.OpenEntryStream()).ReadLine()); + } } } diff --git a/tests/SharpCompress.Test/Zip/Zip64Tests.cs b/tests/SharpCompress.Test/Zip/Zip64Tests.cs index c5cb7c7ab..a24fedc23 100644 --- a/tests/SharpCompress.Test/Zip/Zip64Tests.cs +++ b/tests/SharpCompress.Test/Zip/Zip64Tests.cs @@ -152,6 +152,7 @@ public void CreateZipArchive(string filename, long files, long filesize, long ch { for (var i = 0; i < files; i++) + { using (var str = zipWriter.WriteToStream(i.ToString(), eo)) { var left = filesize; @@ -162,6 +163,7 @@ public void CreateZipArchive(string filename, long files, long filesize, long ch left -= b; } } + } } } @@ -172,21 +174,23 @@ public Tuple ReadForwardOnly(string filename) Common.Zip.ZipEntry prev = null; using (var fs = File.OpenRead(filename)) using (var rd = ZipReader.Open(fs, new ReaderOptions() { LookForHeader = false })) + { while (rd.MoveToNextEntry()) { - using (rd.OpenEntryStream()) - { } + using (rd.OpenEntryStream()) + { } count++; - if (prev != null) + if (prev != null) { size += prev.Size; } prev = rd.Entry; } + } - if (prev != null) + if (prev != null) { size += prev.Size; } diff --git a/tests/SharpCompress.Test/Zip/ZipArchiveTests.cs b/tests/SharpCompress.Test/Zip/ZipArchiveTests.cs index 5238d5dfa..8493289d2 100644 --- a/tests/SharpCompress.Test/Zip/ZipArchiveTests.cs +++ b/tests/SharpCompress.Test/Zip/ZipArchiveTests.cs @@ -401,7 +401,9 @@ public void Zip_Random_Entry_Access() ZipArchive a = ZipArchive.Open(unmodified); int count = 0; foreach (var e in a.Entries) + { count++; + } //Prints 3 Assert.Equal(3, count); @@ -426,7 +428,9 @@ public void Zip_Random_Entry_Access() int count3 = 0; foreach (var e in a.Entries) + { count3++; + } Assert.Equal(3, count3); } @@ -447,7 +451,9 @@ public void Zip_Deflate_PKWear_Multipy_Entry_Access() { using (var memoryStream = new MemoryStream()) using (Stream entryStream = entry.OpenEntryStream()) + { entryStream.CopyTo(memoryStream); + } } } } @@ -507,7 +513,9 @@ public void TestSharpCompressWithEmptyStream() byte[] buf = new byte[bufSize]; int bytesRead = 0; while ((bytesRead = entryStream.Read(buf, 0, bufSize)) > 0) + { tempStream.Write(buf, 0, bytesRead); + } } } }