📄 bch_hm.c
字号:
static const FLByte wPowerTable2[256] = {
0x0D,0x0C,0x0B,0x0A,0x09,0x08,0x07,0x19,
0x06,0xA9,0x18,0x05,0x7B,0x9C,0xB3,0xA8,
0x17,0x04,0x7A,0x9B,0xB2,0x88,0x3F,0xA7,
0x16,0x22,0x56,0xC1,0xDD,0x03,0x79,0x9A,
0xFB,0xB1,0x87,0x3E,0x93,0x11,0x5A,0xA6,
0x15,0x21,0x55,0xC0,0xA2,0xDC,0x2B,0xF3,
0x02,0x78,0x4A,0x99,0xB9,0xE2,0x6B,0xFA,
0xB0,0xC5,0x86,0x6F,0x4E,0x3D,0xCC,0x82,
0x61,0x92,0x7E,0x10,0x59,0xA5,0x14,0x64,
0xC8,0x20,0x39,0x54,0xBF,0xD6,0xA1,0x44,
0xDB,0x2A,0xF2,0x32,0x01,0x77,0x49,0x98,
0xB8,0xE1,0x8C,0x1D,0x36,0xEA,0x6A,0xF9,
0xAF,0xC4,0x25,0x5D,0xFE,0x51,0x85,0x6E,
0xBC,0x4D,0x3C,0xCB,0x81,0xED,0x60,0xD3,
0x91,0x9E,0x7D,0x0F,0xAB,0x58,0x41,0xA4,
0x2D,0xF5,0x13,0xE4,0x63,0xC7,0x71,0xCE,
0x66,0xD8,0x8E,0x1F,0x38,0x53,0xBE,0x27,
0xD5,0xEF,0x2F,0xA0,0x43,0xDA,0xE6,0xD0,
0x29,0xF1,0x31,0x00,0x76,0x48,0x97,0xB7,
0x75,0x96,0xE0,0x8B,0xB6,0x1C,0x47,0x35,
0xE9,0x69,0x74,0xF8,0xAE,0xC3,0x24,0xDF,
0x8A,0xB5,0x1B,0x5C,0x95,0xFD,0x50,0x84,
0x6D,0xBB,0x4C,0x46,0x34,0x3B,0xCA,0x80,
0xEC,0x5F,0x68,0x73,0xF7,0xAD,0xD2,0xE8,
0x90,0x9D,0x7C,0xB4,0x0E,0x1A,0xAA,0x23,
0x57,0xC2,0xDE,0x40,0x89,0x94,0xFC,0xA3,
0x2C,0xF4,0x12,0x5B,0xE3,0x6C,0x4B,0xBA,
0x62,0xC6,0x4F,0x70,0x83,0xCD,0x65,0xC9,
0x7F,0x45,0x33,0x3A,0xD7,0xEB,0x8D,0x1E,
0x37,0x52,0xBD,0x26,0x5E,0xFF,0xD4,0xEE,
0x2E,0xF6,0x9F,0xAC,0x42,0x67,0xD9,0xE5,
0xCF,0x72,0x28,0xF0,0x8F,0x30,0xE7,0xD1,
} ;
/* Table of alfa ^ -jm */
static const FLWord wAlfajmNeg[64] = {
0x2000,0x3917,0x2AC5,0x3AB1,0x2129,0x09A5,0x253B,0x110A,
0x1071,0x3150,0x3DAB,0x25CD,0x23BF,0x157E,0x0426,0x05C4,
0x3791,0x39E7,0x2B00,0x068B,0x1A3D,0x2B3E,0x2F61,0x32A8,
0x2025,0x006A7,0x8B4,0x0F13,0x0F54,0x3EB3,0x333A,0x2744,
0x2B91,0x10A1,0x25ED,0x36C7,0x2228,0x38D7,0x3C37,0x1482,
0x0AE1,0x1471,0x129C,0x0459,0x2EB9,0x2BB8,0x28E0,0x14E7,
0x1FA1,0x39BD,0x3FCD,0x38D3,0x3E98,0x3AA4,0x015D,0x0729,
0x0298,0x2AF5,0x2575,0x0DD4,0x1BA4,0x30B9,0x1318,0x3AF4,
} ;
/* ------------------------------------------------------------------------- */
/* Extended Hamming 1 bit correction 2 bits detection table */
static const FLByte bHammingError[256] = {
0xFF,0x78,0x79,0xFF,0x7A,0xFF,0xFF,0x42,
0x7B,0xFF,0xFF,0x21,0xFF,0x5C,0x43,0xFF,
0x7C,0xFF,0xFF,0x10,0xFF,0x0C,0x22,0xFF,
0xFF,0x39,0x5D,0xFF,0x44,0xFF,0xFF,0x4D,
0x7D,0xFF,0xFF,0x26,0xFF,0x52,0x11,0xFF,
0xFF,0x1E,0x0D,0xFF,0x23,0xFF,0xFF,0x2B,
0xFF,0x6A,0x3A,0xFF,0x5E,0xFF,0xFF,0x19,
0x45,0xFF,0xFF,0x2E,0xFF,0x08,0x4E,0xFF,
0x7E,0xFF,0xFF,0x01,0xFF,0x49,0x27,0xFF,
0xFF,0x63,0x53,0xFF,0x12,0xFF,0xFF,0x03,
0xFF,0x40,0x1F,0xFF,0x0E,0xFF,0xFF,0x4B,
0x24,0xFF,0xFF,0x29,0xFF,0x17,0x2C,0xFF,
0xFF,0x71,0x6B,0xFF,0x3B,0xFF,0xFF,0x65,
0x5F,0xFF,0xFF,0x55,0xFF,0x35,0x1A,0xFF,
0x46,0xFF,0xFF,0x14,0xFF,0x32,0x2F,0xFF,
0xFF,0x59,0x9,0xFF,0x04F,0xFF,0xFF,0x05,
0x7F,0xFF,0xFF,0x48,0xFF,0x62,0x02,0xFF,
0xFF,0x3F,0x4A,0xFF,0x28,0xFF,0xFF,0x16,
0xFF,0x70,0x64,0xFF,0x54,0xFF,0xFF,0x34,
0x13,0xFF,0xFF,0x31,0xFF,0x58,0x04,0xFF,
0xFF,0x77,0x41,0xFF,0x20,0xFF,0xFF,0x5B,
0x0F,0xFF,0xFF,0x0B,0xFF,0x38,0x4C,0xFF,
0x25,0xFF,0xFF,0x51,0xFF,0x1D,0x2A,0xFF,
0xFF,0x69,0x18,0xFF,0x2D,0xFF,0xFF,0x07,
0xFF,0xFF,0x72,0xFF,0x6C,0xFF,0xFF,0x73,
0x3C,0xFF,0xFF,0x6D,0xFF,0x74,0x66,0xFF,
0x60,0xFF,0xFF,0x3D,0xFF,0x6E,0x56,0xFF,
0xFF,0x75,0x36,0xFF,0x1B,0xFF,0xFF,0x67,
0x47,0xFF,0xFF,0x61,0xFF,0x3E,0x15,0xFF,
0xFF,0x6F,0x33,0xFF,0x30,0xFF,0xFF,0x57,
0xFF,0x76,0x5A,0xFF,0x0A,0xFF,0xFF,0x37,
0x50,0xFF,0xFF,0x1C,0xFF,0x68,0x06,0xFF,
} ;
/* ------------------------------------------------------------------------- */
/* Hamming multiplications of g(X) */
FLByte HammingTable[256] = {
0x0,0x91,0x61,0xF0,0xC2,0x53,0xA3,0x32,0xC7,0x56,0xA6,0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xBB,0x4E,0xDF,0x2F,0xBE,0x8C,0x1D,0xED,0x7C,
0x44,0xD5,0x25,0xB4,0x86,0x17,0xE7,0x76,0x83,0x12,0xE2,0x73,0x41,0xD0,0x20,0xB1,
} ;
/* ------------------------------------------------------------------------- */
static const FLWord wAlpha2Table[4] =
{
0x0000,0x1844,0x3088,0x28cc
};
static const FLWord wAlpha4Table[16] =
{
0x0000,0x0611,0x0c22,0x0a33,0x1844,0x1e55,0x1466,0x1277,
0x3088,0x3699,0x3caa,0x3abb,0x28cc,0x2edd,0x24ee,0x22ff
};
static const FLWord wW_2Erros[13] =
{
0x1295,0x1ab4,0x242d,0x0154,0x12ad,0x29f6,0x3c43,0x38e8,
0x08dd,0x2cfc,0x30e5,0x35be,0x0aab
};
static const FLWord wSqrtTable[14] = {
0x2000,0x2540,0x1000,0x12A0,0x0800,0x0950,0x0400,0x04A8,
0x0200,0x0254,0x0100,0x012A,0x0080,0x0095
};
/* ------------------------- Internal routine declaration ----------------- */
static FLSNative ECC_BCH_Mul(FLSNative nsa, FLSNative nsb);
/* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */
/* BCH Arithmetical routines */
/* The following routines implement GF(2^14) arithmetics */
/* Algorithms and programming were written by : */
/* */
/* Itai Dror */
/* */
/* Fortress Security Division, Omer */
/* M - Systems, Flash Disk Pioneers */
/* email: itaid@m-sys.com */
/* */
/* version 1.0, Feb. 2, 2002 */
/* */
/* All finite field elements are stored and treated in big endian format */
/* */
/* For example: */
/* */
/* 0x3088 represents: 1 + X + X ^ 6 + X ^ 10 + X ^ 14 */
/* */
/* Coefficient of X ^ 14 is an hidden bit */
/* */
/* ------------------------------------------------------------------------- */
/* */
/* function: */
/* */
/* FLSNative ECC_BCH_DivSyndromebyFi(FLByte bS[], FLWord wFiTable[]) */
/* */
/* description: Divide syndrome by minimal polynomial by implementing fast */
/* division with the help of a multiplication table of the */
/* modulo */
/* */
/* Performance is accelerated by flat coding the function */
/* */
/* Input: */
/* */
/* bS[] - The BCH 56 bits, 7 bytes syndrome */
/* FiTable[] - Multiplication table of the minimal polynomial */
/* */
/* returned value - */
/* */
/* nsbi = bS mod Fi(i), a 14 bits GF(2^14) polynomial */
/* */
/* nsbi for 4 errors BCH code can be b1, b3, b5 or b7 */
/* */
static FLSNative ECC_BCH_DivSyndromebyFi(const FLByte bS[], const FLWord wFiTable[])
{
FLNative nb = 0 ; /* Zero result */
FLSNative nsind = bS[0] ; /* Initial value of index */
/* Division is implemented by a table and an index */
/* Iterate 5 times */
nb = ( nb >> 8 ) ^ wFiTable[nsind] ; /* First divisor byte is bS[0] */
nsind = ( FLByte ) nb ^ bS[1] ;
nb = ( nb >> 8 ) ^ wFiTable[nsind] ; /* Second divisor byte is bS[1] */
nsind = ( FLByte ) nb ^ bS[2] ; /* 3'rd divisor byte is bS[2] */
nb = ( nb >> 8 ) ^ wFiTable[nsind] ;
nsind = ( FLByte ) nb ^ bS[3] ; /* 4'th divisor byte is bS[3] */
nb = ( nb >> 8 ) ^ wFiTable[nsind] ;
nsind = ( FLByte ) nb ^ bS[4] ; /* 5'th divisor byte is bS[4] */
nb = ( nb >> 8 ) ^ wFiTable[nsind] ;
/* Consider last 2 bytes */
nb ^= ( bS[6] << 8 ) ^ bS[5] ;
nsind = ( nb << 6 ) & 0xc0 ;
nb = ( nb >> 2 ) ^ wFiTable[nsind] ;
return (FLSNative) nb ;
}
/* ------------------------------------------------------------------------- */
/* */
/* function: */
/* */
/* FLNative ECC_BCH_EvalSyndromeComponent(FLNative b, FLSNative AlfaPowers[])*/
/* */
/* description: */
/* */
/* Fast evaluation of a syndrome component by use of precalculated constants */
/* */
/* Perfomance in accelerated by flat coding the function */
/* */
/* Input: */
/* */
/* nsb - A 14 bits polynomial in GF(2^14) */
/* AlfaPowers[] - An array that contains subsequent powers of alfa ^ k */
/* */
/* for example, */
/* */
/* AlfaPowers[2] contains: alfa^2, alfa^4, alfa^6, ..., alfa^26 */
/* AlfaPowers[5] contains: alfa^5, alfa^10, ...........,alfa^65 */
/* */
/* */
/* */
/* returned value - */
/* */
/* Sj = bi(alfa^j), A syndrome component which is a finite field elememt */
/* */
static FLSNative ECC_BCH_EvalSyndromeComponent(FLSNative nsb, const FLWord wAlfaPowers[])
{
FLSNative nsS = 0 ; /* Zero syndrome component */
if ( nsb & 0x2000 ) nsS = 0x2000 ;
/* If MSbit of nsb is 1 then S = 1 in Big Endian notation */
/* if '1' then nsS = nsS + alfa ^ j */
if ( nsb & 0x1000 ) nsS ^= wAlfaPowers[0] ;
/* if '1' then nsS = nsS + alfa ^ 2j */
if ( nsb & 0x0800 ) nsS ^= wAlfaPowers[1] ;
/* if '1' then nsS = nsS + alfa ^ 3j */
if ( nsb & 0x0400 ) nsS ^= wAlfaPowers[2] ;
/* if '1' then nsS = nsS + alfa ^ 4j */
if ( nsb & 0x0200 ) nsS ^= wAlfaPowers[3] ;
/* if '1' then nsS = nsS + alfa ^ 5j */
if ( nsb & 0x0100 ) nsS ^= wAlfaPowers[4] ;
/* if '1' then nsS = nsS + alfa ^ 6j */
if ( nsb & 0x0080 ) nsS ^= wAlfaPowers[5] ;
/* if '1' then nsS = nsS + alfa ^ 7j */
if ( nsb & 0x0040 ) nsS ^= wAlfaPowers[6] ;
/* if '1' then nsS = nsS + alfa ^ 8j */
if ( nsb & 0x0020 ) nsS ^= wAlfaPowers[7] ;
/* if '1' then nsS = nsS + alfa ^ 9j */
if ( nsb & 0x0010 ) nsS ^= wAlfaPowers[8] ;
/* if '1' then nsS = nsS + alfa ^ 10j */
if ( nsb & 0x0008 ) nsS ^= wAlfaPowers[9] ;
/* if '1' then nsS = nsS + alfa ^ 11j */
if ( nsb & 0x0004 ) nsS ^= wAlfaPowers[10] ;
/* if '1' then nsS = nsS + alfa ^ 12j */
if ( nsb & 0x0002 ) nsS ^= wAlfaPowers[11] ;
/* if '1' then nsS = nsS + alfa ^ 13j */
if ( nsb & 0x0001 ) nsS ^= wAlfaPowers[12] ;
return nsS ; /* return the syndrome component, ( 14 bits ) */
}
/* ------------------------------------------------------------------------- */
/* */
/* function: FLSNative ECC_BCH_LogDown(FLSNative nsbeta) */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -