REFACTOR: Fixed MOST name inconsistencies.

This commit is contained in:
2017-12-20 17:15:26 +00:00
parent 24137b10c8
commit e0d96ac81b
13 changed files with 530 additions and 530 deletions

View File

@@ -39,7 +39,7 @@ namespace DiscImageChef.Checksums
public class Adler32Context public class Adler32Context
{ {
ushort sum1, sum2; ushort sum1, sum2;
const ushort AdlerModule = 65521; const ushort ADLER_MODULE = 65521;
/// <summary> /// <summary>
/// Initializes the Adler-32 sums /// Initializes the Adler-32 sums
@@ -59,8 +59,8 @@ namespace DiscImageChef.Checksums
{ {
for(int i = 0; i < len; i++) for(int i = 0; i < len; i++)
{ {
sum1 = (ushort)((sum1 + data[i]) % AdlerModule); sum1 = (ushort)((sum1 + data[i]) % ADLER_MODULE);
sum2 = (ushort)((sum2 + sum1) % AdlerModule); sum2 = (ushort)((sum2 + sum1) % ADLER_MODULE);
} }
} }
@@ -127,8 +127,8 @@ namespace DiscImageChef.Checksums
for(int i = 0; i < fileStream.Length; i++) for(int i = 0; i < fileStream.Length; i++)
{ {
localSum1 = (ushort)((localSum1 + fileStream.ReadByte()) % AdlerModule); localSum1 = (ushort)((localSum1 + fileStream.ReadByte()) % ADLER_MODULE);
localSum2 = (ushort)((localSum2 + localSum1) % AdlerModule); localSum2 = (ushort)((localSum2 + localSum1) % ADLER_MODULE);
} }
finalSum = (uint)((localSum2 << 16) | localSum1); finalSum = (uint)((localSum2 << 16) | localSum1);
@@ -161,8 +161,8 @@ namespace DiscImageChef.Checksums
for(int i = 0; i < len; i++) for(int i = 0; i < len; i++)
{ {
localSum1 = (ushort)((localSum1 + data[i]) % AdlerModule); localSum1 = (ushort)((localSum1 + data[i]) % ADLER_MODULE);
localSum2 = (ushort)((localSum2 + localSum1) % AdlerModule); localSum2 = (ushort)((localSum2 + localSum1) % ADLER_MODULE);
} }
finalSum = (uint)((localSum2 << 16) | localSum1); finalSum = (uint)((localSum2 << 16) | localSum1);

View File

@@ -36,14 +36,14 @@ using DiscImageChef.Console;
namespace DiscImageChef.Checksums namespace DiscImageChef.Checksums
{ {
public static class CDChecksums public static class CdChecksums
{ {
static byte[] ECC_F_Table; static byte[] eccFTable;
static byte[] ECC_B_Table; static byte[] eccBTable;
const uint CDCRC32Poly = 0xD8018001; const uint CDCRC32_POLY = 0xD8018001;
const uint CDCRC32Seed = 0x00000000; const uint CDCRC32_SEED = 0x00000000;
public static bool? CheckCDSector(byte[] buffer) public static bool? CheckCdSector(byte[] buffer)
{ {
switch(buffer.Length) switch(buffer.Length)
{ {
@@ -55,8 +55,8 @@ namespace DiscImageChef.Checksums
Array.Copy(buffer, 0, channel, 0, 2352); Array.Copy(buffer, 0, channel, 0, 2352);
Array.Copy(buffer, 2352, subchannel, 0, 96); Array.Copy(buffer, 2352, subchannel, 0, 96);
bool? channelStatus = CheckCDSectorChannel(channel); bool? channelStatus = CheckCdSectorChannel(channel);
bool? subchannelStatus = CheckCDSectorSubChannel(subchannel); bool? subchannelStatus = CheckCdSectorSubChannel(subchannel);
bool? status = null; bool? status = null;
if(channelStatus == null && subchannelStatus == null) status = null; if(channelStatus == null && subchannelStatus == null) status = null;
@@ -67,57 +67,57 @@ namespace DiscImageChef.Checksums
return status; return status;
} }
case 2352: return CheckCDSectorChannel(buffer); case 2352: return CheckCdSectorChannel(buffer);
default: return null; default: return null;
} }
} }
static void ECCInit() static void EccInit()
{ {
ECC_F_Table = new byte[256]; eccFTable = new byte[256];
ECC_B_Table = new byte[256]; eccBTable = new byte[256];
for(uint i = 0; i < 256; i++) for(uint i = 0; i < 256; i++)
{ {
uint j = (uint)((i << 1) ^ ((i & 0x80) == 0x80 ? 0x11D : 0)); uint j = (uint)((i << 1) ^ ((i & 0x80) == 0x80 ? 0x11D : 0));
ECC_F_Table[i] = (byte)j; eccFTable[i] = (byte)j;
ECC_B_Table[i ^ j] = (byte)i; eccBTable[i ^ j] = (byte)i;
} }
} }
static bool CheckECC(byte[] address, byte[] data, uint major_count, uint minor_count, uint major_mult, static bool CheckEcc(byte[] address, byte[] data, uint majorCount, uint minorCount, uint majorMult,
uint minor_inc, byte[] ecc) uint minorInc, byte[] ecc)
{ {
uint size = major_count * minor_count; uint size = majorCount * minorCount;
uint major; uint major;
for(major = 0; major < major_count; major++) for(major = 0; major < majorCount; major++)
{ {
uint index = (major >> 1) * major_mult + (major & 1); uint index = (major >> 1) * majorMult + (major & 1);
byte ecc_a = 0; byte eccA = 0;
byte ecc_b = 0; byte eccB = 0;
uint minor; uint minor;
for(minor = 0; minor < minor_count; minor++) for(minor = 0; minor < minorCount; minor++)
{ {
byte temp; byte temp;
if(index < 4) { temp = address[index]; } if(index < 4) { temp = address[index]; }
else { temp = data[index - 4]; } else { temp = data[index - 4]; }
index += minor_inc; index += minorInc;
if(index >= size) { index -= size; } if(index >= size) { index -= size; }
ecc_a ^= temp; eccA ^= temp;
ecc_b ^= temp; eccB ^= temp;
ecc_a = ECC_F_Table[ecc_a]; eccA = eccFTable[eccA];
} }
ecc_a = ECC_B_Table[ECC_F_Table[ecc_a] ^ ecc_b]; eccA = eccBTable[eccFTable[eccA] ^ eccB];
if(ecc[major] != (ecc_a) || ecc[major + major_count] != (ecc_a ^ ecc_b)) { return false; } if(ecc[major] != (eccA) || ecc[major + majorCount] != (eccA ^ eccB)) { return false; }
} }
return true; return true;
} }
static bool? CheckCDSectorChannel(byte[] channel) static bool? CheckCdSectorChannel(byte[] channel)
{ {
ECCInit(); EccInit();
if(channel[0x000] == 0x00 && // sync (12 bytes) if(channel[0x000] == 0x00 && // sync (12 bytes)
channel[0x001] == 0xFF && channel[0x002] == 0xFF && channel[0x003] == 0xFF && channel[0x004] == 0xFF && channel[0x001] == 0xFF && channel[0x002] == 0xFF && channel[0x003] == 0xFF && channel[0x004] == 0xFF &&
@@ -163,28 +163,28 @@ namespace DiscImageChef.Checksums
byte[] address = new byte[4]; byte[] address = new byte[4];
byte[] data = new byte[2060]; byte[] data = new byte[2060];
byte[] data2 = new byte[2232]; byte[] data2 = new byte[2232];
byte[] ecc_p = new byte[172]; byte[] eccP = new byte[172];
byte[] ecc_q = new byte[104]; byte[] eccQ = new byte[104];
Array.Copy(channel, 0x0C, address, 0, 4); Array.Copy(channel, 0x0C, address, 0, 4);
Array.Copy(channel, 0x0C, data, 0, 2060); Array.Copy(channel, 0x0C, data, 0, 2060);
Array.Copy(channel, 0x0C, data2, 0, 2232); Array.Copy(channel, 0x0C, data2, 0, 2232);
Array.Copy(channel, 0x81C, ecc_p, 0, 172); Array.Copy(channel, 0x81C, eccP, 0, 172);
Array.Copy(channel, 0x8C8, ecc_q, 0, 104); Array.Copy(channel, 0x8C8, eccQ, 0, 104);
bool FailedECC_P = CheckECC(address, data, 86, 24, 2, 86, ecc_p); bool failedEccP = CheckEcc(address, data, 86, 24, 2, 86, eccP);
bool FailedECC_Q = CheckECC(address, data2, 52, 43, 86, 88, ecc_q); bool failedEccQ = CheckEcc(address, data2, 52, 43, 86, 88, eccQ);
if(FailedECC_P) if(failedEccP)
DicConsole.DebugWriteLine("CD checksums", DicConsole.DebugWriteLine("CD checksums",
"Mode 1 sector at address: {0:X2}:{1:X2}:{2:X2}, fails ECC P check", "Mode 1 sector at address: {0:X2}:{1:X2}:{2:X2}, fails ECC P check",
channel[0x00C], channel[0x00D], channel[0x00E]); channel[0x00C], channel[0x00D], channel[0x00E]);
if(FailedECC_Q) if(failedEccQ)
DicConsole.DebugWriteLine("CD checksums", DicConsole.DebugWriteLine("CD checksums",
"Mode 1 sector at address: {0:X2}:{1:X2}:{2:X2}, fails ECC Q check", "Mode 1 sector at address: {0:X2}:{1:X2}:{2:X2}, fails ECC Q check",
channel[0x00C], channel[0x00D], channel[0x00E]); channel[0x00C], channel[0x00D], channel[0x00E]);
if(FailedECC_P || FailedECC_Q) return false; if(failedEccP || failedEccQ) return false;
/* TODO: This is not working /* TODO: This is not working
byte[] SectorForCheck = new byte[0x810]; byte[] SectorForCheck = new byte[0x810];
@@ -244,8 +244,8 @@ namespace DiscImageChef.Checksums
byte[] address = new byte[4]; byte[] address = new byte[4];
byte[] data = new byte[2060]; byte[] data = new byte[2060];
byte[] data2 = new byte[2232]; byte[] data2 = new byte[2232];
byte[] ecc_p = new byte[172]; byte[] eccP = new byte[172];
byte[] ecc_q = new byte[104]; byte[] eccQ = new byte[104];
address[0] = 0; address[0] = 0;
address[1] = 0; address[1] = 0;
@@ -253,22 +253,22 @@ namespace DiscImageChef.Checksums
address[3] = 0; address[3] = 0;
Array.Copy(channel, 0x0C, data, 0, 2060); Array.Copy(channel, 0x0C, data, 0, 2060);
Array.Copy(channel, 0x0C, data2, 0, 2232); Array.Copy(channel, 0x0C, data2, 0, 2232);
Array.Copy(channel, 0x80C, ecc_p, 0, 172); Array.Copy(channel, 0x80C, eccP, 0, 172);
Array.Copy(channel, 0x8B8, ecc_q, 0, 104); Array.Copy(channel, 0x8B8, eccQ, 0, 104);
bool FailedECC_P = CheckECC(address, data, 86, 24, 2, 86, ecc_p); bool failedEccP = CheckEcc(address, data, 86, 24, 2, 86, eccP);
bool FailedECC_Q = CheckECC(address, data2, 52, 43, 86, 88, ecc_q); bool failedEccQ = CheckEcc(address, data2, 52, 43, 86, 88, eccQ);
if(FailedECC_P) if(failedEccP)
DicConsole.DebugWriteLine("CD checksums", DicConsole.DebugWriteLine("CD checksums",
"Mode 2 form 1 sector at address: {0:X2}:{1:X2}:{2:X2}, fails ECC P check", "Mode 2 form 1 sector at address: {0:X2}:{1:X2}:{2:X2}, fails ECC P check",
channel[0x00C], channel[0x00D], channel[0x00E]); channel[0x00C], channel[0x00D], channel[0x00E]);
if(FailedECC_Q) if(failedEccQ)
DicConsole.DebugWriteLine("CD checksums", DicConsole.DebugWriteLine("CD checksums",
"Mode 2 form 1 sector at address: {0:X2}:{1:X2}:{2:X2}, fails ECC Q check", "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]); channel[0x00F], channel[0x00C], channel[0x00D], channel[0x00E]);
if(FailedECC_P || FailedECC_Q) return false; if(failedEccP || failedEccQ) return false;
/* TODO: This is not working /* TODO: This is not working
byte[] SectorForCheck = new byte[0x808]; byte[] SectorForCheck = new byte[0x808];
@@ -299,93 +299,93 @@ namespace DiscImageChef.Checksums
return null; return null;
} }
static bool? CheckCDSectorSubChannel(byte[] subchannel) static bool? CheckCdSectorSubChannel(byte[] subchannel)
{ {
bool? status = true; bool? status = true;
byte[] QSubChannel = new byte[12]; byte[] qSubChannel = new byte[12];
byte[] CDTextPack1 = new byte[18]; byte[] cdTextPack1 = new byte[18];
byte[] CDTextPack2 = new byte[18]; byte[] cdTextPack2 = new byte[18];
byte[] CDTextPack3 = new byte[18]; byte[] cdTextPack3 = new byte[18];
byte[] CDTextPack4 = new byte[18]; byte[] cdTextPack4 = new byte[18];
byte[] CDSubRWPack1 = new byte[24]; byte[] cdSubRwPack1 = new byte[24];
byte[] CDSubRWPack2 = new byte[24]; byte[] cdSubRwPack2 = new byte[24];
byte[] CDSubRWPack3 = new byte[24]; byte[] cdSubRwPack3 = new byte[24];
byte[] CDSubRWPack4 = new byte[24]; byte[] cdSubRwPack4 = new byte[24];
int i = 0; int i = 0;
for(int j = 0; j < 12; j++) QSubChannel[j] = 0; for(int j = 0; j < 12; j++) qSubChannel[j] = 0;
for(int j = 0; j < 18; j++) for(int j = 0; j < 18; j++)
{ {
CDTextPack1[j] = 0; cdTextPack1[j] = 0;
CDTextPack2[j] = 0; cdTextPack2[j] = 0;
CDTextPack3[j] = 0; cdTextPack3[j] = 0;
CDTextPack4[j] = 0; cdTextPack4[j] = 0;
} }
for(int j = 0; j < 24; j++) for(int j = 0; j < 24; j++)
{ {
CDSubRWPack1[j] = 0; cdSubRwPack1[j] = 0;
CDSubRWPack2[j] = 0; cdSubRwPack2[j] = 0;
CDSubRWPack3[j] = 0; cdSubRwPack3[j] = 0;
CDSubRWPack4[j] = 0; cdSubRwPack4[j] = 0;
} }
for(int j = 0; j < 12; j++) for(int j = 0; j < 12; j++)
{ {
QSubChannel[j] = (byte)(QSubChannel[j] | ((subchannel[i++] & 0x40) << 1)); qSubChannel[j] = (byte)(qSubChannel[j] | ((subchannel[i++] & 0x40) << 1));
QSubChannel[j] = (byte)(QSubChannel[j] | (subchannel[i++] & 0x40)); qSubChannel[j] = (byte)(qSubChannel[j] | (subchannel[i++] & 0x40));
QSubChannel[j] = (byte)(QSubChannel[j] | ((subchannel[i++] & 0x40) >> 1)); qSubChannel[j] = (byte)(qSubChannel[j] | ((subchannel[i++] & 0x40) >> 1));
QSubChannel[j] = (byte)(QSubChannel[j] | ((subchannel[i++] & 0x40) >> 2)); qSubChannel[j] = (byte)(qSubChannel[j] | ((subchannel[i++] & 0x40) >> 2));
QSubChannel[j] = (byte)(QSubChannel[j] | ((subchannel[i++] & 0x40) >> 3)); qSubChannel[j] = (byte)(qSubChannel[j] | ((subchannel[i++] & 0x40) >> 3));
QSubChannel[j] = (byte)(QSubChannel[j] | ((subchannel[i++] & 0x40) >> 4)); qSubChannel[j] = (byte)(qSubChannel[j] | ((subchannel[i++] & 0x40) >> 4));
QSubChannel[j] = (byte)(QSubChannel[j] | ((subchannel[i++] & 0x40) >> 5)); qSubChannel[j] = (byte)(qSubChannel[j] | ((subchannel[i++] & 0x40) >> 5));
QSubChannel[j] = (byte)(QSubChannel[j] | ((subchannel[i++] & 0x40) >> 6)); qSubChannel[j] = (byte)(qSubChannel[j] | ((subchannel[i++] & 0x40) >> 6));
} }
i = 0; i = 0;
for(int j = 0; j < 18; j++) for(int j = 0; j < 18; j++)
{ {
if(j < 18) CDTextPack1[j] = (byte)(CDTextPack1[j] | ((subchannel[i++] & 0x3F) << 2)); if(j < 18) cdTextPack1[j] = (byte)(cdTextPack1[j] | ((subchannel[i++] & 0x3F) << 2));
if(j < 18) CDTextPack1[j] = (byte)(CDTextPack1[j++] | ((subchannel[i] & 0xC0) >> 4)); if(j < 18) cdTextPack1[j] = (byte)(cdTextPack1[j++] | ((subchannel[i] & 0xC0) >> 4));
if(j < 18) CDTextPack1[j] = (byte)(CDTextPack1[j] | ((subchannel[i++] & 0x0F) << 4)); if(j < 18) cdTextPack1[j] = (byte)(cdTextPack1[j] | ((subchannel[i++] & 0x0F) << 4));
if(j < 18) CDTextPack1[j] = (byte)(CDTextPack1[j++] | ((subchannel[i] & 0x3C) >> 2)); if(j < 18) cdTextPack1[j] = (byte)(cdTextPack1[j++] | ((subchannel[i] & 0x3C) >> 2));
if(j < 18) CDTextPack1[j] = (byte)(CDTextPack1[j] | ((subchannel[i++] & 0x03) << 6)); if(j < 18) cdTextPack1[j] = (byte)(cdTextPack1[j] | ((subchannel[i++] & 0x03) << 6));
if(j < 18) CDTextPack1[j] = (byte)(CDTextPack1[j] | (subchannel[i++] & 0x3F)); if(j < 18) cdTextPack1[j] = (byte)(cdTextPack1[j] | (subchannel[i++] & 0x3F));
} }
for(int j = 0; j < 18; j++) for(int j = 0; j < 18; j++)
{ {
if(j < 18) CDTextPack2[j] = (byte)(CDTextPack2[j] | ((subchannel[i++] & 0x3F) << 2)); if(j < 18) cdTextPack2[j] = (byte)(cdTextPack2[j] | ((subchannel[i++] & 0x3F) << 2));
if(j < 18) CDTextPack2[j] = (byte)(CDTextPack2[j++] | ((subchannel[i] & 0xC0) >> 4)); if(j < 18) cdTextPack2[j] = (byte)(cdTextPack2[j++] | ((subchannel[i] & 0xC0) >> 4));
if(j < 18) CDTextPack2[j] = (byte)(CDTextPack2[j] | ((subchannel[i++] & 0x0F) << 4)); if(j < 18) cdTextPack2[j] = (byte)(cdTextPack2[j] | ((subchannel[i++] & 0x0F) << 4));
if(j < 18) CDTextPack2[j] = (byte)(CDTextPack2[j++] | ((subchannel[i] & 0x3C) >> 2)); if(j < 18) cdTextPack2[j] = (byte)(cdTextPack2[j++] | ((subchannel[i] & 0x3C) >> 2));
if(j < 18) CDTextPack2[j] = (byte)(CDTextPack2[j] | ((subchannel[i++] & 0x03) << 6)); if(j < 18) cdTextPack2[j] = (byte)(cdTextPack2[j] | ((subchannel[i++] & 0x03) << 6));
if(j < 18) CDTextPack2[j] = (byte)(CDTextPack2[j] | (subchannel[i++] & 0x3F)); if(j < 18) cdTextPack2[j] = (byte)(cdTextPack2[j] | (subchannel[i++] & 0x3F));
} }
for(int j = 0; j < 18; j++) for(int j = 0; j < 18; j++)
{ {
if(j < 18) CDTextPack3[j] = (byte)(CDTextPack3[j] | ((subchannel[i++] & 0x3F) << 2)); if(j < 18) cdTextPack3[j] = (byte)(cdTextPack3[j] | ((subchannel[i++] & 0x3F) << 2));
if(j < 18) CDTextPack3[j] = (byte)(CDTextPack3[j++] | ((subchannel[i] & 0xC0) >> 4)); if(j < 18) cdTextPack3[j] = (byte)(cdTextPack3[j++] | ((subchannel[i] & 0xC0) >> 4));
if(j < 18) CDTextPack3[j] = (byte)(CDTextPack3[j] | ((subchannel[i++] & 0x0F) << 4)); if(j < 18) cdTextPack3[j] = (byte)(cdTextPack3[j] | ((subchannel[i++] & 0x0F) << 4));
if(j < 18) CDTextPack3[j] = (byte)(CDTextPack3[j++] | ((subchannel[i] & 0x3C) >> 2)); if(j < 18) cdTextPack3[j] = (byte)(cdTextPack3[j++] | ((subchannel[i] & 0x3C) >> 2));
if(j < 18) CDTextPack3[j] = (byte)(CDTextPack3[j] | ((subchannel[i++] & 0x03) << 6)); if(j < 18) cdTextPack3[j] = (byte)(cdTextPack3[j] | ((subchannel[i++] & 0x03) << 6));
if(j < 18) CDTextPack3[j] = (byte)(CDTextPack3[j] | (subchannel[i++] & 0x3F)); if(j < 18) cdTextPack3[j] = (byte)(cdTextPack3[j] | (subchannel[i++] & 0x3F));
} }
for(int j = 0; j < 18; j++) for(int j = 0; j < 18; j++)
{ {
if(j < 18) CDTextPack4[j] = (byte)(CDTextPack4[j] | ((subchannel[i++] & 0x3F) << 2)); if(j < 18) cdTextPack4[j] = (byte)(cdTextPack4[j] | ((subchannel[i++] & 0x3F) << 2));
if(j < 18) CDTextPack4[j] = (byte)(CDTextPack4[j++] | ((subchannel[i] & 0xC0) >> 4)); if(j < 18) cdTextPack4[j] = (byte)(cdTextPack4[j++] | ((subchannel[i] & 0xC0) >> 4));
if(j < 18) CDTextPack4[j] = (byte)(CDTextPack4[j] | ((subchannel[i++] & 0x0F) << 4)); if(j < 18) cdTextPack4[j] = (byte)(cdTextPack4[j] | ((subchannel[i++] & 0x0F) << 4));
if(j < 18) CDTextPack4[j] = (byte)(CDTextPack4[j++] | ((subchannel[i] & 0x3C) >> 2)); if(j < 18) cdTextPack4[j] = (byte)(cdTextPack4[j++] | ((subchannel[i] & 0x3C) >> 2));
if(j < 18) CDTextPack4[j] = (byte)(CDTextPack4[j] | ((subchannel[i++] & 0x03) << 6)); if(j < 18) cdTextPack4[j] = (byte)(cdTextPack4[j] | ((subchannel[i++] & 0x03) << 6));
if(j < 18) CDTextPack4[j] = (byte)(CDTextPack4[j] | (subchannel[i++] & 0x3F)); if(j < 18) cdTextPack4[j] = (byte)(cdTextPack4[j] | (subchannel[i++] & 0x3F));
} }
i = 0; i = 0;
for(int j = 0; j < 24; j++) { CDSubRWPack1[j] = (byte)(subchannel[i++] & 0x3F); } for(int j = 0; j < 24; j++) { cdSubRwPack1[j] = (byte)(subchannel[i++] & 0x3F); }
for(int j = 0; j < 24; j++) { CDSubRWPack2[j] = (byte)(subchannel[i++] & 0x3F); } for(int j = 0; j < 24; j++) { cdSubRwPack2[j] = (byte)(subchannel[i++] & 0x3F); }
for(int j = 0; j < 24; j++) { CDSubRWPack3[j] = (byte)(subchannel[i++] & 0x3F); } for(int j = 0; j < 24; j++) { cdSubRwPack3[j] = (byte)(subchannel[i++] & 0x3F); }
for(int j = 0; j < 24; j++) { CDSubRWPack4[j] = (byte)(subchannel[i++] & 0x3F); } for(int j = 0; j < 24; j++) { cdSubRwPack4[j] = (byte)(subchannel[i++] & 0x3F); }
switch(CDSubRWPack1[0]) switch(cdSubRwPack1[0])
{ {
case 0x00: case 0x00:
DicConsole.DebugWriteLine("CD checksums", "Detected Zero Pack in subchannel"); DicConsole.DebugWriteLine("CD checksums", "Detected Zero Pack in subchannel");
@@ -411,87 +411,87 @@ namespace DiscImageChef.Checksums
default: default:
DicConsole.DebugWriteLine("CD checksums", DicConsole.DebugWriteLine("CD checksums",
"Detected unknown Pack type in subchannel: mode {0}, item {1}", "Detected unknown Pack type in subchannel: mode {0}, item {1}",
Convert.ToString(CDSubRWPack1[0] & 0x38, 2), Convert.ToString(cdSubRwPack1[0] & 0x38, 2),
Convert.ToString(CDSubRWPack1[0] & 0x07, 2)); Convert.ToString(cdSubRwPack1[0] & 0x07, 2));
break; break;
} }
BigEndianBitConverter.IsLittleEndian = true; BigEndianBitConverter.IsLittleEndian = true;
ushort QSubChannelCRC = BigEndianBitConverter.ToUInt16(QSubChannel, 10); ushort qSubChannelCrc = BigEndianBitConverter.ToUInt16(qSubChannel, 10);
byte[] QSubChannelForCRC = new byte[10]; byte[] qSubChannelForCrc = new byte[10];
Array.Copy(QSubChannel, 0, QSubChannelForCRC, 0, 10); Array.Copy(qSubChannel, 0, qSubChannelForCrc, 0, 10);
ushort CalculatedQCRC = CalculateCCITT_CRC16(QSubChannelForCRC); ushort calculatedQcrc = CalculateCCITT_CRC16(qSubChannelForCrc);
if(QSubChannelCRC != CalculatedQCRC) if(qSubChannelCrc != calculatedQcrc)
{ {
DicConsole.DebugWriteLine("CD checksums", "Q subchannel CRC 0x{0:X4}, expected 0x{1:X4}", DicConsole.DebugWriteLine("CD checksums", "Q subchannel CRC 0x{0:X4}, expected 0x{1:X4}",
CalculatedQCRC, QSubChannelCRC); calculatedQcrc, qSubChannelCrc);
status = false; status = false;
} }
if((CDTextPack1[0] & 0x80) == 0x80) if((cdTextPack1[0] & 0x80) == 0x80)
{ {
ushort CDTextPack1CRC = BigEndianBitConverter.ToUInt16(CDTextPack1, 16); ushort cdTextPack1Crc = BigEndianBitConverter.ToUInt16(cdTextPack1, 16);
byte[] CDTextPack1ForCRC = new byte[16]; byte[] cdTextPack1ForCrc = new byte[16];
Array.Copy(CDTextPack1, 0, CDTextPack1ForCRC, 0, 16); Array.Copy(cdTextPack1, 0, cdTextPack1ForCrc, 0, 16);
ushort CalculatedCDTP1CRC = CalculateCCITT_CRC16(CDTextPack1ForCRC); ushort calculatedCdtp1Crc = CalculateCCITT_CRC16(cdTextPack1ForCrc);
if(CDTextPack1CRC != CalculatedCDTP1CRC && CDTextPack1CRC != 0) if(cdTextPack1Crc != calculatedCdtp1Crc && cdTextPack1Crc != 0)
{ {
DicConsole.DebugWriteLine("CD checksums", "CD-Text Pack 1 CRC 0x{0:X4}, expected 0x{1:X4}", DicConsole.DebugWriteLine("CD checksums", "CD-Text Pack 1 CRC 0x{0:X4}, expected 0x{1:X4}",
CDTextPack1CRC, CalculatedCDTP1CRC); cdTextPack1Crc, calculatedCdtp1Crc);
status = false; status = false;
} }
} }
if((CDTextPack2[0] & 0x80) == 0x80) if((cdTextPack2[0] & 0x80) == 0x80)
{ {
ushort CDTextPack2CRC = BigEndianBitConverter.ToUInt16(CDTextPack2, 16); ushort cdTextPack2Crc = BigEndianBitConverter.ToUInt16(cdTextPack2, 16);
byte[] CDTextPack2ForCRC = new byte[16]; byte[] cdTextPack2ForCrc = new byte[16];
Array.Copy(CDTextPack2, 0, CDTextPack2ForCRC, 0, 16); Array.Copy(cdTextPack2, 0, cdTextPack2ForCrc, 0, 16);
ushort CalculatedCDTP2CRC = CalculateCCITT_CRC16(CDTextPack2ForCRC); ushort calculatedCdtp2Crc = CalculateCCITT_CRC16(cdTextPack2ForCrc);
DicConsole.DebugWriteLine("CD checksums", "Cyclic CDTP2 0x{0:X4}, Calc CDTP2 0x{1:X4}", CDTextPack2CRC, DicConsole.DebugWriteLine("CD checksums", "Cyclic CDTP2 0x{0:X4}, Calc CDTP2 0x{1:X4}", cdTextPack2Crc,
CalculatedCDTP2CRC); calculatedCdtp2Crc);
if(CDTextPack2CRC != CalculatedCDTP2CRC && CDTextPack2CRC != 0) if(cdTextPack2Crc != calculatedCdtp2Crc && cdTextPack2Crc != 0)
{ {
DicConsole.DebugWriteLine("CD checksums", "CD-Text Pack 2 CRC 0x{0:X4}, expected 0x{1:X4}", DicConsole.DebugWriteLine("CD checksums", "CD-Text Pack 2 CRC 0x{0:X4}, expected 0x{1:X4}",
CDTextPack2CRC, CalculatedCDTP2CRC); cdTextPack2Crc, calculatedCdtp2Crc);
status = false; status = false;
} }
} }
if((CDTextPack3[0] & 0x80) == 0x80) if((cdTextPack3[0] & 0x80) == 0x80)
{ {
ushort CDTextPack3CRC = BigEndianBitConverter.ToUInt16(CDTextPack3, 16); ushort cdTextPack3Crc = BigEndianBitConverter.ToUInt16(cdTextPack3, 16);
byte[] CDTextPack3ForCRC = new byte[16]; byte[] cdTextPack3ForCrc = new byte[16];
Array.Copy(CDTextPack3, 0, CDTextPack3ForCRC, 0, 16); Array.Copy(cdTextPack3, 0, cdTextPack3ForCrc, 0, 16);
ushort CalculatedCDTP3CRC = CalculateCCITT_CRC16(CDTextPack3ForCRC); ushort calculatedCdtp3Crc = CalculateCCITT_CRC16(cdTextPack3ForCrc);
DicConsole.DebugWriteLine("CD checksums", "Cyclic CDTP3 0x{0:X4}, Calc CDTP3 0x{1:X4}", CDTextPack3CRC, DicConsole.DebugWriteLine("CD checksums", "Cyclic CDTP3 0x{0:X4}, Calc CDTP3 0x{1:X4}", cdTextPack3Crc,
CalculatedCDTP3CRC); calculatedCdtp3Crc);
if(CDTextPack3CRC != CalculatedCDTP3CRC && CDTextPack3CRC != 0) if(cdTextPack3Crc != calculatedCdtp3Crc && cdTextPack3Crc != 0)
{ {
DicConsole.DebugWriteLine("CD checksums", "CD-Text Pack 3 CRC 0x{0:X4}, expected 0x{1:X4}", DicConsole.DebugWriteLine("CD checksums", "CD-Text Pack 3 CRC 0x{0:X4}, expected 0x{1:X4}",
CDTextPack3CRC, CalculatedCDTP3CRC); cdTextPack3Crc, calculatedCdtp3Crc);
status = false; status = false;
} }
} }
if((CDTextPack4[0] & 0x80) == 0x80) if((cdTextPack4[0] & 0x80) == 0x80)
{ {
ushort CDTextPack4CRC = BigEndianBitConverter.ToUInt16(CDTextPack4, 16); ushort cdTextPack4Crc = BigEndianBitConverter.ToUInt16(cdTextPack4, 16);
byte[] CDTextPack4ForCRC = new byte[16]; byte[] cdTextPack4ForCrc = new byte[16];
Array.Copy(CDTextPack4, 0, CDTextPack4ForCRC, 0, 16); Array.Copy(cdTextPack4, 0, cdTextPack4ForCrc, 0, 16);
ushort CalculatedCDTP4CRC = CalculateCCITT_CRC16(CDTextPack4ForCRC); ushort calculatedCdtp4Crc = CalculateCCITT_CRC16(cdTextPack4ForCrc);
DicConsole.DebugWriteLine("CD checksums", "Cyclic CDTP4 0x{0:X4}, Calc CDTP4 0x{1:X4}", CDTextPack4CRC, DicConsole.DebugWriteLine("CD checksums", "Cyclic CDTP4 0x{0:X4}, Calc CDTP4 0x{1:X4}", cdTextPack4Crc,
CalculatedCDTP4CRC); calculatedCdtp4Crc);
if(CDTextPack4CRC != CalculatedCDTP4CRC && CDTextPack4CRC != 0) if(cdTextPack4Crc != calculatedCdtp4Crc && cdTextPack4Crc != 0)
{ {
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, CalculatedCDTP4CRC); cdTextPack4Crc, calculatedCdtp4Crc);
status = false; status = false;
} }
} }
@@ -499,7 +499,7 @@ namespace DiscImageChef.Checksums
return status; return status;
} }
static readonly ushort[] CCITT_CRC16Table = static readonly ushort[] CcittCrc16Table =
{ {
0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7, 0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 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, 0xd1ad, 0xe1ce, 0xf1ef, 0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6, 0x9339, 0x8318,
@@ -525,15 +525,15 @@ namespace DiscImageChef.Checksums
static ushort CalculateCCITT_CRC16(byte[] buffer) static ushort CalculateCCITT_CRC16(byte[] buffer)
{ {
ushort CRC16 = 0; ushort crc16 = 0;
for(int i = 0; i < buffer.Length; i++) for(int i = 0; i < buffer.Length; i++)
{ {
CRC16 = (ushort)(CCITT_CRC16Table[(CRC16 >> 8) ^ buffer[i]] ^ (CRC16 << 8)); crc16 = (ushort)(CcittCrc16Table[(crc16 >> 8) ^ buffer[i]] ^ (crc16 << 8));
} }
CRC16 = (ushort)~CRC16; crc16 = (ushort)~crc16;
return CRC16; return crc16;
} }
} }
} }

View File

@@ -39,10 +39,10 @@ namespace DiscImageChef.Checksums
/// <summary> /// <summary>
/// Provides a UNIX similar API to calculate CRC16. /// Provides a UNIX similar API to calculate CRC16.
/// </summary> /// </summary>
public class CRC16Context public class Crc16Context
{ {
const ushort crc16Poly = 0xA001; const ushort CRC16_POLY = 0xA001;
const ushort crc16Seed = 0x0000; const ushort CRC16_SEED = 0x0000;
ushort[] table; ushort[] table;
ushort hashInt; ushort hashInt;
@@ -52,14 +52,14 @@ namespace DiscImageChef.Checksums
/// </summary> /// </summary>
public void Init() public void Init()
{ {
hashInt = crc16Seed; hashInt = CRC16_SEED;
table = new ushort[256]; table = new ushort[256];
for(int i = 0; i < 256; i++) for(int i = 0; i < 256; i++)
{ {
ushort entry = (ushort)i; ushort entry = (ushort)i;
for(int j = 0; j < 8; j++) for(int j = 0; j < 8; j++)
if((entry & 1) == 1) entry = (ushort)((entry >> 1) ^ crc16Poly); if((entry & 1) == 1) entry = (ushort)((entry >> 1) ^ CRC16_POLY);
else entry = (ushort)(entry >> 1); else entry = (ushort)(entry >> 1);
table[i] = entry; table[i] = entry;
@@ -90,7 +90,7 @@ namespace DiscImageChef.Checksums
/// </summary> /// </summary>
public byte[] Final() public byte[] Final()
{ {
hashInt ^= crc16Seed; hashInt ^= CRC16_SEED;
BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian; BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
return BigEndianBitConverter.GetBytes(hashInt); return BigEndianBitConverter.GetBytes(hashInt);
} }
@@ -100,7 +100,7 @@ namespace DiscImageChef.Checksums
/// </summary> /// </summary>
public string End() public string End()
{ {
hashInt ^= crc16Seed; hashInt ^= CRC16_SEED;
StringBuilder crc16Output = new StringBuilder(); StringBuilder crc16Output = new StringBuilder();
BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian; BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
@@ -134,14 +134,14 @@ namespace DiscImageChef.Checksums
ushort[] localTable; ushort[] localTable;
ushort localhashInt; ushort localhashInt;
localhashInt = crc16Seed; localhashInt = CRC16_SEED;
localTable = new ushort[256]; localTable = new ushort[256];
for(int i = 0; i < 256; i++) for(int i = 0; i < 256; i++)
{ {
ushort entry = (ushort)i; ushort entry = (ushort)i;
for(int j = 0; j < 8; j++) for(int j = 0; j < 8; j++)
if((entry & 1) == 1) entry = (ushort)((entry >> 1) ^ crc16Poly); if((entry & 1) == 1) entry = (ushort)((entry >> 1) ^ CRC16_POLY);
else entry = (ushort)(entry >> 1); else entry = (ushort)(entry >> 1);
localTable[i] = entry; localTable[i] = entry;
@@ -170,7 +170,7 @@ namespace DiscImageChef.Checksums
/// <param name="hash">Byte array of the hash value.</param> /// <param name="hash">Byte array of the hash value.</param>
public static string Data(byte[] data, uint len, out byte[] hash) public static string Data(byte[] data, uint len, out byte[] hash)
{ {
return Data(data, len, out hash, crc16Poly, crc16Seed); return Data(data, len, out hash, CRC16_POLY, CRC16_SEED);
} }
/// <summary> /// <summary>

View File

@@ -39,10 +39,10 @@ namespace DiscImageChef.Checksums
/// <summary> /// <summary>
/// Provides a UNIX similar API to calculate CRC32. /// Provides a UNIX similar API to calculate CRC32.
/// </summary> /// </summary>
public class CRC32Context public class Crc32Context
{ {
const uint crc32Poly = 0xEDB88320; const uint CRC32_POLY = 0xEDB88320;
const uint crc32Seed = 0xFFFFFFFF; const uint CRC32_SEED = 0xFFFFFFFF;
uint[] table; uint[] table;
uint hashInt; uint hashInt;
@@ -52,14 +52,14 @@ namespace DiscImageChef.Checksums
/// </summary> /// </summary>
public void Init() public void Init()
{ {
hashInt = crc32Seed; hashInt = CRC32_SEED;
table = new uint[256]; table = new uint[256];
for(int i = 0; i < 256; i++) for(int i = 0; i < 256; i++)
{ {
uint entry = (uint)i; uint entry = (uint)i;
for(int j = 0; j < 8; j++) for(int j = 0; j < 8; j++)
if((entry & 1) == 1) entry = (entry >> 1) ^ crc32Poly; if((entry & 1) == 1) entry = (entry >> 1) ^ CRC32_POLY;
else entry = entry >> 1; else entry = entry >> 1;
table[i] = entry; table[i] = entry;
@@ -90,7 +90,7 @@ namespace DiscImageChef.Checksums
/// </summary> /// </summary>
public byte[] Final() public byte[] Final()
{ {
hashInt ^= crc32Seed; hashInt ^= CRC32_SEED;
BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian; BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
return BigEndianBitConverter.GetBytes(hashInt); return BigEndianBitConverter.GetBytes(hashInt);
} }
@@ -100,7 +100,7 @@ namespace DiscImageChef.Checksums
/// </summary> /// </summary>
public string End() public string End()
{ {
hashInt ^= crc32Seed; hashInt ^= CRC32_SEED;
StringBuilder crc32Output = new StringBuilder(); StringBuilder crc32Output = new StringBuilder();
BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian; BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
@@ -134,14 +134,14 @@ namespace DiscImageChef.Checksums
uint[] localTable; uint[] localTable;
uint localhashInt; uint localhashInt;
localhashInt = crc32Seed; localhashInt = CRC32_SEED;
localTable = new uint[256]; localTable = new uint[256];
for(int i = 0; i < 256; i++) for(int i = 0; i < 256; i++)
{ {
uint entry = (uint)i; uint entry = (uint)i;
for(int j = 0; j < 8; j++) for(int j = 0; j < 8; j++)
if((entry & 1) == 1) entry = (entry >> 1) ^ crc32Poly; if((entry & 1) == 1) entry = (entry >> 1) ^ CRC32_POLY;
else entry = entry >> 1; else entry = entry >> 1;
localTable[i] = entry; localTable[i] = entry;
@@ -150,7 +150,7 @@ namespace DiscImageChef.Checksums
for(int i = 0; i < fileStream.Length; i++) for(int i = 0; i < fileStream.Length; i++)
localhashInt = (localhashInt >> 8) ^ localTable[fileStream.ReadByte() ^ localhashInt & 0xff]; localhashInt = (localhashInt >> 8) ^ localTable[fileStream.ReadByte() ^ localhashInt & 0xff];
localhashInt ^= crc32Seed; localhashInt ^= CRC32_SEED;
BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian; BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
hash = BigEndianBitConverter.GetBytes(localhashInt); hash = BigEndianBitConverter.GetBytes(localhashInt);
@@ -171,7 +171,7 @@ namespace DiscImageChef.Checksums
/// <param name="hash">Byte array of the hash value.</param> /// <param name="hash">Byte array of the hash value.</param>
public static string Data(byte[] data, uint len, out byte[] hash) public static string Data(byte[] data, uint len, out byte[] hash)
{ {
return Data(data, len, out hash, crc32Poly, crc32Seed); return Data(data, len, out hash, CRC32_POLY, CRC32_SEED);
} }
/// <summary> /// <summary>
@@ -202,7 +202,7 @@ namespace DiscImageChef.Checksums
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 ^= crc32Seed; localhashInt ^= CRC32_SEED;
BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian; BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
hash = BigEndianBitConverter.GetBytes(localhashInt); hash = BigEndianBitConverter.GetBytes(localhashInt);

View File

@@ -38,10 +38,10 @@ namespace DiscImageChef.Checksums
/// <summary> /// <summary>
/// Provides a UNIX similar API to calculate CRC64 (ECMA). /// Provides a UNIX similar API to calculate CRC64 (ECMA).
/// </summary> /// </summary>
public class CRC64Context public class Crc64Context
{ {
const ulong crc64Poly = 0xC96C5795D7870F42; const ulong CRC64_POLY = 0xC96C5795D7870F42;
const ulong crc64Seed = 0xFFFFFFFFFFFFFFFF; const ulong CRC64_SEED = 0xFFFFFFFFFFFFFFFF;
ulong[] table; ulong[] table;
ulong hashInt; ulong hashInt;
@@ -51,14 +51,14 @@ namespace DiscImageChef.Checksums
/// </summary> /// </summary>
public void Init() public void Init()
{ {
hashInt = crc64Seed; hashInt = CRC64_SEED;
table = new ulong[256]; table = new ulong[256];
for(int i = 0; i < 256; i++) for(int i = 0; i < 256; i++)
{ {
ulong entry = (ulong)i; ulong entry = (ulong)i;
for(int j = 0; j < 8; j++) for(int j = 0; j < 8; j++)
if((entry & 1) == 1) entry = (entry >> 1) ^ crc64Poly; if((entry & 1) == 1) entry = (entry >> 1) ^ CRC64_POLY;
else entry = entry >> 1; else entry = entry >> 1;
table[i] = entry; table[i] = entry;
@@ -89,7 +89,7 @@ namespace DiscImageChef.Checksums
/// </summary> /// </summary>
public byte[] Final() public byte[] Final()
{ {
hashInt ^= crc64Seed; hashInt ^= CRC64_SEED;
BigEndianBitConverter.IsLittleEndian = BigEndianBitConverter.IsLittleEndian; BigEndianBitConverter.IsLittleEndian = BigEndianBitConverter.IsLittleEndian;
return BigEndianBitConverter.GetBytes(hashInt); return BigEndianBitConverter.GetBytes(hashInt);
} }
@@ -99,7 +99,7 @@ namespace DiscImageChef.Checksums
/// </summary> /// </summary>
public string End() public string End()
{ {
hashInt ^= crc64Seed; hashInt ^= CRC64_SEED;
StringBuilder crc64Output = new StringBuilder(); StringBuilder crc64Output = new StringBuilder();
BigEndianBitConverter.IsLittleEndian = BigEndianBitConverter.IsLittleEndian; BigEndianBitConverter.IsLittleEndian = BigEndianBitConverter.IsLittleEndian;
@@ -133,14 +133,14 @@ namespace DiscImageChef.Checksums
ulong[] localTable; ulong[] localTable;
ulong localhashInt; ulong localhashInt;
localhashInt = crc64Seed; localhashInt = CRC64_SEED;
localTable = new ulong[256]; localTable = new ulong[256];
for(int i = 0; i < 256; i++) for(int i = 0; i < 256; i++)
{ {
ulong entry = (ulong)i; ulong entry = (ulong)i;
for(int j = 0; j < 8; j++) for(int j = 0; j < 8; j++)
if((entry & 1) == 1) entry = (entry >> 1) ^ crc64Poly; if((entry & 1) == 1) entry = (entry >> 1) ^ CRC64_POLY;
else entry = entry >> 1; else entry = entry >> 1;
localTable[i] = entry; localTable[i] = entry;
@@ -149,7 +149,7 @@ namespace DiscImageChef.Checksums
for(int i = 0; i < fileStream.Length; i++) for(int i = 0; i < fileStream.Length; i++)
localhashInt = (localhashInt >> 8) ^ localTable[(ulong)fileStream.ReadByte() ^ localhashInt & 0xffL]; localhashInt = (localhashInt >> 8) ^ localTable[(ulong)fileStream.ReadByte() ^ localhashInt & 0xffL];
localhashInt ^= crc64Seed; localhashInt ^= CRC64_SEED;
BigEndianBitConverter.IsLittleEndian = BigEndianBitConverter.IsLittleEndian; BigEndianBitConverter.IsLittleEndian = BigEndianBitConverter.IsLittleEndian;
hash = BigEndianBitConverter.GetBytes(localhashInt); hash = BigEndianBitConverter.GetBytes(localhashInt);
@@ -170,7 +170,7 @@ namespace DiscImageChef.Checksums
/// <param name="hash">Byte array of the hash value.</param> /// <param name="hash">Byte array of the hash value.</param>
public static string Data(byte[] data, uint len, out byte[] hash) public static string Data(byte[] data, uint len, out byte[] hash)
{ {
return Data(data, len, out hash, crc64Poly, crc64Seed); return Data(data, len, out hash, CRC64_POLY, CRC64_SEED);
} }
/// <summary> /// <summary>
@@ -201,7 +201,7 @@ namespace DiscImageChef.Checksums
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 ^= crc64Seed; localhashInt ^= CRC64_SEED;
BigEndianBitConverter.IsLittleEndian = BigEndianBitConverter.IsLittleEndian; BigEndianBitConverter.IsLittleEndian = BigEndianBitConverter.IsLittleEndian;
hash = BigEndianBitConverter.GetBytes(localhashInt); hash = BigEndianBitConverter.GetBytes(localhashInt);

View File

@@ -39,16 +39,16 @@ namespace DiscImageChef.Checksums
/// <summary> /// <summary>
/// Provides a UNIX similar API to .NET MD5. /// Provides a UNIX similar API to .NET MD5.
/// </summary> /// </summary>
public class MD5Context public class Md5Context
{ {
MD5 _md5Provider; MD5 md5Provider;
/// <summary> /// <summary>
/// Initializes the MD5 hash provider /// Initializes the MD5 hash provider
/// </summary> /// </summary>
public void Init() public void Init()
{ {
_md5Provider = MD5.Create(); md5Provider = MD5.Create();
} }
/// <summary> /// <summary>
@@ -58,7 +58,7 @@ namespace DiscImageChef.Checksums
/// <param name="len">Length of buffer to hash.</param> /// <param name="len">Length of buffer to hash.</param>
public void Update(byte[] data, uint len) public void Update(byte[] data, uint len)
{ {
_md5Provider.TransformBlock(data, 0, (int)len, data, 0); md5Provider.TransformBlock(data, 0, (int)len, data, 0);
} }
/// <summary> /// <summary>
@@ -75,8 +75,8 @@ namespace DiscImageChef.Checksums
/// </summary> /// </summary>
public byte[] Final() public byte[] Final()
{ {
_md5Provider.TransformFinalBlock(new byte[0], 0, 0); md5Provider.TransformFinalBlock(new byte[0], 0, 0);
return _md5Provider.Hash; return md5Provider.Hash;
} }
/// <summary> /// <summary>
@@ -84,10 +84,10 @@ namespace DiscImageChef.Checksums
/// </summary> /// </summary>
public string End() public string End()
{ {
_md5Provider.TransformFinalBlock(new byte[0], 0, 0); md5Provider.TransformFinalBlock(new byte[0], 0, 0);
StringBuilder md5Output = new StringBuilder(); StringBuilder md5Output = new StringBuilder();
for(int i = 0; i < _md5Provider.Hash.Length; i++) { md5Output.Append(_md5Provider.Hash[i].ToString("x2")); } for(int i = 0; i < md5Provider.Hash.Length; i++) { md5Output.Append(md5Provider.Hash[i].ToString("x2")); }
return md5Output.ToString(); return md5Output.ToString();
} }
@@ -99,7 +99,7 @@ namespace DiscImageChef.Checksums
public byte[] File(string filename) public byte[] File(string filename)
{ {
FileStream fileStream = new FileStream(filename, FileMode.Open); FileStream fileStream = new FileStream(filename, FileMode.Open);
byte[] result = _md5Provider.ComputeHash(fileStream); byte[] result = md5Provider.ComputeHash(fileStream);
fileStream.Close(); fileStream.Close();
return result; return result;
} }
@@ -112,7 +112,7 @@ namespace DiscImageChef.Checksums
public string File(string filename, out byte[] hash) public string File(string filename, out byte[] hash)
{ {
FileStream fileStream = new FileStream(filename, FileMode.Open); FileStream fileStream = new FileStream(filename, FileMode.Open);
hash = _md5Provider.ComputeHash(fileStream); hash = md5Provider.ComputeHash(fileStream);
StringBuilder md5Output = new StringBuilder(); StringBuilder md5Output = new StringBuilder();
for(int i = 0; i < hash.Length; i++) { md5Output.Append(hash[i].ToString("x2")); } for(int i = 0; i < hash.Length; i++) { md5Output.Append(hash[i].ToString("x2")); }
@@ -130,7 +130,7 @@ namespace DiscImageChef.Checksums
/// <param name="hash">Byte array of the hash value.</param> /// <param name="hash">Byte array of the hash value.</param>
public string Data(byte[] data, uint len, out byte[] hash) public string Data(byte[] data, uint len, out byte[] hash)
{ {
hash = _md5Provider.ComputeHash(data, 0, (int)len); hash = md5Provider.ComputeHash(data, 0, (int)len);
StringBuilder md5Output = new StringBuilder(); StringBuilder md5Output = new StringBuilder();
for(int i = 0; i < hash.Length; i++) { md5Output.Append(hash[i].ToString("x2")); } for(int i = 0; i < hash.Length; i++) { md5Output.Append(hash[i].ToString("x2")); }

View File

@@ -39,16 +39,16 @@ namespace DiscImageChef.Checksums
/// <summary> /// <summary>
/// Provides a UNIX similar API to .NET RIPEMD160. /// Provides a UNIX similar API to .NET RIPEMD160.
/// </summary> /// </summary>
public class RIPEMD160Context public class Ripemd160Context
{ {
RIPEMD160 _ripemd160Provider; RIPEMD160 ripemd160Provider;
/// <summary> /// <summary>
/// Initializes the RIPEMD160 hash provider /// Initializes the RIPEMD160 hash provider
/// </summary> /// </summary>
public void Init() public void Init()
{ {
_ripemd160Provider = RIPEMD160.Create(); ripemd160Provider = RIPEMD160.Create();
} }
/// <summary> /// <summary>
@@ -58,7 +58,7 @@ namespace DiscImageChef.Checksums
/// <param name="len">Length of buffer to hash.</param> /// <param name="len">Length of buffer to hash.</param>
public void Update(byte[] data, uint len) public void Update(byte[] data, uint len)
{ {
_ripemd160Provider.TransformBlock(data, 0, (int)len, data, 0); ripemd160Provider.TransformBlock(data, 0, (int)len, data, 0);
} }
/// <summary> /// <summary>
@@ -75,8 +75,8 @@ namespace DiscImageChef.Checksums
/// </summary> /// </summary>
public byte[] Final() public byte[] Final()
{ {
_ripemd160Provider.TransformFinalBlock(new byte[0], 0, 0); ripemd160Provider.TransformFinalBlock(new byte[0], 0, 0);
return _ripemd160Provider.Hash; return ripemd160Provider.Hash;
} }
/// <summary> /// <summary>
@@ -84,12 +84,12 @@ namespace DiscImageChef.Checksums
/// </summary> /// </summary>
public string End() public string End()
{ {
_ripemd160Provider.TransformFinalBlock(new byte[0], 0, 0); ripemd160Provider.TransformFinalBlock(new byte[0], 0, 0);
StringBuilder ripemd160Output = new StringBuilder(); StringBuilder ripemd160Output = new StringBuilder();
for(int i = 0; i < _ripemd160Provider.Hash.Length; i++) for(int i = 0; i < ripemd160Provider.Hash.Length; i++)
{ {
ripemd160Output.Append(_ripemd160Provider.Hash[i].ToString("x2")); ripemd160Output.Append(ripemd160Provider.Hash[i].ToString("x2"));
} }
return ripemd160Output.ToString(); return ripemd160Output.ToString();
@@ -102,7 +102,7 @@ namespace DiscImageChef.Checksums
public byte[] File(string filename) public byte[] File(string filename)
{ {
FileStream fileStream = new FileStream(filename, FileMode.Open); FileStream fileStream = new FileStream(filename, FileMode.Open);
byte[] result = _ripemd160Provider.ComputeHash(fileStream); byte[] result = ripemd160Provider.ComputeHash(fileStream);
fileStream.Close(); fileStream.Close();
return result; return result;
} }
@@ -115,7 +115,7 @@ namespace DiscImageChef.Checksums
public string File(string filename, out byte[] hash) public string File(string filename, out byte[] hash)
{ {
FileStream fileStream = new FileStream(filename, FileMode.Open); FileStream fileStream = new FileStream(filename, FileMode.Open);
hash = _ripemd160Provider.ComputeHash(fileStream); hash = ripemd160Provider.ComputeHash(fileStream);
StringBuilder ripemd160Output = new StringBuilder(); StringBuilder ripemd160Output = new StringBuilder();
for(int i = 0; i < hash.Length; i++) { ripemd160Output.Append(hash[i].ToString("x2")); } for(int i = 0; i < hash.Length; i++) { ripemd160Output.Append(hash[i].ToString("x2")); }
@@ -133,7 +133,7 @@ namespace DiscImageChef.Checksums
/// <param name="hash">Byte array of the hash value.</param> /// <param name="hash">Byte array of the hash value.</param>
public string Data(byte[] data, uint len, out byte[] hash) public string Data(byte[] data, uint len, out byte[] hash)
{ {
hash = _ripemd160Provider.ComputeHash(data, 0, (int)len); hash = ripemd160Provider.ComputeHash(data, 0, (int)len);
StringBuilder ripemd160Output = new StringBuilder(); StringBuilder ripemd160Output = new StringBuilder();
for(int i = 0; i < hash.Length; i++) { ripemd160Output.Append(hash[i].ToString("x2")); } for(int i = 0; i < hash.Length; i++) { ripemd160Output.Append(hash[i].ToString("x2")); }

View File

@@ -67,21 +67,21 @@ namespace DiscImageChef.Checksums
/* Primitive polynomials - see Lin & Costello, Error Control Coding Appendix A, /* Primitive polynomials - see Lin & Costello, Error Control Coding Appendix A,
* and Lee & Messerschmitt, Digital Communication p. 453. * and Lee & Messerschmitt, Digital Communication p. 453.
*/ */
int[] Pp; int[] pp;
/* index->polynomial form conversion table */ /* index->polynomial form conversion table */
int[] Alpha_to; int[] alpha_to;
/* Polynomial->index form conversion table */ /* Polynomial->index form conversion table */
int[] Index_of; int[] index_of;
/* Generator polynomial g(x) /* Generator polynomial g(x)
* Degree of g(x) = 2*TT * Degree of g(x) = 2*TT
* has roots @**B0, @**(B0+1), ... ,@^(B0+2*TT-1) * has roots @**B0, @**(B0+1), ... ,@^(B0+2*TT-1)
*/ */
int[] Gg; int[] gg;
int MM, KK, NN; int mm, kk, nn;
/* No legal value in index form represents zero, so /* No legal value in index form represents zero, so
* we need a special value for this purpose * we need a special value for this purpose
*/ */
int A0; int a0;
bool initialized; bool initialized;
/* Alpha exponent for the first root of the generator polynomial */ /* Alpha exponent for the first root of the generator polynomial */
const int B0 = 1; const int B0 = 1;
@@ -89,66 +89,66 @@ namespace DiscImageChef.Checksums
/// <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)
/// </summary> /// </summary>
public void InitRS(int n, int k, int m) public void InitRs(int n, int k, int m)
{ {
switch(m) switch(m)
{ {
case 2: case 2:
Pp = new[] {1, 1, 1}; pp = new[] {1, 1, 1};
break; break;
case 3: case 3:
Pp = new[] {1, 1, 0, 1}; pp = new[] {1, 1, 0, 1};
break; break;
case 4: case 4:
Pp = new[] {1, 1, 0, 0, 1}; pp = new[] {1, 1, 0, 0, 1};
break; break;
case 5: case 5:
Pp = new[] {1, 0, 1, 0, 0, 1}; pp = new[] {1, 0, 1, 0, 0, 1};
break; break;
case 6: case 6:
Pp = new[] {1, 1, 0, 0, 0, 0, 1}; pp = new[] {1, 1, 0, 0, 0, 0, 1};
break; break;
case 7: case 7:
Pp = new[] {1, 0, 0, 1, 0, 0, 0, 1}; pp = new[] {1, 0, 0, 1, 0, 0, 0, 1};
break; break;
case 8: case 8:
Pp = new[] {1, 0, 1, 1, 1, 0, 0, 0, 1}; pp = new[] {1, 0, 1, 1, 1, 0, 0, 0, 1};
break; break;
case 9: case 9:
Pp = new[] {1, 0, 0, 0, 1, 0, 0, 0, 0, 1}; pp = new[] {1, 0, 0, 0, 1, 0, 0, 0, 0, 1};
break; break;
case 10: case 10:
Pp = new[] {1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1}; pp = new[] {1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1};
break; break;
case 11: case 11:
Pp = new[] {1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1}; pp = new[] {1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1};
break; break;
case 12: case 12:
Pp = new[] {1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1}; pp = new[] {1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1};
break; break;
case 13: case 13:
Pp = new[] {1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1}; pp = new[] {1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1};
break; break;
case 14: case 14:
Pp = new[] {1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1}; pp = new[] {1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1};
break; break;
case 15: case 15:
Pp = new[] {1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}; pp = new[] {1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
break; break;
case 16: case 16:
Pp = new[] {1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1}; pp = new[] {1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1};
break; break;
default: throw new ArgumentOutOfRangeException(nameof(m), "m must be between 2 and 16 inclusive"); default: throw new ArgumentOutOfRangeException(nameof(m), "m must be between 2 and 16 inclusive");
} }
MM = m; mm = m;
KK = k; kk = k;
NN = n; nn = n;
A0 = n; a0 = n;
Alpha_to = new int[n + 1]; alpha_to = new int[n + 1];
Index_of = new int[n + 1]; index_of = new int[n + 1];
Gg = new int[NN - KK + 1]; gg = new int[nn - kk + 1];
generate_gf(); generate_gf();
gen_poly(); gen_poly();
@@ -156,35 +156,35 @@ namespace DiscImageChef.Checksums
initialized = true; initialized = true;
} }
int modnn(int x) int Modnn(int x)
{ {
while(x >= NN) while(x >= nn)
{ {
x -= NN; x -= nn;
x = (x >> MM) + (x & NN); x = (x >> mm) + (x & nn);
} }
return x; return x;
} }
static int min(int a, int b) static int Min(int a, int b)
{ {
return ((a) < (b) ? (a) : (b)); return ((a) < (b) ? (a) : (b));
} }
static void CLEAR(ref int[] a, int n) static void Clear(ref int[] a, int n)
{ {
int ci; int ci;
for(ci = (n) - 1; ci >= 0; ci--) (a)[ci] = 0; for(ci = (n) - 1; ci >= 0; ci--) (a)[ci] = 0;
} }
static void COPY(ref int[] a, ref int[] b, int n) static void Copy(ref int[] a, ref int[] b, int n)
{ {
int ci; int ci;
for(ci = (n) - 1; ci >= 0; ci--) (a)[ci] = (b)[ci]; for(ci = (n) - 1; ci >= 0; ci--) (a)[ci] = (b)[ci];
} }
static void COPYDOWN(ref int[] a, ref int[] b, int n) static void Copydown(ref int[] a, ref int[] b, int n)
{ {
int ci; int ci;
for(ci = (n) - 1; ci >= 0; ci--) (a)[ci] = (b)[ci]; for(ci = (n) - 1; ci >= 0; ci--) (a)[ci] = (b)[ci];
@@ -225,32 +225,32 @@ namespace DiscImageChef.Checksums
int i, mask; int i, mask;
mask = 1; mask = 1;
Alpha_to[MM] = 0; alpha_to[mm] = 0;
for(i = 0; i < MM; i++) for(i = 0; i < mm; i++)
{ {
Alpha_to[i] = mask; alpha_to[i] = mask;
Index_of[Alpha_to[i]] = i; index_of[alpha_to[i]] = i;
/* If Pp[i] == 1 then, term @^i occurs in poly-repr of @^MM */ /* If Pp[i] == 1 then, term @^i occurs in poly-repr of @^MM */
if(Pp[i] != 0) Alpha_to[MM] ^= mask; /* Bit-wise EXOR operation */ if(pp[i] != 0) alpha_to[mm] ^= mask; /* Bit-wise EXOR operation */
mask <<= 1; /* single left-shift */ mask <<= 1; /* single left-shift */
} }
Index_of[Alpha_to[MM]] = MM; index_of[alpha_to[mm]] = mm;
/* /*
* Have obtained poly-repr of @^MM. Poly-repr of @^(i+1) is given by * Have obtained poly-repr of @^MM. Poly-repr of @^(i+1) is given by
* poly-repr of @^i shifted left one-bit and accounting for any @^MM * poly-repr of @^i shifted left one-bit and accounting for any @^MM
* term that may occur when poly-repr of @^i is shifted. * term that may occur when poly-repr of @^i is shifted.
*/ */
mask >>= 1; mask >>= 1;
for(i = MM + 1; i < NN; i++) for(i = mm + 1; i < nn; i++)
{ {
if(Alpha_to[i - 1] >= mask) Alpha_to[i] = Alpha_to[MM] ^ ((Alpha_to[i - 1] ^ mask) << 1); if(alpha_to[i - 1] >= mask) alpha_to[i] = alpha_to[mm] ^ ((alpha_to[i - 1] ^ mask) << 1);
else Alpha_to[i] = Alpha_to[i - 1] << 1; else alpha_to[i] = alpha_to[i - 1] << 1;
Index_of[Alpha_to[i]] = i; index_of[alpha_to[i]] = i;
} }
Index_of[0] = A0; index_of[0] = a0;
Alpha_to[NN] = 0; alpha_to[nn] = 0;
} }
/* /*
@@ -270,23 +270,23 @@ namespace DiscImageChef.Checksums
{ {
int i, j; int i, j;
Gg[0] = Alpha_to[B0]; gg[0] = alpha_to[B0];
Gg[1] = 1; /* g(x) = (X+@**B0) initially */ gg[1] = 1; /* g(x) = (X+@**B0) initially */
for(i = 2; i <= NN - KK; i++) for(i = 2; i <= nn - kk; i++)
{ {
Gg[i] = 1; gg[i] = 1;
/* /*
* Below multiply (Gg[0]+Gg[1]*x + ... +Gg[i]x^i) by * Below multiply (Gg[0]+Gg[1]*x + ... +Gg[i]x^i) by
* (@**(B0+i-1) + x) * (@**(B0+i-1) + x)
*/ */
for(j = i - 1; j > 0; j--) for(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)]; 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]; else gg[j] = gg[j - 1];
/* Gg[0] can never be zero */ /* Gg[0] can never be zero */
Gg[0] = Alpha_to[modnn((Index_of[Gg[0]]) + B0 + i - 1)]; gg[0] = alpha_to[Modnn((index_of[gg[0]]) + B0 + i - 1)];
} }
/* convert Gg[] to index form for quicker encoding */ /* convert Gg[] to index form for quicker encoding */
for(i = 0; i <= NN - KK; i++) Gg[i] = Index_of[Gg[i]]; for(i = 0; i <= nn - kk; i++) gg[i] = index_of[gg[i]];
} }
/* /*
@@ -309,28 +309,28 @@ namespace DiscImageChef.Checksums
{ {
int i, j; int i, j;
int feedback; int feedback;
bb = new int[NN - KK]; bb = new int[nn - kk];
CLEAR(ref bb, NN - KK); Clear(ref bb, nn - kk);
for(i = KK - 1; i >= 0; i--) for(i = kk - 1; i >= 0; i--)
{ {
if(MM != 8) { if(data[i] > NN) return -1; /* Illegal symbol */ } if(mm != 8) { if(data[i] > nn) return -1; /* Illegal symbol */ }
feedback = Index_of[data[i] ^ bb[NN - KK - 1]]; feedback = index_of[data[i] ^ bb[nn - kk - 1]];
if(feedback != A0) if(feedback != a0)
{ {
/* feedback term is non-zero */ /* feedback term is non-zero */
for(j = NN - KK - 1; j > 0; j--) for(j = nn - kk - 1; j > 0; j--)
if(Gg[j] != A0) bb[j] = bb[j - 1] ^ Alpha_to[modnn(Gg[j] + feedback)]; if(gg[j] != a0) bb[j] = bb[j - 1] ^ alpha_to[Modnn(gg[j] + feedback)];
else bb[j] = bb[j - 1]; else bb[j] = bb[j - 1];
bb[0] = Alpha_to[modnn(Gg[0] + feedback)]; bb[0] = alpha_to[Modnn(gg[0] + feedback)];
} }
else else
{ {
/* feedback term is zero. encoder becomes a /* feedback term is zero. encoder becomes a
* single-byte shifter */ * single-byte shifter */
for(j = NN - KK - 1; j > 0; j--) bb[j] = bb[j - 1]; for(j = nn - kk - 1; j > 0; j--) bb[j] = bb[j - 1];
bb[0] = 0; bb[0] = 0;
} }
@@ -360,52 +360,52 @@ namespace DiscImageChef.Checksums
/// </summary> /// </summary>
/// <returns>Returns corrected symbols, -1 if illegal or uncorrectable</returns> /// <returns>Returns corrected symbols, -1 if illegal or uncorrectable</returns>
/// <param name="data">Data symbols.</param> /// <param name="data">Data symbols.</param>
/// <param name="eras_pos">Position of erasures.</param> /// <param name="erasPos">Position of erasures.</param>
/// <param name="no_eras">Number of erasures.</param> /// <param name="noEras">Number of erasures.</param>
public int eras_dec_rs(ref int[] data, out int[] eras_pos, int no_eras) public int eras_dec_rs(ref int[] data, out int[] erasPos, int noEras)
{ {
if(initialized) if(initialized)
{ {
eras_pos = new int[NN - KK]; erasPos = new int[nn - kk];
int deg_lambda, el, deg_omega; int degLambda, el, degOmega;
int i, j, r; int i, j, r;
int u, q, tmp, num1, num2, den, discr_r; int u, q, tmp, num1, num2, den, discrR;
int[] recd = new int[NN]; int[] recd = new int[nn];
int[] lambda = new int[NN - KK + 1]; /* Err+Eras Locator poly */ int[] lambda = new int[nn - kk + 1]; /* Err+Eras Locator poly */
int[] s = new int[NN - KK + 1]; /* syndrome poly */ int[] s = new int[nn - kk + 1]; /* syndrome poly */
int[] b = new int[NN - KK + 1]; int[] b = new int[nn - kk + 1];
int[] t = new int[NN - KK + 1]; int[] t = new int[nn - kk + 1];
int[] omega = new int[NN - KK + 1]; int[] omega = new int[nn - kk + 1];
int[] root = new int[NN - KK]; int[] root = new int[nn - kk];
int[] reg = new int[NN - KK + 1]; int[] reg = new int[nn - kk + 1];
int[] loc = new int[NN - KK]; int[] loc = new int[nn - kk];
int syn_error, count; int synError, count;
/* data[] is in polynomial form, copy and convert to index form */ /* data[] is in polynomial form, copy and convert to index form */
for(i = NN - 1; i >= 0; i--) for(i = nn - 1; i >= 0; i--)
{ {
if(MM != 8) { if(data[i] > NN) return -1; /* Illegal symbol */ } if(mm != 8) { if(data[i] > nn) return -1; /* Illegal symbol */ }
recd[i] = Index_of[data[i]]; recd[i] = index_of[data[i]];
} }
/* first form the syndromes; i.e., evaluate recd(x) at roots of g(x) /* first form the syndromes; i.e., evaluate recd(x) at roots of g(x)
* namely @**(B0+i), i = 0, ... ,(NN-KK-1) * namely @**(B0+i), i = 0, ... ,(NN-KK-1)
*/ */
syn_error = 0; synError = 0;
for(i = 1; i <= NN - KK; i++) for(i = 1; i <= nn - kk; i++)
{ {
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)];
syn_error |= tmp; /* set flag if non-zero syndrome => synError |= tmp; /* set flag if non-zero syndrome =>
* error */ * error */
/* store syndrome in index form */ /* store syndrome in index form */
s[i] = Index_of[tmp]; s[i] = index_of[tmp];
} }
if(syn_error == 0) if(synError == 0)
{ {
/* /*
* if syndrome is zero, data[] is a codeword and there are no * if syndrome is zero, data[] is a codeword and there are no
@@ -414,35 +414,35 @@ namespace DiscImageChef.Checksums
return 0; return 0;
} }
CLEAR(ref lambda, NN - KK); Clear(ref lambda, nn - kk);
lambda[0] = 1; lambda[0] = 1;
if(no_eras > 0) if(noEras > 0)
{ {
/* Init lambda to be the erasure locator polynomial */ /* Init lambda to be the erasure locator polynomial */
lambda[1] = Alpha_to[eras_pos[0]]; lambda[1] = alpha_to[erasPos[0]];
for(i = 1; i < no_eras; i++) for(i = 1; i < noEras; i++)
{ {
u = eras_pos[i]; u = erasPos[i];
for(j = i + 1; j > 0; j--) for(j = i + 1; j > 0; j--)
{ {
tmp = Index_of[lambda[j - 1]]; tmp = index_of[lambda[j - 1]];
if(tmp != A0) lambda[j] ^= Alpha_to[modnn(u + tmp)]; if(tmp != a0) lambda[j] ^= alpha_to[Modnn(u + tmp)];
} }
} }
#if DEBUG #if DEBUG
/* find roots of the erasure location polynomial */ /* find roots of the erasure location polynomial */
for(i = 1; i <= no_eras; i++) reg[i] = Index_of[lambda[i]]; for(i = 1; i <= noEras; i++) reg[i] = index_of[lambda[i]];
count = 0; count = 0;
for(i = 1; i <= NN; i++) for(i = 1; i <= nn; i++)
{ {
q = 1; q = 1;
for(j = 1; j <= no_eras; j++) for(j = 1; j <= noEras; j++)
if(reg[j] != A0) if(reg[j] != a0)
{ {
reg[j] = modnn(reg[j] + j); reg[j] = Modnn(reg[j] + j);
q ^= Alpha_to[reg[j]]; q ^= alpha_to[reg[j]];
} }
if(q == 0) if(q == 0)
@@ -451,12 +451,12 @@ namespace DiscImageChef.Checksums
* number indices * number indices
*/ */
root[count] = i; root[count] = i;
loc[count] = NN - i; loc[count] = nn - i;
count++; count++;
} }
} }
if(count != no_eras) if(count != noEras)
{ {
DicConsole.DebugWriteLine("Reed Solomon", "\n lambda(x) is WRONG\n"); DicConsole.DebugWriteLine("Reed Solomon", "\n lambda(x) is WRONG\n");
return -1; return -1;
@@ -470,95 +470,95 @@ namespace DiscImageChef.Checksums
#endif #endif
} }
for(i = 0; i < NN - KK + 1; i++) b[i] = Index_of[lambda[i]]; for(i = 0; i < nn - kk + 1; i++) b[i] = index_of[lambda[i]];
/* /*
* Begin Berlekamp-Massey algorithm to determine error+erasure * Begin Berlekamp-Massey algorithm to determine error+erasure
* locator polynomial * locator polynomial
*/ */
r = no_eras; r = noEras;
el = no_eras; el = noEras;
while(++r <= NN - KK) while(++r <= nn - kk)
{ {
/* 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 */
discr_r = 0; discrR = 0;
for(i = 0; i < r; i++) for(i = 0; i < r; i++)
{ {
if((lambda[i] != 0) && (s[r - i] != A0)) if((lambda[i] != 0) && (s[r - i] != a0))
{ {
discr_r ^= Alpha_to[modnn(Index_of[lambda[i]] + s[r - i])]; discrR ^= alpha_to[Modnn(index_of[lambda[i]] + s[r - i])];
} }
} }
discr_r = Index_of[discr_r]; /* Index form */ discrR = index_of[discrR]; /* Index form */
if(discr_r == A0) if(discrR == a0)
{ {
/* 2 lines below: B(x) <-- x*B(x) */ /* 2 lines below: B(x) <-- x*B(x) */
COPYDOWN(ref b, ref b, NN - KK); Copydown(ref b, ref b, nn - kk);
b[0] = A0; b[0] = a0;
} }
else else
{ {
/* 7 lines below: T(x) <-- lambda(x) - discr_r*x*b(x) */ /* 7 lines below: T(x) <-- lambda(x) - discr_r*x*b(x) */
t[0] = lambda[0]; t[0] = lambda[0];
for(i = 0; i < NN - KK; i++) for(i = 0; i < nn - kk; i++)
{ {
if(b[i] != A0) t[i + 1] = lambda[i + 1] ^ Alpha_to[modnn(discr_r + b[i])]; if(b[i] != a0) t[i + 1] = lambda[i + 1] ^ alpha_to[Modnn(discrR + b[i])];
else t[i + 1] = lambda[i + 1]; else t[i + 1] = lambda[i + 1];
} }
if(2 * el <= r + no_eras - 1) if(2 * el <= r + noEras - 1)
{ {
el = r + no_eras - el; el = r + noEras - el;
/* /*
* 2 lines below: B(x) <-- inv(discr_r) * * 2 lines below: B(x) <-- inv(discr_r) *
* lambda(x) * lambda(x)
*/ */
for(i = 0; i <= NN - KK; i++) for(i = 0; i <= nn - kk; i++)
b[i] = (lambda[i] == 0) ? A0 : modnn(Index_of[lambda[i]] - discr_r + NN); b[i] = (lambda[i] == 0) ? a0 : Modnn(index_of[lambda[i]] - discrR + nn);
} }
else else
{ {
/* 2 lines below: B(x) <-- x*B(x) */ /* 2 lines below: B(x) <-- x*B(x) */
COPYDOWN(ref b, ref b, NN - KK); Copydown(ref b, ref b, nn - kk);
b[0] = A0; b[0] = a0;
} }
COPY(ref lambda, ref t, NN - KK + 1); Copy(ref lambda, ref t, nn - kk + 1);
} }
} }
/* Convert lambda to index form and compute deg(lambda(x)) */ /* Convert lambda to index form and compute deg(lambda(x)) */
deg_lambda = 0; degLambda = 0;
for(i = 0; i < NN - KK + 1; i++) for(i = 0; i < nn - kk + 1; i++)
{ {
lambda[i] = Index_of[lambda[i]]; lambda[i] = index_of[lambda[i]];
if(lambda[i] != A0) deg_lambda = i; if(lambda[i] != a0) degLambda = i;
} }
/* /*
* Find roots of the error+erasure locator polynomial. By Chien * Find roots of the error+erasure locator polynomial. By Chien
* Search * Search
*/ */
int temp = reg[0]; int temp = reg[0];
COPY(ref reg, ref lambda, NN - KK); Copy(ref reg, ref lambda, nn - kk);
reg[0] = temp; reg[0] = temp;
count = 0; /* Number of roots of lambda(x) */ count = 0; /* Number of roots of lambda(x) */
for(i = 1; i <= NN; i++) for(i = 1; i <= nn; i++)
{ {
q = 1; q = 1;
for(j = deg_lambda; j > 0; j--) for(j = degLambda; j > 0; j--)
if(reg[j] != A0) if(reg[j] != a0)
{ {
reg[j] = modnn(reg[j] + j); reg[j] = Modnn(reg[j] + j);
q ^= Alpha_to[reg[j]]; q ^= alpha_to[reg[j]];
} }
if(q == 0) if(q == 0)
{ {
/* store root (index-form) and error location number */ /* store root (index-form) and error location number */
root[count] = i; root[count] = i;
loc[count] = NN - i; loc[count] = nn - i;
count++; count++;
} }
} }
@@ -570,7 +570,7 @@ namespace DiscImageChef.Checksums
DicConsole.DebugWriteLine("Reed Solomon", "\n"); DicConsole.DebugWriteLine("Reed Solomon", "\n");
#endif #endif
if(deg_lambda != count) if(degLambda != count)
{ {
/* /*
* deg(lambda) unequal to number of roots => uncorrectable * deg(lambda) unequal to number of roots => uncorrectable
@@ -582,21 +582,21 @@ namespace DiscImageChef.Checksums
* Compute err+eras evaluator poly omega(x) = s(x)*lambda(x) (modulo * Compute err+eras evaluator poly omega(x) = s(x)*lambda(x) (modulo
* x**(NN-KK)). in index form. Also find deg(omega). * x**(NN-KK)). in index form. Also find deg(omega).
*/ */
deg_omega = 0; degOmega = 0;
for(i = 0; i < NN - KK; i++) for(i = 0; i < nn - kk; i++)
{ {
tmp = 0; tmp = 0;
j = (deg_lambda < i) ? deg_lambda : i; j = (degLambda < i) ? degLambda : i;
for(; j >= 0; 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((s[i + 1 - j] != a0) && (lambda[j] != a0)) tmp ^= alpha_to[Modnn(s[i + 1 - j] + lambda[j])];
} }
if(tmp != 0) deg_omega = i; if(tmp != 0) degOmega = i;
omega[i] = Index_of[tmp]; omega[i] = index_of[tmp];
} }
omega[NN - KK] = A0; omega[nn - kk] = a0;
/* /*
* Compute error values in poly-form. num1 = omega(inv(X(l))), num2 = * Compute error values in poly-form. num1 = omega(inv(X(l))), num2 =
@@ -605,18 +605,18 @@ namespace DiscImageChef.Checksums
for(j = count - 1; j >= 0; j--) for(j = count - 1; j >= 0; j--)
{ {
num1 = 0; num1 = 0;
for(i = deg_omega; i >= 0; i--) for(i = degOmega; i >= 0; i--)
{ {
if(omega[i] != A0) num1 ^= Alpha_to[modnn(omega[i] + i * root[j])]; if(omega[i] != a0) num1 ^= alpha_to[Modnn(omega[i] + i * root[j])];
} }
num2 = Alpha_to[modnn(root[j] * (B0 - 1) + NN)]; num2 = alpha_to[Modnn(root[j] * (B0 - 1) + nn)];
den = 0; 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(deg_lambda, NN - KK - 1) & ~1; i >= 0; i -= 2) 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(lambda[i + 1] != a0) den ^= alpha_to[Modnn(lambda[i + 1] + i * root[j])];
} }
if(den == 0) if(den == 0)
@@ -627,7 +627,7 @@ namespace DiscImageChef.Checksums
/* Apply error to data */ /* Apply error to data */
if(num1 != 0) if(num1 != 0)
{ {
data[loc[j]] ^= Alpha_to[modnn(Index_of[num1] + Index_of[num2] + NN - Index_of[den])]; data[loc[j]] ^= alpha_to[Modnn(index_of[num1] + index_of[num2] + nn - index_of[den])];
} }
} }

View File

@@ -39,16 +39,16 @@ namespace DiscImageChef.Checksums
/// <summary> /// <summary>
/// Provides a UNIX similar API to .NET SHA1. /// Provides a UNIX similar API to .NET SHA1.
/// </summary> /// </summary>
public class SHA1Context public class Sha1Context
{ {
SHA1 _sha1Provider; SHA1 sha1Provider;
/// <summary> /// <summary>
/// Initializes the SHA1 hash provider /// Initializes the SHA1 hash provider
/// </summary> /// </summary>
public void Init() public void Init()
{ {
_sha1Provider = SHA1.Create(); sha1Provider = SHA1.Create();
} }
/// <summary> /// <summary>
@@ -58,7 +58,7 @@ namespace DiscImageChef.Checksums
/// <param name="len">Length of buffer to hash.</param> /// <param name="len">Length of buffer to hash.</param>
public void Update(byte[] data, uint len) public void Update(byte[] data, uint len)
{ {
_sha1Provider.TransformBlock(data, 0, (int)len, data, 0); sha1Provider.TransformBlock(data, 0, (int)len, data, 0);
} }
/// <summary> /// <summary>
@@ -75,8 +75,8 @@ namespace DiscImageChef.Checksums
/// </summary> /// </summary>
public byte[] Final() public byte[] Final()
{ {
_sha1Provider.TransformFinalBlock(new byte[0], 0, 0); sha1Provider.TransformFinalBlock(new byte[0], 0, 0);
return _sha1Provider.Hash; return sha1Provider.Hash;
} }
/// <summary> /// <summary>
@@ -84,12 +84,12 @@ namespace DiscImageChef.Checksums
/// </summary> /// </summary>
public string End() public string End()
{ {
_sha1Provider.TransformFinalBlock(new byte[0], 0, 0); sha1Provider.TransformFinalBlock(new byte[0], 0, 0);
StringBuilder sha1Output = new StringBuilder(); StringBuilder sha1Output = new StringBuilder();
for(int i = 0; i < _sha1Provider.Hash.Length; i++) for(int i = 0; i < sha1Provider.Hash.Length; i++)
{ {
sha1Output.Append(_sha1Provider.Hash[i].ToString("x2")); sha1Output.Append(sha1Provider.Hash[i].ToString("x2"));
} }
return sha1Output.ToString(); return sha1Output.ToString();
@@ -102,7 +102,7 @@ namespace DiscImageChef.Checksums
public byte[] File(string filename) public byte[] File(string filename)
{ {
FileStream fileStream = new FileStream(filename, FileMode.Open); FileStream fileStream = new FileStream(filename, FileMode.Open);
byte[] result = _sha1Provider.ComputeHash(fileStream); byte[] result = sha1Provider.ComputeHash(fileStream);
fileStream.Close(); fileStream.Close();
return result; return result;
} }
@@ -115,7 +115,7 @@ namespace DiscImageChef.Checksums
public string File(string filename, out byte[] hash) public string File(string filename, out byte[] hash)
{ {
FileStream fileStream = new FileStream(filename, FileMode.Open); FileStream fileStream = new FileStream(filename, FileMode.Open);
hash = _sha1Provider.ComputeHash(fileStream); hash = sha1Provider.ComputeHash(fileStream);
StringBuilder sha1Output = new StringBuilder(); StringBuilder sha1Output = new StringBuilder();
for(int i = 0; i < hash.Length; i++) { sha1Output.Append(hash[i].ToString("x2")); } for(int i = 0; i < hash.Length; i++) { sha1Output.Append(hash[i].ToString("x2")); }
@@ -133,7 +133,7 @@ namespace DiscImageChef.Checksums
/// <param name="hash">Byte array of the hash value.</param> /// <param name="hash">Byte array of the hash value.</param>
public string Data(byte[] data, uint len, out byte[] hash) public string Data(byte[] data, uint len, out byte[] hash)
{ {
hash = _sha1Provider.ComputeHash(data, 0, (int)len); hash = sha1Provider.ComputeHash(data, 0, (int)len);
StringBuilder sha1Output = new StringBuilder(); StringBuilder sha1Output = new StringBuilder();
for(int i = 0; i < hash.Length; i++) { sha1Output.Append(hash[i].ToString("x2")); } for(int i = 0; i < hash.Length; i++) { sha1Output.Append(hash[i].ToString("x2")); }

View File

@@ -39,16 +39,16 @@ namespace DiscImageChef.Checksums
/// <summary> /// <summary>
/// Provides a UNIX similar API to .NET SHA256. /// Provides a UNIX similar API to .NET SHA256.
/// </summary> /// </summary>
public class SHA256Context public class Sha256Context
{ {
SHA256 _sha256Provider; SHA256 sha256Provider;
/// <summary> /// <summary>
/// Initializes the SHA256 hash provider /// Initializes the SHA256 hash provider
/// </summary> /// </summary>
public void Init() public void Init()
{ {
_sha256Provider = SHA256.Create(); sha256Provider = SHA256.Create();
} }
/// <summary> /// <summary>
@@ -58,7 +58,7 @@ namespace DiscImageChef.Checksums
/// <param name="len">Length of buffer to hash.</param> /// <param name="len">Length of buffer to hash.</param>
public void Update(byte[] data, uint len) public void Update(byte[] data, uint len)
{ {
_sha256Provider.TransformBlock(data, 0, (int)len, data, 0); sha256Provider.TransformBlock(data, 0, (int)len, data, 0);
} }
/// <summary> /// <summary>
@@ -75,8 +75,8 @@ namespace DiscImageChef.Checksums
/// </summary> /// </summary>
public byte[] Final() public byte[] Final()
{ {
_sha256Provider.TransformFinalBlock(new byte[0], 0, 0); sha256Provider.TransformFinalBlock(new byte[0], 0, 0);
return _sha256Provider.Hash; return sha256Provider.Hash;
} }
/// <summary> /// <summary>
@@ -84,12 +84,12 @@ namespace DiscImageChef.Checksums
/// </summary> /// </summary>
public string End() public string End()
{ {
_sha256Provider.TransformFinalBlock(new byte[0], 0, 0); sha256Provider.TransformFinalBlock(new byte[0], 0, 0);
StringBuilder sha256Output = new StringBuilder(); StringBuilder sha256Output = new StringBuilder();
for(int i = 0; i < _sha256Provider.Hash.Length; i++) for(int i = 0; i < sha256Provider.Hash.Length; i++)
{ {
sha256Output.Append(_sha256Provider.Hash[i].ToString("x2")); sha256Output.Append(sha256Provider.Hash[i].ToString("x2"));
} }
return sha256Output.ToString(); return sha256Output.ToString();
@@ -102,7 +102,7 @@ namespace DiscImageChef.Checksums
public byte[] File(string filename) public byte[] File(string filename)
{ {
FileStream fileStream = new FileStream(filename, FileMode.Open); FileStream fileStream = new FileStream(filename, FileMode.Open);
byte[] result = _sha256Provider.ComputeHash(fileStream); byte[] result = sha256Provider.ComputeHash(fileStream);
fileStream.Close(); fileStream.Close();
return result; return result;
} }
@@ -115,7 +115,7 @@ namespace DiscImageChef.Checksums
public string File(string filename, out byte[] hash) public string File(string filename, out byte[] hash)
{ {
FileStream fileStream = new FileStream(filename, FileMode.Open); FileStream fileStream = new FileStream(filename, FileMode.Open);
hash = _sha256Provider.ComputeHash(fileStream); hash = sha256Provider.ComputeHash(fileStream);
StringBuilder sha256Output = new StringBuilder(); StringBuilder sha256Output = new StringBuilder();
for(int i = 0; i < hash.Length; i++) { sha256Output.Append(hash[i].ToString("x2")); } for(int i = 0; i < hash.Length; i++) { sha256Output.Append(hash[i].ToString("x2")); }
@@ -133,7 +133,7 @@ namespace DiscImageChef.Checksums
/// <param name="hash">Byte array of the hash value.</param> /// <param name="hash">Byte array of the hash value.</param>
public string Data(byte[] data, uint len, out byte[] hash) public string Data(byte[] data, uint len, out byte[] hash)
{ {
hash = _sha256Provider.ComputeHash(data, 0, (int)len); hash = sha256Provider.ComputeHash(data, 0, (int)len);
StringBuilder sha256Output = new StringBuilder(); StringBuilder sha256Output = new StringBuilder();
for(int i = 0; i < hash.Length; i++) { sha256Output.Append(hash[i].ToString("x2")); } for(int i = 0; i < hash.Length; i++) { sha256Output.Append(hash[i].ToString("x2")); }

View File

@@ -39,16 +39,16 @@ namespace DiscImageChef.Checksums
/// <summary> /// <summary>
/// Provides a UNIX similar API to .NET SHA384. /// Provides a UNIX similar API to .NET SHA384.
/// </summary> /// </summary>
public class SHA384Context public class Sha384Context
{ {
SHA384 _sha384Provider; SHA384 sha384Provider;
/// <summary> /// <summary>
/// Initializes the SHA384 hash provider /// Initializes the SHA384 hash provider
/// </summary> /// </summary>
public void Init() public void Init()
{ {
_sha384Provider = SHA384.Create(); sha384Provider = SHA384.Create();
} }
/// <summary> /// <summary>
@@ -58,7 +58,7 @@ namespace DiscImageChef.Checksums
/// <param name="len">Length of buffer to hash.</param> /// <param name="len">Length of buffer to hash.</param>
public void Update(byte[] data, uint len) public void Update(byte[] data, uint len)
{ {
_sha384Provider.TransformBlock(data, 0, (int)len, data, 0); sha384Provider.TransformBlock(data, 0, (int)len, data, 0);
} }
/// <summary> /// <summary>
@@ -75,8 +75,8 @@ namespace DiscImageChef.Checksums
/// </summary> /// </summary>
public byte[] Final() public byte[] Final()
{ {
_sha384Provider.TransformFinalBlock(new byte[0], 0, 0); sha384Provider.TransformFinalBlock(new byte[0], 0, 0);
return _sha384Provider.Hash; return sha384Provider.Hash;
} }
/// <summary> /// <summary>
@@ -84,12 +84,12 @@ namespace DiscImageChef.Checksums
/// </summary> /// </summary>
public string End() public string End()
{ {
_sha384Provider.TransformFinalBlock(new byte[0], 0, 0); sha384Provider.TransformFinalBlock(new byte[0], 0, 0);
StringBuilder sha384Output = new StringBuilder(); StringBuilder sha384Output = new StringBuilder();
for(int i = 0; i < _sha384Provider.Hash.Length; i++) for(int i = 0; i < sha384Provider.Hash.Length; i++)
{ {
sha384Output.Append(_sha384Provider.Hash[i].ToString("x2")); sha384Output.Append(sha384Provider.Hash[i].ToString("x2"));
} }
return sha384Output.ToString(); return sha384Output.ToString();
@@ -102,7 +102,7 @@ namespace DiscImageChef.Checksums
public byte[] File(string filename) public byte[] File(string filename)
{ {
FileStream fileStream = new FileStream(filename, FileMode.Open); FileStream fileStream = new FileStream(filename, FileMode.Open);
byte[] result = _sha384Provider.ComputeHash(fileStream); byte[] result = sha384Provider.ComputeHash(fileStream);
fileStream.Close(); fileStream.Close();
return result; return result;
} }
@@ -115,7 +115,7 @@ namespace DiscImageChef.Checksums
public string File(string filename, out byte[] hash) public string File(string filename, out byte[] hash)
{ {
FileStream fileStream = new FileStream(filename, FileMode.Open); FileStream fileStream = new FileStream(filename, FileMode.Open);
hash = _sha384Provider.ComputeHash(fileStream); hash = sha384Provider.ComputeHash(fileStream);
StringBuilder sha384Output = new StringBuilder(); StringBuilder sha384Output = new StringBuilder();
for(int i = 0; i < hash.Length; i++) { sha384Output.Append(hash[i].ToString("x2")); } for(int i = 0; i < hash.Length; i++) { sha384Output.Append(hash[i].ToString("x2")); }
@@ -133,7 +133,7 @@ namespace DiscImageChef.Checksums
/// <param name="hash">Byte array of the hash value.</param> /// <param name="hash">Byte array of the hash value.</param>
public string Data(byte[] data, uint len, out byte[] hash) public string Data(byte[] data, uint len, out byte[] hash)
{ {
hash = _sha384Provider.ComputeHash(data, 0, (int)len); hash = sha384Provider.ComputeHash(data, 0, (int)len);
StringBuilder sha384Output = new StringBuilder(); StringBuilder sha384Output = new StringBuilder();
for(int i = 0; i < hash.Length; i++) { sha384Output.Append(hash[i].ToString("x2")); } for(int i = 0; i < hash.Length; i++) { sha384Output.Append(hash[i].ToString("x2")); }

View File

@@ -39,16 +39,16 @@ namespace DiscImageChef.Checksums
/// <summary> /// <summary>
/// Provides a UNIX similar API to .NET SHA512. /// Provides a UNIX similar API to .NET SHA512.
/// </summary> /// </summary>
public class SHA512Context public class Sha512Context
{ {
SHA512 _sha512Provider; SHA512 sha512Provider;
/// <summary> /// <summary>
/// Initializes the SHA512 hash provider /// Initializes the SHA512 hash provider
/// </summary> /// </summary>
public void Init() public void Init()
{ {
_sha512Provider = SHA512.Create(); sha512Provider = SHA512.Create();
} }
/// <summary> /// <summary>
@@ -58,7 +58,7 @@ namespace DiscImageChef.Checksums
/// <param name="len">Length of buffer to hash.</param> /// <param name="len">Length of buffer to hash.</param>
public void Update(byte[] data, uint len) public void Update(byte[] data, uint len)
{ {
_sha512Provider.TransformBlock(data, 0, (int)len, data, 0); sha512Provider.TransformBlock(data, 0, (int)len, data, 0);
} }
/// <summary> /// <summary>
@@ -75,8 +75,8 @@ namespace DiscImageChef.Checksums
/// </summary> /// </summary>
public byte[] Final() public byte[] Final()
{ {
_sha512Provider.TransformFinalBlock(new byte[0], 0, 0); sha512Provider.TransformFinalBlock(new byte[0], 0, 0);
return _sha512Provider.Hash; return sha512Provider.Hash;
} }
/// <summary> /// <summary>
@@ -84,12 +84,12 @@ namespace DiscImageChef.Checksums
/// </summary> /// </summary>
public string End() public string End()
{ {
_sha512Provider.TransformFinalBlock(new byte[0], 0, 0); sha512Provider.TransformFinalBlock(new byte[0], 0, 0);
StringBuilder sha512Output = new StringBuilder(); StringBuilder sha512Output = new StringBuilder();
for(int i = 0; i < _sha512Provider.Hash.Length; i++) for(int i = 0; i < sha512Provider.Hash.Length; i++)
{ {
sha512Output.Append(_sha512Provider.Hash[i].ToString("x2")); sha512Output.Append(sha512Provider.Hash[i].ToString("x2"));
} }
return sha512Output.ToString(); return sha512Output.ToString();
@@ -102,7 +102,7 @@ namespace DiscImageChef.Checksums
public byte[] File(string filename) public byte[] File(string filename)
{ {
FileStream fileStream = new FileStream(filename, FileMode.Open); FileStream fileStream = new FileStream(filename, FileMode.Open);
byte[] result = _sha512Provider.ComputeHash(fileStream); byte[] result = sha512Provider.ComputeHash(fileStream);
fileStream.Close(); fileStream.Close();
return result; return result;
} }
@@ -115,7 +115,7 @@ namespace DiscImageChef.Checksums
public string File(string filename, out byte[] hash) public string File(string filename, out byte[] hash)
{ {
FileStream fileStream = new FileStream(filename, FileMode.Open); FileStream fileStream = new FileStream(filename, FileMode.Open);
hash = _sha512Provider.ComputeHash(fileStream); hash = sha512Provider.ComputeHash(fileStream);
StringBuilder sha512Output = new StringBuilder(); StringBuilder sha512Output = new StringBuilder();
for(int i = 0; i < hash.Length; i++) { sha512Output.Append(hash[i].ToString("x2")); } for(int i = 0; i < hash.Length; i++) { sha512Output.Append(hash[i].ToString("x2")); }
@@ -133,7 +133,7 @@ namespace DiscImageChef.Checksums
/// <param name="hash">Byte array of the hash value.</param> /// <param name="hash">Byte array of the hash value.</param>
public string Data(byte[] data, uint len, out byte[] hash) public string Data(byte[] data, uint len, out byte[] hash)
{ {
hash = _sha512Provider.ComputeHash(data, 0, (int)len); hash = sha512Provider.ComputeHash(data, 0, (int)len);
StringBuilder sha512Output = new StringBuilder(); StringBuilder sha512Output = new StringBuilder();
for(int i = 0; i < hash.Length; i++) { sha512Output.Append(hash[i].ToString("x2")); } for(int i = 0; i < hash.Length; i++) { sha512Output.Append(hash[i].ToString("x2")); }

View File

@@ -65,14 +65,14 @@ namespace DiscImageChef.Checksums
0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x2B, 0x2F 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x2B, 0x2F
}; };
struct roll_state struct RollState
{ {
public byte[] window; public byte[] Window;
// ROLLING_WINDOW // ROLLING_WINDOW
public uint h1; public uint H1;
public uint h2; public uint H2;
public uint h3; public uint H3;
public uint n; public uint N;
} }
/* A blockhash contains a signature state for a specific (implicit) blocksize. /* A blockhash contains a signature state for a specific (implicit) blocksize.
@@ -80,32 +80,32 @@ namespace DiscImageChef.Checksums
* FNV hashes, where halfh stops to be reset after digest is SPAMSUM_LENGTH/2 * 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 * long. The halfh hash is needed be able to truncate digest for the second
* output hash to stay compatible with ssdeep output. */ * output hash to stay compatible with ssdeep output. */
struct blockhash_context struct BlockhashContext
{ {
public uint h; public uint H;
public uint halfh; public uint Halfh;
public byte[] digest; public byte[] Digest;
// SPAMSUM_LENGTH // SPAMSUM_LENGTH
public byte halfdigest; public byte Halfdigest;
public uint dlen; public uint Dlen;
} }
struct fuzzy_state struct FuzzyState
{ {
public uint bhstart; public uint Bhstart;
public uint bhend; public uint Bhend;
public blockhash_context[] bh; public BlockhashContext[] Bh;
//NUM_BLOCKHASHES //NUM_BLOCKHASHES
public ulong total_size; public ulong TotalSize;
public roll_state roll; public RollState Roll;
} }
fuzzy_state self; FuzzyState self;
void roll_init() void roll_init()
{ {
self.roll = new roll_state(); self.Roll = new RollState();
self.roll.window = new byte[ROLLING_WINDOW]; self.Roll.Window = new byte[ROLLING_WINDOW];
} }
/// <summary> /// <summary>
@@ -113,18 +113,18 @@ namespace DiscImageChef.Checksums
/// </summary> /// </summary>
public void Init() public void Init()
{ {
self = new fuzzy_state(); self = new FuzzyState();
self.bh = new blockhash_context[NUM_BLOCKHASHES]; self.Bh = new BlockhashContext[NUM_BLOCKHASHES];
for(int i = 0; i < NUM_BLOCKHASHES; i++) self.bh[i].digest = new byte[SPAMSUM_LENGTH]; for(int i = 0; i < NUM_BLOCKHASHES; i++) self.Bh[i].Digest = new byte[SPAMSUM_LENGTH];
self.bhstart = 0; self.Bhstart = 0;
self.bhend = 1; self.Bhend = 1;
self.bh[0].h = HASH_INIT; self.Bh[0].H = HASH_INIT;
self.bh[0].halfh = HASH_INIT; self.Bh[0].Halfh = HASH_INIT;
self.bh[0].digest[0] = 0; self.Bh[0].Digest[0] = 0;
self.bh[0].halfdigest = 0; self.Bh[0].Halfdigest = 0;
self.bh[0].dlen = 0; self.Bh[0].Dlen = 0;
self.total_size = 0; self.TotalSize = 0;
roll_init(); roll_init();
} }
@@ -140,25 +140,25 @@ namespace DiscImageChef.Checksums
*/ */
void roll_hash(byte c) void roll_hash(byte c)
{ {
self.roll.h2 -= self.roll.h1; self.Roll.H2 -= self.Roll.H1;
self.roll.h2 += ROLLING_WINDOW * c; self.Roll.H2 += ROLLING_WINDOW * c;
self.roll.h1 += c; self.Roll.H1 += c;
self.roll.h1 -= self.roll.window[self.roll.n % ROLLING_WINDOW]; self.Roll.H1 -= self.Roll.Window[self.Roll.N % ROLLING_WINDOW];
self.roll.window[self.roll.n % ROLLING_WINDOW] = c; self.Roll.Window[self.Roll.N % ROLLING_WINDOW] = c;
self.roll.n++; self.Roll.N++;
/* The original spamsum AND'ed this value with 0xFFFFFFFF which /* The original spamsum AND'ed this value with 0xFFFFFFFF which
* in theory should have no effect. This AND has been removed * in theory should have no effect. This AND has been removed
* for performance (jk) */ * for performance (jk) */
self.roll.h3 <<= 5; self.Roll.H3 <<= 5;
self.roll.h3 ^= c; self.Roll.H3 ^= c;
} }
uint roll_sum() uint roll_sum()
{ {
return self.roll.h1 + self.roll.h2 + self.roll.h3; return self.Roll.H1 + self.Roll.H2 + self.Roll.H3;
} }
/* A simple non-rolling hash, based on the FNV hash. */ /* A simple non-rolling hash, based on the FNV hash. */
@@ -176,35 +176,35 @@ namespace DiscImageChef.Checksums
{ {
uint obh, nbh; uint obh, nbh;
if(self.bhend >= NUM_BLOCKHASHES) return; if(self.Bhend >= NUM_BLOCKHASHES) return;
if(self.bhend == 0) // assert if(self.Bhend == 0) // assert
throw new Exception("Assertion failed"); throw new Exception("Assertion failed");
obh = self.bhend - 1; obh = self.Bhend - 1;
nbh = self.bhend; nbh = self.Bhend;
self.bh[nbh].h = self.bh[obh].h; self.Bh[nbh].H = self.Bh[obh].H;
self.bh[nbh].halfh = self.bh[obh].halfh; self.Bh[nbh].Halfh = self.Bh[obh].Halfh;
self.bh[nbh].digest[0] = 0; self.Bh[nbh].Digest[0] = 0;
self.bh[nbh].halfdigest = 0; self.Bh[nbh].Halfdigest = 0;
self.bh[nbh].dlen = 0; self.Bh[nbh].Dlen = 0;
++self.bhend; ++self.Bhend;
} }
void fuzzy_try_reduce_blockhash() 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) if(self.Bhend - self.Bhstart < 2)
/* Need at least two working hashes. */ return; /* Need at least two working hashes. */ return;
if((ulong)SSDEEP_BS(self.bhstart) * SPAMSUM_LENGTH >= self.total_size) if((ulong)SSDEEP_BS(self.Bhstart) * SPAMSUM_LENGTH >= self.TotalSize)
/* Initial blocksize estimate would select this or a smaller /* Initial blocksize estimate would select this or a smaller
* blocksize. */ return; * blocksize. */ return;
if(self.bh[self.bhstart + 1].dlen < SPAMSUM_LENGTH / 2) if(self.Bh[self.Bhstart + 1].Dlen < SPAMSUM_LENGTH / 2)
/* Estimate adjustment would select this blocksize. */ return; /* Estimate adjustment would select this blocksize. */ return;
/* At this point we are clearly no longer interested in the /* At this point we are clearly no longer interested in the
* start_blocksize. Get rid of it. */ * start_blocksize. Get rid of it. */
++self.bhstart; ++self.Bhstart;
} }
void fuzzy_engine_step(byte c) void fuzzy_engine_step(byte c)
@@ -217,13 +217,13 @@ namespace DiscImageChef.Checksums
roll_hash(c); roll_hash(c);
h = roll_sum(); h = roll_sum();
for(i = self.bhstart; i < self.bhend; ++i) for(i = self.Bhstart; i < self.Bhend; ++i)
{ {
self.bh[i].h = sum_hash(c, self.bh[i].h); self.Bh[i].H = sum_hash(c, self.Bh[i].H);
self.bh[i].halfh = sum_hash(c, self.bh[i].halfh); self.Bh[i].Halfh = sum_hash(c, self.Bh[i].Halfh);
} }
for(i = self.bhstart; i < self.bhend; ++i) for(i = self.Bhstart; i < self.Bhend; ++i)
{ {
/* With growing blocksize almost no runs fail the next test. */ /* With growing blocksize almost no runs fail the next test. */
if(h % SSDEEP_BS(i) != SSDEEP_BS(i) - 1) if(h % SSDEEP_BS(i) != SSDEEP_BS(i) - 1)
@@ -233,15 +233,15 @@ namespace DiscImageChef.Checksums
/* We have hit a reset point. We now emit hashes which are /* We have hit a reset point. We now emit hashes which are
* based on all characters in the piece of the message between * based on all characters in the piece of the message between
* the last reset point and this one */ * the last reset point and this one */
if(0 == self.bh[i].dlen) if(0 == self.Bh[i].Dlen)
{ {
/* Can only happen 30 times. */ /* Can only happen 30 times. */
/* First step for this blocksize. Clone next. */ /* First step for this blocksize. Clone next. */
fuzzy_try_fork_blockhash(); fuzzy_try_fork_blockhash();
} }
self.bh[i].digest[self.bh[i].dlen] = b64[self.bh[i].h % 64]; self.Bh[i].Digest[self.Bh[i].Dlen] = b64[self.Bh[i].H % 64];
self.bh[i].halfdigest = b64[self.bh[i].halfh % 64]; self.Bh[i].Halfdigest = b64[self.Bh[i].Halfh % 64];
if(self.bh[i].dlen < SPAMSUM_LENGTH - 1) if(self.Bh[i].Dlen < SPAMSUM_LENGTH - 1)
{ {
/* We can have a problem with the tail overflowing. The /* We can have a problem with the tail overflowing. The
* easiest way to cope with this is to only reset the * easiest way to cope with this is to only reset the
@@ -249,12 +249,12 @@ namespace DiscImageChef.Checksums
* our signature. This has the effect of combining the * our signature. This has the effect of combining the
* last few pieces of the message into a single piece * last few pieces of the message into a single piece
* */ * */
self.bh[i].digest[++(self.bh[i].dlen)] = 0; self.Bh[i].Digest[++(self.Bh[i].Dlen)] = 0;
self.bh[i].h = HASH_INIT; self.Bh[i].H = HASH_INIT;
if(self.bh[i].dlen < SPAMSUM_LENGTH / 2) if(self.Bh[i].Dlen < SPAMSUM_LENGTH / 2)
{ {
self.bh[i].halfh = HASH_INIT; self.Bh[i].Halfh = HASH_INIT;
self.bh[i].halfdigest = 0; self.Bh[i].Halfdigest = 0;
} }
} }
else fuzzy_try_reduce_blockhash(); else fuzzy_try_reduce_blockhash();
@@ -268,7 +268,7 @@ namespace DiscImageChef.Checksums
/// <param name="len">Length of buffer to hash.</param> /// <param name="len">Length of buffer to hash.</param>
public void Update(byte[] data, uint len) public void Update(byte[] data, uint len)
{ {
self.total_size += len; self.TotalSize += len;
for(int i = 0; i < len; i++) fuzzy_engine_step(data[i]); for(int i = 0; i < len; i++) fuzzy_engine_step(data[i]);
} }
@@ -285,28 +285,28 @@ namespace DiscImageChef.Checksums
uint fuzzy_digest(out byte[] result) uint fuzzy_digest(out byte[] result)
{ {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
uint bi = self.bhstart; uint bi = self.Bhstart;
uint h = roll_sum(); uint h = roll_sum();
int i, result_off; int i, resultOff;
int remain = (int)(FUZZY_MAX_RESULT - 1); /* Exclude terminating '\0'. */ int remain = (int)(FUZZY_MAX_RESULT - 1); /* Exclude terminating '\0'. */
result = new byte[FUZZY_MAX_RESULT]; result = new byte[FUZZY_MAX_RESULT];
/* Verify that our elimination was not overeager. */ /* Verify that our elimination was not overeager. */
if(!(bi == 0 || (ulong)SSDEEP_BS(bi) / 2 * SPAMSUM_LENGTH < self.total_size)) if(!(bi == 0 || (ulong)SSDEEP_BS(bi) / 2 * SPAMSUM_LENGTH < self.TotalSize))
throw new Exception("Assertion failed"); throw new Exception("Assertion failed");
result_off = 0; resultOff = 0;
/* Initial blocksize guess. */ /* Initial blocksize guess. */
while((ulong)SSDEEP_BS(bi) * SPAMSUM_LENGTH < self.total_size) while((ulong)SSDEEP_BS(bi) * SPAMSUM_LENGTH < self.TotalSize)
{ {
++bi; ++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. */ /* 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)); sb.AppendFormat("{0}:", SSDEEP_BS(bi));
i = Encoding.ASCII.GetBytes(sb.ToString()).Length; i = Encoding.ASCII.GetBytes(sb.ToString()).Length;
@@ -318,78 +318,78 @@ namespace DiscImageChef.Checksums
Array.Copy(Encoding.ASCII.GetBytes(sb.ToString()), 0, result, 0, i); Array.Copy(Encoding.ASCII.GetBytes(sb.ToString()), 0, result, 0, i);
result_off += i; resultOff += i;
i = (int)self.bh[bi].dlen; 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, result_off, i); Array.Copy(self.Bh[bi].Digest, 0, result, resultOff, i);
result_off += i; resultOff += i;
remain -= i; remain -= i;
if(h != 0) if(h != 0)
{ {
if(remain <= 0) throw new Exception("Assertion failed"); if(remain <= 0) throw new Exception("Assertion failed");
result[result_off] = b64[self.bh[bi].h % 64]; result[resultOff] = b64[self.Bh[bi].H % 64];
if(i < 3 || result[result_off] != result[result_off - 1] || if(i < 3 || result[resultOff] != result[resultOff - 1] ||
result[result_off] != result[result_off - 2] || result[result_off] != result[result_off - 3]) result[resultOff] != result[resultOff - 2] || result[resultOff] != result[resultOff - 3])
{ {
++result_off; ++resultOff;
--remain; --remain;
} }
} }
else if(self.bh[bi].digest[i] != 0) 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[result_off] = self.bh[bi].digest[i]; result[resultOff] = self.Bh[bi].Digest[i];
if(i < 3 || result[result_off] != result[result_off - 1] || if(i < 3 || result[resultOff] != result[resultOff - 1] ||
result[result_off] != result[result_off - 2] || result[result_off] != result[result_off - 3]) result[resultOff] != result[resultOff - 2] || result[resultOff] != result[resultOff - 3])
{ {
++result_off; ++resultOff;
--remain; --remain;
} }
} }
if(remain <= 0) throw new Exception("Assertion failed"); if(remain <= 0) throw new Exception("Assertion failed");
result[result_off++] = 0x3A; // ':' result[resultOff++] = 0x3A; // ':'
--remain; --remain;
if(bi < self.bhend - 1) if(bi < self.Bhend - 1)
{ {
++bi; ++bi;
i = (int)self.bh[bi].dlen; 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, result_off, i); Array.Copy(self.Bh[bi].Digest, 0, result, resultOff, i);
result_off += i; resultOff += i;
remain -= i; remain -= i;
if(h != 0) if(h != 0)
{ {
if(remain <= 0) throw new Exception("Assertion failed"); if(remain <= 0) throw new Exception("Assertion failed");
h = self.bh[bi].halfh; h = self.Bh[bi].Halfh;
result[result_off] = b64[h % 64]; result[resultOff] = b64[h % 64];
if(i < 3 || result[result_off] != result[result_off - 1] || if(i < 3 || result[resultOff] != result[resultOff - 1] ||
result[result_off] != result[result_off - 2] || result[result_off] != result[result_off - 3]) result[resultOff] != result[resultOff - 2] || result[resultOff] != result[resultOff - 3])
{ {
++result_off; ++resultOff;
--remain; --remain;
} }
} }
else else
{ {
i = self.bh[bi].halfdigest; i = self.Bh[bi].Halfdigest;
if(i != 0) if(i != 0)
{ {
if(remain <= 0) throw new Exception("Assertion failed"); if(remain <= 0) throw new Exception("Assertion failed");
result[result_off] = (byte)i; result[resultOff] = (byte)i;
if(i < 3 || result[result_off] != result[result_off - 1] || if(i < 3 || result[resultOff] != result[resultOff - 1] ||
result[result_off] != result[result_off - 2] || result[result_off] != result[result_off - 3]) result[resultOff] != result[resultOff - 2] || result[resultOff] != result[resultOff - 3])
{ {
++result_off; ++resultOff;
--remain; --remain;
} }
} }
@@ -397,16 +397,16 @@ namespace DiscImageChef.Checksums
} }
else if(h != 0) 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[result_off++] = b64[self.bh[bi].h % 64]; result[resultOff++] = b64[self.Bh[bi].H % 64];
/* No need to bother with FUZZY_FLAG_ELIMSEQ, because this /* No need to bother with FUZZY_FLAG_ELIMSEQ, because this
* digest has length 1. */ * digest has length 1. */
--remain; --remain;
} }
result[result_off] = 0; result[resultOff] = 0;
return 0; return 0;
} }
@@ -482,15 +482,15 @@ namespace DiscImageChef.Checksums
} }
// Converts an ASCII null-terminated string to .NET string // Converts an ASCII null-terminated string to .NET string
private string CToString(byte[] CString) private string CToString(byte[] cString)
{ {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
for(int i = 0; i < CString.Length; i++) for(int i = 0; i < cString.Length; i++)
{ {
if(CString[i] == 0) break; if(cString[i] == 0) break;
sb.Append(Encoding.ASCII.GetString(CString, i, 1)); sb.Append(Encoding.ASCII.GetString(cString, i, 1));
} }
return sb.ToString(); return sb.ToString();