diff --git a/Aaru.Checksums.csproj b/Aaru.Checksums.csproj index f7dbe4f..ad2e443 100644 --- a/Aaru.Checksums.csproj +++ b/Aaru.Checksums.csproj @@ -1,86 +1,86 @@  - - 2.0 - {CC48B324-A532-4A45-87A6-6F91F7141E8D} - Library - Aaru.Checksums - Aaru.Checksums - $(Version) - false - true - 6.0.0-alpha8 - Claunia.com - Copyright © 2011-2022 Natalia Portillo - Aaru Data Preservation Suite - Aaru.Checksums - $(Version) - net7.0 - 11 - C# implementation of CRC16, CRC32, CRC64, Fletcher, MD5, SHA1, SHA2 and SpamSum. - https://github.com/aaru-dps/ - LGPL-2.1-only - https://github.com/aaru-dps/Aaru.Checksums - true - en-US - true - true - snupkg - Natalia Portillo <claunia@claunia.com> - true - true - - - CS1591;CS1574 - - - - - - - $(Version)+{chash:8} - true - true - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - LICENSE.LGPL - - - LICENSE - - - - - - + + 2.0 + {CC48B324-A532-4A45-87A6-6F91F7141E8D} + Library + Aaru.Checksums + Aaru.Checksums + $(Version) + false + true + 6.0.0-alpha8 + Claunia.com + Copyright © 2011-2022 Natalia Portillo + Aaru Data Preservation Suite + Aaru.Checksums + $(Version) + net7.0 + 11 + C# implementation of CRC16, CRC32, CRC64, Fletcher, MD5, SHA1, SHA2 and SpamSum. + https://github.com/aaru-dps/ + LGPL-2.1-only + https://github.com/aaru-dps/Aaru.Checksums + true + en-US + true + true + snupkg + Natalia Portillo <claunia@claunia.com> + true + true + + + CS1591;CS1574 + + + + + + + $(Version)+{chash:8} + true + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + LICENSE.LGPL + + + LICENSE + + + + + + \ No newline at end of file diff --git a/Adler32/neon.cs b/Adler32/neon.cs index 1b1de40..c5065d4 100644 --- a/Adler32/neon.cs +++ b/Adler32/neon.cs @@ -45,11 +45,11 @@ // Copyright 2017 The Chromium Authors. All rights reserved. // ****************************************************************************/ -namespace Aaru.Checksums.Adler32; - using System.Runtime.Intrinsics; using System.Runtime.Intrinsics.Arm; +namespace Aaru.Checksums.Adler32; + static class Neon { internal static void Step(ref ushort preSum1, ref ushort preSum2, byte[] buf, uint len) @@ -60,7 +60,7 @@ static class Neon uint s1 = preSum1; uint s2 = preSum2; - var bufPos = 0; + int bufPos = 0; /* * Process the data in blocks. @@ -81,8 +81,8 @@ static class Neon * Process n blocks of data. At most NMAX data bytes can be * processed before s2 must be reduced modulo ADLER_MODULE. */ - var vS2 = Vector128.Create(s1 * n, 0, 0, 0); - var vS1 = Vector128.Create(0u, 0, 0, 0); + Vector128 vS2 = Vector128.Create(s1 * n, 0, 0, 0); + Vector128 vS1 = Vector128.Create(0u, 0, 0, 0); Vector128 vColumnSum1 = AdvSimd.DuplicateToVector128((ushort)0); Vector128 vColumnSum2 = AdvSimd.DuplicateToVector128((ushort)0); Vector128 vColumnSum3 = AdvSimd.DuplicateToVector128((ushort)0); @@ -93,17 +93,21 @@ static class Neon /* * Load 32 input bytes. */ - var bytes1 = Vector128.Create(buf[bufPos], buf[bufPos + 1], buf[bufPos + 2], buf[bufPos + 3], - buf[bufPos + 4], buf[bufPos + 5], buf[bufPos + 6], buf[bufPos + 7], - buf[bufPos + 8], buf[bufPos + 9], buf[bufPos + 10], buf[bufPos + 11], - buf[bufPos + 12], buf[bufPos + 13], buf[bufPos + 14], buf[bufPos + 15]); + Vector128 bytes1 = Vector128.Create(buf[bufPos], buf[bufPos + 1], buf[bufPos + 2], + buf[bufPos + 3], buf[bufPos + 4], buf[bufPos + 5], + buf[bufPos + 6], buf[bufPos + 7], buf[bufPos + 8], + buf[bufPos + 9], buf[bufPos + 10], buf[bufPos + 11], + buf[bufPos + 12], buf[bufPos + 13], buf[bufPos + 14], + buf[bufPos + 15]); bufPos += 16; - var bytes2 = Vector128.Create(buf[bufPos], buf[bufPos + 1], buf[bufPos + 2], buf[bufPos + 3], - buf[bufPos + 4], buf[bufPos + 5], buf[bufPos + 6], buf[bufPos + 7], - buf[bufPos + 8], buf[bufPos + 9], buf[bufPos + 10], buf[bufPos + 11], - buf[bufPos + 12], buf[bufPos + 13], buf[bufPos + 14], buf[bufPos + 15]); + Vector128 bytes2 = Vector128.Create(buf[bufPos], buf[bufPos + 1], buf[bufPos + 2], + buf[bufPos + 3], buf[bufPos + 4], buf[bufPos + 5], + buf[bufPos + 6], buf[bufPos + 7], buf[bufPos + 8], + buf[bufPos + 9], buf[bufPos + 10], buf[bufPos + 11], + buf[bufPos + 12], buf[bufPos + 13], buf[bufPos + 14], + buf[bufPos + 15]); bufPos += 16; /* diff --git a/Adler32/ssse3.cs b/Adler32/ssse3.cs index e79d7ef..12d2f4f 100644 --- a/Adler32/ssse3.cs +++ b/Adler32/ssse3.cs @@ -45,19 +45,19 @@ // Copyright 2017 The Chromium Authors. All rights reserved. // ****************************************************************************/ -namespace Aaru.Checksums.Adler32; - using System; using System.Runtime.Intrinsics; using System.Runtime.Intrinsics.X86; +namespace Aaru.Checksums.Adler32; + static class Ssse3 { internal static void Step(ref ushort sum1, ref ushort sum2, byte[] buf, uint len) { uint s1 = sum1; uint s2 = sum2; - var bufPos = 0; + int bufPos = 0; /* * Process the data in blocks. @@ -78,33 +78,33 @@ static class Ssse3 Vector128 tap1 = Vector128.Create(32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17). AsByte(); - Vector128 tap2 = Vector128.Create(16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1).AsByte(); - Vector128 zero = Vector128.Create(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0).AsByte(); - var ones = Vector128.Create(1, 1, 1, 1, 1, 1, 1, 1); + Vector128 tap2 = Vector128.Create(16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1).AsByte(); + Vector128 zero = Vector128.Create(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0).AsByte(); + Vector128 ones = Vector128.Create(1, 1, 1, 1, 1, 1, 1, 1); /* * Process n blocks of data. At most NMAX data bytes can be * processed before s2 must be reduced modulo BASE. */ - var vPs = Vector128.Create(s1 * n, 0, 0, 0); - var vS2 = Vector128.Create(s2, 0, 0, 0); - var vS1 = Vector128.Create(0u, 0, 0, 0); + Vector128 vPs = Vector128.Create(s1 * n, 0, 0, 0); + Vector128 vS2 = Vector128.Create(s2, 0, 0, 0); + Vector128 vS1 = Vector128.Create(0u, 0, 0, 0); do { /* * Load 32 input bytes. */ - var bytes1 = Vector128.Create(BitConverter.ToUInt32(buf, bufPos), - BitConverter.ToUInt32(buf, bufPos + 4), - BitConverter.ToUInt32(buf, bufPos + 8), - BitConverter.ToUInt32(buf, bufPos + 12)); + Vector128 bytes1 = Vector128.Create(BitConverter.ToUInt32(buf, bufPos), + BitConverter.ToUInt32(buf, bufPos + 4), + BitConverter.ToUInt32(buf, bufPos + 8), + BitConverter.ToUInt32(buf, bufPos + 12)); bufPos += 16; - var bytes2 = Vector128.Create(BitConverter.ToUInt32(buf, bufPos), - BitConverter.ToUInt32(buf, bufPos + 4), - BitConverter.ToUInt32(buf, bufPos + 8), - BitConverter.ToUInt32(buf, bufPos + 12)); + Vector128 bytes2 = Vector128.Create(BitConverter.ToUInt32(buf, bufPos), + BitConverter.ToUInt32(buf, bufPos + 4), + BitConverter.ToUInt32(buf, bufPos + 8), + BitConverter.ToUInt32(buf, bufPos + 12)); bufPos += 16; diff --git a/Adler32Context.cs b/Adler32Context.cs index ee3dd72..d2e8971 100644 --- a/Adler32Context.cs +++ b/Adler32Context.cs @@ -36,8 +36,6 @@ // Copyright (C) Jean-loup Gailly // ****************************************************************************/ -namespace Aaru.Checksums; - using System; using System.IO; using System.Runtime.InteropServices; @@ -48,6 +46,8 @@ using Aaru.CommonTypes.Interfaces; using Aaru.Helpers; using Ssse3 = System.Runtime.Intrinsics.X86.Ssse3; +namespace Aaru.Checksums; + /// /// Implements the Adler-32 algorithm public sealed class Adler32Context : IChecksum diff --git a/CDChecksums.cs b/CDChecksums.cs index 6fe12b8..d1d9867 100644 --- a/CDChecksums.cs +++ b/CDChecksums.cs @@ -31,13 +31,13 @@ // ECC algorithm from ECM(c) 2002-2011 Neill Corlett // ****************************************************************************/ -namespace Aaru.Checksums; - using System; using System.Collections.Generic; using Aaru.Console; using Aaru.Helpers; +namespace Aaru.Checksums; + /// Implements ReedSolomon and CRC32 algorithms as used by CD-ROM public static class CdChecksums { @@ -81,8 +81,8 @@ public static class CdChecksums { case 2448: { - var subchannel = new byte[96]; - var channel = new byte[2352]; + byte[] subchannel = new byte[96]; + byte[] channel = new byte[2352]; Array.Copy(buffer, 0, channel, 0, 2352); Array.Copy(buffer, 2352, subchannel, 0, 96); @@ -97,11 +97,11 @@ public static class CdChecksums status = false; status = channelStatus switch - { - null when subchannelStatus == true => true, - true when subchannelStatus == null => true, - _ => status - }; + { + null when subchannelStatus == true => true, + true when subchannelStatus == null => true, + _ => status + }; return status; } @@ -120,7 +120,7 @@ public static class CdChecksums for(uint i = 0; i < 256; i++) { uint edc = i; - var j = (uint)((i << 1) ^ ((i & 0x80) == 0x80 ? 0x11D : 0)); + uint j = (uint)((i << 1) ^ ((i & 0x80) == 0x80 ? 0x11D : 0)); _eccFTable[i] = (byte)j; _eccBTable[i ^ j] = (byte)i; @@ -139,7 +139,7 @@ public static class CdChecksums for(major = 0; major < majorCount; major++) { - uint index = (major >> 1) * majorMult + (major & 1); + uint index = ((major >> 1) * majorMult) + (major & 1); byte eccA = 0; byte eccB = 0; uint minor; @@ -200,7 +200,7 @@ public static class CdChecksums { //AaruConsole.DebugWriteLine("CD checksums", "Mode 0 sector at address {0:X2}:{1:X2}:{2:X2}", // channel[0x00C], channel[0x00D], channel[0x00E]); - for(var i = 0x010; i < 0x930; i++) + for(int i = 0x010; i < 0x930; i++) if(channel[i] != 0x00) { AaruConsole.DebugWriteLine("CD checksums", @@ -227,11 +227,11 @@ public static class CdChecksums return false; case 0x01: { - var address = new byte[4]; - var data = new byte[2060]; - var data2 = new byte[2232]; - var eccP = new byte[172]; - var eccQ = new byte[104]; + byte[] address = new byte[4]; + byte[] data = new byte[2060]; + byte[] data2 = new byte[2232]; + byte[] eccP = new byte[172]; + byte[] eccQ = new byte[104]; Array.Copy(channel, 0x0C, address, 0, 4); Array.Copy(channel, 0x10, data, 0, 2060); @@ -255,7 +255,7 @@ public static class CdChecksums "Mode 1 sector at address: {0:X2}:{1:X2}:{2:X2}, fails ECC Q check", channel[0x00C], channel[0x00D], channel[0x00E]); - var storedEdc = BitConverter.ToUInt32(channel, 0x810); + uint storedEdc = BitConverter.ToUInt32(channel, 0x810); uint calculatedEdc = ComputeEdc(0, channel, 0x810); correctEdc = calculatedEdc == storedEdc; @@ -275,7 +275,7 @@ public static class CdChecksums { //AaruConsole.DebugWriteLine("CD checksums", "Mode 2 sector at address {0:X2}:{1:X2}:{2:X2}", // channel[0x00C], channel[0x00D], channel[0x00E]); - var mode2Sector = new byte[channel.Length - 0x10]; + byte[] mode2Sector = new byte[channel.Length - 0x10]; Array.Copy(channel, 0x10, mode2Sector, 0, mode2Sector.Length); if((channel[0x012] & 0x20) == 0x20) // mode 2 form 2 @@ -288,7 +288,7 @@ public static class CdChecksums "Subheader copies differ in mode 2 form 2 sector at address: {0:X2}:{1:X2}:{2:X2}", channel[0x00C], channel[0x00D], channel[0x00E]); - var storedEdc = BitConverter.ToUInt32(mode2Sector, 0x91C); + uint storedEdc = BitConverter.ToUInt32(mode2Sector, 0x91C); // No CRC stored! if(storedEdc == 0x00000000) @@ -318,9 +318,9 @@ public static class CdChecksums "Subheader copies differ in mode 2 form 1 sector at address: {0:X2}:{1:X2}:{2:X2}", channel[0x00C], channel[0x00D], channel[0x00E]); - var address = new byte[4]; - var eccP = new byte[172]; - var eccQ = new byte[104]; + byte[] address = new byte[4]; + byte[] eccP = new byte[172]; + byte[] eccQ = new byte[104]; Array.Copy(mode2Sector, 0x80C, eccP, 0, 172); Array.Copy(mode2Sector, 0x8B8, eccQ, 0, 104); @@ -341,7 +341,7 @@ public static class CdChecksums "Mode 2 form 1 sector at address: {0:X2}:{1:X2}:{2:X2}, fails ECC Q check", channel[0x00C], channel[0x00D], channel[0x00E]); - var storedEdc = BitConverter.ToUInt32(mode2Sector, 0x808); + uint storedEdc = BitConverter.ToUInt32(mode2Sector, 0x808); uint calculatedEdc = ComputeEdc(0, mode2Sector, 0x808); correctEdc = calculatedEdc == storedEdc; @@ -367,7 +367,7 @@ public static class CdChecksums static uint ComputeEdc(uint edc, IReadOnlyList src, int size) { - var pos = 0; + int pos = 0; for(; size > 0; size--) edc = (edc >> 8) ^ _edcTable[(edc ^ src[pos++]) & 0xFF]; @@ -377,23 +377,23 @@ public static class CdChecksums static bool? CheckCdSectorSubChannel(IReadOnlyList subchannel) { - bool? status = true; - var qSubChannel = new byte[12]; - var cdTextPack1 = new byte[18]; - var cdTextPack2 = new byte[18]; - var cdTextPack3 = new byte[18]; - var cdTextPack4 = new byte[18]; - var cdSubRwPack1 = new byte[24]; - var cdSubRwPack2 = new byte[24]; - var cdSubRwPack3 = new byte[24]; - var cdSubRwPack4 = new byte[24]; + bool? status = true; + byte[] qSubChannel = new byte[12]; + byte[] cdTextPack1 = new byte[18]; + byte[] cdTextPack2 = new byte[18]; + byte[] cdTextPack3 = new byte[18]; + byte[] cdTextPack4 = new byte[18]; + byte[] cdSubRwPack1 = new byte[24]; + byte[] cdSubRwPack2 = new byte[24]; + byte[] cdSubRwPack3 = new byte[24]; + byte[] cdSubRwPack4 = new byte[24]; - var i = 0; + int i = 0; - for(var j = 0; j < 12; j++) + for(int j = 0; j < 12; j++) qSubChannel[j] = 0; - for(var j = 0; j < 18; j++) + for(int j = 0; j < 18; j++) { cdTextPack1[j] = 0; cdTextPack2[j] = 0; @@ -401,7 +401,7 @@ public static class CdChecksums cdTextPack4[j] = 0; } - for(var j = 0; j < 24; j++) + for(int j = 0; j < 24; j++) { cdSubRwPack1[j] = 0; cdSubRwPack2[j] = 0; @@ -409,7 +409,7 @@ public static class CdChecksums cdSubRwPack4[j] = 0; } - for(var j = 0; j < 12; j++) + for(int j = 0; j < 12; j++) { qSubChannel[j] = (byte)(qSubChannel[j] | ((subchannel[i++] & 0x40) << 1)); qSubChannel[j] = (byte)(qSubChannel[j] | (subchannel[i++] & 0x40)); @@ -423,7 +423,7 @@ public static class CdChecksums i = 0; - for(var j = 0; j < 18; j++) + for(int j = 0; j < 18; j++) { cdTextPack1[j] = (byte)(cdTextPack1[j] | ((subchannel[i++] & 0x3F) << 2)); @@ -440,7 +440,7 @@ public static class CdChecksums cdTextPack1[j] = (byte)(cdTextPack1[j] | (subchannel[i++] & 0x3F)); } - for(var j = 0; j < 18; j++) + for(int j = 0; j < 18; j++) { cdTextPack2[j] = (byte)(cdTextPack2[j] | ((subchannel[i++] & 0x3F) << 2)); @@ -457,7 +457,7 @@ public static class CdChecksums cdTextPack2[j] = (byte)(cdTextPack2[j] | (subchannel[i++] & 0x3F)); } - for(var j = 0; j < 18; j++) + for(int j = 0; j < 18; j++) { cdTextPack3[j] = (byte)(cdTextPack3[j] | ((subchannel[i++] & 0x3F) << 2)); @@ -474,7 +474,7 @@ public static class CdChecksums cdTextPack3[j] = (byte)(cdTextPack3[j] | (subchannel[i++] & 0x3F)); } - for(var j = 0; j < 18; j++) + for(int j = 0; j < 18; j++) { cdTextPack4[j] = (byte)(cdTextPack4[j] | ((subchannel[i++] & 0x3F) << 2)); @@ -493,16 +493,16 @@ public static class CdChecksums i = 0; - for(var j = 0; j < 24; j++) + for(int j = 0; j < 24; j++) cdSubRwPack1[j] = (byte)(subchannel[i++] & 0x3F); - for(var j = 0; j < 24; j++) + for(int j = 0; j < 24; j++) cdSubRwPack2[j] = (byte)(subchannel[i++] & 0x3F); - for(var j = 0; j < 24; j++) + for(int j = 0; j < 24; j++) cdSubRwPack3[j] = (byte)(subchannel[i++] & 0x3F); - for(var j = 0; j < 24; j++) + for(int j = 0; j < 24; j++) cdSubRwPack4[j] = (byte)(subchannel[i++] & 0x3F); switch(cdSubRwPack1[0]) @@ -544,8 +544,8 @@ public static class CdChecksums break; } - var qSubChannelCrc = BigEndianBitConverter.ToUInt16(qSubChannel, 10); - var qSubChannelForCrc = new byte[10]; + ushort qSubChannelCrc = BigEndianBitConverter.ToUInt16(qSubChannel, 10); + byte[] qSubChannelForCrc = new byte[10]; Array.Copy(qSubChannel, 0, qSubChannelForCrc, 0, 10); ushort calculatedQcrc = CRC16CCITTContext.Calculate(qSubChannelForCrc); @@ -559,8 +559,8 @@ public static class CdChecksums if((cdTextPack1[0] & 0x80) == 0x80) { - var cdTextPack1Crc = BigEndianBitConverter.ToUInt16(cdTextPack1, 16); - var cdTextPack1ForCrc = new byte[16]; + ushort cdTextPack1Crc = BigEndianBitConverter.ToUInt16(cdTextPack1, 16); + byte[] cdTextPack1ForCrc = new byte[16]; Array.Copy(cdTextPack1, 0, cdTextPack1ForCrc, 0, 16); ushort calculatedCdtp1Crc = CRC16CCITTContext.Calculate(cdTextPack1ForCrc); @@ -576,8 +576,8 @@ public static class CdChecksums if((cdTextPack2[0] & 0x80) == 0x80) { - var cdTextPack2Crc = BigEndianBitConverter.ToUInt16(cdTextPack2, 16); - var cdTextPack2ForCrc = new byte[16]; + ushort cdTextPack2Crc = BigEndianBitConverter.ToUInt16(cdTextPack2, 16); + byte[] cdTextPack2ForCrc = new byte[16]; Array.Copy(cdTextPack2, 0, cdTextPack2ForCrc, 0, 16); ushort calculatedCdtp2Crc = CRC16CCITTContext.Calculate(cdTextPack2ForCrc); @@ -596,8 +596,8 @@ public static class CdChecksums if((cdTextPack3[0] & 0x80) == 0x80) { - var cdTextPack3Crc = BigEndianBitConverter.ToUInt16(cdTextPack3, 16); - var cdTextPack3ForCrc = new byte[16]; + ushort cdTextPack3Crc = BigEndianBitConverter.ToUInt16(cdTextPack3, 16); + byte[] cdTextPack3ForCrc = new byte[16]; Array.Copy(cdTextPack3, 0, cdTextPack3ForCrc, 0, 16); ushort calculatedCdtp3Crc = CRC16CCITTContext.Calculate(cdTextPack3ForCrc); @@ -617,8 +617,8 @@ public static class CdChecksums if((cdTextPack4[0] & 0x80) != 0x80) return status; - var cdTextPack4Crc = BigEndianBitConverter.ToUInt16(cdTextPack4, 16); - var cdTextPack4ForCrc = new byte[16]; + ushort cdTextPack4Crc = BigEndianBitConverter.ToUInt16(cdTextPack4, 16); + byte[] cdTextPack4ForCrc = new byte[16]; Array.Copy(cdTextPack4, 0, cdTextPack4ForCrc, 0, 16); ushort calculatedCdtp4Crc = CRC16CCITTContext.Calculate(cdTextPack4ForCrc); diff --git a/CRC16Context.cs b/CRC16Context.cs index b914c6e..77ba3a6 100644 --- a/CRC16Context.cs +++ b/CRC16Context.cs @@ -30,8 +30,6 @@ // Copyright © 2011-2022 Natalia Portillo // ****************************************************************************/ -namespace Aaru.Checksums; - using System; using System.IO; using System.Runtime.InteropServices; @@ -39,6 +37,8 @@ using System.Text; using Aaru.CommonTypes.Interfaces; using Aaru.Helpers; +namespace Aaru.Checksums; + /// /// Implements a CRC16 algorithm public class Crc16Context : IChecksum diff --git a/CRC32/arm_simd.cs b/CRC32/arm_simd.cs index 67dc908..1c7d10e 100644 --- a/CRC32/arm_simd.cs +++ b/CRC32/arm_simd.cs @@ -45,18 +45,18 @@ // Copyright 2017 The Chromium Authors. All rights reserved. // ****************************************************************************/ -namespace Aaru.Checksums.CRC32; - using System; using System.Runtime.Intrinsics.Arm; +namespace Aaru.Checksums.CRC32; + static class ArmSimd { internal static uint Step64(byte[] buf, long len, uint crc) { uint c = crc; - var bufPos = 0; + int bufPos = 0; while(len >= 64) { @@ -96,7 +96,7 @@ static class ArmSimd { uint c = crc; - var bufPos = 0; + int bufPos = 0; while(len >= 32) { diff --git a/CRC32/clmul.cs b/CRC32/clmul.cs index 5bb11a3..d995212 100644 --- a/CRC32/clmul.cs +++ b/CRC32/clmul.cs @@ -47,13 +47,13 @@ // Copyright (C) 2013 Intel Corporation. All rights reserved. // ****************************************************************************/ -namespace Aaru.Checksums.CRC32; - using System; using System.Runtime.CompilerServices; using System.Runtime.Intrinsics; using System.Runtime.Intrinsics.X86; +namespace Aaru.Checksums.CRC32; + static class Clmul { static readonly uint[] _crcK = @@ -66,7 +66,7 @@ static class Clmul static void Fold4(ref Vector128 xmmCRC0, ref Vector128 xmmCRC1, ref Vector128 xmmCRC2, ref Vector128 xmmCRC3) { - var xmmFold4 = Vector128.Create(0xc6e41596, 0x00000001, 0x54442bd4, 0x00000001); + Vector128 xmmFold4 = Vector128.Create(0xc6e41596, 0x00000001, 0x54442bd4, 0x00000001); Vector128 xTmp0 = xmmCRC0; Vector128 xTmp1 = xmmCRC1; @@ -110,37 +110,41 @@ static class Clmul Vector128 xmmCRC1 = Vector128.Zero; Vector128 xmmCRC2 = Vector128.Zero; Vector128 xmmCRC3 = Vector128.Zero; - var bufPos = 0; + int bufPos = 0; - var first = true; + bool first = true; /* fold 512 to 32 step variable declarations for ISO-C90 compat. */ - var xmmMask = Vector128.Create(0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000); - var xmmMask2 = Vector128.Create(0x00000000, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF); + Vector128 xmmMask = Vector128.Create(0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000); + Vector128 xmmMask2 = Vector128.Create(0x00000000, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF); while((len -= 64) >= 0) { - var xmmT0 = Vector128.Create(BitConverter.ToUInt32(src, bufPos), BitConverter.ToUInt32(src, bufPos + 4), - BitConverter.ToUInt32(src, bufPos + 8), - BitConverter.ToUInt32(src, bufPos + 12)); + Vector128 xmmT0 = Vector128.Create(BitConverter.ToUInt32(src, bufPos), + BitConverter.ToUInt32(src, bufPos + 4), + BitConverter.ToUInt32(src, bufPos + 8), + BitConverter.ToUInt32(src, bufPos + 12)); bufPos += 16; - var xmmT1 = Vector128.Create(BitConverter.ToUInt32(src, bufPos), BitConverter.ToUInt32(src, bufPos + 4), - BitConverter.ToUInt32(src, bufPos + 8), - BitConverter.ToUInt32(src, bufPos + 12)); + Vector128 xmmT1 = Vector128.Create(BitConverter.ToUInt32(src, bufPos), + BitConverter.ToUInt32(src, bufPos + 4), + BitConverter.ToUInt32(src, bufPos + 8), + BitConverter.ToUInt32(src, bufPos + 12)); bufPos += 16; - var xmmT2 = Vector128.Create(BitConverter.ToUInt32(src, bufPos), BitConverter.ToUInt32(src, bufPos + 4), - BitConverter.ToUInt32(src, bufPos + 8), - BitConverter.ToUInt32(src, bufPos + 12)); + Vector128 xmmT2 = Vector128.Create(BitConverter.ToUInt32(src, bufPos), + BitConverter.ToUInt32(src, bufPos + 4), + BitConverter.ToUInt32(src, bufPos + 8), + BitConverter.ToUInt32(src, bufPos + 12)); bufPos += 16; - var xmmT3 = Vector128.Create(BitConverter.ToUInt32(src, bufPos), BitConverter.ToUInt32(src, bufPos + 4), - BitConverter.ToUInt32(src, bufPos + 8), - BitConverter.ToUInt32(src, bufPos + 12)); + Vector128 xmmT3 = Vector128.Create(BitConverter.ToUInt32(src, bufPos), + BitConverter.ToUInt32(src, bufPos + 4), + BitConverter.ToUInt32(src, bufPos + 8), + BitConverter.ToUInt32(src, bufPos + 12)); bufPos += 16; @@ -163,7 +167,7 @@ static class Clmul /* * k1 */ - var crcFold = Vector128.Create(_crcK[0], _crcK[1], _crcK[2], _crcK[3]); + Vector128 crcFold = Vector128.Create(_crcK[0], _crcK[1], _crcK[2], _crcK[3]); Vector128 xTmp0 = Pclmulqdq.CarrylessMultiply(xmmCRC0.AsUInt64(), crcFold.AsUInt64(), 0x10).AsUInt32(); diff --git a/CRC32Context.cs b/CRC32Context.cs index eae5124..5086f80 100644 --- a/CRC32Context.cs +++ b/CRC32Context.cs @@ -30,8 +30,6 @@ // Copyright © 2011-2022 Natalia Portillo // ****************************************************************************/ -namespace Aaru.Checksums; - using System; using System.IO; using System.Runtime.InteropServices; @@ -42,6 +40,8 @@ using Aaru.Checksums.CRC32; using Aaru.CommonTypes.Interfaces; using Aaru.Helpers; +namespace Aaru.Checksums; + /// /// Implements a CRC32 algorithm public sealed class Crc32Context : IChecksum diff --git a/CRC64/clmul.cs b/CRC64/clmul.cs index 82794bc..c925a09 100644 --- a/CRC64/clmul.cs +++ b/CRC64/clmul.cs @@ -21,13 +21,13 @@ // Copyright © 2011-2022 Natalia Portillo // ****************************************************************************/ -namespace Aaru.Checksums.CRC64; - using System; using System.Runtime.CompilerServices; using System.Runtime.Intrinsics; using System.Runtime.Intrinsics.X86; +namespace Aaru.Checksums.CRC64; + static class Clmul { static readonly byte[] _shuffleMasks = @@ -42,13 +42,14 @@ static class Clmul { uint maskPos = 16 - n; - var maskA = Vector128.Create(_shuffleMasks[maskPos], _shuffleMasks[maskPos + 1], _shuffleMasks[maskPos + 2], - _shuffleMasks[maskPos + 3], _shuffleMasks[maskPos + 4], _shuffleMasks[maskPos + 5], - _shuffleMasks[maskPos + 6], _shuffleMasks[maskPos + 7], _shuffleMasks[maskPos + 8], - _shuffleMasks[maskPos + 9], _shuffleMasks[maskPos + 10], - _shuffleMasks[maskPos + 11], _shuffleMasks[maskPos + 12], - _shuffleMasks[maskPos + 13], _shuffleMasks[maskPos + 14], - _shuffleMasks[maskPos + 15]); + Vector128 maskA = Vector128.Create(_shuffleMasks[maskPos], _shuffleMasks[maskPos + 1], + _shuffleMasks[maskPos + 2], _shuffleMasks[maskPos + 3], + _shuffleMasks[maskPos + 4], _shuffleMasks[maskPos + 5], + _shuffleMasks[maskPos + 6], _shuffleMasks[maskPos + 7], + _shuffleMasks[maskPos + 8], _shuffleMasks[maskPos + 9], + _shuffleMasks[maskPos + 10], _shuffleMasks[maskPos + 11], + _shuffleMasks[maskPos + 12], _shuffleMasks[maskPos + 13], + _shuffleMasks[maskPos + 14], _shuffleMasks[maskPos + 15]); Vector128 maskB = Sse2.Xor(maskA, Sse2.CompareEqual(Vector128.Zero, Vector128.Zero)); @@ -63,14 +64,14 @@ static class Clmul internal static ulong Step(ulong crc, byte[] data, uint length) { - var bufPos = 16; - const ulong k1 = 0xe05dd497ca393ae4; - const ulong k2 = 0xdabe95afc7875f40; - const ulong mu = 0x9c3e466c172963d5; - const ulong pol = 0x92d8af2baf0e1e85; - var foldConstants1 = Vector128.Create(k1, k2); - var foldConstants2 = Vector128.Create(mu, pol); - var initialCrc = Vector128.Create(~crc, 0); + int bufPos = 16; + const ulong k1 = 0xe05dd497ca393ae4; + const ulong k2 = 0xdabe95afc7875f40; + const ulong mu = 0x9c3e466c172963d5; + const ulong pol = 0x92d8af2baf0e1e85; + Vector128 foldConstants1 = Vector128.Create(k1, k2); + Vector128 foldConstants2 = Vector128.Create(mu, pol); + Vector128 initialCrc = Vector128.Create(~crc, 0); length -= 16; // Initial CRC can simply be added to data diff --git a/CRC64Context.cs b/CRC64Context.cs index 5f8b55a..98c03a5 100644 --- a/CRC64Context.cs +++ b/CRC64Context.cs @@ -30,8 +30,6 @@ // Copyright © 2011-2022 Natalia Portillo // ****************************************************************************/ -namespace Aaru.Checksums; - using System; using System.IO; using System.Runtime.InteropServices; @@ -41,6 +39,8 @@ using Aaru.Checksums.CRC64; using Aaru.CommonTypes.Interfaces; using Aaru.Helpers; +namespace Aaru.Checksums; + /// /// Implements a CRC64 algorithm public sealed class Crc64Context : IChecksum diff --git a/FletcherContext.cs b/FletcherContext.cs index 3b507c5..2865b27 100644 --- a/FletcherContext.cs +++ b/FletcherContext.cs @@ -32,8 +32,6 @@ // Disabled because the speed is abnormally slow -namespace Aaru.Checksums; - using System; using System.IO; using System.Runtime.InteropServices; @@ -41,6 +39,8 @@ using System.Text; using Aaru.CommonTypes.Interfaces; using Aaru.Helpers; +namespace Aaru.Checksums; + /// /// Implements the Fletcher-32 algorithm public sealed class Fletcher32Context : IChecksum diff --git a/MD5Context.cs b/MD5Context.cs index 985a5a0..82079a0 100644 --- a/MD5Context.cs +++ b/MD5Context.cs @@ -30,14 +30,14 @@ // Copyright © 2011-2022 Natalia Portillo // ****************************************************************************/ -namespace Aaru.Checksums; - using System; using System.IO; using System.Security.Cryptography; using System.Text; using Aaru.CommonTypes.Interfaces; +namespace Aaru.Checksums; + /// /// Wraps up .NET MD5 implementation to a Init(), Update(), Final() context. public sealed class Md5Context : IChecksum diff --git a/Native.cs b/Native.cs index 481aa95..86fea35 100644 --- a/Native.cs +++ b/Native.cs @@ -30,10 +30,10 @@ // Copyright © 2011-2022 Natalia Portillo // ****************************************************************************/ -namespace Aaru.Checksums; - using System.Runtime.InteropServices; +namespace Aaru.Checksums; + /// Handles native implementations of compression algorithms public static class Native { diff --git a/ReedSolomon.cs b/ReedSolomon.cs index f545fb6..0e6f97d 100644 --- a/ReedSolomon.cs +++ b/ReedSolomon.cs @@ -57,11 +57,11 @@ * */ -namespace Aaru.Checksums; - using System; using Aaru.Console; +namespace Aaru.Checksums; + /// Implements the Reed-Solomon algorithm public class ReedSolomon { @@ -87,69 +87,69 @@ public class ReedSolomon public void InitRs(int n, int k, int m) { _pp = m switch - { - 2 => new[] - { - 1, 1, 1 - }, - 3 => new[] - { - 1, 1, 0, 1 - }, - 4 => new[] - { - 1, 1, 0, 0, 1 - }, - 5 => new[] - { - 1, 0, 1, 0, 0, 1 - }, - 6 => new[] - { - 1, 1, 0, 0, 0, 0, 1 - }, - 7 => new[] - { - 1, 0, 0, 1, 0, 0, 0, 1 - }, - 8 => new[] - { - 1, 0, 1, 1, 1, 0, 0, 0, 1 - }, - 9 => new[] - { - 1, 0, 0, 0, 1, 0, 0, 0, 0, 1 - }, - 10 => new[] - { - 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1 - }, - 11 => new[] - { - 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 - }, - 12 => new[] - { - 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1 - }, - 13 => new[] - { - 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 - }, - 14 => new[] - { - 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1 - }, - 15 => new[] - { - 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 - }, - 16 => new[] - { - 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1 - }, - _ => throw new ArgumentOutOfRangeException(nameof(m), "m must be between 2 and 16 inclusive") - }; + { + 2 => new[] + { + 1, 1, 1 + }, + 3 => new[] + { + 1, 1, 0, 1 + }, + 4 => new[] + { + 1, 1, 0, 0, 1 + }, + 5 => new[] + { + 1, 0, 1, 0, 0, 1 + }, + 6 => new[] + { + 1, 1, 0, 0, 0, 0, 1 + }, + 7 => new[] + { + 1, 0, 0, 1, 0, 0, 0, 1 + }, + 8 => new[] + { + 1, 0, 1, 1, 1, 0, 0, 0, 1 + }, + 9 => new[] + { + 1, 0, 0, 0, 1, 0, 0, 0, 0, 1 + }, + 10 => new[] + { + 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1 + }, + 11 => new[] + { + 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 + }, + 12 => new[] + { + 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1 + }, + 13 => new[] + { + 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 + }, + 14 => new[] + { + 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1 + }, + 15 => new[] + { + 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 + }, + 16 => new[] + { + 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1 + }, + _ => throw new ArgumentOutOfRangeException(nameof(m), "m must be between 2 and 16 inclusive") + }; _mm = m; _kk = k; @@ -237,7 +237,7 @@ public class ReedSolomon { int i; - var mask = 1; + int mask = 1; _alphaTo[_mm] = 0; for(i = 0; i < _mm; i++) @@ -396,18 +396,18 @@ public class ReedSolomon throw new UnauthorizedAccessException("Trying to calculate RS without initializing!"); erasPos = new int[_nn - _kk]; - int i, j; - int q, tmp; - var recd = new int[_nn]; - var lambda = new int[_nn - _kk + 1]; /* Err+Eras Locator poly */ - var s = new int[_nn - _kk + 1]; /* syndrome poly */ - var b = new int[_nn - _kk + 1]; - var t = new int[_nn - _kk + 1]; - var omega = new int[_nn - _kk + 1]; - var root = new int[_nn - _kk]; - var reg = new int[_nn - _kk + 1]; - var loc = new int[_nn - _kk]; - int count; + int i, j; + int q, tmp; + int[] recd = new int[_nn]; + int[] lambda = new int[_nn - _kk + 1]; /* Err+Eras Locator poly */ + int[] s = new int[_nn - _kk + 1]; /* syndrome poly */ + int[] b = new int[_nn - _kk + 1]; + int[] t = new int[_nn - _kk + 1]; + int[] omega = new int[_nn - _kk + 1]; + int[] root = new int[_nn - _kk]; + int[] reg = new int[_nn - _kk + 1]; + int[] loc = new int[_nn - _kk]; + int count; /* data[] is in polynomial form, copy and convert to index form */ for(i = _nn - 1; i >= 0; i--) @@ -422,7 +422,7 @@ public class ReedSolomon /* first form the syndromes; i.e., evaluate recd(x) at roots of g(x) * namely @**(B0+i), i = 0, ... ,(NN-KK-1) */ - var synError = 0; + int synError = 0; for(i = 1; i <= _nn - _kk; i++) { @@ -430,7 +430,7 @@ public class ReedSolomon for(j = 0; j < _nn; j++) if(recd[j] != _a0) /* recd[j] in index form */ - tmp ^= _alphaTo[Modnn(recd[j] + (B0 + i - 1) * j)]; + tmp ^= _alphaTo[Modnn(recd[j] + ((B0 + i - 1) * j))]; synError |= tmp; /* set flag if non-zero syndrome => * error */ @@ -523,7 +523,7 @@ public class ReedSolomon { /* r is the step number */ /* Compute discrepancy at the r-th step in poly-form */ - var discrR = 0; + int discrR = 0; for(i = 0; i < r; i++) if(lambda[i] != 0 && @@ -572,7 +572,7 @@ public class ReedSolomon } /* Convert lambda to index form and compute deg(lambda(x)) */ - var degLambda = 0; + int degLambda = 0; for(i = 0; i < _nn - _kk + 1; i++) { @@ -627,7 +627,7 @@ public class ReedSolomon * Compute err+eras evaluator poly omega(x) = s(x)*lambda(x) (modulo * x**(NN-KK)). in index form. Also find deg(omega). */ - var degOmega = 0; + int degOmega = 0; for(i = 0; i < _nn - _kk; i++) { @@ -653,19 +653,19 @@ public class ReedSolomon */ for(j = count - 1; j >= 0; j--) { - var num1 = 0; + int num1 = 0; for(i = degOmega; i >= 0; i--) if(omega[i] != _a0) - num1 ^= _alphaTo[Modnn(omega[i] + i * root[j])]; + num1 ^= _alphaTo[Modnn(omega[i] + (i * root[j]))]; - int num2 = _alphaTo[Modnn(root[j] * (B0 - 1) + _nn)]; - var den = 0; + int num2 = _alphaTo[Modnn((root[j] * (B0 - 1)) + _nn)]; + int den = 0; /* lambda[i+1] for i even is the formal derivative lambda_pr of lambda[i] */ for(i = Min(degLambda, _nn - _kk - 1) & ~1; i >= 0; i -= 2) if(lambda[i + 1] != _a0) - den ^= _alphaTo[Modnn(lambda[i + 1] + i * root[j])]; + den ^= _alphaTo[Modnn(lambda[i + 1] + (i * root[j]))]; if(den == 0) { diff --git a/Register.cs b/Register.cs index c416400..96d94eb 100644 --- a/Register.cs +++ b/Register.cs @@ -36,14 +36,14 @@ // Copyright © 2011-2022 Natalia Portillo // ****************************************************************************/ -namespace Aaru.Checksums; - using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using Aaru.CommonTypes.Interfaces; +namespace Aaru.Checksums; + /// public sealed class Register : IPluginRegister { diff --git a/SHA1Context.cs b/SHA1Context.cs index ccb360a..b559d62 100644 --- a/SHA1Context.cs +++ b/SHA1Context.cs @@ -30,14 +30,14 @@ // Copyright © 2011-2022 Natalia Portillo // ****************************************************************************/ -namespace Aaru.Checksums; - using System; using System.IO; using System.Security.Cryptography; using System.Text; using Aaru.CommonTypes.Interfaces; +namespace Aaru.Checksums; + /// /// Wraps up .NET SHA1 implementation to a Init(), Update(), Final() context. public sealed class Sha1Context : IChecksum diff --git a/SHA256Context.cs b/SHA256Context.cs index be65193..51ac0e7 100644 --- a/SHA256Context.cs +++ b/SHA256Context.cs @@ -30,14 +30,14 @@ // Copyright © 2011-2022 Natalia Portillo // ****************************************************************************/ -namespace Aaru.Checksums; - using System; using System.IO; using System.Security.Cryptography; using System.Text; using Aaru.CommonTypes.Interfaces; +namespace Aaru.Checksums; + /// /// Wraps up .NET SHA256 implementation to a Init(), Update(), Final() context. public sealed class Sha256Context : IChecksum diff --git a/SHA384Context.cs b/SHA384Context.cs index 26601c7..1ad4896 100644 --- a/SHA384Context.cs +++ b/SHA384Context.cs @@ -30,14 +30,14 @@ // Copyright © 2011-2022 Natalia Portillo // ****************************************************************************/ -namespace Aaru.Checksums; - using System; using System.IO; using System.Security.Cryptography; using System.Text; using Aaru.CommonTypes.Interfaces; +namespace Aaru.Checksums; + /// /// Wraps up .NET SHA384 implementation to a Init(), Update(), Final() context. public sealed class Sha384Context : IChecksum diff --git a/SHA512Context.cs b/SHA512Context.cs index 8dbe6fa..3ab7ef6 100644 --- a/SHA512Context.cs +++ b/SHA512Context.cs @@ -30,14 +30,14 @@ // Copyright © 2011-2022 Natalia Portillo // ****************************************************************************/ -namespace Aaru.Checksums; - using System; using System.IO; using System.Security.Cryptography; using System.Text; using Aaru.CommonTypes.Interfaces; +namespace Aaru.Checksums; + /// /// Wraps up .NET SHA512 implementation to a Init(), Update(), Final() context. public sealed class Sha512Context : IChecksum diff --git a/SpamSumContext.cs b/SpamSumContext.cs index 3fc0dc8..4768f17 100644 --- a/SpamSumContext.cs +++ b/SpamSumContext.cs @@ -39,13 +39,13 @@ // http://www.samba.org/ftp/unpacked/junkcode/spamsum/ // http://ssdeep.sf.net/ -namespace Aaru.Checksums; - using System; using System.Runtime.CompilerServices; using System.Text; using Aaru.CommonTypes.Interfaces; +namespace Aaru.Checksums; + /// /// Implements the SpamSum fuzzy hashing algorithm. public sealed class SpamSumContext : IChecksum @@ -56,7 +56,7 @@ public sealed class SpamSumContext : IChecksum const uint HASH_INIT = 0x28021967; const uint NUM_BLOCKHASHES = 31; const uint SPAMSUM_LENGTH = 64; - const uint FUZZY_MAX_RESULT = 2 * SPAMSUM_LENGTH + 20; + const uint FUZZY_MAX_RESULT = (2 * SPAMSUM_LENGTH) + 20; //"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; readonly byte[] _b64 = @@ -77,7 +77,7 @@ public sealed class SpamSumContext : IChecksum Bh = new BlockhashContext[NUM_BLOCKHASHES] }; - for(var i = 0; i < NUM_BLOCKHASHES; i++) + for(int i = 0; i < NUM_BLOCKHASHES; i++) _self.Bh[i].Digest = new byte[SPAMSUM_LENGTH]; _self.Bhstart = 0; @@ -99,7 +99,7 @@ public sealed class SpamSumContext : IChecksum { _self.TotalSize += len; - for(var i = 0; i < len; i++) + for(int i = 0; i < len; i++) fuzzy_engine_step(data[i]); } @@ -274,7 +274,7 @@ public sealed class SpamSumContext : IChecksum var sb = new StringBuilder(); uint bi = _self.Bhstart; uint h = roll_sum(); - var remain = (int)(FUZZY_MAX_RESULT - 1); /* Exclude terminating '\0'. */ + int remain = (int)(FUZZY_MAX_RESULT - 1); /* Exclude terminating '\0'. */ result = new byte[FUZZY_MAX_RESULT]; /* Verify that our elimination was not overeager. */ @@ -471,7 +471,7 @@ public sealed class SpamSumContext : IChecksum [MethodImpl(MethodImplOptions.AggressiveInlining)] static string CToString(byte[] cString) { - var count = 0; + int count = 0; // ReSharper disable once LoopCanBeConvertedToQuery // LINQ is six times slower