[Aaru.Checksums] Reformat and cleanup.

This commit is contained in:
2023-10-03 22:45:06 +01:00
parent 03a488e834
commit 3429b9012b
28 changed files with 2656 additions and 2416 deletions

View File

@@ -82,10 +82,10 @@ public static class CdChecksums
{
case 2448:
{
byte[] subchannel = new byte[96];
byte[] channel = new byte[2352];
var subchannel = new byte[96];
var channel = new byte[2352];
Array.Copy(buffer, 0, channel, 0, 2352);
Array.Copy(buffer, 0, channel, 0, 2352);
Array.Copy(buffer, 2352, subchannel, 0, 96);
bool? channelStatus = CheckCdSectorChannel(channel, out correctEccP, out correctEccQ, out correctEdc);
@@ -98,17 +98,19 @@ public static class CdChecksums
status = false;
status = channelStatus switch
{
null when subchannelStatus == true => true,
true when subchannelStatus == null => true,
_ => status
};
{
null when subchannelStatus == true => true,
true when subchannelStatus == null => true,
_ => status
};
return status;
}
case 2352: return CheckCdSectorChannel(buffer, out correctEccP, out correctEccQ, out correctEdc);
default: return null;
case 2352:
return CheckCdSectorChannel(buffer, out correctEccP, out correctEccQ, out correctEdc);
default:
return null;
}
}
@@ -121,12 +123,12 @@ public static class CdChecksums
for(uint i = 0; i < 256; i++)
{
uint edc = i;
uint j = (uint)((i << 1) ^ ((i & 0x80) == 0x80 ? 0x11D : 0));
var j = (uint)(i << 1 ^ ((i & 0x80) == 0x80 ? 0x11D : 0));
_eccFTable[i] = (byte)j;
_eccBTable[i ^ j] = (byte)i;
for(j = 0; j < 8; j++)
edc = (edc >> 1) ^ ((edc & 1) > 0 ? 0xD8018001 : 0);
edc = edc >> 1 ^ ((edc & 1) > 0 ? 0xD8018001 : 0);
_edcTable[i] = edc;
}
@@ -140,7 +142,7 @@ public static class CdChecksums
for(major = 0; major < majorCount; major++)
{
uint index = ((major >> 1) * majorMult) + (major & 1);
uint index = (major >> 1) * majorMult + (major & 1);
byte eccA = 0;
byte eccB = 0;
uint minor;
@@ -168,7 +170,7 @@ public static class CdChecksums
return true;
}
static bool? CheckCdSectorChannel(byte[] channel, out bool? correctEccP, out bool? correctEccQ,
static bool? CheckCdSectorChannel(byte[] channel, out bool? correctEccP, out bool? correctEccQ,
out bool? correctEdc)
{
EccInit();
@@ -201,7 +203,8 @@ public static class CdChecksums
{
//AaruConsole.DebugWriteLine(MODULE_NAME, "Mode 0 sector at address {0:X2}:{1:X2}:{2:X2}",
// channel[0x00C], channel[0x00D], channel[0x00E]);
for(int i = 0x010; i < 0x930; i++)
for(var i = 0x010; i < 0x930; i++)
{
if(channel[i] != 0x00)
{
AaruConsole.DebugWriteLine(MODULE_NAME,
@@ -210,6 +213,7 @@ public static class CdChecksums
return false;
}
}
return true;
}
@@ -228,35 +232,39 @@ public static class CdChecksums
return false;
case 0x01:
{
byte[] address = new byte[4];
byte[] data = new byte[2060];
byte[] data2 = new byte[2232];
byte[] eccP = new byte[172];
byte[] eccQ = new byte[104];
var address = new byte[4];
var data = new byte[2060];
var data2 = new byte[2232];
var eccP = new byte[172];
var eccQ = new byte[104];
Array.Copy(channel, 0x0C, address, 0, 4);
Array.Copy(channel, 0x10, data, 0, 2060);
Array.Copy(channel, 0x10, data2, 0, 2232);
Array.Copy(channel, 0x81C, eccP, 0, 172);
Array.Copy(channel, 0x8C8, eccQ, 0, 104);
Array.Copy(channel, 0x0C, address, 0, 4);
Array.Copy(channel, 0x10, data, 0, 2060);
Array.Copy(channel, 0x10, data2, 0, 2232);
Array.Copy(channel, 0x81C, eccP, 0, 172);
Array.Copy(channel, 0x8C8, eccQ, 0, 104);
bool failedEccP = !CheckEcc(address, data, 86, 24, 2, 86, eccP);
bool failedEccP = !CheckEcc(address, data, 86, 24, 2, 86, eccP);
bool failedEccQ = !CheckEcc(address, data2, 52, 43, 86, 88, eccQ);
correctEccP = !failedEccP;
correctEccQ = !failedEccQ;
if(failedEccP)
{
AaruConsole.DebugWriteLine(MODULE_NAME,
"Mode 1 sector at address: {0:X2}:{1:X2}:{2:X2}, fails ECC P check",
channel[0x00C], channel[0x00D], channel[0x00E]);
}
if(failedEccQ)
{
AaruConsole.DebugWriteLine(MODULE_NAME,
"Mode 1 sector at address: {0:X2}:{1:X2}:{2:X2}, fails ECC Q check",
channel[0x00C], channel[0x00D], channel[0x00E]);
}
uint storedEdc = BitConverter.ToUInt32(channel, 0x810);
var storedEdc = BitConverter.ToUInt32(channel, 0x810);
uint calculatedEdc = ComputeEdc(0, channel, 0x810);
correctEdc = calculatedEdc == storedEdc;
@@ -276,7 +284,7 @@ public static class CdChecksums
{
//AaruConsole.DebugWriteLine(MODULE_NAME, "Mode 2 sector at address {0:X2}:{1:X2}:{2:X2}",
// channel[0x00C], channel[0x00D], channel[0x00E]);
byte[] mode2Sector = new byte[channel.Length - 0x10];
var mode2Sector = new byte[channel.Length - 0x10];
Array.Copy(channel, 0x10, mode2Sector, 0, mode2Sector.Length);
if((channel[0x012] & 0x20) == 0x20) // mode 2 form 2
@@ -285,11 +293,13 @@ public static class CdChecksums
channel[0x011] != channel[0x015] ||
channel[0x012] != channel[0x016] ||
channel[0x013] != channel[0x017])
{
AaruConsole.DebugWriteLine(MODULE_NAME,
"Subheader copies differ in mode 2 form 2 sector at address: {0:X2}:{1:X2}:{2:X2}",
channel[0x00C], channel[0x00D], channel[0x00E]);
}
uint storedEdc = BitConverter.ToUInt32(mode2Sector, 0x91C);
var storedEdc = BitConverter.ToUInt32(mode2Sector, 0x91C);
// No CRC stored!
if(storedEdc == 0x00000000)
@@ -315,34 +325,40 @@ public static class CdChecksums
channel[0x011] != channel[0x015] ||
channel[0x012] != channel[0x016] ||
channel[0x013] != channel[0x017])
{
AaruConsole.DebugWriteLine(MODULE_NAME,
"Subheader copies differ in mode 2 form 1 sector at address: {0:X2}:{1:X2}:{2:X2}",
channel[0x00C], channel[0x00D], channel[0x00E]);
}
byte[] address = new byte[4];
byte[] eccP = new byte[172];
byte[] eccQ = new byte[104];
var address = new byte[4];
var eccP = new byte[172];
var eccQ = new byte[104];
Array.Copy(mode2Sector, 0x80C, eccP, 0, 172);
Array.Copy(mode2Sector, 0x8B8, eccQ, 0, 104);
bool failedEccP = !CheckEcc(address, mode2Sector, 86, 24, 2, 86, eccP);
bool failedEccP = !CheckEcc(address, mode2Sector, 86, 24, 2, 86, eccP);
bool failedEccQ = !CheckEcc(address, mode2Sector, 52, 43, 86, 88, eccQ);
correctEccP = !failedEccP;
correctEccQ = !failedEccQ;
if(failedEccP)
{
AaruConsole.DebugWriteLine(MODULE_NAME,
"Mode 2 form 1 sector at address: {0:X2}:{1:X2}:{2:X2}, fails ECC P check",
channel[0x00C], channel[0x00D], channel[0x00E]);
}
if(failedEccQ)
{
AaruConsole.DebugWriteLine(MODULE_NAME,
"Mode 2 form 1 sector at address: {0:X2}:{1:X2}:{2:X2}, fails ECC Q check",
channel[0x00C], channel[0x00D], channel[0x00E]);
}
uint storedEdc = BitConverter.ToUInt32(mode2Sector, 0x808);
var storedEdc = BitConverter.ToUInt32(mode2Sector, 0x808);
uint calculatedEdc = ComputeEdc(0, mode2Sector, 0x808);
correctEdc = calculatedEdc == storedEdc;
@@ -368,33 +384,33 @@ public static class CdChecksums
static uint ComputeEdc(uint edc, IReadOnlyList<byte> src, int size)
{
int pos = 0;
var pos = 0;
for(; size > 0; size--)
edc = (edc >> 8) ^ _edcTable[(edc ^ src[pos++]) & 0xFF];
edc = edc >> 8 ^ _edcTable[(edc ^ src[pos++]) & 0xFF];
return edc;
}
static bool? CheckCdSectorSubChannel(IReadOnlyList<byte> subchannel)
{
bool? status = true;
byte[] qSubChannel = new byte[12];
byte[] cdTextPack1 = new byte[18];
byte[] cdTextPack2 = new byte[18];
byte[] cdTextPack3 = new byte[18];
byte[] cdTextPack4 = new byte[18];
byte[] cdSubRwPack1 = new byte[24];
byte[] cdSubRwPack2 = new byte[24];
byte[] cdSubRwPack3 = new byte[24];
byte[] cdSubRwPack4 = new byte[24];
bool? status = true;
var qSubChannel = new byte[12];
var cdTextPack1 = new byte[18];
var cdTextPack2 = new byte[18];
var cdTextPack3 = new byte[18];
var cdTextPack4 = new byte[18];
var cdSubRwPack1 = new byte[24];
var cdSubRwPack2 = new byte[24];
var cdSubRwPack3 = new byte[24];
var cdSubRwPack4 = new byte[24];
int i = 0;
var i = 0;
for(int j = 0; j < 12; j++)
for(var j = 0; j < 12; j++)
qSubChannel[j] = 0;
for(int j = 0; j < 18; j++)
for(var j = 0; j < 18; j++)
{
cdTextPack1[j] = 0;
cdTextPack2[j] = 0;
@@ -402,7 +418,7 @@ public static class CdChecksums
cdTextPack4[j] = 0;
}
for(int j = 0; j < 24; j++)
for(var j = 0; j < 24; j++)
{
cdSubRwPack1[j] = 0;
cdSubRwPack2[j] = 0;
@@ -410,100 +426,100 @@ public static class CdChecksums
cdSubRwPack4[j] = 0;
}
for(int j = 0; j < 12; j++)
for(var j = 0; j < 12; j++)
{
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) >> 1));
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) >> 4));
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) << 1);
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) >> 2);
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) >> 5);
qSubChannel[j] = (byte)(qSubChannel[j] | (subchannel[i++] & 0x40) >> 6);
}
i = 0;
for(int j = 0; j < 18; j++)
for(var j = 0; j < 18; j++)
{
cdTextPack1[j] = (byte)(cdTextPack1[j] | ((subchannel[i++] & 0x3F) << 2));
cdTextPack1[j] = (byte)(cdTextPack1[j] | (subchannel[i++] & 0x3F) << 2);
if(j < 17)
cdTextPack1[j] = (byte)(cdTextPack1[j++] | ((subchannel[i] & 0xC0) >> 4));
cdTextPack1[j] = (byte)(cdTextPack1[j++] | (subchannel[i] & 0xC0) >> 4);
cdTextPack1[j] = (byte)(cdTextPack1[j] | ((subchannel[i++] & 0x0F) << 4));
cdTextPack1[j] = (byte)(cdTextPack1[j] | (subchannel[i++] & 0x0F) << 4);
if(j < 17)
cdTextPack1[j] = (byte)(cdTextPack1[j++] | ((subchannel[i] & 0x3C) >> 2));
cdTextPack1[j] = (byte)(cdTextPack1[j++] | (subchannel[i] & 0x3C) >> 2);
cdTextPack1[j] = (byte)(cdTextPack1[j] | ((subchannel[i++] & 0x03) << 6));
cdTextPack1[j] = (byte)(cdTextPack1[j] | (subchannel[i++] & 0x03) << 6);
cdTextPack1[j] = (byte)(cdTextPack1[j] | (subchannel[i++] & 0x3F));
cdTextPack1[j] = (byte)(cdTextPack1[j] | subchannel[i++] & 0x3F);
}
for(int j = 0; j < 18; j++)
for(var j = 0; j < 18; j++)
{
cdTextPack2[j] = (byte)(cdTextPack2[j] | ((subchannel[i++] & 0x3F) << 2));
cdTextPack2[j] = (byte)(cdTextPack2[j] | (subchannel[i++] & 0x3F) << 2);
if(j < 17)
cdTextPack2[j] = (byte)(cdTextPack2[j++] | ((subchannel[i] & 0xC0) >> 4));
cdTextPack2[j] = (byte)(cdTextPack2[j++] | (subchannel[i] & 0xC0) >> 4);
cdTextPack2[j] = (byte)(cdTextPack2[j] | ((subchannel[i++] & 0x0F) << 4));
cdTextPack2[j] = (byte)(cdTextPack2[j] | (subchannel[i++] & 0x0F) << 4);
if(j < 17)
cdTextPack2[j] = (byte)(cdTextPack2[j++] | ((subchannel[i] & 0x3C) >> 2));
cdTextPack2[j] = (byte)(cdTextPack2[j++] | (subchannel[i] & 0x3C) >> 2);
cdTextPack2[j] = (byte)(cdTextPack2[j] | ((subchannel[i++] & 0x03) << 6));
cdTextPack2[j] = (byte)(cdTextPack2[j] | (subchannel[i++] & 0x03) << 6);
cdTextPack2[j] = (byte)(cdTextPack2[j] | (subchannel[i++] & 0x3F));
cdTextPack2[j] = (byte)(cdTextPack2[j] | subchannel[i++] & 0x3F);
}
for(int j = 0; j < 18; j++)
for(var j = 0; j < 18; j++)
{
cdTextPack3[j] = (byte)(cdTextPack3[j] | ((subchannel[i++] & 0x3F) << 2));
cdTextPack3[j] = (byte)(cdTextPack3[j] | (subchannel[i++] & 0x3F) << 2);
if(j < 17)
cdTextPack3[j] = (byte)(cdTextPack3[j++] | ((subchannel[i] & 0xC0) >> 4));
cdTextPack3[j] = (byte)(cdTextPack3[j++] | (subchannel[i] & 0xC0) >> 4);
cdTextPack3[j] = (byte)(cdTextPack3[j] | ((subchannel[i++] & 0x0F) << 4));
cdTextPack3[j] = (byte)(cdTextPack3[j] | (subchannel[i++] & 0x0F) << 4);
if(j < 17)
cdTextPack3[j] = (byte)(cdTextPack3[j++] | ((subchannel[i] & 0x3C) >> 2));
cdTextPack3[j] = (byte)(cdTextPack3[j++] | (subchannel[i] & 0x3C) >> 2);
cdTextPack3[j] = (byte)(cdTextPack3[j] | ((subchannel[i++] & 0x03) << 6));
cdTextPack3[j] = (byte)(cdTextPack3[j] | (subchannel[i++] & 0x03) << 6);
cdTextPack3[j] = (byte)(cdTextPack3[j] | (subchannel[i++] & 0x3F));
cdTextPack3[j] = (byte)(cdTextPack3[j] | subchannel[i++] & 0x3F);
}
for(int j = 0; j < 18; j++)
for(var j = 0; j < 18; j++)
{
cdTextPack4[j] = (byte)(cdTextPack4[j] | ((subchannel[i++] & 0x3F) << 2));
cdTextPack4[j] = (byte)(cdTextPack4[j] | (subchannel[i++] & 0x3F) << 2);
if(j < 17)
cdTextPack4[j] = (byte)(cdTextPack4[j++] | ((subchannel[i] & 0xC0) >> 4));
cdTextPack4[j] = (byte)(cdTextPack4[j++] | (subchannel[i] & 0xC0) >> 4);
cdTextPack4[j] = (byte)(cdTextPack4[j] | ((subchannel[i++] & 0x0F) << 4));
cdTextPack4[j] = (byte)(cdTextPack4[j] | (subchannel[i++] & 0x0F) << 4);
if(j < 17)
cdTextPack4[j] = (byte)(cdTextPack4[j++] | ((subchannel[i] & 0x3C) >> 2));
cdTextPack4[j] = (byte)(cdTextPack4[j++] | (subchannel[i] & 0x3C) >> 2);
cdTextPack4[j] = (byte)(cdTextPack4[j] | ((subchannel[i++] & 0x03) << 6));
cdTextPack4[j] = (byte)(cdTextPack4[j] | (subchannel[i++] & 0x03) << 6);
cdTextPack4[j] = (byte)(cdTextPack4[j] | (subchannel[i++] & 0x3F));
cdTextPack4[j] = (byte)(cdTextPack4[j] | subchannel[i++] & 0x3F);
}
i = 0;
for(int j = 0; j < 24; j++)
for(var j = 0; j < 24; j++)
cdSubRwPack1[j] = (byte)(subchannel[i++] & 0x3F);
for(int j = 0; j < 24; j++)
for(var j = 0; j < 24; j++)
cdSubRwPack2[j] = (byte)(subchannel[i++] & 0x3F);
for(int j = 0; j < 24; j++)
for(var j = 0; j < 24; j++)
cdSubRwPack3[j] = (byte)(subchannel[i++] & 0x3F);
for(int j = 0; j < 24; j++)
for(var j = 0; j < 24; j++)
cdSubRwPack4[j] = (byte)(subchannel[i++] & 0x3F);
switch(cdSubRwPack1[0])
@@ -545,8 +561,8 @@ public static class CdChecksums
break;
}
ushort qSubChannelCrc = BigEndianBitConverter.ToUInt16(qSubChannel, 10);
byte[] qSubChannelForCrc = new byte[10];
var qSubChannelCrc = BigEndianBitConverter.ToUInt16(qSubChannel, 10);
var qSubChannelForCrc = new byte[10];
Array.Copy(qSubChannel, 0, qSubChannelForCrc, 0, 10);
ushort calculatedQcrc = CRC16CCITTContext.Calculate(qSubChannelForCrc);
@@ -560,8 +576,8 @@ public static class CdChecksums
if((cdTextPack1[0] & 0x80) == 0x80)
{
ushort cdTextPack1Crc = BigEndianBitConverter.ToUInt16(cdTextPack1, 16);
byte[] cdTextPack1ForCrc = new byte[16];
var cdTextPack1Crc = BigEndianBitConverter.ToUInt16(cdTextPack1, 16);
var cdTextPack1ForCrc = new byte[16];
Array.Copy(cdTextPack1, 0, cdTextPack1ForCrc, 0, 16);
ushort calculatedCdtp1Crc = CRC16CCITTContext.Calculate(cdTextPack1ForCrc);
@@ -577,8 +593,8 @@ public static class CdChecksums
if((cdTextPack2[0] & 0x80) == 0x80)
{
ushort cdTextPack2Crc = BigEndianBitConverter.ToUInt16(cdTextPack2, 16);
byte[] cdTextPack2ForCrc = new byte[16];
var cdTextPack2Crc = BigEndianBitConverter.ToUInt16(cdTextPack2, 16);
var cdTextPack2ForCrc = new byte[16];
Array.Copy(cdTextPack2, 0, cdTextPack2ForCrc, 0, 16);
ushort calculatedCdtp2Crc = CRC16CCITTContext.Calculate(cdTextPack2ForCrc);
@@ -597,8 +613,8 @@ public static class CdChecksums
if((cdTextPack3[0] & 0x80) == 0x80)
{
ushort cdTextPack3Crc = BigEndianBitConverter.ToUInt16(cdTextPack3, 16);
byte[] cdTextPack3ForCrc = new byte[16];
var cdTextPack3Crc = BigEndianBitConverter.ToUInt16(cdTextPack3, 16);
var cdTextPack3ForCrc = new byte[16];
Array.Copy(cdTextPack3, 0, cdTextPack3ForCrc, 0, 16);
ushort calculatedCdtp3Crc = CRC16CCITTContext.Calculate(cdTextPack3ForCrc);
@@ -618,8 +634,8 @@ public static class CdChecksums
if((cdTextPack4[0] & 0x80) != 0x80)
return status;
ushort cdTextPack4Crc = BigEndianBitConverter.ToUInt16(cdTextPack4, 16);
byte[] cdTextPack4ForCrc = new byte[16];
var cdTextPack4Crc = BigEndianBitConverter.ToUInt16(cdTextPack4, 16);
var cdTextPack4ForCrc = new byte[16];
Array.Copy(cdTextPack4, 0, cdTextPack4ForCrc, 0, 16);
ushort calculatedCdtp4Crc = CRC16CCITTContext.Calculate(cdTextPack4ForCrc);