/* polynomial multiplication */ void mult_polys (int dst[], int p1[], int p2[]) { int i, j; int tmp1[MAXDEG*2]; for (i=0; i < (MAXDEG*2); i++) dst[i] = 0; for (i = 0; i < MAXDEG; i++) { for(j=MAXDEG; j<(MAXDEG*2); j++) tmp1[j]=0; /* scale tmp1 by p1[i] */ for(j=0; j<MAXDEG; j++) tmp1[j]=gmult(p2[j], p1[i]); /* and mult (shift) tmp1 right by i */ for (j = (MAXDEG*2)-1; j >= i; j--) tmp1[j] = tmp1[j-i]; for (j = 0; j < i; j++) tmp1[j] = 0; /* add into partial product */ for(j=0; j < (MAXDEG*2); j++) dst[j] ^= tmp1[j]; } }
BOOL CReedSolomon::Decode (unsigned char * szData, int iDataSize, unsigned char * szCorrectData, int *iCorrectDataSize) { int sum; int nz = 0; int erasures[256]; int nerasures = 0; int iRet; for (int i = 0; i < MAX_LENGTH; i ++) { m_synBytes [i] = 0; } for (int j = 0; j < m_iCorrectCodeSize; j ++) { sum = 0; for (int i = 0; i < iDataSize; i ++) { sum = szData [i] ^ gmult (byExpToInt [j + 1], sum); } m_synBytes [j] = sum; } for (int i = 0; i < m_iCorrectCodeSize; i ++) { if (m_synBytes [i] != 0) { nz = 1; } } if (nz == 0) { memcpy (szCorrectData, szData, iDataSize); *iCorrectDataSize = iDataSize - m_iCorrectCodeSize; return TRUE; } iRet = correct_errors_erasures (szData, iDataSize, nerasures, erasures); if (iRet == 0) { return FALSE; } memcpy (szCorrectData, szData, iDataSize - m_iCorrectCodeSize); *iCorrectDataSize = iDataSize - m_iCorrectCodeSize; return TRUE; }
void mult_polys (unsigned short dst[], unsigned short p1[], unsigned short p2[]) { int i, j; int tmp1[MAXDEG*2]; for (i=0; i < (MAXDEG*2); i++) dst[i] = 0; for (i = 0; i < MAXDEG; i++) { for(j=MAXDEG; j<(MAXDEG*2); j++) tmp1[j]=0; for(j=0; j<MAXDEG; j++) tmp1[j]=gmult(p2[j], p1[i]); for (j = (MAXDEG*2)-1; j >= i; j--) tmp1[j] = tmp1[j-i]; for (j = 0; j < i; j++) tmp1[j] = 0; for(j=0; j < (MAXDEG*2); j++) dst[j] ^= tmp1[j]; } }
// Create a generator polynomial for an n byte RS code. void init_genpoly_cache(int par) { int i; unsigned char tp[256], tp1[256]; int tmp; unsigned char *genpoly = genpoly_cache[par-1]; //multiply (x + a^n) for n = 1 to nbytes zero_poly(tp1, par*2); tp1[0] = 1; for (i = 1; i <= par; i++) { zero_poly(tp, par*2); tp[0] = gexp[i]; // set up x+a^n tp[1] = 1; mult_polys(genpoly, tp, tp1, par*2); copy_poly(tp1, genpoly, par*2); } for(i=0; i<par/2; i++) { tmp = genpoly[i] ; genpoly[i] = genpoly[par-1-i]; genpoly[par-1-i]=tmp; } for(i=0; i<256; i++) { for(int j=0; j<23; j++) genpoly_23_mult_cache[i][j] = gmult(i, genpoly_cache[22][j]); genpoly_23_mult_cache[i][23] = 0; } }
void CReedSolomon::scale_poly (int k, int poly []) { for (int i = 0; i < MAX_LENGTH; i ++) { poly [i] = gmult (k, poly [i]); } }
/* * Multiplication of 4 byte words * m(x) = x4+1 */ void coef_mult(uint8_t *a, uint8_t *b, uint8_t *d) { d[0] = gmult(a[0],b[0])^gmult(a[3],b[1])^gmult(a[2],b[2])^gmult(a[1],b[3]); d[1] = gmult(a[1],b[0])^gmult(a[0],b[1])^gmult(a[3],b[2])^gmult(a[2],b[3]); d[2] = gmult(a[2],b[0])^gmult(a[1],b[1])^gmult(a[0],b[2])^gmult(a[3],b[3]); d[3] = gmult(a[3],b[0])^gmult(a[2],b[1])^gmult(a[1],b[2])^gmult(a[0],b[3]); }
void scale_poly (int k, int poly[]) { int i; for (i = 0; i < MAXDEG; i++) poly[i] = gmult(k, poly[i]); }
void scale_poly (int k, unsigned char *poly, int size) { int i; for (i = 0; i < size; i++) poly[i] = gmult(k, poly[i]); }