mirror of
https://github.com/aaru-dps/Aaru.Checksums.git
synced 2025-12-16 19:24:29 +00:00
General code refactor and cleanup.
This commit is contained in:
176
ReedSolomon.cs
176
ReedSolomon.cs
@@ -57,11 +57,11 @@
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Aaru.Checksums;
|
||||
|
||||
using System;
|
||||
using Aaru.Console;
|
||||
|
||||
namespace Aaru.Checksums;
|
||||
|
||||
/// <summary>Implements the Reed-Solomon algorithm</summary>
|
||||
public class ReedSolomon
|
||||
{
|
||||
@@ -87,69 +87,69 @@ public class ReedSolomon
|
||||
public void InitRs(int n, int k, int m)
|
||||
{
|
||||
_pp = m switch
|
||||
{
|
||||
2 => new[]
|
||||
{
|
||||
1, 1, 1
|
||||
},
|
||||
3 => new[]
|
||||
{
|
||||
1, 1, 0, 1
|
||||
},
|
||||
4 => new[]
|
||||
{
|
||||
1, 1, 0, 0, 1
|
||||
},
|
||||
5 => new[]
|
||||
{
|
||||
1, 0, 1, 0, 0, 1
|
||||
},
|
||||
6 => new[]
|
||||
{
|
||||
1, 1, 0, 0, 0, 0, 1
|
||||
},
|
||||
7 => new[]
|
||||
{
|
||||
1, 0, 0, 1, 0, 0, 0, 1
|
||||
},
|
||||
8 => new[]
|
||||
{
|
||||
1, 0, 1, 1, 1, 0, 0, 0, 1
|
||||
},
|
||||
9 => new[]
|
||||
{
|
||||
1, 0, 0, 0, 1, 0, 0, 0, 0, 1
|
||||
},
|
||||
10 => new[]
|
||||
{
|
||||
1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1
|
||||
},
|
||||
11 => new[]
|
||||
{
|
||||
1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1
|
||||
},
|
||||
12 => new[]
|
||||
{
|
||||
1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1
|
||||
},
|
||||
13 => new[]
|
||||
{
|
||||
1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1
|
||||
},
|
||||
14 => new[]
|
||||
{
|
||||
1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1
|
||||
},
|
||||
15 => new[]
|
||||
{
|
||||
1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
|
||||
},
|
||||
16 => new[]
|
||||
{
|
||||
1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1
|
||||
},
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(m), "m must be between 2 and 16 inclusive")
|
||||
};
|
||||
{
|
||||
2 => new[]
|
||||
{
|
||||
1, 1, 1
|
||||
},
|
||||
3 => new[]
|
||||
{
|
||||
1, 1, 0, 1
|
||||
},
|
||||
4 => new[]
|
||||
{
|
||||
1, 1, 0, 0, 1
|
||||
},
|
||||
5 => new[]
|
||||
{
|
||||
1, 0, 1, 0, 0, 1
|
||||
},
|
||||
6 => new[]
|
||||
{
|
||||
1, 1, 0, 0, 0, 0, 1
|
||||
},
|
||||
7 => new[]
|
||||
{
|
||||
1, 0, 0, 1, 0, 0, 0, 1
|
||||
},
|
||||
8 => new[]
|
||||
{
|
||||
1, 0, 1, 1, 1, 0, 0, 0, 1
|
||||
},
|
||||
9 => new[]
|
||||
{
|
||||
1, 0, 0, 0, 1, 0, 0, 0, 0, 1
|
||||
},
|
||||
10 => new[]
|
||||
{
|
||||
1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1
|
||||
},
|
||||
11 => new[]
|
||||
{
|
||||
1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1
|
||||
},
|
||||
12 => new[]
|
||||
{
|
||||
1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1
|
||||
},
|
||||
13 => new[]
|
||||
{
|
||||
1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1
|
||||
},
|
||||
14 => new[]
|
||||
{
|
||||
1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1
|
||||
},
|
||||
15 => new[]
|
||||
{
|
||||
1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
|
||||
},
|
||||
16 => new[]
|
||||
{
|
||||
1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1
|
||||
},
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(m), "m must be between 2 and 16 inclusive")
|
||||
};
|
||||
|
||||
_mm = m;
|
||||
_kk = k;
|
||||
@@ -237,7 +237,7 @@ public class ReedSolomon
|
||||
{
|
||||
int i;
|
||||
|
||||
var mask = 1;
|
||||
int mask = 1;
|
||||
_alphaTo[_mm] = 0;
|
||||
|
||||
for(i = 0; i < _mm; i++)
|
||||
@@ -396,18 +396,18 @@ public class ReedSolomon
|
||||
throw new UnauthorizedAccessException("Trying to calculate RS without initializing!");
|
||||
|
||||
erasPos = new int[_nn - _kk];
|
||||
int i, j;
|
||||
int q, tmp;
|
||||
var recd = new int[_nn];
|
||||
var lambda = new int[_nn - _kk + 1]; /* Err+Eras Locator poly */
|
||||
var s = new int[_nn - _kk + 1]; /* syndrome poly */
|
||||
var b = new int[_nn - _kk + 1];
|
||||
var t = new int[_nn - _kk + 1];
|
||||
var omega = new int[_nn - _kk + 1];
|
||||
var root = new int[_nn - _kk];
|
||||
var reg = new int[_nn - _kk + 1];
|
||||
var loc = new int[_nn - _kk];
|
||||
int count;
|
||||
int i, j;
|
||||
int q, tmp;
|
||||
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 count;
|
||||
|
||||
/* data[] is in polynomial form, copy and convert to index form */
|
||||
for(i = _nn - 1; i >= 0; i--)
|
||||
@@ -422,7 +422,7 @@ public class ReedSolomon
|
||||
/* first form the syndromes; i.e., evaluate recd(x) at roots of g(x)
|
||||
* namely @**(B0+i), i = 0, ... ,(NN-KK-1)
|
||||
*/
|
||||
var synError = 0;
|
||||
int synError = 0;
|
||||
|
||||
for(i = 1; i <= _nn - _kk; i++)
|
||||
{
|
||||
@@ -430,7 +430,7 @@ public class ReedSolomon
|
||||
|
||||
for(j = 0; j < _nn; j++)
|
||||
if(recd[j] != _a0) /* recd[j] in index form */
|
||||
tmp ^= _alphaTo[Modnn(recd[j] + (B0 + i - 1) * j)];
|
||||
tmp ^= _alphaTo[Modnn(recd[j] + ((B0 + i - 1) * j))];
|
||||
|
||||
synError |= tmp; /* set flag if non-zero syndrome =>
|
||||
* error */
|
||||
@@ -523,7 +523,7 @@ public class ReedSolomon
|
||||
{
|
||||
/* r is the step number */
|
||||
/* Compute discrepancy at the r-th step in poly-form */
|
||||
var discrR = 0;
|
||||
int discrR = 0;
|
||||
|
||||
for(i = 0; i < r; i++)
|
||||
if(lambda[i] != 0 &&
|
||||
@@ -572,7 +572,7 @@ public class ReedSolomon
|
||||
}
|
||||
|
||||
/* Convert lambda to index form and compute deg(lambda(x)) */
|
||||
var degLambda = 0;
|
||||
int degLambda = 0;
|
||||
|
||||
for(i = 0; i < _nn - _kk + 1; i++)
|
||||
{
|
||||
@@ -627,7 +627,7 @@ public class ReedSolomon
|
||||
* Compute err+eras evaluator poly omega(x) = s(x)*lambda(x) (modulo
|
||||
* x**(NN-KK)). in index form. Also find deg(omega).
|
||||
*/
|
||||
var degOmega = 0;
|
||||
int degOmega = 0;
|
||||
|
||||
for(i = 0; i < _nn - _kk; i++)
|
||||
{
|
||||
@@ -653,19 +653,19 @@ public class ReedSolomon
|
||||
*/
|
||||
for(j = count - 1; j >= 0; j--)
|
||||
{
|
||||
var num1 = 0;
|
||||
int num1 = 0;
|
||||
|
||||
for(i = degOmega; i >= 0; i--)
|
||||
if(omega[i] != _a0)
|
||||
num1 ^= _alphaTo[Modnn(omega[i] + i * root[j])];
|
||||
num1 ^= _alphaTo[Modnn(omega[i] + (i * root[j]))];
|
||||
|
||||
int num2 = _alphaTo[Modnn(root[j] * (B0 - 1) + _nn)];
|
||||
var den = 0;
|
||||
int num2 = _alphaTo[Modnn((root[j] * (B0 - 1)) + _nn)];
|
||||
int den = 0;
|
||||
|
||||
/* lambda[i+1] for i even is the formal derivative lambda_pr of lambda[i] */
|
||||
for(i = Min(degLambda, _nn - _kk - 1) & ~1; i >= 0; i -= 2)
|
||||
if(lambda[i + 1] != _a0)
|
||||
den ^= _alphaTo[Modnn(lambda[i + 1] + i * root[j])];
|
||||
den ^= _alphaTo[Modnn(lambda[i + 1] + (i * root[j]))];
|
||||
|
||||
if(den == 0)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user