General refactor and cleanup.

This commit is contained in:
2024-04-30 04:30:11 +01:00
parent 886bd83e85
commit fa6f1b2e46
34 changed files with 2153 additions and 2226 deletions

View File

@@ -77,8 +77,7 @@ public sealed class SpamSumContext : IChecksum
Bh = new BlockhashContext[NUM_BLOCKHASHES]
};
for(var i = 0; i < NUM_BLOCKHASHES; i++)
_self.Bh[i].Digest = new byte[SPAMSUM_LENGTH];
for(var i = 0; i < NUM_BLOCKHASHES; i++) _self.Bh[i].Digest = new byte[SPAMSUM_LENGTH];
_self.Bhstart = 0;
_self.Bhend = 1;
@@ -91,40 +90,6 @@ public sealed class SpamSumContext : IChecksum
roll_init();
}
#region IChecksum Members
/// <inheritdoc />
/// <summary>Updates the hash with data.</summary>
/// <param name="data">Data buffer.</param>
/// <param name="len">Length of buffer to hash.</param>
public void Update(byte[] data, uint len)
{
_self.TotalSize += len;
for(var i = 0; i < len; i++)
fuzzy_engine_step(data[i]);
}
/// <inheritdoc />
/// <summary>Updates the hash with data.</summary>
/// <param name="data">Data buffer.</param>
public void Update(byte[] data) => Update(data, (uint)data.Length);
/// <inheritdoc />
/// <summary>Returns a byte array of the hash value.</summary>
public byte[] Final() => throw new NotImplementedException("SpamSum does not have a binary representation.");
/// <inheritdoc />
/// <summary>Returns a base64 representation of the hash value.</summary>
public string End()
{
FuzzyDigest(out byte[] result);
return CToString(result);
}
#endregion
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void roll_init() => _self.Roll = new RollState
{
@@ -173,8 +138,7 @@ public sealed class SpamSumContext : IChecksum
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void fuzzy_try_fork_blockhash()
{
if(_self.Bhend >= NUM_BLOCKHASHES)
return;
if(_self.Bhend >= NUM_BLOCKHASHES) return;
if(_self.Bhend == 0) // assert
throw new Exception("Assertion failed");
@@ -192,8 +156,7 @@ public sealed class SpamSumContext : IChecksum
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void fuzzy_try_reduce_blockhash()
{
if(_self.Bhstart >= _self.Bhend)
throw new Exception("Assertion failed");
if(_self.Bhstart >= _self.Bhend) throw new Exception("Assertion failed");
if(_self.Bhend - _self.Bhstart < 2)
/* Need at least two working hashes. */
@@ -241,8 +204,7 @@ public sealed class SpamSumContext : IChecksum
/* We have hit a reset point. We now emit hashes which are
* based on all characters in the piece of the message between
* the last reset point and this one */
if(0 == _self.Bh[i].Dlen)
fuzzy_try_fork_blockhash();
if(0 == _self.Bh[i].Dlen) fuzzy_try_fork_blockhash();
_self.Bh[i].Digest[_self.Bh[i].Dlen] = _b64[_self.Bh[i].H % 64];
_self.Bh[i].Halfdigest = _b64[_self.Bh[i].Halfh % 64];
@@ -258,8 +220,7 @@ public sealed class SpamSumContext : IChecksum
_self.Bh[i].Digest[++_self.Bh[i].Dlen] = 0;
_self.Bh[i].H = HASH_INIT;
if(_self.Bh[i].Dlen >= SPAMSUM_LENGTH / 2)
continue;
if(_self.Bh[i].Dlen >= SPAMSUM_LENGTH / 2) continue;
_self.Bh[i].Halfh = HASH_INIT;
_self.Bh[i].Halfdigest = 0;
@@ -290,21 +251,15 @@ public sealed class SpamSumContext : IChecksum
{
++bi;
if(bi >= NUM_BLOCKHASHES)
throw new OverflowException("The input exceeds data types.");
if(bi >= NUM_BLOCKHASHES) throw new OverflowException("The input exceeds data types.");
}
/* Adapt blocksize guess to actual digest length. */
while(bi >= _self.Bhend)
--bi;
while(bi >= _self.Bhend) --bi;
while(bi > _self.Bhstart &&
_self.Bh[bi].Dlen < SPAMSUM_LENGTH / 2)
--bi;
while(bi > _self.Bhstart && _self.Bh[bi].Dlen < SPAMSUM_LENGTH / 2) --bi;
if(bi > 0 &&
_self.Bh[bi].Dlen < SPAMSUM_LENGTH / 2)
throw new Exception("Assertion failed");
if(bi > 0 && _self.Bh[bi].Dlen < SPAMSUM_LENGTH / 2) throw new Exception("Assertion failed");
sb.AppendFormat("{0}:", SSDEEP_BS(bi));
int i = Encoding.ASCII.GetBytes(sb.ToString()).Length;
@@ -313,8 +268,7 @@ public sealed class SpamSumContext : IChecksum
/* Maybe snprintf has set errno here? */
throw new OverflowException("The input exceeds data types.");
if(i >= remain)
throw new Exception("Assertion failed");
if(i >= remain) throw new Exception("Assertion failed");
remain -= i;
@@ -324,8 +278,7 @@ public sealed class SpamSumContext : IChecksum
i = (int)_self.Bh[bi].Dlen;
if(i > remain)
throw new Exception("Assertion failed");
if(i > remain) throw new Exception("Assertion failed");
Array.Copy(_self.Bh[bi].Digest, 0, result, resultOff, i);
resultOff += i;
@@ -333,8 +286,7 @@ public sealed class SpamSumContext : IChecksum
if(h != 0)
{
if(remain <= 0)
throw new Exception("Assertion failed");
if(remain <= 0) throw new Exception("Assertion failed");
result[resultOff] = _b64[_self.Bh[bi].H % 64];
@@ -349,8 +301,7 @@ public sealed class SpamSumContext : IChecksum
}
else if(_self.Bh[bi].Digest[i] != 0)
{
if(remain <= 0)
throw new Exception("Assertion failed");
if(remain <= 0) throw new Exception("Assertion failed");
result[resultOff] = _self.Bh[bi].Digest[i];
@@ -364,8 +315,7 @@ public sealed class SpamSumContext : IChecksum
}
}
if(remain <= 0)
throw new Exception("Assertion failed");
if(remain <= 0) throw new Exception("Assertion failed");
result[resultOff++] = 0x3A; // ':'
--remain;
@@ -375,8 +325,7 @@ public sealed class SpamSumContext : IChecksum
++bi;
i = (int)_self.Bh[bi].Dlen;
if(i > remain)
throw new Exception("Assertion failed");
if(i > remain) throw new Exception("Assertion failed");
Array.Copy(_self.Bh[bi].Digest, 0, result, resultOff, i);
resultOff += i;
@@ -384,8 +333,7 @@ public sealed class SpamSumContext : IChecksum
if(h != 0)
{
if(remain <= 0)
throw new Exception("Assertion failed");
if(remain <= 0) throw new Exception("Assertion failed");
h = _self.Bh[bi].Halfh;
result[resultOff] = _b64[h % 64];
@@ -405,8 +353,7 @@ public sealed class SpamSumContext : IChecksum
if(i != 0)
{
if(remain <= 0)
throw new Exception("Assertion failed");
if(remain <= 0) throw new Exception("Assertion failed");
result[resultOff] = (byte)i;
@@ -423,11 +370,9 @@ public sealed class SpamSumContext : IChecksum
}
else if(h != 0)
{
if(_self.Bh[bi].Dlen != 0)
throw new Exception("Assertion failed");
if(_self.Bh[bi].Dlen != 0) throw new Exception("Assertion failed");
if(remain <= 0)
throw new Exception("Assertion failed");
if(remain <= 0) throw new Exception("Assertion failed");
result[resultOff++] = _b64[_self.Bh[bi].H % 64];
/* No need to bother with FUZZY_FLAG_ELIMSEQ, because this
@@ -481,8 +426,7 @@ public sealed class SpamSumContext : IChecksum
// LINQ is six times slower
foreach(byte c in cString)
{
if(c == 0)
break;
if(c == 0) break;
count++;
}
@@ -538,5 +482,38 @@ public sealed class SpamSumContext : IChecksum
public uint N;
}
#endregion
#region IChecksum Members
/// <inheritdoc />
/// <summary>Updates the hash with data.</summary>
/// <param name="data">Data buffer.</param>
/// <param name="len">Length of buffer to hash.</param>
public void Update(byte[] data, uint len)
{
_self.TotalSize += len;
for(var i = 0; i < len; i++) fuzzy_engine_step(data[i]);
}
/// <inheritdoc />
/// <summary>Updates the hash with data.</summary>
/// <param name="data">Data buffer.</param>
public void Update(byte[] data) => Update(data, (uint)data.Length);
/// <inheritdoc />
/// <summary>Returns a byte array of the hash value.</summary>
public byte[] Final() => throw new NotImplementedException("SpamSum does not have a binary representation.");
/// <inheritdoc />
/// <summary>Returns a base64 representation of the hash value.</summary>
public string End()
{
FuzzyDigest(out byte[] result);
return CToString(result);
}
#endregion
}