Reformatted.

This commit is contained in:
2016-04-19 02:11:47 +01:00
parent 6a8499f8ed
commit f8bc81d4f5
149 changed files with 6983 additions and 6970 deletions

View File

@@ -97,52 +97,52 @@ namespace DiscImageChef.Checksums
/// </summary>
public void InitRS(int n, int k, int m)
{
switch (m)
switch(m)
{
case 2:
Pp = new []{ 1, 1, 1 };
Pp = new[] { 1, 1, 1 };
break;
case 3:
Pp = new []{ 1, 1, 0, 1 };
Pp = new[] { 1, 1, 0, 1 };
break;
case 4:
Pp = new []{ 1, 1, 0, 0, 1 };
Pp = new[] { 1, 1, 0, 0, 1 };
break;
case 5:
Pp = new []{ 1, 0, 1, 0, 0, 1 };
Pp = new[] { 1, 0, 1, 0, 0, 1 };
break;
case 6:
Pp = new []{ 1, 1, 0, 0, 0, 0, 1 };
Pp = new[] { 1, 1, 0, 0, 0, 0, 1 };
break;
case 7:
Pp = new []{ 1, 0, 0, 1, 0, 0, 0, 1 };
Pp = new[] { 1, 0, 0, 1, 0, 0, 0, 1 };
break;
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;
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;
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;
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;
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;
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;
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;
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;
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;
default:
throw new ArgumentOutOfRangeException("m", "m must be between 2 and 16 inclusive");
@@ -166,7 +166,7 @@ namespace DiscImageChef.Checksums
int modnn(int x)
{
while (x >= NN)
while(x >= NN)
{
x -= NN;
x = (x >> MM) + (x & NN);
@@ -182,21 +182,21 @@ namespace DiscImageChef.Checksums
static void CLEAR(ref int[] a, int n)
{
int ci;
for (ci = (n) - 1; ci >= 0; ci--)
for(ci = (n) - 1; ci >= 0; ci--)
(a)[ci] = 0;
}
static void COPY(ref int[] a, ref int[] b, int n)
{
int ci;
for (ci = (n) - 1; ci >= 0; ci--)
for(ci = (n) - 1; ci >= 0; ci--)
(a)[ci] = (b)[ci];
}
static void COPYDOWN(ref int[] a, ref int[] b, int n)
{
int ci;
for (ci = (n) - 1; ci >= 0; ci--)
for(ci = (n) - 1; ci >= 0; ci--)
(a)[ci] = (b)[ci];
}
@@ -236,12 +236,12 @@ namespace DiscImageChef.Checksums
mask = 1;
Alpha_to[MM] = 0;
for (i = 0; i < MM; i++)
for(i = 0; i < MM; i++)
{
Alpha_to[i] = mask;
Index_of[Alpha_to[i]] = i;
/* If Pp[i] == 1 then, term @^i occurs in poly-repr of @^MM */
if (Pp[i] != 0)
if(Pp[i] != 0)
Alpha_to[MM] ^= mask; /* Bit-wise EXOR operation */
mask <<= 1; /* single left-shift */
}
@@ -252,9 +252,9 @@ namespace DiscImageChef.Checksums
* term that may occur when poly-repr of @^i is shifted.
*/
mask >>= 1;
for (i = MM + 1; i < NN; i++)
for(i = MM + 1; i < NN; i++)
{
if (Alpha_to[i - 1] >= mask)
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;
@@ -283,15 +283,15 @@ namespace DiscImageChef.Checksums
Gg[0] = Alpha_to[B0];
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;
/*
* Below multiply (Gg[0]+Gg[1]*x + ... +Gg[i]x^i) by
* (@**(B0+i-1) + x)
*/
for (j = i - 1; j > 0; j--)
if (Gg[j] != 0)
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)];
else
Gg[j] = Gg[j - 1];
@@ -299,7 +299,7 @@ namespace DiscImageChef.Checksums
Gg[0] = Alpha_to[modnn((Index_of[Gg[0]]) + B0 + i - 1)];
}
/* convert Gg[] to index form for quicker encoding */
for (i = 0; i <= NN - KK; i++)
for(i = 0; i <= NN - KK; i++)
Gg[i] = Index_of[Gg[i]];
}
@@ -319,25 +319,25 @@ namespace DiscImageChef.Checksums
/// <param name="bb">Outs parity symbols.</param>
public int encode_rs(int[] data, out int[] bb)
{
if (initialized)
if(initialized)
{
int i, j;
int feedback;
bb = new int[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(MM != 8)
{
if (data[i] > NN)
if(data[i] > NN)
return -1; /* Illegal symbol */
}
feedback = Index_of[data[i] ^ bb[NN - KK - 1]];
if (feedback != A0)
if(feedback != A0)
{ /* feedback term is non-zero */
for (j = NN - KK - 1; j > 0; j--)
if (Gg[j] != A0)
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];
@@ -346,7 +346,7 @@ namespace DiscImageChef.Checksums
else
{ /* feedback term is zero. encoder becomes a
* single-byte shifter */
for (j = NN - KK - 1; j > 0; j--)
for(j = NN - KK - 1; j > 0; j--)
bb[j] = bb[j - 1];
bb[0] = 0;
}
@@ -378,7 +378,7 @@ namespace DiscImageChef.Checksums
/// <param name="no_eras">Number of erasures.</param>
public int eras_dec_rs(ref int[] data, out int[] eras_pos, int no_eras)
{
if (initialized)
if(initialized)
{
eras_pos = new int[NN - KK];
int deg_lambda, el, deg_omega;
@@ -396,11 +396,11 @@ namespace DiscImageChef.Checksums
int syn_error, count;
/* 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(MM != 8)
{
if (data[i] > NN)
if(data[i] > NN)
return -1; /* Illegal symbol */
}
recd[i] = Index_of[data[i]];
@@ -409,18 +409,18 @@ namespace DiscImageChef.Checksums
* namely @**(B0+i), i = 0, ... ,(NN-KK-1)
*/
syn_error = 0;
for (i = 1; i <= NN - KK; i++)
for(i = 1; i <= NN - KK; i++)
{
tmp = 0;
for (j = 0; j < NN; j++)
if (recd[j] != A0) /* recd[j] in index form */
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)];
syn_error |= tmp; /* set flag if non-zero syndrome =>
* error */
/* store syndrome in index form */
s[i] = Index_of[tmp];
}
if (syn_error == 0)
if(syn_error == 0)
{
/*
* if syndrome is zero, data[] is a codeword and there are no
@@ -430,36 +430,36 @@ namespace DiscImageChef.Checksums
}
CLEAR(ref lambda, NN - KK);
lambda[0] = 1;
if (no_eras > 0)
if(no_eras > 0)
{
/* Init lambda to be the erasure locator polynomial */
lambda[1] = Alpha_to[eras_pos[0]];
for (i = 1; i < no_eras; i++)
for(i = 1; i < no_eras; i++)
{
u = eras_pos[i];
for (j = i + 1; j > 0; j--)
for(j = i + 1; j > 0; j--)
{
tmp = Index_of[lambda[j - 1]];
if (tmp != A0)
if(tmp != A0)
lambda[j] ^= Alpha_to[modnn(u + tmp)];
}
}
#if DEBUG
#if DEBUG
/* find roots of the erasure location polynomial */
for (i = 1; i <= no_eras; i++)
for(i = 1; i <= no_eras; i++)
reg[i] = Index_of[lambda[i]];
count = 0;
for (i = 1; i <= NN; i++)
for(i = 1; i <= NN; i++)
{
q = 1;
for (j = 1; j <= no_eras; j++)
if (reg[j] != A0)
for(j = 1; j <= no_eras; j++)
if(reg[j] != A0)
{
reg[j] = modnn(reg[j] + j);
q ^= Alpha_to[reg[j]];
}
if (q == 0)
if(q == 0)
{
/* store root and error location
* number indices
@@ -469,19 +469,19 @@ namespace DiscImageChef.Checksums
count++;
}
}
if (count != no_eras)
if(count != no_eras)
{
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++)
for(i = 0; i < count; i++)
DicConsole.DebugWriteLine("Reed Solomon", "{0} ", loc[i]);
DicConsole.DebugWriteLine("Reed Solomon", "\n");
#endif
#endif
}
for (i = 0; i < NN - KK + 1; i++)
for(i = 0; i < NN - KK + 1; i++)
b[i] = Index_of[lambda[i]];
/*
@@ -490,19 +490,19 @@ namespace DiscImageChef.Checksums
*/
r = no_eras;
el = no_eras;
while (++r <= NN - KK)
while(++r <= NN - KK)
{ /* r is the step number */
/* Compute discrepancy at the r-th step in poly-form */
discr_r = 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])];
}
}
discr_r = Index_of[discr_r]; /* Index form */
if (discr_r == A0)
if(discr_r == A0)
{
/* 2 lines below: B(x) <-- x*B(x) */
COPYDOWN(ref b, ref b, NN - KK);
@@ -512,21 +512,21 @@ namespace DiscImageChef.Checksums
{
/* 7 lines below: T(x) <-- lambda(x) - discr_r*x*b(x) */
t[0] = lambda[0];
for (i = 0; i < NN - KK; i++)
for(i = 0; i < NN - KK; i++)
{
if (b[i] != A0)
if(b[i] != A0)
t[i + 1] = lambda[i + 1] ^ Alpha_to[modnn(discr_r + b[i])];
else
t[i + 1] = lambda[i + 1];
}
if (2 * el <= r + no_eras - 1)
if(2 * el <= r + no_eras - 1)
{
el = r + no_eras - el;
/*
* 2 lines below: B(x) <-- inv(discr_r) *
* 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);
}
else
@@ -541,10 +541,10 @@ namespace DiscImageChef.Checksums
/* Convert lambda to index form and compute deg(lambda(x)) */
deg_lambda = 0;
for (i = 0; i < NN - KK + 1; i++)
for(i = 0; i < NN - KK + 1; i++)
{
lambda[i] = Index_of[lambda[i]];
if (lambda[i] != A0)
if(lambda[i] != A0)
deg_lambda = i;
}
/*
@@ -555,16 +555,16 @@ namespace DiscImageChef.Checksums
COPY(ref reg, ref lambda, NN - KK);
reg[0] = temp;
count = 0; /* Number of roots of lambda(x) */
for (i = 1; i <= NN; i++)
for(i = 1; i <= NN; i++)
{
q = 1;
for (j = deg_lambda; j > 0; j--)
if (reg[j] != A0)
for(j = deg_lambda; j > 0; j--)
if(reg[j] != A0)
{
reg[j] = modnn(reg[j] + j);
q ^= Alpha_to[reg[j]];
}
if (q == 0)
if(q == 0)
{
/* store root (index-form) and error location number */
root[count] = i;
@@ -573,14 +573,14 @@ namespace DiscImageChef.Checksums
}
}
#if DEBUG
#if DEBUG
DicConsole.DebugWriteLine("Reed Solomon", "\n Final error positions:\t");
for (i = 0; i < count; i++)
for(i = 0; i < count; i++)
DicConsole.DebugWriteLine("Reed Solomon", "{0} ", loc[i]);
DicConsole.DebugWriteLine("Reed Solomon", "\n");
#endif
#endif
if (deg_lambda != count)
if(deg_lambda != count)
{
/*
* deg(lambda) unequal to number of roots => uncorrectable
@@ -593,16 +593,16 @@ namespace DiscImageChef.Checksums
* x**(NN-KK)). in index form. Also find deg(omega).
*/
deg_omega = 0;
for (i = 0; i < NN - KK; i++)
for(i = 0; i < NN - KK; i++)
{
tmp = 0;
j = (deg_lambda < i) ? deg_lambda : i;
for (; j >= 0; j--)
for(; j >= 0; j--)
{
if ((s[i + 1 - j] != A0) && (lambda[j] != A0))
if((s[i + 1 - j] != A0) && (lambda[j] != A0))
tmp ^= Alpha_to[modnn(s[i + 1 - j] + lambda[j])];
}
if (tmp != 0)
if(tmp != 0)
deg_omega = i;
omega[i] = Index_of[tmp];
}
@@ -612,30 +612,30 @@ namespace DiscImageChef.Checksums
* 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 = deg_omega; i >= 0; i--)
for(i = deg_omega; i >= 0; i--)
{
if (omega[i] != A0)
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(deg_lambda, NN - KK - 1) & ~1; i >= 0; i -= 2)
for(i = min(deg_lambda, NN - KK - 1) & ~1; i >= 0; i -= 2)
{
if (lambda[i + 1] != A0)
if(lambda[i + 1] != A0)
den ^= Alpha_to[modnn(lambda[i + 1] + i * root[j])];
}
if (den == 0)
if(den == 0)
{
DicConsole.DebugWriteLine("Reed Solomon", "\n ERROR: denominator = 0\n");
return -1;
}
/* 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])];
}