diff --git a/CDChecksums.cs b/CDChecksums.cs index 0a5c118..6f959a9 100644 --- a/CDChecksums.cs +++ b/CDChecksums.cs @@ -124,73 +124,73 @@ namespace DiscImageChef.Checksums { EccInit(); - if(channel[0x000] == 0x00 && // sync (12 bytes) - channel[0x001] == 0xFF && channel[0x002] == 0xFF && channel[0x003] == 0xFF && channel[0x004] == 0xFF && - channel[0x005] == 0xFF && channel[0x006] == 0xFF && channel[0x007] == 0xFF && channel[0x008] == 0xFF && - channel[0x009] == 0xFF && channel[0x00A] == 0xFF && channel[0x00B] == 0x00) + if(channel[0x000] != 0x00 || channel[0x001] != 0xFF || channel[0x002] != 0xFF || channel[0x003] != 0xFF || + channel[0x004] != 0xFF || channel[0x005] != 0xFF || channel[0x006] != 0xFF || channel[0x007] != 0xFF || + channel[0x008] != 0xFF || channel[0x009] != 0xFF || channel[0x00A] != 0xFF || + channel[0x00B] != 0x00) return null; + + DicConsole.DebugWriteLine("CD checksums", "Data sector, address {0:X2}:{1:X2}:{2:X2}", channel[0x00C], + channel[0x00D], channel[0x00E]); + + if(channel[0x00F] == 0x00) // mode (1 byte) { - DicConsole.DebugWriteLine("CD checksums", "Data sector, address {0:X2}:{1:X2}:{2:X2}", channel[0x00C], - channel[0x00D], channel[0x00E]); - - if(channel[0x00F] == 0x00) // mode (1 byte) - { - DicConsole.DebugWriteLine("CD checksums", "Mode 0 sector at address {0:X2}:{1:X2}:{2:X2}", - channel[0x00C], channel[0x00D], channel[0x00E]); - for(int i = 0x010; i < 0x930; i++) - if(channel[i] != 0x00) - { - DicConsole.DebugWriteLine("CD checksums", - "Mode 0 sector with error at address: {0:X2}:{1:X2}:{2:X2}", - channel[0x00C], channel[0x00D], channel[0x00E]); - return false; - } - - return true; - } - - if(channel[0x00F] == 0x01) // mode (1 byte) - { - DicConsole.DebugWriteLine("CD checksums", "Mode 1 sector at address {0:X2}:{1:X2}:{2:X2}", - channel[0x00C], channel[0x00D], channel[0x00E]); - - if(channel[0x814] != 0x00 || // reserved (8 bytes) - channel[0x815] != 0x00 || channel[0x816] != 0x00 || channel[0x817] != 0x00 || - channel[0x818] != 0x00 || channel[0x819] != 0x00 || channel[0x81A] != 0x00 || - channel[0x81B] != 0x00) + DicConsole.DebugWriteLine("CD checksums", "Mode 0 sector at address {0:X2}:{1:X2}:{2:X2}", + channel[0x00C], channel[0x00D], channel[0x00E]); + for(int i = 0x010; i < 0x930; i++) + if(channel[i] != 0x00) { DicConsole.DebugWriteLine("CD checksums", - "Mode 1 sector with data in reserved bytes at address: {0:X2}:{1:X2}:{2:X2}", + "Mode 0 sector with error at address: {0:X2}:{1:X2}:{2:X2}", channel[0x00C], channel[0x00D], channel[0x00E]); return false; } - 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]; + return true; + } - Array.Copy(channel, 0x0C, address, 0, 4); - Array.Copy(channel, 0x0C, data, 0, 2060); - Array.Copy(channel, 0x0C, data2, 0, 2232); - Array.Copy(channel, 0x81C, eccP, 0, 172); - Array.Copy(channel, 0x8C8, eccQ, 0, 104); + if(channel[0x00F] == 0x01) // mode (1 byte) + { + DicConsole.DebugWriteLine("CD checksums", "Mode 1 sector at address {0:X2}:{1:X2}:{2:X2}", + channel[0x00C], channel[0x00D], channel[0x00E]); - bool failedEccP = CheckEcc(address, data, 86, 24, 2, 86, eccP); - bool failedEccQ = CheckEcc(address, data2, 52, 43, 86, 88, eccQ); + if(channel[0x814] != 0x00 || // reserved (8 bytes) + channel[0x815] != 0x00 || channel[0x816] != 0x00 || channel[0x817] != 0x00 || + channel[0x818] != 0x00 || channel[0x819] != 0x00 || channel[0x81A] != 0x00 || + channel[0x81B] != 0x00) + { + DicConsole.DebugWriteLine("CD checksums", + "Mode 1 sector with data in reserved bytes at address: {0:X2}:{1:X2}:{2:X2}", + channel[0x00C], channel[0x00D], channel[0x00E]); + return false; + } - if(failedEccP) - DicConsole.DebugWriteLine("CD checksums", - "Mode 1 sector at address: {0:X2}:{1:X2}:{2:X2}, fails ECC P check", - channel[0x00C], channel[0x00D], channel[0x00E]); - if(failedEccQ) - DicConsole.DebugWriteLine("CD checksums", - "Mode 1 sector at address: {0:X2}:{1:X2}:{2:X2}, fails ECC Q check", - channel[0x00C], channel[0x00D], channel[0x00E]); + 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]; - if(failedEccP || failedEccQ) return false; + Array.Copy(channel, 0x0C, address, 0, 4); + Array.Copy(channel, 0x0C, data, 0, 2060); + Array.Copy(channel, 0x0C, data2, 0, 2232); + Array.Copy(channel, 0x81C, eccP, 0, 172); + Array.Copy(channel, 0x8C8, eccQ, 0, 104); - /* TODO: This is not working + bool failedEccP = CheckEcc(address, data, 86, 24, 2, 86, eccP); + bool failedEccQ = CheckEcc(address, data2, 52, 43, 86, 88, eccQ); + + if(failedEccP) + DicConsole.DebugWriteLine("CD checksums", + "Mode 1 sector at address: {0:X2}:{1:X2}:{2:X2}, fails ECC P check", + channel[0x00C], channel[0x00D], channel[0x00E]); + if(failedEccQ) + DicConsole.DebugWriteLine("CD checksums", + "Mode 1 sector at address: {0:X2}:{1:X2}:{2:X2}, fails ECC Q check", + channel[0x00C], channel[0x00D], channel[0x00E]); + + if(failedEccP || failedEccQ) return false; + + /* TODO: This is not working byte[] SectorForCheck = new byte[0x810]; uint StoredEDC = BitConverter.ToUInt32(channel, 0x810); byte[] CalculatedEDCBytes; @@ -204,23 +204,23 @@ namespace DiscImageChef.Checksums return false; }*/ - return true; - } + return true; + } - if(channel[0x00F] == 0x02) // mode (1 byte) + if(channel[0x00F] == 0x02) // mode (1 byte) + { + DicConsole.DebugWriteLine("CD checksums", "Mode 2 sector at address {0:X2}:{1:X2}:{2:X2}", + channel[0x00C], channel[0x00D], channel[0x00E]); + + if((channel[0x012] & 0x20) == 0x20) // mode 2 form 2 { - DicConsole.DebugWriteLine("CD checksums", "Mode 2 sector at address {0:X2}:{1:X2}:{2:X2}", - channel[0x00C], channel[0x00D], channel[0x00E]); + if(channel[0x010] != channel[0x014] || channel[0x011] != channel[0x015] || + channel[0x012] != channel[0x016] || channel[0x013] != channel[0x017]) + DicConsole.DebugWriteLine("CD checksums", + "Subheader copies differ in mode 2 form 2 sector at address: {0:X2}:{1:X2}:{2:X2}", + channel[0x00C], channel[0x00D], channel[0x00E]); - if((channel[0x012] & 0x20) == 0x20) // mode 2 form 2 - { - if(channel[0x010] != channel[0x014] || channel[0x011] != channel[0x015] || - channel[0x012] != channel[0x016] || channel[0x013] != channel[0x017]) - DicConsole.DebugWriteLine("CD checksums", - "Subheader copies differ in mode 2 form 2 sector at address: {0:X2}:{1:X2}:{2:X2}", - channel[0x00C], channel[0x00D], channel[0x00E]); - - /* TODO: This is not working + /* TODO: This is not working byte[] SectorForCheck = new byte[0x91C]; uint StoredEDC = BitConverter.ToUInt32(channel, 0x92C); byte[] CalculatedEDCBytes; @@ -233,45 +233,45 @@ namespace DiscImageChef.Checksums DicConsole.DebugWriteLine("CD checksums", "Mode 2 form 2 sector at address: {0:X2}:{1:X2}:{2:X2}, got CRC 0x{3:X8} expected 0x{4:X8}", channel[0x00C], channel[0x00D], channel[0x00E], CalculatedEDC, StoredEDC); return false; }*/ - } - else - { - if(channel[0x010] != channel[0x014] || channel[0x011] != channel[0x015] || - channel[0x012] != channel[0x016] || channel[0x013] != channel[0x017]) - DicConsole.DebugWriteLine("CD checksums", - "Subheader copies differ in mode 2 form 1 sector at address: {0:X2}:{1:X2}:{2:X2}", - channel[0x00C], channel[0x00D], channel[0x00E]); + } + else + { + if(channel[0x010] != channel[0x014] || channel[0x011] != channel[0x015] || + channel[0x012] != channel[0x016] || channel[0x013] != channel[0x017]) + DicConsole.DebugWriteLine("CD checksums", + "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[] data = new byte[2060]; - byte[] data2 = new byte[2232]; - byte[] eccP = new byte[172]; - byte[] eccQ = new byte[104]; + 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]; - address[0] = 0; - address[1] = 0; - address[2] = 0; - address[3] = 0; - Array.Copy(channel, 0x0C, data, 0, 2060); - Array.Copy(channel, 0x0C, data2, 0, 2232); - Array.Copy(channel, 0x80C, eccP, 0, 172); - Array.Copy(channel, 0x8B8, eccQ, 0, 104); + address[0] = 0; + address[1] = 0; + address[2] = 0; + address[3] = 0; + Array.Copy(channel, 0x0C, data, 0, 2060); + Array.Copy(channel, 0x0C, data2, 0, 2232); + Array.Copy(channel, 0x80C, eccP, 0, 172); + Array.Copy(channel, 0x8B8, eccQ, 0, 104); - bool failedEccP = CheckEcc(address, data, 86, 24, 2, 86, eccP); - bool failedEccQ = CheckEcc(address, data2, 52, 43, 86, 88, eccQ); + bool failedEccP = CheckEcc(address, data, 86, 24, 2, 86, eccP); + bool failedEccQ = CheckEcc(address, data2, 52, 43, 86, 88, eccQ); - if(failedEccP) - DicConsole.DebugWriteLine("CD checksums", - "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) - DicConsole.DebugWriteLine("CD checksums", - "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]); + if(failedEccP) + DicConsole.DebugWriteLine("CD checksums", + "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) + DicConsole.DebugWriteLine("CD checksums", + "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]); - if(failedEccP || failedEccQ) return false; + if(failedEccP || failedEccQ) return false; - /* TODO: This is not working + /* TODO: This is not working byte[] SectorForCheck = new byte[0x808]; uint StoredEDC = BitConverter.ToUInt32(channel, 0x818); byte[] CalculatedEDCBytes; @@ -284,17 +284,14 @@ namespace DiscImageChef.Checksums DicConsole.DebugWriteLine("CD checksums", "Mode 2 form 1 sector at address: {0:X2}:{1:X2}:{2:X2}, got CRC 0x{3:X8} expected 0x{4:X8}", channel[0x00C], channel[0x00D], channel[0x00E], CalculatedEDC, StoredEDC); return false; }*/ - } - - return true; } - DicConsole.DebugWriteLine("CD checksums", - "Unknown mode {0} sector at address: {1:X2}:{2:X2}:{3:X2}", - channel[0x00F], channel[0x00C], channel[0x00D], channel[0x00E]); - return null; + return true; } + DicConsole.DebugWriteLine("CD checksums", + "Unknown mode {0} sector at address: {1:X2}:{2:X2}:{3:X2}", + channel[0x00F], channel[0x00C], channel[0x00D], channel[0x00E]); return null; } @@ -478,22 +475,20 @@ namespace DiscImageChef.Checksums } } - if((cdTextPack4[0] & 0x80) == 0x80) - { - ushort cdTextPack4Crc = BigEndianBitConverter.ToUInt16(cdTextPack4, 16); - byte[] cdTextPack4ForCrc = new byte[16]; - Array.Copy(cdTextPack4, 0, cdTextPack4ForCrc, 0, 16); - ushort calculatedCdtp4Crc = CalculateCCITT_CRC16(cdTextPack4ForCrc); - DicConsole.DebugWriteLine("CD checksums", "Cyclic CDTP4 0x{0:X4}, Calc CDTP4 0x{1:X4}", cdTextPack4Crc, - calculatedCdtp4Crc); + if((cdTextPack4[0] & 0x80) != 0x80) return status; - if(cdTextPack4Crc != calculatedCdtp4Crc && cdTextPack4Crc != 0) - { - DicConsole.DebugWriteLine("CD checksums", "CD-Text Pack 4 CRC 0x{0:X4}, expected 0x{1:X4}", - cdTextPack4Crc, calculatedCdtp4Crc); - status = false; - } - } + ushort cdTextPack4Crc = BigEndianBitConverter.ToUInt16(cdTextPack4, 16); + byte[] cdTextPack4ForCrc = new byte[16]; + Array.Copy(cdTextPack4, 0, cdTextPack4ForCrc, 0, 16); + ushort calculatedCdtp4Crc = CalculateCCITT_CRC16(cdTextPack4ForCrc); + DicConsole.DebugWriteLine("CD checksums", "Cyclic CDTP4 0x{0:X4}, Calc CDTP4 0x{1:X4}", cdTextPack4Crc, + calculatedCdtp4Crc); + + if(cdTextPack4Crc == calculatedCdtp4Crc || cdTextPack4Crc == 0) return status; + + DicConsole.DebugWriteLine("CD checksums", "CD-Text Pack 4 CRC 0x{0:X4}, expected 0x{1:X4}", + cdTextPack4Crc, calculatedCdtp4Crc); + status = false; return status; } diff --git a/ReedSolomon.cs b/ReedSolomon.cs index 8eeda85..5dbdcd3 100644 --- a/ReedSolomon.cs +++ b/ReedSolomon.cs @@ -305,41 +305,38 @@ namespace DiscImageChef.Checksums /// Outs parity symbols. public int encode_rs(int[] data, out int[] bb) { - if(initialized) + if(!initialized) throw new UnauthorizedAccessException("Trying to calculate RS without initializing!"); + + int i, j; + int feedback; + bb = new int[nn - kk]; + + Clear(ref bb, nn - kk); + for(i = kk - 1; i >= 0; i--) { - int i, j; - int feedback; - bb = new int[nn - kk]; + if(mm != 8) if(data[i] > nn) return -1; /* Illegal symbol */ - Clear(ref bb, nn - kk); - for(i = kk - 1; i >= 0; i--) + feedback = index_of[data[i] ^ bb[nn - kk - 1]]; + if(feedback != a0) { - if(mm != 8) if(data[i] > nn) return -1; /* Illegal symbol */ + /* feedback term is non-zero */ + for(j = nn - kk - 1; j > 0; j--) + if(gg[j] != a0) bb[j] = bb[j - 1] ^ alpha_to[Modnn(gg[j] + feedback)]; + else bb[j] = bb[j - 1]; - feedback = index_of[data[i] ^ bb[nn - kk - 1]]; - if(feedback != a0) - { - /* feedback term is non-zero */ - for(j = nn - kk - 1; j > 0; j--) - if(gg[j] != a0) bb[j] = bb[j - 1] ^ alpha_to[Modnn(gg[j] + feedback)]; - else bb[j] = bb[j - 1]; - - bb[0] = alpha_to[Modnn(gg[0] + feedback)]; - } - else - { - /* feedback term is zero. encoder becomes a - * single-byte shifter */ - for(j = nn - kk - 1; j > 0; j--) bb[j] = bb[j - 1]; - - bb[0] = 0; - } + bb[0] = alpha_to[Modnn(gg[0] + feedback)]; } + else + { + /* feedback term is zero. encoder becomes a + * single-byte shifter */ + for(j = nn - kk - 1; j > 0; j--) bb[j] = bb[j - 1]; - return 0; + bb[0] = 0; + } } - throw new UnauthorizedAccessException("Trying to calculate RS without initializing!"); + return 0; } /* @@ -364,243 +361,236 @@ namespace DiscImageChef.Checksums /// Number of erasures. public int eras_dec_rs(ref int[] data, out int[] erasPos, int noEras) { - if(initialized) + if(!initialized) throw new UnauthorizedAccessException("Trying to calculate RS without initializing!"); + + erasPos = new int[nn - kk]; + int degLambda, el, degOmega; + int i, j, r; + int u, q, tmp, num1, num2, den, discrR; + int[] recd = new int[nn]; + int[] lambda = new int[nn - kk + 1]; /* Err+Eras Locator poly */ + int[] s = new int[nn - kk + 1]; /* syndrome poly */ + int[] b = new int[nn - kk + 1]; + int[] t = new int[nn - kk + 1]; + int[] omega = new int[nn - kk + 1]; + int[] root = new int[nn - kk]; + int[] reg = new int[nn - kk + 1]; + int[] loc = new int[nn - kk]; + int synError, count; + + /* data[] is in polynomial form, copy and convert to index form */ + for(i = nn - 1; i >= 0; i--) { - erasPos = new int[nn - kk]; - int degLambda, el, degOmega; - int i, j, r; - int u, q, tmp, num1, num2, den, discrR; - int[] recd = new int[nn]; - int[] lambda = new int[nn - kk + 1]; /* Err+Eras Locator poly */ - int[] s = new int[nn - kk + 1]; /* syndrome poly */ - int[] b = new int[nn - kk + 1]; - int[] t = new int[nn - kk + 1]; - int[] omega = new int[nn - kk + 1]; - int[] root = new int[nn - kk]; - int[] reg = new int[nn - kk + 1]; - int[] loc = new int[nn - kk]; - int synError, count; + if(mm != 8) if(data[i] > nn) return -1; /* Illegal symbol */ - /* data[] is in polynomial form, copy and convert to index form */ - for(i = nn - 1; i >= 0; i--) - { - if(mm != 8) if(data[i] > nn) return -1; /* Illegal symbol */ - - recd[i] = index_of[data[i]]; - } - /* first form the syndromes; i.e., evaluate recd(x) at roots of g(x) + recd[i] = index_of[data[i]]; + } + /* first form the syndromes; i.e., evaluate recd(x) at roots of g(x) * namely @**(B0+i), i = 0, ... ,(NN-KK-1) */ - synError = 0; - for(i = 1; i <= nn - kk; i++) - { - tmp = 0; - for(j = 0; j < nn; j++) - if(recd[j] != a0) /* recd[j] in index form */ - tmp ^= alpha_to[Modnn(recd[j] + (B0 + i - 1) * j)]; + synError = 0; + for(i = 1; i <= nn - kk; i++) + { + tmp = 0; + for(j = 0; j < nn; j++) + if(recd[j] != a0) /* recd[j] in index form */ + tmp ^= alpha_to[Modnn(recd[j] + (B0 + i - 1) * j)]; - synError |= tmp; /* set flag if non-zero syndrome => + synError |= tmp; /* set flag if non-zero syndrome => * error */ - /* store syndrome in index form */ - s[i] = index_of[tmp]; - } + /* store syndrome in index form */ + s[i] = index_of[tmp]; + } - if(synError == 0) return 0; + if(synError == 0) return 0; - Clear(ref lambda, nn - kk); - lambda[0] = 1; - if(noEras > 0) + Clear(ref lambda, nn - kk); + lambda[0] = 1; + if(noEras > 0) + { + /* Init lambda to be the erasure locator polynomial */ + lambda[1] = alpha_to[erasPos[0]]; + for(i = 1; i < noEras; i++) { - /* Init lambda to be the erasure locator polynomial */ - lambda[1] = alpha_to[erasPos[0]]; - for(i = 1; i < noEras; i++) + u = erasPos[i]; + for(j = i + 1; j > 0; j--) { - u = erasPos[i]; - for(j = i + 1; j > 0; j--) - { - tmp = index_of[lambda[j - 1]]; - if(tmp != a0) lambda[j] ^= alpha_to[Modnn(u + tmp)]; - } + tmp = index_of[lambda[j - 1]]; + if(tmp != a0) lambda[j] ^= alpha_to[Modnn(u + tmp)]; } + } #if DEBUG - /* find roots of the erasure location polynomial */ - for(i = 1; i <= noEras; i++) reg[i] = index_of[lambda[i]]; + /* find roots of the erasure location polynomial */ + for(i = 1; i <= noEras; i++) reg[i] = index_of[lambda[i]]; - count = 0; - for(i = 1; i <= nn; i++) - { - q = 1; - for(j = 1; j <= noEras; j++) - if(reg[j] != a0) - { - reg[j] = Modnn(reg[j] + j); - q ^= alpha_to[reg[j]]; - } - - if(q == 0) - { - /* store root and error location - * number indices - */ - root[count] = i; - loc[count] = nn - i; - count++; - } - } - - if(count != noEras) - { - DicConsole.DebugWriteLine("Reed Solomon", "\n lambda(x) is WRONG\n"); - return -1; - } - - DicConsole.DebugWriteLine("Reed Solomon", - "\n Erasure positions as determined by roots of Eras Loc Poly:\n"); - for(i = 0; i < count; i++) DicConsole.DebugWriteLine("Reed Solomon", "{0} ", loc[i]); - - DicConsole.DebugWriteLine("Reed Solomon", "\n"); -#endif - } - - for(i = 0; i < nn - kk + 1; i++) b[i] = index_of[lambda[i]]; - - /* - * Begin Berlekamp-Massey algorithm to determine error+erasure - * locator polynomial - */ - r = noEras; - el = noEras; - while(++r <= nn - kk) - { - /* r is the step number */ - /* Compute discrepancy at the r-th step in poly-form */ - discrR = 0; - for(i = 0; i < r; i++) if(lambda[i] != 0 && s[r - i] != a0) discrR ^= alpha_to[Modnn(index_of[lambda[i]] + s[r - i])]; - - discrR = index_of[discrR]; /* Index form */ - if(discrR == a0) - { - /* 2 lines below: B(x) <-- x*B(x) */ - Copydown(ref b, ref b, nn - kk); - b[0] = a0; - } - else - { - /* 7 lines below: T(x) <-- lambda(x) - discr_r*x*b(x) */ - t[0] = lambda[0]; - for(i = 0; i < nn - kk; i++) - if(b[i] != a0) t[i + 1] = lambda[i + 1] ^ alpha_to[Modnn(discrR + b[i])]; - else t[i + 1] = lambda[i + 1]; - - if(2 * el <= r + noEras - 1) - { - el = r + noEras - el; - /* - * 2 lines below: B(x) <-- inv(discr_r) * - * lambda(x) - */ - for(i = 0; i <= nn - kk; i++) - b[i] = lambda[i] == 0 ? a0 : Modnn(index_of[lambda[i]] - discrR + nn); - } - else - { - /* 2 lines below: B(x) <-- x*B(x) */ - Copydown(ref b, ref b, nn - kk); - b[0] = a0; - } - - Copy(ref lambda, ref t, nn - kk + 1); - } - } - - /* Convert lambda to index form and compute deg(lambda(x)) */ - degLambda = 0; - for(i = 0; i < nn - kk + 1; i++) - { - lambda[i] = index_of[lambda[i]]; - if(lambda[i] != a0) degLambda = i; - } - /* - * Find roots of the error+erasure locator polynomial. By Chien - * Search - */ - int temp = reg[0]; - Copy(ref reg, ref lambda, nn - kk); - reg[0] = temp; - count = 0; /* Number of roots of lambda(x) */ + count = 0; for(i = 1; i <= nn; i++) { q = 1; - for(j = degLambda; j > 0; j--) + for(j = 1; j <= noEras; j++) if(reg[j] != a0) { reg[j] = Modnn(reg[j] + j); q ^= alpha_to[reg[j]]; } - if(q == 0) - { - /* store root (index-form) and error location number */ - root[count] = i; - loc[count] = nn - i; - count++; - } + if(q != 0) continue; + /* store root and error location + * number indices + */ + root[count] = i; + loc[count] = nn - i; + count++; } -#if DEBUG - DicConsole.DebugWriteLine("Reed Solomon", "\n Final error positions:\t"); + if(count != noEras) + { + DicConsole.DebugWriteLine("Reed Solomon", "\n lambda(x) is WRONG\n"); + return -1; + } + + DicConsole.DebugWriteLine("Reed Solomon", + "\n Erasure positions as determined by roots of Eras Loc Poly:\n"); for(i = 0; i < count; i++) DicConsole.DebugWriteLine("Reed Solomon", "{0} ", loc[i]); DicConsole.DebugWriteLine("Reed Solomon", "\n"); #endif + } - if(degLambda != count) return -1; - /* + for(i = 0; i < nn - kk + 1; i++) b[i] = index_of[lambda[i]]; + + /* + * Begin Berlekamp-Massey algorithm to determine error+erasure + * locator polynomial + */ + r = noEras; + el = noEras; + while(++r <= nn - kk) + { + /* r is the step number */ + /* Compute discrepancy at the r-th step in poly-form */ + discrR = 0; + for(i = 0; i < r; i++) if(lambda[i] != 0 && s[r - i] != a0) discrR ^= alpha_to[Modnn(index_of[lambda[i]] + s[r - i])]; + + discrR = index_of[discrR]; /* Index form */ + if(discrR == a0) + { + /* 2 lines below: B(x) <-- x*B(x) */ + Copydown(ref b, ref b, nn - kk); + b[0] = a0; + } + else + { + /* 7 lines below: T(x) <-- lambda(x) - discr_r*x*b(x) */ + t[0] = lambda[0]; + for(i = 0; i < nn - kk; i++) + if(b[i] != a0) t[i + 1] = lambda[i + 1] ^ alpha_to[Modnn(discrR + b[i])]; + else t[i + 1] = lambda[i + 1]; + + if(2 * el <= r + noEras - 1) + { + el = r + noEras - el; + /* + * 2 lines below: B(x) <-- inv(discr_r) * + * lambda(x) + */ + for(i = 0; i <= nn - kk; i++) + b[i] = lambda[i] == 0 ? a0 : Modnn(index_of[lambda[i]] - discrR + nn); + } + else + { + /* 2 lines below: B(x) <-- x*B(x) */ + Copydown(ref b, ref b, nn - kk); + b[0] = a0; + } + + Copy(ref lambda, ref t, nn - kk + 1); + } + } + + /* Convert lambda to index form and compute deg(lambda(x)) */ + degLambda = 0; + for(i = 0; i < nn - kk + 1; i++) + { + lambda[i] = index_of[lambda[i]]; + if(lambda[i] != a0) degLambda = i; + } + /* + * Find roots of the error+erasure locator polynomial. By Chien + * Search + */ + int temp = reg[0]; + Copy(ref reg, ref lambda, nn - kk); + reg[0] = temp; + count = 0; /* Number of roots of lambda(x) */ + for(i = 1; i <= nn; i++) + { + q = 1; + for(j = degLambda; j > 0; j--) + if(reg[j] != a0) + { + reg[j] = Modnn(reg[j] + j); + q ^= alpha_to[reg[j]]; + } + + if(q != 0) continue; + /* store root (index-form) and error location number */ + root[count] = i; + loc[count] = nn - i; + count++; + } + +#if DEBUG + DicConsole.DebugWriteLine("Reed Solomon", "\n Final error positions:\t"); + for(i = 0; i < count; i++) DicConsole.DebugWriteLine("Reed Solomon", "{0} ", loc[i]); + + DicConsole.DebugWriteLine("Reed Solomon", "\n"); +#endif + + if(degLambda != count) return -1; + /* * Compute err+eras evaluator poly omega(x) = s(x)*lambda(x) (modulo * x**(NN-KK)). in index form. Also find deg(omega). */ - degOmega = 0; - for(i = 0; i < nn - kk; i++) - { - tmp = 0; - j = degLambda < i ? degLambda : i; - for(; j >= 0; j--) if(s[i + 1 - j] != a0 && lambda[j] != a0) tmp ^= alpha_to[Modnn(s[i + 1 - j] + lambda[j])]; + degOmega = 0; + for(i = 0; i < nn - kk; i++) + { + tmp = 0; + j = degLambda < i ? degLambda : i; + for(; j >= 0; j--) if(s[i + 1 - j] != a0 && lambda[j] != a0) tmp ^= alpha_to[Modnn(s[i + 1 - j] + lambda[j])]; - if(tmp != 0) degOmega = i; - omega[i] = index_of[tmp]; - } + if(tmp != 0) degOmega = i; + 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 = * inv(X(l))**(B0-1) and den = lambda_pr(inv(X(l))) all in poly-form */ - for(j = count - 1; j >= 0; j--) + for(j = count - 1; j >= 0; j--) + { + num1 = 0; + for(i = degOmega; i >= 0; i--) if(omega[i] != a0) num1 ^= alpha_to[Modnn(omega[i] + i * root[j])]; + + num2 = alpha_to[Modnn(root[j] * (B0 - 1) + nn)]; + den = 0; + + /* lambda[i+1] for i even is the formal derivative lambda_pr of lambda[i] */ + for(i = Min(degLambda, nn - kk - 1) & ~1; i >= 0; i -= 2) if(lambda[i + 1] != a0) den ^= alpha_to[Modnn(lambda[i + 1] + i * root[j])]; + + if(den == 0) { - num1 = 0; - for(i = degOmega; i >= 0; i--) if(omega[i] != a0) num1 ^= alpha_to[Modnn(omega[i] + i * root[j])]; - - num2 = alpha_to[Modnn(root[j] * (B0 - 1) + nn)]; - den = 0; - - /* lambda[i+1] for i even is the formal derivative lambda_pr of lambda[i] */ - for(i = Min(degLambda, nn - kk - 1) & ~1; i >= 0; i -= 2) if(lambda[i + 1] != a0) den ^= alpha_to[Modnn(lambda[i + 1] + i * root[j])]; - - if(den == 0) - { - DicConsole.DebugWriteLine("Reed Solomon", "\n ERROR: denominator = 0\n"); - return -1; - } - /* Apply error to data */ - if(num1 != 0) data[loc[j]] ^= alpha_to[Modnn(index_of[num1] + index_of[num2] + nn - index_of[den])]; + DicConsole.DebugWriteLine("Reed Solomon", "\n ERROR: denominator = 0\n"); + return -1; } - - return count; + /* Apply error to data */ + if(num1 != 0) data[loc[j]] ^= alpha_to[Modnn(index_of[num1] + index_of[num2] + nn - index_of[den])]; } - throw new UnauthorizedAccessException("Trying to calculate RS without initializing!"); + return count; } } } \ No newline at end of file diff --git a/SpamSumContext.cs b/SpamSumContext.cs index 8b18a6f..6e815e5 100644 --- a/SpamSumContext.cs +++ b/SpamSumContext.cs @@ -246,11 +246,10 @@ namespace DiscImageChef.Checksums * */ self.Bh[i].Digest[++self.Bh[i].Dlen] = 0; self.Bh[i].H = HASH_INIT; - if(self.Bh[i].Dlen < SPAMSUM_LENGTH / 2) - { - self.Bh[i].Halfh = HASH_INIT; - self.Bh[i].Halfdigest = 0; - } + if(self.Bh[i].Dlen >= SPAMSUM_LENGTH / 2) continue; + + self.Bh[i].Halfh = HASH_INIT; + self.Bh[i].Halfdigest = 0; } else fuzzy_try_reduce_blockhash(); }