REFACTOR: All refactor in DiscImageChef.Checksums.

This commit is contained in:
2017-12-21 20:26:46 +00:00
parent 899232216e
commit 18ddb2b575
13 changed files with 60 additions and 78 deletions

View File

@@ -103,8 +103,7 @@ namespace DiscImageChef.Checksums
/// <param name="filename">File path.</param>
public static byte[] File(string filename)
{
byte[] hash;
File(filename, out hash);
File(filename, out byte[] hash);
return hash;
}
@@ -135,7 +134,7 @@ namespace DiscImageChef.Checksums
StringBuilder adlerOutput = new StringBuilder();
for(int i = 0; i < hash.Length; i++) adlerOutput.Append(hash[i].ToString("x2"));
foreach(byte h in hash) adlerOutput.Append(h.ToString("x2"));
fileStream.Close();
@@ -169,7 +168,7 @@ namespace DiscImageChef.Checksums
StringBuilder adlerOutput = new StringBuilder();
for(int i = 0; i < hash.Length; i++) adlerOutput.Append(hash[i].ToString("x2"));
foreach(byte h in hash) adlerOutput.Append(h.ToString("x2"));
return adlerOutput.ToString();
}

View File

@@ -32,6 +32,7 @@
// ****************************************************************************/
using System;
using System.Linq;
using DiscImageChef.Console;
namespace DiscImageChef.Checksums
@@ -101,8 +102,7 @@ namespace DiscImageChef.Checksums
for(minor = 0; minor < minorCount; minor++)
{
byte temp;
if(index < 4) temp = address[index];
else temp = data[index - 4];
temp = index < 4 ? address[index] : data[index - 4];
index += minorInc;
if(index >= size) index -= size;
eccA ^= temp;
@@ -185,7 +185,7 @@ namespace DiscImageChef.Checksums
"Mode 1 sector at address: {0:X2}:{1:X2}:{2:X2}, fails ECC Q check",
channel[0x00C], channel[0x00D], channel[0x00E]);
if(failedEccP || failedEccQ) return false;
return !failedEccP && !failedEccQ;
/* TODO: This is not working
byte[] SectorForCheck = new byte[0x810];
@@ -200,8 +200,6 @@ namespace DiscImageChef.Checksums
DicConsole.DebugWriteLine("CD checksums", "Mode 1 sector at address: {0:X2}:{1:X2}:{2:X2}, got CRC 0x{3:X8} expected 0x{4:X8}", channel[0x00C], channel[0x00D], channel[0x00E], CalculatedEDC, StoredEDC);
return false;
}*/
return true;
}
if(channel[0x00F] == 0x02) // mode (1 byte)
@@ -266,7 +264,7 @@ namespace DiscImageChef.Checksums
"Mode 2 form 1 sector at address: {0:X2}:{1:X2}:{2:X2}, fails ECC Q check",
channel[0x00F], channel[0x00C], channel[0x00D], channel[0x00E]);
if(failedEccP || failedEccQ) return false;
return !failedEccP && !failedEccQ;
/* TODO: This is not working
byte[] SectorForCheck = new byte[0x808];
@@ -515,8 +513,7 @@ namespace DiscImageChef.Checksums
static ushort CalculateCCITT_CRC16(byte[] buffer)
{
ushort crc16 = 0;
for(int i = 0; i < buffer.Length; i++) crc16 = (ushort)(CcittCrc16Table[(crc16 >> 8) ^ buffer[i]] ^ (crc16 << 8));
ushort crc16 = buffer.Aggregate<byte, ushort>(0, (current, t) => (ushort)(CcittCrc16Table[(current >> 8) ^ t] ^ (current << 8)));
crc16 = (ushort)~crc16;

View File

@@ -115,8 +115,7 @@ namespace DiscImageChef.Checksums
/// <param name="filename">File path.</param>
public static byte[] File(string filename)
{
byte[] hash;
File(filename, out hash);
File(filename, out byte[] hash);
return hash;
}
@@ -128,12 +127,11 @@ namespace DiscImageChef.Checksums
public static string File(string filename, out byte[] hash)
{
FileStream fileStream = new FileStream(filename, FileMode.Open);
ushort[] localTable;
ushort localhashInt;
localhashInt = CRC16_SEED;
localTable = new ushort[256];
ushort[] localTable = new ushort[256];
for(int i = 0; i < 256; i++)
{
ushort entry = (ushort)i;
@@ -152,7 +150,7 @@ namespace DiscImageChef.Checksums
StringBuilder crc16Output = new StringBuilder();
for(int i = 0; i < hash.Length; i++) crc16Output.Append(hash[i].ToString("x2"));
foreach(byte h in hash) crc16Output.Append(h.ToString("x2"));
fileStream.Close();
@@ -180,12 +178,11 @@ namespace DiscImageChef.Checksums
/// <param name="seed">CRC seed</param>
public static string Data(byte[] data, uint len, out byte[] hash, ushort polynomial, ushort seed)
{
ushort[] localTable;
ushort localhashInt;
localhashInt = seed;
localTable = new ushort[256];
ushort[] localTable = new ushort[256];
for(int i = 0; i < 256; i++)
{
ushort entry = (ushort)i;
@@ -204,7 +201,7 @@ namespace DiscImageChef.Checksums
StringBuilder crc16Output = new StringBuilder();
for(int i = 0; i < hash.Length; i++) crc16Output.Append(hash[i].ToString("x2"));
foreach(byte h in hash) crc16Output.Append(h.ToString("x2"));
return crc16Output.ToString();
}

View File

@@ -115,8 +115,7 @@ namespace DiscImageChef.Checksums
/// <param name="filename">File path.</param>
public static byte[] File(string filename)
{
byte[] hash;
File(filename, out hash);
File(filename, out byte[] hash);
return hash;
}
@@ -128,12 +127,11 @@ namespace DiscImageChef.Checksums
public static string File(string filename, out byte[] hash)
{
FileStream fileStream = new FileStream(filename, FileMode.Open);
uint[] localTable;
uint localhashInt;
localhashInt = CRC32_SEED;
localTable = new uint[256];
uint[] localTable = new uint[256];
for(int i = 0; i < 256; i++)
{
uint entry = (uint)i;
@@ -153,7 +151,7 @@ namespace DiscImageChef.Checksums
StringBuilder crc32Output = new StringBuilder();
for(int i = 0; i < hash.Length; i++) crc32Output.Append(hash[i].ToString("x2"));
foreach(byte h in hash) crc32Output.Append(h.ToString("x2"));
fileStream.Close();
@@ -181,12 +179,11 @@ namespace DiscImageChef.Checksums
/// <param name="seed">CRC seed</param>
public static string Data(byte[] data, uint len, out byte[] hash, uint polynomial, uint seed)
{
uint[] localTable;
uint localhashInt;
localhashInt = seed;
localTable = new uint[256];
uint[] localTable = new uint[256];
for(int i = 0; i < 256; i++)
{
uint entry = (uint)i;
@@ -205,7 +202,7 @@ namespace DiscImageChef.Checksums
StringBuilder crc32Output = new StringBuilder();
for(int i = 0; i < hash.Length; i++) crc32Output.Append(hash[i].ToString("x2"));
foreach(byte h in hash) crc32Output.Append(h.ToString("x2"));
return crc32Output.ToString();
}

View File

@@ -114,8 +114,7 @@ namespace DiscImageChef.Checksums
/// <param name="filename">File path.</param>
public static byte[] File(string filename)
{
byte[] localHash;
File(filename, out localHash);
File(filename, out byte[] localHash);
return localHash;
}
@@ -127,12 +126,11 @@ namespace DiscImageChef.Checksums
public static string File(string filename, out byte[] hash)
{
FileStream fileStream = new FileStream(filename, FileMode.Open);
ulong[] localTable;
ulong localhashInt;
localhashInt = CRC64_SEED;
localTable = new ulong[256];
ulong[] localTable = new ulong[256];
for(int i = 0; i < 256; i++)
{
ulong entry = (ulong)i;
@@ -152,7 +150,7 @@ namespace DiscImageChef.Checksums
StringBuilder crc64Output = new StringBuilder();
for(int i = 0; i < hash.Length; i++) crc64Output.Append(hash[i].ToString("x2"));
foreach(byte h in hash) crc64Output.Append(h.ToString("x2"));
fileStream.Close();
@@ -180,12 +178,11 @@ namespace DiscImageChef.Checksums
/// <param name="seed">CRC seed</param>
public static string Data(byte[] data, uint len, out byte[] hash, ulong polynomial, ulong seed)
{
ulong[] localTable;
ulong localhashInt;
localhashInt = seed;
localTable = new ulong[256];
ulong[] localTable = new ulong[256];
for(int i = 0; i < 256; i++)
{
ulong entry = (ulong)i;
@@ -204,7 +201,7 @@ namespace DiscImageChef.Checksums
StringBuilder crc64Output = new StringBuilder();
for(int i = 0; i < hash.Length; i++) crc64Output.Append(hash[i].ToString("x2"));
foreach(byte h in hash) crc64Output.Append(h.ToString("x2"));
return crc64Output.ToString();
}

View File

@@ -87,7 +87,7 @@ namespace DiscImageChef.Checksums
md5Provider.TransformFinalBlock(new byte[0], 0, 0);
StringBuilder md5Output = new StringBuilder();
for(int i = 0; i < md5Provider.Hash.Length; i++) md5Output.Append(md5Provider.Hash[i].ToString("x2"));
foreach(byte h in md5Provider.Hash) md5Output.Append(h.ToString("x2"));
return md5Output.ToString();
}
@@ -115,7 +115,7 @@ namespace DiscImageChef.Checksums
hash = md5Provider.ComputeHash(fileStream);
StringBuilder md5Output = new StringBuilder();
for(int i = 0; i < hash.Length; i++) md5Output.Append(hash[i].ToString("x2"));
foreach(byte h in hash) md5Output.Append(h.ToString("x2"));
fileStream.Close();
@@ -133,7 +133,7 @@ namespace DiscImageChef.Checksums
hash = md5Provider.ComputeHash(data, 0, (int)len);
StringBuilder md5Output = new StringBuilder();
for(int i = 0; i < hash.Length; i++) md5Output.Append(hash[i].ToString("x2"));
foreach(byte h in hash) md5Output.Append(h.ToString("x2"));
return md5Output.ToString();
}

View File

@@ -87,7 +87,7 @@ namespace DiscImageChef.Checksums
ripemd160Provider.TransformFinalBlock(new byte[0], 0, 0);
StringBuilder ripemd160Output = new StringBuilder();
for(int i = 0; i < ripemd160Provider.Hash.Length; i++) ripemd160Output.Append(ripemd160Provider.Hash[i].ToString("x2"));
foreach(byte h in ripemd160Provider.Hash) ripemd160Output.Append(h.ToString("x2"));
return ripemd160Output.ToString();
}
@@ -115,7 +115,7 @@ namespace DiscImageChef.Checksums
hash = ripemd160Provider.ComputeHash(fileStream);
StringBuilder ripemd160Output = new StringBuilder();
for(int i = 0; i < hash.Length; i++) ripemd160Output.Append(hash[i].ToString("x2"));
foreach(byte h in hash) ripemd160Output.Append(h.ToString("x2"));
fileStream.Close();
@@ -133,7 +133,7 @@ namespace DiscImageChef.Checksums
hash = ripemd160Provider.ComputeHash(data, 0, (int)len);
StringBuilder ripemd160Output = new StringBuilder();
for(int i = 0; i < hash.Length; i++) ripemd160Output.Append(hash[i].ToString("x2"));
foreach(byte h in hash) ripemd160Output.Append(h.ToString("x2"));
return ripemd160Output.ToString();
}

View File

@@ -268,7 +268,7 @@ namespace DiscImageChef.Checksums
*/
void gen_poly()
{
int i, j;
int i;
gg[0] = alpha_to[B0];
gg[1] = 1; /* g(x) = (X+@**B0) initially */
@@ -279,7 +279,7 @@ namespace DiscImageChef.Checksums
* Below multiply (Gg[0]+Gg[1]*x + ... +Gg[i]x^i) by
* (@**(B0+i-1) + x)
*/
for(j = i - 1; j > 0; j--)
for(int j = i - 1; j > 0; j--)
if(gg[j] != 0) gg[j] = gg[j - 1] ^ alpha_to[Modnn(index_of[gg[j]] + B0 + i - 1)];
else gg[j] = gg[j - 1];
/* Gg[0] can never be zero */
@@ -307,8 +307,7 @@ namespace DiscImageChef.Checksums
{
if(!initialized) throw new UnauthorizedAccessException("Trying to calculate RS without initializing!");
int i, j;
int feedback;
int i;
bb = new int[nn - kk];
Clear(ref bb, nn - kk);
@@ -316,11 +315,11 @@ namespace DiscImageChef.Checksums
{
if(mm != 8) if(data[i] > nn) return -1; /* Illegal symbol */
feedback = index_of[data[i] ^ bb[nn - kk - 1]];
int feedback = index_of[data[i] ^ bb[nn - kk - 1]];
if(feedback != a0)
{
/* feedback term is non-zero */
for(j = nn - kk - 1; j > 0; j--)
for(int j = nn - kk - 1; j > 0; j--)
if(gg[j] != a0) bb[j] = bb[j - 1] ^ alpha_to[Modnn(gg[j] + feedback)];
else bb[j] = bb[j - 1];
@@ -330,7 +329,7 @@ namespace DiscImageChef.Checksums
{
/* feedback term is zero. encoder becomes a
* single-byte shifter */
for(j = nn - kk - 1; j > 0; j--) bb[j] = bb[j - 1];
for(int j = nn - kk - 1; j > 0; j--) bb[j] = bb[j - 1];
bb[0] = 0;
}
@@ -366,7 +365,7 @@ namespace DiscImageChef.Checksums
erasPos = new int[nn - kk];
int degLambda, el, degOmega;
int i, j, r;
int u, q, tmp, num1, num2, den, discrR;
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 */
@@ -412,7 +411,7 @@ namespace DiscImageChef.Checksums
lambda[1] = alpha_to[erasPos[0]];
for(i = 1; i < noEras; i++)
{
u = erasPos[i];
int u = erasPos[i];
for(j = i + 1; j > 0; j--)
{
tmp = index_of[lambda[j - 1]];
@@ -470,7 +469,7 @@ namespace DiscImageChef.Checksums
{
/* r is the step number */
/* Compute discrepancy at the r-th step in poly-form */
discrR = 0;
int discrR = 0;
for(i = 0; i < r; i++) if(lambda[i] != 0 && s[r - i] != a0) discrR ^= alpha_to[Modnn(index_of[lambda[i]] + s[r - i])];
discrR = index_of[discrR]; /* Index form */
@@ -572,11 +571,11 @@ namespace DiscImageChef.Checksums
*/
for(j = count - 1; j >= 0; j--)
{
num1 = 0;
int num1 = 0;
for(i = degOmega; i >= 0; i--) if(omega[i] != a0) num1 ^= alpha_to[Modnn(omega[i] + i * root[j])];
num2 = alpha_to[Modnn(root[j] * (B0 - 1) + nn)];
den = 0;
int num2 = alpha_to[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 ^= alpha_to[Modnn(lambda[i + 1] + i * root[j])];

View File

@@ -87,7 +87,7 @@ namespace DiscImageChef.Checksums
sha1Provider.TransformFinalBlock(new byte[0], 0, 0);
StringBuilder sha1Output = new StringBuilder();
for(int i = 0; i < sha1Provider.Hash.Length; i++) sha1Output.Append(sha1Provider.Hash[i].ToString("x2"));
foreach(byte h in sha1Provider.Hash) sha1Output.Append(h.ToString("x2"));
return sha1Output.ToString();
}
@@ -115,7 +115,7 @@ namespace DiscImageChef.Checksums
hash = sha1Provider.ComputeHash(fileStream);
StringBuilder sha1Output = new StringBuilder();
for(int i = 0; i < hash.Length; i++) sha1Output.Append(hash[i].ToString("x2"));
foreach(byte h in hash) sha1Output.Append(h.ToString("x2"));
fileStream.Close();
@@ -133,7 +133,7 @@ namespace DiscImageChef.Checksums
hash = sha1Provider.ComputeHash(data, 0, (int)len);
StringBuilder sha1Output = new StringBuilder();
for(int i = 0; i < hash.Length; i++) sha1Output.Append(hash[i].ToString("x2"));
foreach(byte h in hash) sha1Output.Append(h.ToString("x2"));
return sha1Output.ToString();
}

View File

@@ -87,8 +87,7 @@ namespace DiscImageChef.Checksums
sha256Provider.TransformFinalBlock(new byte[0], 0, 0);
StringBuilder sha256Output = new StringBuilder();
for(int i = 0; i < sha256Provider.Hash.Length; i++)
sha256Output.Append(sha256Provider.Hash[i].ToString("x2"));
foreach(byte h in sha256Provider.Hash) sha256Output.Append(h.ToString("x2"));
return sha256Output.ToString();
}
@@ -116,7 +115,7 @@ namespace DiscImageChef.Checksums
hash = sha256Provider.ComputeHash(fileStream);
StringBuilder sha256Output = new StringBuilder();
for(int i = 0; i < hash.Length; i++) sha256Output.Append(hash[i].ToString("x2"));
foreach(byte h in hash) sha256Output.Append(h.ToString("x2"));
fileStream.Close();
@@ -134,7 +133,7 @@ namespace DiscImageChef.Checksums
hash = sha256Provider.ComputeHash(data, 0, (int)len);
StringBuilder sha256Output = new StringBuilder();
for(int i = 0; i < hash.Length; i++) sha256Output.Append(hash[i].ToString("x2"));
foreach(byte h in hash) sha256Output.Append(h.ToString("x2"));
return sha256Output.ToString();
}

View File

@@ -87,7 +87,7 @@ namespace DiscImageChef.Checksums
sha384Provider.TransformFinalBlock(new byte[0], 0, 0);
StringBuilder sha384Output = new StringBuilder();
for(int i = 0; i < sha384Provider.Hash.Length; i++) sha384Output.Append(sha384Provider.Hash[i].ToString("x2"));
foreach(byte h in sha384Provider.Hash) sha384Output.Append(h.ToString("x2"));
return sha384Output.ToString();
}
@@ -115,7 +115,7 @@ namespace DiscImageChef.Checksums
hash = sha384Provider.ComputeHash(fileStream);
StringBuilder sha384Output = new StringBuilder();
for(int i = 0; i < hash.Length; i++) sha384Output.Append(hash[i].ToString("x2"));
foreach(byte h in hash) sha384Output.Append(h.ToString("x2"));
fileStream.Close();
@@ -133,7 +133,7 @@ namespace DiscImageChef.Checksums
hash = sha384Provider.ComputeHash(data, 0, (int)len);
StringBuilder sha384Output = new StringBuilder();
for(int i = 0; i < hash.Length; i++) sha384Output.Append(hash[i].ToString("x2"));
foreach(byte h in hash) sha384Output.Append(h.ToString("x2"));
return sha384Output.ToString();
}

View File

@@ -87,7 +87,7 @@ namespace DiscImageChef.Checksums
sha512Provider.TransformFinalBlock(new byte[0], 0, 0);
StringBuilder sha512Output = new StringBuilder();
for(int i = 0; i < sha512Provider.Hash.Length; i++) sha512Output.Append(sha512Provider.Hash[i].ToString("x2"));
foreach(byte h in sha512Provider.Hash) sha512Output.Append(h.ToString("x2"));
return sha512Output.ToString();
}
@@ -115,7 +115,7 @@ namespace DiscImageChef.Checksums
hash = sha512Provider.ComputeHash(fileStream);
StringBuilder sha512Output = new StringBuilder();
for(int i = 0; i < hash.Length; i++) sha512Output.Append(hash[i].ToString("x2"));
foreach(byte h in hash) sha512Output.Append(h.ToString("x2"));
fileStream.Close();
@@ -133,7 +133,7 @@ namespace DiscImageChef.Checksums
hash = sha512Provider.ComputeHash(data, 0, (int)len);
StringBuilder sha512Output = new StringBuilder();
for(int i = 0; i < hash.Length; i++) sha512Output.Append(hash[i].ToString("x2"));
foreach(byte h in hash) sha512Output.Append(h.ToString("x2"));
return sha512Output.ToString();
}

View File

@@ -104,8 +104,7 @@ namespace DiscImageChef.Checksums
void roll_init()
{
self.Roll = new RollState();
self.Roll.Window = new byte[ROLLING_WINDOW];
self.Roll = new RollState {Window = new byte[ROLLING_WINDOW]};
}
/// <summary>
@@ -113,8 +112,7 @@ namespace DiscImageChef.Checksums
/// </summary>
public void Init()
{
self = new FuzzyState();
self.Bh = new BlockhashContext[NUM_BLOCKHASHES];
self = new FuzzyState {Bh = new BlockhashContext[NUM_BLOCKHASHES]};
for(int i = 0; i < NUM_BLOCKHASHES; i++) self.Bh[i].Digest = new byte[SPAMSUM_LENGTH];
self.Bhstart = 0;
@@ -276,7 +274,7 @@ namespace DiscImageChef.Checksums
}
// CLAUNIA: Flags seems to never be used in ssdeep, so I just removed it for code simplicity
uint fuzzy_digest(out byte[] result)
uint FuzzyDigest(out byte[] result)
{
StringBuilder sb = new StringBuilder();
uint bi = self.Bhstart;
@@ -418,8 +416,7 @@ namespace DiscImageChef.Checksums
/// </summary>
public string End()
{
byte[] result;
fuzzy_digest(out result);
FuzzyDigest(out byte[] result);
return CToString(result);
}
@@ -452,7 +449,7 @@ namespace DiscImageChef.Checksums
/// <param name="len">Length of the data buffer to hash.</param>
/// <param name="hash">null</param>
/// <returns>Base64 representation of SpamSum $blocksize:$hash:$hash</returns>
public string Data(byte[] data, uint len, out byte[] hash)
public static string Data(byte[] data, uint len, out byte[] hash)
{
SpamSumContext fuzzyContext = new SpamSumContext();
fuzzyContext.Init();
@@ -470,13 +467,13 @@ namespace DiscImageChef.Checksums
/// <param name="data">Data buffer.</param>
/// <param name="hash">null</param>
/// <returns>Base64 representation of SpamSum $blocksize:$hash:$hash</returns>
public string Data(byte[] data, out byte[] hash)
public static string Data(byte[] data, out byte[] hash)
{
return Data(data, (uint)data.Length, out hash);
}
// Converts an ASCII null-terminated string to .NET string
string CToString(byte[] cString)
static string CToString(byte[] cString)
{
StringBuilder sb = new StringBuilder();