www.pudn.com > TestRS.rar > EnDe_Code.cpp


// EnDe_Code.cpp: implementation of the CEnDe_Code class. 
// 
////////////////////////////////////////////////////////////////////// 
 
#include "stdafx.h" 
#include "EnDe_Code.h" 
#include "stdio.h" 
 
#if (KK>=NN) 
#error "kk must b less than 2**MM-1" 
#endif 
 
typedef int gf; 
 
#if(MM==2)  //admittedly silly*/ 
int Pp[MM+1]={1,1,1}; 
 
#elif(MM==3) 
/*1+x+x^3*/ 
int Pp[MM+1]={1,1,0,1}; 
 
#elif(MM==4) 
/*1+x+x^4*/ 
int Pp[MM+1]={1,1,0,0,1}; 
 
#elif(MM==5) 
int Pp[MM+1]={1,0,1,0,0,1}; 
 
#elif(MM==6) 
int Pp[MM+1]={1,1,0,0,0,0,1}; 
 
#elif(MM==7) 
int Pp[MM+1]={1,0,0,1,0,0,0,1}; 
 
#elif(MM==8) 
/*1+x^2+x^3+x^4+x^8*/ 
int Pp[MM+1]={1,1,1,0,0,0,0,1,1}; 
 
#elif(MM==9) 
int Pp[MM+1]={1,0,0,0,1,0,0,0,0,1}; 
 
#elif(MM==10) 
int Pp[MM+1]={1,0,0,1,0,0,0,0,0,0,1}; 
 
#elif(MM==11) 
int Pp[MM+1]={1,0,1,0,0,0,0,0,0,0,0,1}; 
 
#elif(MM==12) 
int Pp[MM+1]={1,1,0,0,1,0,1,0,0,0,0,0,1}; 
 
#elif(MM==13) 
int Pp[MM+1]={1,1,0,1,1,0,0,0,0,0,0,0,0,1}; 
 
#elif(MM==14) 
int Pp[MM+1]={1,1,0,0,0,0,1,0,0,0,1,0,0,0,1}; 
 
#elif(MM==15) 
int Pp[MM+1]={1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1}; 
 
#elif(MM==16) 
int Pp[MM+1]={1,1,0,1,0,0,0,0,0,0,0,0,1,0,0,0,1}; 
 
#else  
#error "MM must be in range 2-16" 
#endif 
/*alpha exponent for the first root of the generator polynomial */ 
#define B0 112 
 
gf Alpha_to[NN+1]; 
 
gf Index_of[NN+1]; 
 
#define A0 (NN) 
 
gf Gg[NN-KK+1]; 
 
static inline gf; 
 
modnn(int x) 
{ 
	while(x>=NN){ 
		x-=NN; 
		x=(x>>MM)+(x&NN); 
	} 
	return x; 
} 
 
#define CLEAR(a,n) {\ 
	int ci;\ 
	for(ci=(n)-1;ci>=0;ci--)\ 
	   (a)[ci]=0;\ 
} 
 
#define COPY(a,b,n) {\ 
	int ci;\ 
	for(ci=(n)-1;ci>=0;ci--)\ 
	    (a)[ci]=(b)[ci];\ 
} 
 
#define COPYDOWN(a,b,n) {\ 
	int ci;\ 
	for(ci=(n)-1;ci>=0;ci--)\ 
	  (a)[ci]=(b)[ci];\ 
} 
 
//#define min(a,b) ((a)<(b)?(a):(b)) 
 
////////////////////////////////////////////////////////////////////// 
// Construction/Destruction 
//////////////////////////////////////////////////////////////////////+++++++++ 
 
CEnDe_Code::CEnDe_Code() 
{ 
	 
 
} 
 
CEnDe_Code::~CEnDe_Code() 
{ 
 
} 
 
void CEnDe_Code::encode(BYTE * data) 
{ 
	init_rs(); 
	encode_rs(&data[0],&data[KK]); //RS encodeing 
 
} 
 
void CEnDe_Code::decode(BYTE * data) 
{ 
	int eras_pos[NN-KK]; 
	int no_eras=0; 
	for(int i=0;i>=1; 
	for(i=MM+1;i=mask) 
			Alpha_to[i]=Alpha_to[MM]^((Alpha_to[i-1]^mask)<<1); 
		else 
			Alpha_to[i]=Alpha_to[i-1]<<1; 
		Index_of[Alpha_to[i]]=i; 
 
	} 
	Index_of[0]=A0; 
	Alpha_to[NN]=0; 
	 
	 
/*	for(i=0;i0;j--) 
			if(Gg[j]!=0) 
				Gg[j]=Gg[j-1]^Alpha_to[modnn((Index_of[Gg[j]])+11*(B0+i-1))]; 
			else 
				Gg[j]=Gg[j-1]; 
		Gg[0]=Alpha_to[modnn((Index_of[Gg[0]])+11*(B0+i-1))]; 
	} 
 
	for(i=0;i<=NN-KK;i++) 
	{ 
		Gg[i]=Index_of[Gg[i]]; 
	} 
 
 
} 
 
int CEnDe_Code::encode_rs(dtype data[ ], dtype bb[ ]) 
{ 
  register int i,j; 
  gf feedback; 
 
  CLEAR(bb,NN-KK); 
  for(i=0;i<=KK-1;i++) 
  { 
#if(MM!=8) 
	  if(data[i]>NN) 
		  return -1;//illigal symbl  
#endif 
	  feedback=Index_of[data[i]^bb[0]]; 
	  if(feedback!=A0){ 
		  for(j=1;j<=NN-KK-1;j++) 
			  if(Gg[j]!=A0) 
				  bb[j-1]=bb[j]^Alpha_to[modnn(Gg[j]+feedback)]; 
			  else 
				  bb[j-1]=bb[j]; 
		  bb[NN-KK-1]=Alpha_to[modnn(Gg[NN-KK]+feedback)]; 
	  } 
	  else 
	  { 
		  for(j=1;j=0;i--)  
	{ 
#if (MM!=8) 
		if(data[i]>NN) 
			return -1; 
#endif 
		recd[i]=Index_of[data[i]]; 
	} 
 
	syn_error=0; 
	for(i=1;i<=NN-KK;i++) 
	{ 
		tmp=0; 
		for(j=0;j0){ 
		//AfxMessageBox("ok"); 
		lambda[1]=Alpha_to[eras_pos[0]]; 
		for(i=1;i0;j--){ 
				tmp=Index_of[lambda[j-1]]; 
				if(tmp!=A0) 
					lambda[j]^=Alpha_to[modnn(u+tmp)]; 
			} 
		} 
 
#ifdef ERASURE_DEBUG 
		 
		for(i=1;i<=no_eras;i++) 
			reg[i]=Index_of[lambda[i]]; 
		count=0; 
		for(i=1;i<=NN;i++){ 
			q=1; 
			for(j=1;j<=no_eras;j++) 
				if(reg[j]!=A0){ 
					reg[j]=modnn(reg[j]+j); 
					q^=Alpha_to[reg[j]]; 
				} 
				if(!q){ 
					root[count]=i; 
					loc[count]=NN-i; 
					count++; 
				} 
		} 
		if(count!=no_eras){ 
			AfxMessageBox("lambda(x) is Wrong!"); 
			return -1; 
		} 
#ifndef NO_PRINT 
		 msg="Erasure positions as determined by roots of Eras Loc Poly:\n"; 
         
		for(i=0;i0;j--) 
			if(reg[j]!=A0){ 
				reg[j]=modnn(reg[j]+11*j); 
				q^=Alpha_to[reg[j]]; 
			} 
		if(!q){ 
			root[count]=i; 
		    loc[count]=NN-i; 
			count++; 
		} 
	} 
 
#ifdef DEBUG 
	msg="Final error position :\n"; 
     
	for(i=0;i=0;j--){ 
			if((s[i+1-j]!=A0)&&(lambda[j]!=A0)) 
				tmp^=Alpha_to[modnn(s[i+1-j]+lambda[j])]; 
		} 
		if(tmp!=0) 
			deg_omega=i; 
		omega[i]=Index_of[tmp]; 
	} 
	omega[NN-KK]=A0; 
     
	for(j=count-1;j>=0;j--) 
	{ 
		num1=0; 
		for(i=deg_omega;i>=0;i--){ 
			if(omega[i]!=A0) 
				num1^=Alpha_to[modnn(omega[i]+11*i*root[j])]; 
		} 
		num2=Alpha_to[modnn(root[j]*11*(B0-1)+NN)]; 
		den=0; 
 
		for(i=min(deg_lambda,NN-KK-1)&~1;i>=0;i-=2){ 
			if(lambda[i+1]!=A0) 
				den^=Alpha_to[modnn(lambda[i+1]+11*i*root[j])]; 
		} 
		if(den==0){ 
 
#ifdef DEBUG 
	   msg="Error:denominator=0\n"; 
       AfxMessageBox(msg); 
#endif 
	   return -1; 
		}		 
		if(num1!=0) 
		{ 
            bCorrect=TRUE;			 
			data[loc[j]]^=Alpha_to[modnn(Index_of[num1]+Index_of[num2]+NN-Index_of[den])]; 
		} 
	} 
    if(bCorrect) 
		AfxMessageBox("Correct finished!");  
	 
	 
	return count;	 
 
}