mirror of
https://github.com/aaru-dps/Aaru.Checksums.git
synced 2025-12-16 19:24:29 +00:00
REFACTOR: Final cleanup of DiscImageChef.Checksums.
This commit is contained in:
@@ -41,8 +41,8 @@ namespace DiscImageChef.Checksums
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class Adler32Context
|
public class Adler32Context
|
||||||
{
|
{
|
||||||
ushort sum1, sum2;
|
|
||||||
const ushort ADLER_MODULE = 65521;
|
const ushort ADLER_MODULE = 65521;
|
||||||
|
ushort sum1, sum2;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes the Adler-32 sums
|
/// Initializes the Adler-32 sums
|
||||||
@@ -95,7 +95,8 @@ namespace DiscImageChef.Checksums
|
|||||||
StringBuilder adlerOutput = new StringBuilder();
|
StringBuilder adlerOutput = new StringBuilder();
|
||||||
|
|
||||||
BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
|
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();
|
return adlerOutput.ToString();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,10 +42,34 @@ namespace DiscImageChef.Checksums
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static class CdChecksums
|
public static class CdChecksums
|
||||||
{
|
{
|
||||||
static byte[] eccFTable;
|
|
||||||
static byte[] eccBTable;
|
|
||||||
const uint CDCRC32_POLY = 0xD8018001;
|
const uint CDCRC32_POLY = 0xD8018001;
|
||||||
const uint CDCRC32_SEED = 0x00000000;
|
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)
|
public static bool? CheckCdSector(byte[] buffer)
|
||||||
{
|
{
|
||||||
@@ -64,10 +88,13 @@ namespace DiscImageChef.Checksums
|
|||||||
bool? status = null;
|
bool? status = null;
|
||||||
|
|
||||||
if(channelStatus == false || subchannelStatus == false) status = false;
|
if(channelStatus == false || subchannelStatus == false) status = false;
|
||||||
switch(channelStatus) {
|
switch(channelStatus)
|
||||||
case null when subchannelStatus == true: status = true;
|
{
|
||||||
|
case null when subchannelStatus == true:
|
||||||
|
status = true;
|
||||||
break;
|
break;
|
||||||
case true when subchannelStatus == null: status = true;
|
case true when subchannelStatus == null:
|
||||||
|
status = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -155,8 +182,7 @@ namespace DiscImageChef.Checksums
|
|||||||
|
|
||||||
if(channel[0x814] != 0x00 || // reserved (8 bytes)
|
if(channel[0x814] != 0x00 || // reserved (8 bytes)
|
||||||
channel[0x815] != 0x00 || channel[0x816] != 0x00 || channel[0x817] != 0x00 ||
|
channel[0x815] != 0x00 || channel[0x816] != 0x00 || channel[0x817] != 0x00 ||
|
||||||
channel[0x818] != 0x00 || channel[0x819] != 0x00 || channel[0x81A] != 0x00 ||
|
channel[0x818] != 0x00 || channel[0x819] != 0x00 || channel[0x81A] != 0x00 || channel[0x81B] != 0x00)
|
||||||
channel[0x81B] != 0x00)
|
|
||||||
{
|
{
|
||||||
DicConsole.DebugWriteLine("CD checksums",
|
DicConsole.DebugWriteLine("CD checksums",
|
||||||
"Mode 1 sector with data in reserved bytes at address: {0:X2}:{1:X2}:{2:X2}",
|
"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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
DicConsole.DebugWriteLine("CD checksums",
|
DicConsole.DebugWriteLine("CD checksums", "Unknown mode {0} sector at address: {1:X2}:{2:X2}:{3:X2}",
|
||||||
"Unknown mode {0} sector at address: {1:X2}:{2:X2}:{3:X2}",
|
|
||||||
channel[0x00F], channel[0x00C], channel[0x00D], channel[0x00E]);
|
channel[0x00F], channel[0x00C], channel[0x00D], channel[0x00E]);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -484,39 +509,18 @@ namespace DiscImageChef.Checksums
|
|||||||
|
|
||||||
if(cdTextPack4Crc == calculatedCdtp4Crc || cdTextPack4Crc == 0) return status;
|
if(cdTextPack4Crc == calculatedCdtp4Crc || cdTextPack4Crc == 0) return status;
|
||||||
|
|
||||||
DicConsole.DebugWriteLine("CD checksums", "CD-Text Pack 4 CRC 0x{0:X4}, expected 0x{1:X4}",
|
DicConsole.DebugWriteLine("CD checksums", "CD-Text Pack 4 CRC 0x{0:X4}, expected 0x{1:X4}", cdTextPack4Crc,
|
||||||
cdTextPack4Crc, calculatedCdtp4Crc);
|
calculatedCdtp4Crc);
|
||||||
|
|
||||||
return false;
|
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)
|
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;
|
crc16 = (ushort)~crc16;
|
||||||
|
|
||||||
|
|||||||
@@ -43,9 +43,9 @@ namespace DiscImageChef.Checksums
|
|||||||
{
|
{
|
||||||
const ushort CRC16_POLY = 0xA001;
|
const ushort CRC16_POLY = 0xA001;
|
||||||
const ushort CRC16_SEED = 0x0000;
|
const ushort CRC16_SEED = 0x0000;
|
||||||
|
ushort hashInt;
|
||||||
|
|
||||||
ushort[] table;
|
ushort[] table;
|
||||||
ushort hashInt;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes the CRC16 table and seed
|
/// Initializes the CRC16 table and seed
|
||||||
@@ -104,7 +104,8 @@ namespace DiscImageChef.Checksums
|
|||||||
StringBuilder crc16Output = new StringBuilder();
|
StringBuilder crc16Output = new StringBuilder();
|
||||||
|
|
||||||
BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
|
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();
|
return crc16Output.ToString();
|
||||||
}
|
}
|
||||||
@@ -143,7 +144,8 @@ namespace DiscImageChef.Checksums
|
|||||||
}
|
}
|
||||||
|
|
||||||
for(int i = 0; i < fileStream.Length; i++)
|
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;
|
BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
|
||||||
hash = BigEndianBitConverter.GetBytes(localhashInt);
|
hash = BigEndianBitConverter.GetBytes(localhashInt);
|
||||||
|
|||||||
@@ -43,9 +43,9 @@ namespace DiscImageChef.Checksums
|
|||||||
{
|
{
|
||||||
const uint CRC32_POLY = 0xEDB88320;
|
const uint CRC32_POLY = 0xEDB88320;
|
||||||
const uint CRC32_SEED = 0xFFFFFFFF;
|
const uint CRC32_SEED = 0xFFFFFFFF;
|
||||||
|
uint hashInt;
|
||||||
|
|
||||||
uint[] table;
|
uint[] table;
|
||||||
uint hashInt;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes the CRC32 table and seed
|
/// Initializes the CRC32 table and seed
|
||||||
@@ -104,7 +104,8 @@ namespace DiscImageChef.Checksums
|
|||||||
StringBuilder crc32Output = new StringBuilder();
|
StringBuilder crc32Output = new StringBuilder();
|
||||||
|
|
||||||
BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
|
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();
|
return crc32Output.ToString();
|
||||||
}
|
}
|
||||||
@@ -194,7 +195,8 @@ namespace DiscImageChef.Checksums
|
|||||||
localTable[i] = entry;
|
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;
|
localhashInt ^= CRC32_SEED;
|
||||||
BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
|
BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
|
||||||
|
|||||||
@@ -42,9 +42,9 @@ namespace DiscImageChef.Checksums
|
|||||||
{
|
{
|
||||||
const ulong CRC64_POLY = 0xC96C5795D7870F42;
|
const ulong CRC64_POLY = 0xC96C5795D7870F42;
|
||||||
const ulong CRC64_SEED = 0xFFFFFFFFFFFFFFFF;
|
const ulong CRC64_SEED = 0xFFFFFFFFFFFFFFFF;
|
||||||
|
ulong hashInt;
|
||||||
|
|
||||||
ulong[] table;
|
ulong[] table;
|
||||||
ulong hashInt;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes the CRC64 table and seed
|
/// Initializes the CRC64 table and seed
|
||||||
@@ -103,7 +103,8 @@ namespace DiscImageChef.Checksums
|
|||||||
StringBuilder crc64Output = new StringBuilder();
|
StringBuilder crc64Output = new StringBuilder();
|
||||||
|
|
||||||
BigEndianBitConverter.IsLittleEndian = BigEndianBitConverter.IsLittleEndian;
|
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();
|
return crc64Output.ToString();
|
||||||
}
|
}
|
||||||
@@ -193,7 +194,8 @@ namespace DiscImageChef.Checksums
|
|||||||
localTable[i] = entry;
|
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;
|
localhashInt ^= CRC64_SEED;
|
||||||
BigEndianBitConverter.IsLittleEndian = BigEndianBitConverter.IsLittleEndian;
|
BigEndianBitConverter.IsLittleEndian = BigEndianBitConverter.IsLittleEndian;
|
||||||
|
|||||||
@@ -67,27 +67,33 @@ namespace DiscImageChef.Checksums
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class ReedSolomon
|
public class ReedSolomon
|
||||||
{
|
{
|
||||||
/* Primitive polynomials - see Lin & Costello, Error Control Coding Appendix A,
|
/// <summary>
|
||||||
* and Lee & Messerschmitt, Digital Communication p. 453.
|
/// Alpha exponent for the first root of the generator polynomial
|
||||||
*/
|
/// </summary>
|
||||||
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 */
|
|
||||||
const int B0 = 1;
|
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>
|
/// <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)
|
||||||
@@ -395,8 +401,7 @@ namespace DiscImageChef.Checksums
|
|||||||
{
|
{
|
||||||
tmp = 0;
|
tmp = 0;
|
||||||
for(j = 0; j < nn; j++)
|
for(j = 0; j < nn; j++)
|
||||||
if(recd[j] != a0) /* recd[j] in index form */
|
if(recd[j] != a0) /* recd[j] in index form */ tmp ^= alpha_to[Modnn(recd[j] + (B0 + i - 1) * j)];
|
||||||
tmp ^= alpha_to[Modnn(recd[j] + (B0 + i - 1) * j)];
|
|
||||||
|
|
||||||
synError |= tmp; /* set flag if non-zero syndrome =>
|
synError |= tmp; /* set flag if non-zero syndrome =>
|
||||||
* error */
|
* error */
|
||||||
@@ -473,7 +478,8 @@ namespace DiscImageChef.Checksums
|
|||||||
/* r is the step number */
|
/* r is the step number */
|
||||||
/* Compute discrepancy at the r-th step in poly-form */
|
/* Compute discrepancy at the r-th step in poly-form */
|
||||||
int 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])];
|
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 */
|
discrR = index_of[discrR]; /* Index form */
|
||||||
if(discrR == a0)
|
if(discrR == a0)
|
||||||
@@ -560,7 +566,8 @@ namespace DiscImageChef.Checksums
|
|||||||
{
|
{
|
||||||
tmp = 0;
|
tmp = 0;
|
||||||
j = degLambda < i ? degLambda : i;
|
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;
|
if(tmp != 0) degOmega = i;
|
||||||
omega[i] = index_of[tmp];
|
omega[i] = index_of[tmp];
|
||||||
@@ -581,7 +588,8 @@ namespace DiscImageChef.Checksums
|
|||||||
int den = 0;
|
int den = 0;
|
||||||
|
|
||||||
/* lambda[i+1] for i even is the formal derivative lambda_pr of lambda[i] */
|
/* 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)
|
if(den == 0)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -65,41 +65,6 @@ namespace DiscImageChef.Checksums
|
|||||||
0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x2B, 0x2F
|
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;
|
FuzzyState self;
|
||||||
|
|
||||||
void roll_init()
|
void roll_init()
|
||||||
@@ -323,8 +288,8 @@ namespace DiscImageChef.Checksums
|
|||||||
if(remain <= 0) throw new Exception("Assertion failed");
|
if(remain <= 0) throw new Exception("Assertion failed");
|
||||||
|
|
||||||
result[resultOff] = b64[self.Bh[bi].H % 64];
|
result[resultOff] = b64[self.Bh[bi].H % 64];
|
||||||
if(i < 3 || result[resultOff] != result[resultOff - 1] ||
|
if(i < 3 || result[resultOff] != result[resultOff - 1] || result[resultOff] != result[resultOff - 2] ||
|
||||||
result[resultOff] != result[resultOff - 2] || result[resultOff] != result[resultOff - 3])
|
result[resultOff] != result[resultOff - 3])
|
||||||
{
|
{
|
||||||
++resultOff;
|
++resultOff;
|
||||||
--remain;
|
--remain;
|
||||||
@@ -335,8 +300,8 @@ namespace DiscImageChef.Checksums
|
|||||||
if(remain <= 0) throw new Exception("Assertion failed");
|
if(remain <= 0) throw new Exception("Assertion failed");
|
||||||
|
|
||||||
result[resultOff] = self.Bh[bi].Digest[i];
|
result[resultOff] = self.Bh[bi].Digest[i];
|
||||||
if(i < 3 || result[resultOff] != result[resultOff - 1] ||
|
if(i < 3 || result[resultOff] != result[resultOff - 1] || result[resultOff] != result[resultOff - 2] ||
|
||||||
result[resultOff] != result[resultOff - 2] || result[resultOff] != result[resultOff - 3])
|
result[resultOff] != result[resultOff - 3])
|
||||||
{
|
{
|
||||||
++resultOff;
|
++resultOff;
|
||||||
--remain;
|
--remain;
|
||||||
@@ -486,5 +451,40 @@ namespace DiscImageChef.Checksums
|
|||||||
|
|
||||||
return sb.ToString();
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user