|
/*
Single error correcting (255,253) systematic Reed Solomon Code (over GF(256))
Encodes 253 bytes of data to the block of 255 bytes.
This perfect code can correct one byte error per block,
but can't detect more than one error.
(c)VLV
vlv@writeme.com
http://www.geocities.com/SiliconValley/4795
*/
#define BLOCKSIZE 255 //------- Get R check symbol ------------ unsigned char Get_R(unsigned char *block) r=0; //------- Get S check symbol ------------ unsigned char Get_S(unsigned char *block) //------ Encode block of data ------------- void Encode(unsigned char *block) //----- Decode block of data (correct up to one error) ------- unsigned char Decode(unsigned char *block) // Calculate the syndromes R and S sr=block[BLOCKSIZE-2]^(r=Get_R(block)); if((!sr)&&(!ss)) return 0; // No errors if((sr)&&(!ss)) // Error in R if((!sr)&&(ss)) // Error in S // Error in data - calculate the position i=0; // Field element to error location i=(i>>3) + ((i&7)<<5) - 2; // Correct the error block[i]^=ss; return 1; //------- Example ------------------- for(i=0;i void main(void) // Fill the block with the data to be protected for(i=0;i Encode(block); printf("\n\n Initial coded block:"); while(!(e=rand())); // Random error value, not equal to 0 block[pos]^=e; printf("\n\n Corrupted block (error=%02x,position=%02x):",e,pos); printf("\n\n Corrected block:");
E-mail:
info@telesys.ru
#include
#include
#define POLYNOM 0x1d // Primitive polynomial over GF(256)
{
unsigned char i,j,r,b;
for(i=0;i
b=block[i];
for(j=0;j<8;j++)
{
if(r&0x80) r=(r<<1)^POLYNOM;
else r<<=1;
if(b&0x80) r^=POLYNOM;
b<<=1;
}
}
return r;
}
{
unsigned char i,s;
s=0;
for(i=0;i
}
{
block[BLOCKSIZE-2]=Get_R(block);
block[BLOCKSIZE-1]=Get_S(block);
}
//------ Returns the amount of errors (0,1) ------------------
{
unsigned char r,s,sr,ss,i;
ss=block[BLOCKSIZE-1]^(s=Get_S(block));
{
block[BLOCKSIZE-2]=r;
return 1;
}
{
block[BLOCKSIZE-1]=s;
return 1;
}
while(sr!=ss)
{
if(sr&0x80) sr=(sr<<1)^POLYNOM;
else sr<<=1;
i++;
}
}
void PrintData(unsigned char *block)
{
unsigned char i;
if(!(i&0x07)) printf("\n");
printf("0x%02x,",block[i]);
}
}
{
unsigned char i,pos,e;
unsigned char block[BLOCKSIZE];
PrintData(block);
// Corrupt block with random error at random position
while(0xFF==(pos=rand())); // Random error position, not equal to 255
PrintData(block);
Decode(block);
PrintData(block);
}
Ответы