2009-11-10 16:48:42 +01:00
|
|
|
#include "rar.hpp"
|
|
|
|
|
|
|
|
#define Clean(D,S) {for (int I=0;I<(S);I++) (D)[I]=0;}
|
|
|
|
|
2020-07-15 00:40:26 +02:00
|
|
|
void RSCoder::Init(int ParSize)
|
2009-11-10 16:48:42 +01:00
|
|
|
{
|
2020-07-15 00:40:26 +02:00
|
|
|
RSCoder::ParSize=ParSize; // Store the number of recovery volumes.
|
2009-11-10 16:48:42 +01:00
|
|
|
FirstBlockDone=false;
|
|
|
|
gfInit();
|
|
|
|
pnInit();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-15 00:40:26 +02:00
|
|
|
// Initialize logarithms and exponents Galois field tables.
|
2009-11-10 16:48:42 +01:00
|
|
|
void RSCoder::gfInit()
|
|
|
|
{
|
|
|
|
for (int I=0,J=1;I<MAXPAR;I++)
|
|
|
|
{
|
|
|
|
gfLog[J]=I;
|
|
|
|
gfExp[I]=J;
|
2020-07-15 00:40:26 +02:00
|
|
|
J<<=1;
|
|
|
|
if (J > MAXPAR)
|
|
|
|
J^=0x11D; // 0x11D field-generator polynomial (x^8+x^4+x^3+x^2+1).
|
2009-11-10 16:48:42 +01:00
|
|
|
}
|
2020-07-15 00:40:26 +02:00
|
|
|
for (int I=MAXPAR;I<MAXPOL;I++) // Avoid gfExp overflow check.
|
2009-11-10 16:48:42 +01:00
|
|
|
gfExp[I]=gfExp[I-MAXPAR];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-15 00:40:26 +02:00
|
|
|
// Multiplication over Galois field.
|
2009-11-10 16:48:42 +01:00
|
|
|
inline int RSCoder::gfMult(int a,int b)
|
|
|
|
{
|
|
|
|
return(a==0 || b == 0 ? 0:gfExp[gfLog[a]+gfLog[b]]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-15 00:40:26 +02:00
|
|
|
// Create the generator polynomial g(x).
|
|
|
|
// g(x)=(x-a)(x-a^2)(x-a^3)..(x-a^N)
|
2009-11-10 16:48:42 +01:00
|
|
|
void RSCoder::pnInit()
|
|
|
|
{
|
2020-07-15 00:40:26 +02:00
|
|
|
int p2[MAXPAR+1]; // Currently calculated part of g(x).
|
2009-11-10 16:48:42 +01:00
|
|
|
|
|
|
|
Clean(p2,ParSize);
|
2020-07-15 00:40:26 +02:00
|
|
|
p2[0]=1; // Set p2 polynomial to 1.
|
|
|
|
|
2009-11-10 16:48:42 +01:00
|
|
|
for (int I=1;I<=ParSize;I++)
|
|
|
|
{
|
2020-07-15 00:40:26 +02:00
|
|
|
int p1[MAXPAR+1]; // We use p1 as current (x+a^i) expression.
|
2009-11-10 16:48:42 +01:00
|
|
|
Clean(p1,ParSize);
|
|
|
|
p1[0]=gfExp[I];
|
2020-07-15 00:40:26 +02:00
|
|
|
p1[1]=1; // Set p1 polynomial to x+a^i.
|
|
|
|
|
|
|
|
// Multiply the already calucated part of g(x) to next (x+a^i).
|
2009-11-10 16:48:42 +01:00
|
|
|
pnMult(p1,p2,GXPol);
|
2020-07-15 00:40:26 +02:00
|
|
|
|
|
|
|
// p2=g(x).
|
2009-11-10 16:48:42 +01:00
|
|
|
for (int J=0;J<ParSize;J++)
|
|
|
|
p2[J]=GXPol[J];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-15 00:40:26 +02:00
|
|
|
// Multiply polynomial 'p1' to 'p2' and store the result in 'r'.
|
2009-11-10 16:48:42 +01:00
|
|
|
void RSCoder::pnMult(int *p1,int *p2,int *r)
|
|
|
|
{
|
|
|
|
Clean(r,ParSize);
|
|
|
|
for (int I=0;I<ParSize;I++)
|
|
|
|
if (p1[I]!=0)
|
|
|
|
for(int J=0;J<ParSize-I;J++)
|
|
|
|
r[I+J]^=gfMult(p1[I],p2[J]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void RSCoder::Encode(byte *Data,int DataSize,byte *DestData)
|
|
|
|
{
|
2020-07-15 00:40:26 +02:00
|
|
|
int ShiftReg[MAXPAR+1]; // Linear Feedback Shift Register.
|
2009-11-10 16:48:42 +01:00
|
|
|
|
|
|
|
Clean(ShiftReg,ParSize+1);
|
|
|
|
for (int I=0;I<DataSize;I++)
|
|
|
|
{
|
|
|
|
int D=Data[I]^ShiftReg[ParSize-1];
|
2020-07-15 00:40:26 +02:00
|
|
|
|
|
|
|
// Use g(x) to define feedback taps.
|
2009-11-10 16:48:42 +01:00
|
|
|
for (int J=ParSize-1;J>0;J--)
|
|
|
|
ShiftReg[J]=ShiftReg[J-1]^gfMult(GXPol[J],D);
|
|
|
|
ShiftReg[0]=gfMult(GXPol[0],D);
|
|
|
|
}
|
|
|
|
for (int I=0;I<ParSize;I++)
|
|
|
|
DestData[I]=ShiftReg[ParSize-I-1];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool RSCoder::Decode(byte *Data,int DataSize,int *EraLoc,int EraSize)
|
|
|
|
{
|
2020-07-15 00:40:26 +02:00
|
|
|
int SynData[MAXPOL]; // Syndrome data.
|
|
|
|
|
2009-11-10 16:48:42 +01:00
|
|
|
bool AllZeroes=true;
|
|
|
|
for (int I=0;I<ParSize;I++)
|
|
|
|
{
|
2020-07-15 00:40:26 +02:00
|
|
|
int Sum=0;
|
|
|
|
for (int J=0;J<DataSize;J++)
|
|
|
|
Sum=Data[J]^gfMult(gfExp[I+1],Sum);
|
2009-11-10 16:48:42 +01:00
|
|
|
if ((SynData[I]=Sum)!=0)
|
|
|
|
AllZeroes=false;
|
|
|
|
}
|
2020-07-15 00:40:26 +02:00
|
|
|
|
|
|
|
// If all syndrome numbers are zero, message does not have errors.
|
2009-11-10 16:48:42 +01:00
|
|
|
if (AllZeroes)
|
|
|
|
return(true);
|
|
|
|
|
2020-07-15 00:40:26 +02:00
|
|
|
if (!FirstBlockDone) // Do things which we need to do once for all data.
|
2009-11-10 16:48:42 +01:00
|
|
|
{
|
|
|
|
FirstBlockDone=true;
|
2020-07-15 00:40:26 +02:00
|
|
|
|
|
|
|
// Calculate the error locator polynomial.
|
|
|
|
Clean(ELPol,ParSize+1);
|
|
|
|
ELPol[0]=1;
|
|
|
|
|
2009-11-10 16:48:42 +01:00
|
|
|
for (int EraPos=0;EraPos<EraSize;EraPos++)
|
|
|
|
for (int I=ParSize,M=gfExp[DataSize-EraLoc[EraPos]-1];I>0;I--)
|
2020-07-15 00:40:26 +02:00
|
|
|
ELPol[I]^=gfMult(M,ELPol[I-1]);
|
2009-11-10 16:48:42 +01:00
|
|
|
|
|
|
|
ErrCount=0;
|
2020-07-15 00:40:26 +02:00
|
|
|
|
|
|
|
// Find roots of error locator polynomial.
|
2009-11-10 16:48:42 +01:00
|
|
|
for (int Root=MAXPAR-DataSize;Root<MAXPAR+1;Root++)
|
|
|
|
{
|
|
|
|
int Sum=0;
|
|
|
|
for (int B=0;B<ParSize+1;B++)
|
2020-07-15 00:40:26 +02:00
|
|
|
Sum^=gfMult(gfExp[(B*Root)%MAXPAR],ELPol[B]);
|
|
|
|
if (Sum==0) // Root found.
|
2009-11-10 16:48:42 +01:00
|
|
|
{
|
2020-07-15 00:40:26 +02:00
|
|
|
ErrorLocs[ErrCount]=MAXPAR-Root; // Location of error.
|
|
|
|
|
|
|
|
// Calculate the denominator for every error location.
|
|
|
|
Dnm[ErrCount]=0;
|
2009-11-10 16:48:42 +01:00
|
|
|
for (int I=1;I<ParSize+1;I+=2)
|
2020-07-15 00:40:26 +02:00
|
|
|
Dnm[ErrCount]^= gfMult(ELPol[I],gfExp[Root*(I-1)%MAXPAR]);
|
|
|
|
|
|
|
|
ErrCount++;
|
2009-11-10 16:48:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-15 00:40:26 +02:00
|
|
|
int EEPol[MAXPOL]; // Error Evaluator Polynomial.
|
|
|
|
pnMult(ELPol,SynData,EEPol);
|
|
|
|
// If errors are present and their number is correctable.
|
2009-11-10 16:48:42 +01:00
|
|
|
if ((ErrCount<=ParSize) && ErrCount>0)
|
|
|
|
for (int I=0;I<ErrCount;I++)
|
|
|
|
{
|
|
|
|
int Loc=ErrorLocs[I],DLoc=MAXPAR-Loc,N=0;
|
|
|
|
for (int J=0;J<ParSize;J++)
|
2020-07-15 00:40:26 +02:00
|
|
|
N^=gfMult(EEPol[J],gfExp[DLoc*J%MAXPAR]);
|
2009-11-10 16:48:42 +01:00
|
|
|
int DataPos=DataSize-Loc-1;
|
2020-07-15 00:40:26 +02:00
|
|
|
// Perform bounds check and correct the data error.
|
2009-11-10 16:48:42 +01:00
|
|
|
if (DataPos>=0 && DataPos<DataSize)
|
2020-07-15 00:40:26 +02:00
|
|
|
Data[DataPos]^=gfMult(N,gfExp[MAXPAR-gfLog[Dnm[I]]]);
|
2009-11-10 16:48:42 +01:00
|
|
|
}
|
2020-07-15 00:40:26 +02:00
|
|
|
return(ErrCount<=ParSize); // Return true if success.
|
2009-11-10 16:48:42 +01:00
|
|
|
}
|