REFACTOR: Final cleanup of DiscImageChef.Checksums.

This commit is contained in:
2017-12-23 16:40:14 +00:00
parent 2201d99d39
commit b5a3fa1f58
13 changed files with 251 additions and 232 deletions

View File

@@ -37,15 +37,15 @@ using System.Text;
namespace DiscImageChef.Checksums
{
/// <summary>
/// Implements the Adler-32 algorithm
/// Implements the Adler-32 algorithm
/// </summary>
public class Adler32Context
{
ushort sum1, sum2;
const ushort ADLER_MODULE = 65521;
ushort sum1, sum2;
/// <summary>
/// Initializes the Adler-32 sums
/// Initializes the Adler-32 sums
/// </summary>
public void Init()
{
@@ -54,7 +54,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Updates the hash with data.
/// Updates the hash with data.
/// </summary>
/// <param name="data">Data buffer.</param>
/// <param name="len">Length of buffer to hash.</param>
@@ -68,7 +68,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Updates the hash with data.
/// Updates the hash with data.
/// </summary>
/// <param name="data">Data buffer.</param>
public void Update(byte[] data)
@@ -77,7 +77,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Returns a byte array of the hash value.
/// Returns a byte array of the hash value.
/// </summary>
public byte[] Final()
{
@@ -87,7 +87,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Returns a hexadecimal representation of the hash value.
/// Returns a hexadecimal representation of the hash value.
/// </summary>
public string End()
{
@@ -95,13 +95,14 @@ namespace DiscImageChef.Checksums
StringBuilder adlerOutput = new StringBuilder();
BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
for(int i = 0; i < BigEndianBitConverter.GetBytes(finalSum).Length; i++) adlerOutput.Append(BigEndianBitConverter.GetBytes(finalSum)[i].ToString("x2"));
for(int i = 0; i < BigEndianBitConverter.GetBytes(finalSum).Length; i++)
adlerOutput.Append(BigEndianBitConverter.GetBytes(finalSum)[i].ToString("x2"));
return adlerOutput.ToString();
}
/// <summary>
/// Gets the hash of a file
/// Gets the hash of a file
/// </summary>
/// <param name="filename">File path.</param>
public static byte[] File(string filename)
@@ -111,7 +112,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Gets the hash of a file in hexadecimal and as a byte array.
/// Gets the hash of a file in hexadecimal and as a byte array.
/// </summary>
/// <param name="filename">File path.</param>
/// <param name="hash">Byte array of the hash value.</param>
@@ -145,7 +146,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Gets the hash of the specified data buffer.
/// Gets the hash of the specified data buffer.
/// </summary>
/// <param name="data">Data buffer.</param>
/// <param name="len">Length of the data buffer to hash.</param>
@@ -177,7 +178,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Gets the hash of the specified data buffer.
/// Gets the hash of the specified data buffer.
/// </summary>
/// <param name="data">Data buffer.</param>
/// <param name="hash">Byte array of the hash value.</param>

View File

@@ -38,14 +38,38 @@ using DiscImageChef.Console;
namespace DiscImageChef.Checksums
{
/// <summary>
/// Implements ReedSolomon and CRC32 algorithms as used by CD-ROM
/// Implements ReedSolomon and CRC32 algorithms as used by CD-ROM
/// </summary>
public static class CdChecksums
{
static byte[] eccFTable;
static byte[] eccBTable;
const uint CDCRC32_POLY = 0xD8018001;
const uint CDCRC32_SEED = 0x00000000;
static byte[] eccFTable;
static byte[] eccBTable;
static readonly ushort[] CcittCrc16Table =
{
0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7, 0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c,
0xd1ad, 0xe1ce, 0xf1ef, 0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6, 0x9339, 0x8318,
0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de, 0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4,
0x5485, 0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d, 0x3653, 0x2672, 0x1611, 0x0630,
0x76d7, 0x66f6, 0x5695, 0x46b4, 0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc, 0x48c4,
0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823, 0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969,
0xa90a, 0xb92b, 0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12, 0xdbfd, 0xcbdc, 0xfbbf,
0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a, 0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41,
0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49, 0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13,
0x2e32, 0x1e51, 0x0e70, 0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78, 0x9188, 0x81a9,
0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f, 0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046,
0x6067, 0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e, 0x02b1, 0x1290, 0x22f3, 0x32d2,
0x4235, 0x5214, 0x6277, 0x7256, 0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d, 0x34e2,
0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405, 0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e,
0xc71d, 0xd73c, 0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634, 0xd94c, 0xc96d, 0xf90e,
0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab, 0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3,
0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a, 0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1,
0x1ad0, 0x2ab3, 0x3a92, 0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9, 0x7c26, 0x6c07,
0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1, 0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9,
0x9ff8, 0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0
};
public static bool? CheckCdSector(byte[] buffer)
{
@@ -64,10 +88,13 @@ namespace DiscImageChef.Checksums
bool? status = null;
if(channelStatus == false || subchannelStatus == false) status = false;
switch(channelStatus) {
case null when subchannelStatus == true: status = true;
switch(channelStatus)
{
case null when subchannelStatus == true:
status = true;
break;
case true when subchannelStatus == null: status = true;
case true when subchannelStatus == null:
status = true;
break;
}
@@ -155,8 +182,7 @@ namespace DiscImageChef.Checksums
if(channel[0x814] != 0x00 || // reserved (8 bytes)
channel[0x815] != 0x00 || channel[0x816] != 0x00 || channel[0x817] != 0x00 ||
channel[0x818] != 0x00 || channel[0x819] != 0x00 || channel[0x81A] != 0x00 ||
channel[0x81B] != 0x00)
channel[0x818] != 0x00 || channel[0x819] != 0x00 || channel[0x81A] != 0x00 || channel[0x81B] != 0x00)
{
DicConsole.DebugWriteLine("CD checksums",
"Mode 1 sector with data in reserved bytes at address: {0:X2}:{1:X2}:{2:X2}",
@@ -287,8 +313,7 @@ namespace DiscImageChef.Checksums
return true;
}
DicConsole.DebugWriteLine("CD checksums",
"Unknown mode {0} sector at address: {1:X2}:{2:X2}:{3:X2}",
DicConsole.DebugWriteLine("CD checksums", "Unknown mode {0} sector at address: {1:X2}:{2:X2}:{3:X2}",
channel[0x00F], channel[0x00C], channel[0x00D], channel[0x00E]);
return null;
}
@@ -484,39 +509,18 @@ namespace DiscImageChef.Checksums
if(cdTextPack4Crc == calculatedCdtp4Crc || cdTextPack4Crc == 0) return status;
DicConsole.DebugWriteLine("CD checksums", "CD-Text Pack 4 CRC 0x{0:X4}, expected 0x{1:X4}",
cdTextPack4Crc, calculatedCdtp4Crc);
DicConsole.DebugWriteLine("CD checksums", "CD-Text Pack 4 CRC 0x{0:X4}, expected 0x{1:X4}", cdTextPack4Crc,
calculatedCdtp4Crc);
return false;
}
static readonly ushort[] CcittCrc16Table =
{
0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7, 0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c,
0xd1ad, 0xe1ce, 0xf1ef, 0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6, 0x9339, 0x8318,
0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de, 0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4,
0x5485, 0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d, 0x3653, 0x2672, 0x1611, 0x0630,
0x76d7, 0x66f6, 0x5695, 0x46b4, 0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc, 0x48c4,
0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823, 0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969,
0xa90a, 0xb92b, 0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12, 0xdbfd, 0xcbdc, 0xfbbf,
0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a, 0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41,
0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49, 0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13,
0x2e32, 0x1e51, 0x0e70, 0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78, 0x9188, 0x81a9,
0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f, 0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046,
0x6067, 0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e, 0x02b1, 0x1290, 0x22f3, 0x32d2,
0x4235, 0x5214, 0x6277, 0x7256, 0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d, 0x34e2,
0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405, 0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e,
0xc71d, 0xd73c, 0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634, 0xd94c, 0xc96d, 0xf90e,
0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab, 0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3,
0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a, 0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1,
0x1ad0, 0x2ab3, 0x3a92, 0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9, 0x7c26, 0x6c07,
0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1, 0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9,
0x9ff8, 0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0
};
static ushort CalculateCCITT_CRC16(byte[] buffer)
{
ushort crc16 = buffer.Aggregate<byte, ushort>(0, (current, t) => (ushort)(CcittCrc16Table[(current >> 8) ^ t] ^ (current << 8)));
ushort crc16 =
buffer.Aggregate<byte, ushort>(0,
(current, t) =>
(ushort)(CcittCrc16Table[(current >> 8) ^ t] ^ (current << 8)));
crc16 = (ushort)~crc16;

View File

@@ -37,18 +37,18 @@ using System.Text;
namespace DiscImageChef.Checksums
{
/// <summary>
/// Implements a CRC16-CCITT algorithm
/// Implements a CRC16-CCITT algorithm
/// </summary>
public class Crc16Context
{
const ushort CRC16_POLY = 0xA001;
const ushort CRC16_SEED = 0x0000;
ushort[] table;
ushort hashInt;
ushort[] table;
/// <summary>
/// Initializes the CRC16 table and seed
/// Initializes the CRC16 table and seed
/// </summary>
public void Init()
{
@@ -67,7 +67,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Updates the hash with data.
/// Updates the hash with data.
/// </summary>
/// <param name="data">Data buffer.</param>
/// <param name="len">Length of buffer to hash.</param>
@@ -77,7 +77,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Updates the hash with data.
/// Updates the hash with data.
/// </summary>
/// <param name="data">Data buffer.</param>
public void Update(byte[] data)
@@ -86,7 +86,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Returns a byte array of the hash value.
/// Returns a byte array of the hash value.
/// </summary>
public byte[] Final()
{
@@ -96,7 +96,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Returns a hexadecimal representation of the hash value.
/// Returns a hexadecimal representation of the hash value.
/// </summary>
public string End()
{
@@ -104,13 +104,14 @@ namespace DiscImageChef.Checksums
StringBuilder crc16Output = new StringBuilder();
BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
for(int i = 0; i < BigEndianBitConverter.GetBytes(hashInt).Length; i++) crc16Output.Append(BigEndianBitConverter.GetBytes(hashInt)[i].ToString("x2"));
for(int i = 0; i < BigEndianBitConverter.GetBytes(hashInt).Length; i++)
crc16Output.Append(BigEndianBitConverter.GetBytes(hashInt)[i].ToString("x2"));
return crc16Output.ToString();
}
/// <summary>
/// Gets the hash of a file
/// Gets the hash of a file
/// </summary>
/// <param name="filename">File path.</param>
public static byte[] File(string filename)
@@ -120,7 +121,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Gets the hash of a file in hexadecimal and as a byte array.
/// Gets the hash of a file in hexadecimal and as a byte array.
/// </summary>
/// <param name="filename">File path.</param>
/// <param name="hash">Byte array of the hash value.</param>
@@ -143,7 +144,8 @@ namespace DiscImageChef.Checksums
}
for(int i = 0; i < fileStream.Length; i++)
localhashInt = (ushort)((localhashInt >> 8) ^ localTable[fileStream.ReadByte() ^ (localhashInt & 0xff)]);
localhashInt =
(ushort)((localhashInt >> 8) ^ localTable[fileStream.ReadByte() ^ (localhashInt & 0xff)]);
BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
hash = BigEndianBitConverter.GetBytes(localhashInt);
@@ -158,7 +160,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Gets the hash of the specified data buffer.
/// Gets the hash of the specified data buffer.
/// </summary>
/// <param name="data">Data buffer.</param>
/// <param name="len">Length of the data buffer to hash.</param>
@@ -169,7 +171,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Gets the hash of the specified data buffer.
/// Gets the hash of the specified data buffer.
/// </summary>
/// <param name="data">Data buffer.</param>
/// <param name="len">Length of the data buffer to hash.</param>
@@ -207,7 +209,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Gets the hash of the specified data buffer.
/// Gets the hash of the specified data buffer.
/// </summary>
/// <param name="data">Data buffer.</param>
/// <param name="hash">Byte array of the hash value.</param>

View File

@@ -37,18 +37,18 @@ using System.Text;
namespace DiscImageChef.Checksums
{
/// <summary>
/// Implements a CRC32 algorithm
/// Implements a CRC32 algorithm
/// </summary>
public class Crc32Context
{
const uint CRC32_POLY = 0xEDB88320;
const uint CRC32_SEED = 0xFFFFFFFF;
uint[] table;
uint hashInt;
uint[] table;
/// <summary>
/// Initializes the CRC32 table and seed
/// Initializes the CRC32 table and seed
/// </summary>
public void Init()
{
@@ -67,7 +67,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Updates the hash with data.
/// Updates the hash with data.
/// </summary>
/// <param name="data">Data buffer.</param>
/// <param name="len">Length of buffer to hash.</param>
@@ -77,7 +77,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Updates the hash with data.
/// Updates the hash with data.
/// </summary>
/// <param name="data">Data buffer.</param>
public void Update(byte[] data)
@@ -86,7 +86,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Returns a byte array of the hash value.
/// Returns a byte array of the hash value.
/// </summary>
public byte[] Final()
{
@@ -96,7 +96,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Returns a hexadecimal representation of the hash value.
/// Returns a hexadecimal representation of the hash value.
/// </summary>
public string End()
{
@@ -104,13 +104,14 @@ namespace DiscImageChef.Checksums
StringBuilder crc32Output = new StringBuilder();
BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
for(int i = 0; i < BigEndianBitConverter.GetBytes(hashInt).Length; i++) crc32Output.Append(BigEndianBitConverter.GetBytes(hashInt)[i].ToString("x2"));
for(int i = 0; i < BigEndianBitConverter.GetBytes(hashInt).Length; i++)
crc32Output.Append(BigEndianBitConverter.GetBytes(hashInt)[i].ToString("x2"));
return crc32Output.ToString();
}
/// <summary>
/// Gets the hash of a file
/// Gets the hash of a file
/// </summary>
/// <param name="filename">File path.</param>
public static byte[] File(string filename)
@@ -120,7 +121,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Gets the hash of a file in hexadecimal and as a byte array.
/// Gets the hash of a file in hexadecimal and as a byte array.
/// </summary>
/// <param name="filename">File path.</param>
/// <param name="hash">Byte array of the hash value.</param>
@@ -159,7 +160,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Gets the hash of the specified data buffer.
/// Gets the hash of the specified data buffer.
/// </summary>
/// <param name="data">Data buffer.</param>
/// <param name="len">Length of the data buffer to hash.</param>
@@ -170,7 +171,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Gets the hash of the specified data buffer.
/// Gets the hash of the specified data buffer.
/// </summary>
/// <param name="data">Data buffer.</param>
/// <param name="len">Length of the data buffer to hash.</param>
@@ -194,7 +195,8 @@ namespace DiscImageChef.Checksums
localTable[i] = entry;
}
for(int i = 0; i < len; i++) localhashInt = (localhashInt >> 8) ^ localTable[data[i] ^ (localhashInt & 0xff)];
for(int i = 0; i < len; i++)
localhashInt = (localhashInt >> 8) ^ localTable[data[i] ^ (localhashInt & 0xff)];
localhashInt ^= CRC32_SEED;
BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
@@ -208,7 +210,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Gets the hash of the specified data buffer.
/// Gets the hash of the specified data buffer.
/// </summary>
/// <param name="data">Data buffer.</param>
/// <param name="hash">Byte array of the hash value.</param>

View File

@@ -36,18 +36,18 @@ using System.Text;
namespace DiscImageChef.Checksums
{
/// <summary>
/// Implements a CRC64 (ECMA) algorithm
/// Implements a CRC64 (ECMA) algorithm
/// </summary>
public class Crc64Context
{
const ulong CRC64_POLY = 0xC96C5795D7870F42;
const ulong CRC64_SEED = 0xFFFFFFFFFFFFFFFF;
ulong[] table;
ulong hashInt;
ulong[] table;
/// <summary>
/// Initializes the CRC64 table and seed
/// Initializes the CRC64 table and seed
/// </summary>
public void Init()
{
@@ -66,7 +66,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Updates the hash with data.
/// Updates the hash with data.
/// </summary>
/// <param name="data">Data buffer.</param>
/// <param name="len">Length of buffer to hash.</param>
@@ -76,7 +76,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Updates the hash with data.
/// Updates the hash with data.
/// </summary>
/// <param name="data">Data buffer.</param>
public void Update(byte[] data)
@@ -85,7 +85,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Returns a byte array of the hash value.
/// Returns a byte array of the hash value.
/// </summary>
public byte[] Final()
{
@@ -95,7 +95,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Returns a hexadecimal representation of the hash value.
/// Returns a hexadecimal representation of the hash value.
/// </summary>
public string End()
{
@@ -103,13 +103,14 @@ namespace DiscImageChef.Checksums
StringBuilder crc64Output = new StringBuilder();
BigEndianBitConverter.IsLittleEndian = BigEndianBitConverter.IsLittleEndian;
for(int i = 0; i < BigEndianBitConverter.GetBytes(hashInt).Length; i++) crc64Output.Append(BigEndianBitConverter.GetBytes(hashInt)[i].ToString("x2"));
for(int i = 0; i < BigEndianBitConverter.GetBytes(hashInt).Length; i++)
crc64Output.Append(BigEndianBitConverter.GetBytes(hashInt)[i].ToString("x2"));
return crc64Output.ToString();
}
/// <summary>
/// Gets the hash of a file
/// Gets the hash of a file
/// </summary>
/// <param name="filename">File path.</param>
public static byte[] File(string filename)
@@ -119,7 +120,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Gets the hash of a file in hexadecimal and as a byte array.
/// Gets the hash of a file in hexadecimal and as a byte array.
/// </summary>
/// <param name="filename">File path.</param>
/// <param name="hash">Byte array of the hash value.</param>
@@ -158,7 +159,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Gets the hash of the specified data buffer.
/// Gets the hash of the specified data buffer.
/// </summary>
/// <param name="data">Data buffer.</param>
/// <param name="len">Length of the data buffer to hash.</param>
@@ -169,7 +170,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Gets the hash of the specified data buffer.
/// Gets the hash of the specified data buffer.
/// </summary>
/// <param name="data">Data buffer.</param>
/// <param name="len">Length of the data buffer to hash.</param>
@@ -193,7 +194,8 @@ namespace DiscImageChef.Checksums
localTable[i] = entry;
}
for(int i = 0; i < len; i++) localhashInt = (localhashInt >> 8) ^ localTable[data[i] ^ (localhashInt & 0xff)];
for(int i = 0; i < len; i++)
localhashInt = (localhashInt >> 8) ^ localTable[data[i] ^ (localhashInt & 0xff)];
localhashInt ^= CRC64_SEED;
BigEndianBitConverter.IsLittleEndian = BigEndianBitConverter.IsLittleEndian;
@@ -207,7 +209,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Gets the hash of the specified data buffer.
/// Gets the hash of the specified data buffer.
/// </summary>
/// <param name="data">Data buffer.</param>
/// <param name="hash">Byte array of the hash value.</param>

View File

@@ -37,14 +37,14 @@ using System.Text;
namespace DiscImageChef.Checksums
{
/// <summary>
/// Wraps up .NET MD5 implementation to a Init(), Update(), Final() context.
/// Wraps up .NET MD5 implementation to a Init(), Update(), Final() context.
/// </summary>
public class Md5Context
{
MD5 md5Provider;
/// <summary>
/// Initializes the MD5 hash provider
/// Initializes the MD5 hash provider
/// </summary>
public void Init()
{
@@ -52,7 +52,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Updates the hash with data.
/// Updates the hash with data.
/// </summary>
/// <param name="data">Data buffer.</param>
/// <param name="len">Length of buffer to hash.</param>
@@ -62,7 +62,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Updates the hash with data.
/// Updates the hash with data.
/// </summary>
/// <param name="data">Data buffer.</param>
public void Update(byte[] data)
@@ -71,7 +71,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Returns a byte array of the hash value.
/// Returns a byte array of the hash value.
/// </summary>
public byte[] Final()
{
@@ -80,7 +80,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Returns a hexadecimal representation of the hash value.
/// Returns a hexadecimal representation of the hash value.
/// </summary>
public string End()
{
@@ -93,7 +93,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Gets the hash of a file
/// Gets the hash of a file
/// </summary>
/// <param name="filename">File path.</param>
public byte[] File(string filename)
@@ -105,7 +105,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Gets the hash of a file in hexadecimal and as a byte array.
/// Gets the hash of a file in hexadecimal and as a byte array.
/// </summary>
/// <param name="filename">File path.</param>
/// <param name="hash">Byte array of the hash value.</param>
@@ -123,7 +123,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Gets the hash of the specified data buffer.
/// Gets the hash of the specified data buffer.
/// </summary>
/// <param name="data">Data buffer.</param>
/// <param name="len">Length of the data buffer to hash.</param>
@@ -139,7 +139,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Gets the hash of the specified data buffer.
/// Gets the hash of the specified data buffer.
/// </summary>
/// <param name="data">Data buffer.</param>
/// <param name="hash">Byte array of the hash value.</param>

View File

@@ -37,14 +37,14 @@ using System.Text;
namespace DiscImageChef.Checksums
{
/// <summary>
/// Wraps up .NET RIPEMD160 implementation to a Init(), Update(), Final() context.
/// Wraps up .NET RIPEMD160 implementation to a Init(), Update(), Final() context.
/// </summary>
public class Ripemd160Context
{
RIPEMD160 ripemd160Provider;
/// <summary>
/// Initializes the RIPEMD160 hash provider
/// Initializes the RIPEMD160 hash provider
/// </summary>
public void Init()
{
@@ -52,7 +52,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Updates the hash with data.
/// Updates the hash with data.
/// </summary>
/// <param name="data">Data buffer.</param>
/// <param name="len">Length of buffer to hash.</param>
@@ -62,7 +62,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Updates the hash with data.
/// Updates the hash with data.
/// </summary>
/// <param name="data">Data buffer.</param>
public void Update(byte[] data)
@@ -71,7 +71,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Returns a byte array of the hash value.
/// Returns a byte array of the hash value.
/// </summary>
public byte[] Final()
{
@@ -80,7 +80,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Returns a hexadecimal representation of the hash value.
/// Returns a hexadecimal representation of the hash value.
/// </summary>
public string End()
{
@@ -93,7 +93,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Gets the hash of a file
/// Gets the hash of a file
/// </summary>
/// <param name="filename">File path.</param>
public byte[] File(string filename)
@@ -105,7 +105,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Gets the hash of a file in hexadecimal and as a byte array.
/// Gets the hash of a file in hexadecimal and as a byte array.
/// </summary>
/// <param name="filename">File path.</param>
/// <param name="hash">Byte array of the hash value.</param>
@@ -123,7 +123,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Gets the hash of the specified data buffer.
/// Gets the hash of the specified data buffer.
/// </summary>
/// <param name="data">Data buffer.</param>
/// <param name="len">Length of the data buffer to hash.</param>
@@ -139,7 +139,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Gets the hash of the specified data buffer.
/// Gets the hash of the specified data buffer.
/// </summary>
/// <param name="data">Data buffer.</param>
/// <param name="hash">Byte array of the hash value.</param>

View File

@@ -63,34 +63,40 @@ using DiscImageChef.Console;
namespace DiscImageChef.Checksums
{
/// <summary>
/// Implements the Reed-Solomon algorithm
/// Implements the Reed-Solomon algorithm
/// </summary>
public class ReedSolomon
{
/* Primitive polynomials - see Lin & Costello, Error Control Coding Appendix A,
* and Lee & Messerschmitt, Digital Communication p. 453.
*/
int[] pp;
/* index->polynomial form conversion table */
int[] alpha_to;
/* Polynomial->index form conversion table */
int[] index_of;
/* Generator polynomial g(x)
* Degree of g(x) = 2*TT
* has roots @**B0, @**(B0+1), ... ,@^(B0+2*TT-1)
*/
int[] gg;
int mm, kk, nn;
/* No legal value in index form represents zero, so
* we need a special value for this purpose
*/
int a0;
bool initialized;
/* Alpha exponent for the first root of the generator polynomial */
/// <summary>
/// Alpha exponent for the first root of the generator polynomial
/// </summary>
const int B0 = 1;
/// <summary>
/// No legal value in index form represents zero, so we need a special value for this purpose
/// </summary>
int a0;
/// <summary>
/// index->polynomial form conversion table
/// </summary>
int[] alpha_to;
/// <summary>
/// Generator polynomial g(x) Degree of g(x) = 2*TT has roots @**B0, @**(B0+1), ... ,@^(B0+2*TT-1)
/// </summary>
int[] gg;
/// <summary>
/// Polynomial->index form conversion table
/// </summary>
int[] index_of;
bool initialized;
int mm, kk, nn;
/// <summary>
/// Primitive polynomials - see Lin & Costello, Error Control Coding Appendix A, and Lee & Messerschmitt, Digital
/// Communication p. 453.
/// </summary>
int[] pp;
/// <summary>
/// Initializes the Reed-Solomon with RS(n,k) with GF(2^m)
/// Initializes the Reed-Solomon with RS(n,k) with GF(2^m)
/// </summary>
public void InitRs(int n, int k, int m)
{
@@ -301,7 +307,7 @@ namespace DiscImageChef.Checksums
* data(X)*X**(NN-KK)+ b(X)
*/
/// <summary>
/// Takes the symbols in data to output parity in bb.
/// Takes the symbols in data to output parity in bb.
/// </summary>
/// <returns>Returns -1 if an illegal symbol is found.</returns>
/// <param name="data">Data symbols.</param>
@@ -355,7 +361,7 @@ namespace DiscImageChef.Checksums
* in R. Blahut's "Theory ... of Error-Correcting Codes".
*/
/// <summary>
/// Decodes the RS. If decoding is successful outputs corrected data symbols.
/// Decodes the RS. If decoding is successful outputs corrected data symbols.
/// </summary>
/// <returns>Returns corrected symbols, -1 if illegal or uncorrectable</returns>
/// <param name="data">Data symbols.</param>
@@ -395,8 +401,7 @@ namespace DiscImageChef.Checksums
{
tmp = 0;
for(j = 0; j < nn; j++)
if(recd[j] != a0) /* recd[j] in index form */
tmp ^= alpha_to[Modnn(recd[j] + (B0 + i - 1) * j)];
if(recd[j] != a0) /* recd[j] in index form */ tmp ^= alpha_to[Modnn(recd[j] + (B0 + i - 1) * j)];
synError |= tmp; /* set flag if non-zero syndrome =>
* error */
@@ -473,7 +478,8 @@ namespace DiscImageChef.Checksums
/* r is the step number */
/* Compute discrepancy at the r-th step in poly-form */
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])];
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 */
if(discrR == a0)
@@ -560,7 +566,8 @@ namespace DiscImageChef.Checksums
{
tmp = 0;
j = degLambda < i ? degLambda : i;
for(; j >= 0; j--) if(s[i + 1 - j] != a0 && lambda[j] != a0) tmp ^= alpha_to[Modnn(s[i + 1 - j] + lambda[j])];
for(; j >= 0; j--)
if(s[i + 1 - j] != a0 && lambda[j] != a0) tmp ^= alpha_to[Modnn(s[i + 1 - j] + lambda[j])];
if(tmp != 0) degOmega = i;
omega[i] = index_of[tmp];
@@ -581,7 +588,8 @@ namespace DiscImageChef.Checksums
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])];
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])];
if(den == 0)
{

View File

@@ -37,14 +37,14 @@ using System.Text;
namespace DiscImageChef.Checksums
{
/// <summary>
/// Wraps up .NET SHA1 implementation to a Init(), Update(), Final() context.
/// Wraps up .NET SHA1 implementation to a Init(), Update(), Final() context.
/// </summary>
public class Sha1Context
{
SHA1 sha1Provider;
/// <summary>
/// Initializes the SHA1 hash provider
/// Initializes the SHA1 hash provider
/// </summary>
public void Init()
{
@@ -52,7 +52,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Updates the hash with data.
/// Updates the hash with data.
/// </summary>
/// <param name="data">Data buffer.</param>
/// <param name="len">Length of buffer to hash.</param>
@@ -62,7 +62,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Updates the hash with data.
/// Updates the hash with data.
/// </summary>
/// <param name="data">Data buffer.</param>
public void Update(byte[] data)
@@ -71,7 +71,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Returns a byte array of the hash value.
/// Returns a byte array of the hash value.
/// </summary>
public byte[] Final()
{
@@ -80,7 +80,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Returns a hexadecimal representation of the hash value.
/// Returns a hexadecimal representation of the hash value.
/// </summary>
public string End()
{
@@ -93,7 +93,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Gets the hash of a file
/// Gets the hash of a file
/// </summary>
/// <param name="filename">File path.</param>
public byte[] File(string filename)
@@ -105,7 +105,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Gets the hash of a file in hexadecimal and as a byte array.
/// Gets the hash of a file in hexadecimal and as a byte array.
/// </summary>
/// <param name="filename">File path.</param>
/// <param name="hash">Byte array of the hash value.</param>
@@ -123,7 +123,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Gets the hash of the specified data buffer.
/// Gets the hash of the specified data buffer.
/// </summary>
/// <param name="data">Data buffer.</param>
/// <param name="len">Length of the data buffer to hash.</param>
@@ -139,7 +139,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Gets the hash of the specified data buffer.
/// Gets the hash of the specified data buffer.
/// </summary>
/// <param name="data">Data buffer.</param>
/// <param name="hash">Byte array of the hash value.</param>

View File

@@ -37,14 +37,14 @@ using System.Text;
namespace DiscImageChef.Checksums
{
/// <summary>
/// Wraps up .NET SHA256 implementation to a Init(), Update(), Final() context.
/// Wraps up .NET SHA256 implementation to a Init(), Update(), Final() context.
/// </summary>
public class Sha256Context
{
SHA256 sha256Provider;
/// <summary>
/// Initializes the SHA256 hash provider
/// Initializes the SHA256 hash provider
/// </summary>
public void Init()
{
@@ -52,7 +52,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Updates the hash with data.
/// Updates the hash with data.
/// </summary>
/// <param name="data">Data buffer.</param>
/// <param name="len">Length of buffer to hash.</param>
@@ -62,7 +62,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Updates the hash with data.
/// Updates the hash with data.
/// </summary>
/// <param name="data">Data buffer.</param>
public void Update(byte[] data)
@@ -71,7 +71,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Returns a byte array of the hash value.
/// Returns a byte array of the hash value.
/// </summary>
public byte[] Final()
{
@@ -80,7 +80,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Returns a hexadecimal representation of the hash value.
/// Returns a hexadecimal representation of the hash value.
/// </summary>
public string End()
{
@@ -93,7 +93,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Gets the hash of a file
/// Gets the hash of a file
/// </summary>
/// <param name="filename">File path.</param>
public byte[] File(string filename)
@@ -105,7 +105,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Gets the hash of a file in hexadecimal and as a byte array.
/// Gets the hash of a file in hexadecimal and as a byte array.
/// </summary>
/// <param name="filename">File path.</param>
/// <param name="hash">Byte array of the hash value.</param>
@@ -123,7 +123,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Gets the hash of the specified data buffer.
/// Gets the hash of the specified data buffer.
/// </summary>
/// <param name="data">Data buffer.</param>
/// <param name="len">Length of the data buffer to hash.</param>
@@ -139,7 +139,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Gets the hash of the specified data buffer.
/// Gets the hash of the specified data buffer.
/// </summary>
/// <param name="data">Data buffer.</param>
/// <param name="hash">Byte array of the hash value.</param>

View File

@@ -37,14 +37,14 @@ using System.Text;
namespace DiscImageChef.Checksums
{
/// <summary>
/// Wraps up .NET SHA384 implementation to a Init(), Update(), Final() context.
/// Wraps up .NET SHA384 implementation to a Init(), Update(), Final() context.
/// </summary>
public class Sha384Context
{
SHA384 sha384Provider;
/// <summary>
/// Initializes the SHA384 hash provider
/// Initializes the SHA384 hash provider
/// </summary>
public void Init()
{
@@ -52,7 +52,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Updates the hash with data.
/// Updates the hash with data.
/// </summary>
/// <param name="data">Data buffer.</param>
/// <param name="len">Length of buffer to hash.</param>
@@ -62,7 +62,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Updates the hash with data.
/// Updates the hash with data.
/// </summary>
/// <param name="data">Data buffer.</param>
public void Update(byte[] data)
@@ -71,7 +71,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Returns a byte array of the hash value.
/// Returns a byte array of the hash value.
/// </summary>
public byte[] Final()
{
@@ -80,7 +80,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Returns a hexadecimal representation of the hash value.
/// Returns a hexadecimal representation of the hash value.
/// </summary>
public string End()
{
@@ -93,7 +93,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Gets the hash of a file
/// Gets the hash of a file
/// </summary>
/// <param name="filename">File path.</param>
public byte[] File(string filename)
@@ -105,7 +105,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Gets the hash of a file in hexadecimal and as a byte array.
/// Gets the hash of a file in hexadecimal and as a byte array.
/// </summary>
/// <param name="filename">File path.</param>
/// <param name="hash">Byte array of the hash value.</param>
@@ -123,7 +123,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Gets the hash of the specified data buffer.
/// Gets the hash of the specified data buffer.
/// </summary>
/// <param name="data">Data buffer.</param>
/// <param name="len">Length of the data buffer to hash.</param>
@@ -139,7 +139,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Gets the hash of the specified data buffer.
/// Gets the hash of the specified data buffer.
/// </summary>
/// <param name="data">Data buffer.</param>
/// <param name="hash">Byte array of the hash value.</param>

View File

@@ -37,14 +37,14 @@ using System.Text;
namespace DiscImageChef.Checksums
{
/// <summary>
/// Wraps up .NET SHA512 implementation to a Init(), Update(), Final() context.
/// Wraps up .NET SHA512 implementation to a Init(), Update(), Final() context.
/// </summary>
public class Sha512Context
{
SHA512 sha512Provider;
/// <summary>
/// Initializes the SHA512 hash provider
/// Initializes the SHA512 hash provider
/// </summary>
public void Init()
{
@@ -52,7 +52,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Updates the hash with data.
/// Updates the hash with data.
/// </summary>
/// <param name="data">Data buffer.</param>
/// <param name="len">Length of buffer to hash.</param>
@@ -62,7 +62,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Updates the hash with data.
/// Updates the hash with data.
/// </summary>
/// <param name="data">Data buffer.</param>
public void Update(byte[] data)
@@ -71,7 +71,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Returns a byte array of the hash value.
/// Returns a byte array of the hash value.
/// </summary>
public byte[] Final()
{
@@ -80,7 +80,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Returns a hexadecimal representation of the hash value.
/// Returns a hexadecimal representation of the hash value.
/// </summary>
public string End()
{
@@ -93,7 +93,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Gets the hash of a file
/// Gets the hash of a file
/// </summary>
/// <param name="filename">File path.</param>
public byte[] File(string filename)
@@ -105,7 +105,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Gets the hash of a file in hexadecimal and as a byte array.
/// Gets the hash of a file in hexadecimal and as a byte array.
/// </summary>
/// <param name="filename">File path.</param>
/// <param name="hash">Byte array of the hash value.</param>
@@ -123,7 +123,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Gets the hash of the specified data buffer.
/// Gets the hash of the specified data buffer.
/// </summary>
/// <param name="data">Data buffer.</param>
/// <param name="len">Length of the data buffer to hash.</param>
@@ -139,7 +139,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Gets the hash of the specified data buffer.
/// Gets the hash of the specified data buffer.
/// </summary>
/// <param name="data">Data buffer.</param>
/// <param name="hash">Byte array of the hash value.</param>

View File

@@ -45,7 +45,7 @@ using System.Text;
namespace DiscImageChef.Checksums
{
/// <summary>
/// Implements the SpamSum fuzzy hashing algorithm.
/// Implements the SpamSum fuzzy hashing algorithm.
/// </summary>
public class SpamSumContext
{
@@ -65,41 +65,6 @@ namespace DiscImageChef.Checksums
0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x2B, 0x2F
};
struct RollState
{
public byte[] Window;
// ROLLING_WINDOW
public uint H1;
public uint H2;
public uint H3;
public uint N;
}
/* A blockhash contains a signature state for a specific (implicit) blocksize.
* The blocksize is given by SSDEEP_BS(index). The h and halfh members are the
* FNV hashes, where halfh stops to be reset after digest is SPAMSUM_LENGTH/2
* long. The halfh hash is needed be able to truncate digest for the second
* output hash to stay compatible with ssdeep output. */
struct BlockhashContext
{
public uint H;
public uint Halfh;
public byte[] Digest;
// SPAMSUM_LENGTH
public byte Halfdigest;
public uint Dlen;
}
struct FuzzyState
{
public uint Bhstart;
public uint Bhend;
public BlockhashContext[] Bh;
//NUM_BLOCKHASHES
public ulong TotalSize;
public RollState Roll;
}
FuzzyState self;
void roll_init()
@@ -108,7 +73,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Initializes the SpamSum structures
/// Initializes the SpamSum structures
/// </summary>
public void Init()
{
@@ -254,7 +219,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Updates the hash with data.
/// Updates the hash with data.
/// </summary>
/// <param name="data">Data buffer.</param>
/// <param name="len">Length of buffer to hash.</param>
@@ -265,7 +230,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Updates the hash with data.
/// Updates the hash with data.
/// </summary>
/// <param name="data">Data buffer.</param>
public void Update(byte[] data)
@@ -323,8 +288,8 @@ namespace DiscImageChef.Checksums
if(remain <= 0) throw new Exception("Assertion failed");
result[resultOff] = b64[self.Bh[bi].H % 64];
if(i < 3 || result[resultOff] != result[resultOff - 1] ||
result[resultOff] != result[resultOff - 2] || result[resultOff] != result[resultOff - 3])
if(i < 3 || result[resultOff] != result[resultOff - 1] || result[resultOff] != result[resultOff - 2] ||
result[resultOff] != result[resultOff - 3])
{
++resultOff;
--remain;
@@ -335,8 +300,8 @@ namespace DiscImageChef.Checksums
if(remain <= 0) throw new Exception("Assertion failed");
result[resultOff] = self.Bh[bi].Digest[i];
if(i < 3 || result[resultOff] != result[resultOff - 1] ||
result[resultOff] != result[resultOff - 2] || result[resultOff] != result[resultOff - 3])
if(i < 3 || result[resultOff] != result[resultOff - 1] || result[resultOff] != result[resultOff - 2] ||
result[resultOff] != result[resultOff - 3])
{
++resultOff;
--remain;
@@ -403,7 +368,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Returns a byte array of the hash value.
/// Returns a byte array of the hash value.
/// </summary>
public byte[] Final()
{
@@ -412,7 +377,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Returns a base64 representation of the hash value.
/// Returns a base64 representation of the hash value.
/// </summary>
public string End()
{
@@ -422,7 +387,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Gets the hash of a file
/// Gets the hash of a file
/// </summary>
/// <param name="filename">File path.</param>
public static byte[] File(string filename)
@@ -432,7 +397,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Gets the hash of a file in hexadecimal and as a byte array.
/// Gets the hash of a file in hexadecimal and as a byte array.
/// </summary>
/// <param name="filename">File path.</param>
/// <param name="hash">Byte array of the hash value.</param>
@@ -443,7 +408,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Gets the hash of the specified data buffer.
/// Gets the hash of the specified data buffer.
/// </summary>
/// <param name="data">Data buffer.</param>
/// <param name="len">Length of the data buffer to hash.</param>
@@ -462,7 +427,7 @@ namespace DiscImageChef.Checksums
}
/// <summary>
/// Gets the hash of the specified data buffer.
/// Gets the hash of the specified data buffer.
/// </summary>
/// <param name="data">Data buffer.</param>
/// <param name="hash">null</param>
@@ -486,5 +451,40 @@ namespace DiscImageChef.Checksums
return sb.ToString();
}
struct RollState
{
public byte[] Window;
// ROLLING_WINDOW
public uint H1;
public uint H2;
public uint H3;
public uint N;
}
/* A blockhash contains a signature state for a specific (implicit) blocksize.
* The blocksize is given by SSDEEP_BS(index). The h and halfh members are the
* FNV hashes, where halfh stops to be reset after digest is SPAMSUM_LENGTH/2
* long. The halfh hash is needed be able to truncate digest for the second
* output hash to stay compatible with ssdeep output. */
struct BlockhashContext
{
public uint H;
public uint Halfh;
public byte[] Digest;
// SPAMSUM_LENGTH
public byte Halfdigest;
public uint Dlen;
}
struct FuzzyState
{
public uint Bhstart;
public uint Bhend;
public BlockhashContext[] Bh;
//NUM_BLOCKHASHES
public ulong TotalSize;
public RollState Roll;
}
}
}