对于我的项目,我需要反转基于表的 CRC16,因为我必须在不更改 CRC 的情况下更改一些数据。
我的问题是反转查找表。
我找到了一些其他解决方案来反转查找表,但不适用于我的 CRC 表。
#define SIZE_OF_CRC_TABLE 256u
static const uint16_t crc_polynom = 0x1021;
void Compute_CRCTables(uint16_t *CRCTable_forward, uint16_t *CRCTable_reverse) {
uint16_t result_forward, result_reverse;
int i, j;
for (i = 0; i < SIZE_OF_CRC_TABLE; i++) {
result_forward = (uint16_t) i << 8;
result_reverse = i;
for (j = 0; j < 8; j++) {
/* ---- forward ---- */
if (result_forward & 0x8000) {
result_forward = (result_forward <<= 1) ^ crc_polynom;
}
else {
result_forward = result_forward <<= 1;
}
/* ---- reverse --- */
if ((result_reverse & 0x1) != 0) {
result_reverse = ((result_reverse ^ crc_polynom) >> 1) | 1;
}
else {
result_reverse = result_reverse >>= 1;
}
}
CRCTable_forward[i] = result_forward;
CRCTable_reverse[i] = result_reverse;
}
}
CRC 表:
0x 0u, 0x1021u, 0x2042u, 0x3063u, 0x4084u, 0x50A5u, 0x60C6u, 0x70E7u,
0x8108u, 0x9129u, 0xA14Au, 0xB16Bu, 0xC18Cu, 0xD1ADu, 0xE1CEu, 0xF1EFu,
0x1231u, 0x 210u, 0x3273u, 0x2252u, 0x52B5u, 0x4294u, 0x72F7u, 0x62D6u,
0x9339u, 0x8318u, 0xB37Bu, 0xA35Au, 0xD3BDu, 0xC39Cu, 0xF3FFu, 0xE3DEu,
0x2462u, 0x3443u, 0x 420u, 0x1401u, 0x64E6u, 0x74C7u, 0x44A4u, 0x5485u,
0xA56Au, 0xB54Bu, 0x8528u, 0x9509u, 0xE5EEu, 0xF5CFu, 0xC5ACu, 0xD58Du,
0x3653u, 0x2672u, 0x1611u, 0x 630u, 0x76D7u, 0x66F6u, 0x5695u, 0x46B4u,
0xB75Bu, 0xA77Au, 0x9719u, 0x8738u, 0xF7DFu, 0xE7FEu, 0xD79Du, 0xC7BCu,
0x48C4u, 0x58E5u, 0x6886u, 0x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x FEFu, 0x FFFu, 0x FEFu, 0x FDFu, 0x FEFu, 0x FFFu, 0x FEFu,
0x C19u, 0x FEFu, 0x FFFu, 0x FEFu, 0x FDFu, 0x FEFu, 0x FFFu, 0x FEFu,
0x F9Fu, 0x FEFu, 0x FFFu, 0x FEFu, 0x FDFu, 0x FEFu, 0x FFFu, 0x FEFu,
0x F1Fu, 0x FEFu, 0x FFFu, 0x FEFu, 0x FDFu, 0x FEFu, 0x FFFu, 0x FEFu,
0x F9Fu, 0x FEFu, 0x FFFu, 0x FEFu, 0x FDFu, 0x FEFu, 0x FFFu, 0x FEFu,
0x E1Du, 0x FEFu, 0x FFFu, 0x FEFu, 0x FDFu, 0x FEFu, 0x FFFu, 0x FEFu,
0x F9Fu, 0x FEFu, 0x FFFu, 0x FEFu, 0x FDFu, 0x FEFu, 0x FFFu, 0x FEFu,
0x F1Fu, 0x FEFu, 0x FFFu, 0x FEFu, 0x FDFu, 0x FEFu, 0x FFFu, 0x FEFu,
0x F9Fu, 0x FEFu, 0x FFFu, 0x FEFu, 0x FDFu, 0x FEFu, 0x FFFu, 0x FEFu,
0x 811u, 0x FEFu, 0x FFFu, 0x FEFu, 0x FDFu, 0x FEFu, 0x FFFu, 0x FEFu,
0x F9Fu, 0x FEFu, 0x FFFu, 0x FEFu, 0x FDFu, 0x FEFu, 0x FFFu, 0x FEFu,
0x F1Fu, 0x FEFu, 0x FFFu, 0x FEFu, 0x FDFu, 0x FEFu, 0x FFFu, 0x FEFu,
0x F9Fu, 0x FEFu, 0x FFFu, 0x FEFu, 0x FDFu, 0x FEFu, 0x FFFu, 0x FEFu,
0x E1Du, 0x FEFu, 0x FFFu, 0x FEFu, 0x FDFu, 0x FEFu, 0x FFFu, 0x FEFu,
0x F9Fu, 0x FEFu, 0x FFFu, 0x FEFu, 0x FDFu, 0x FEFu, 0x FFFu, 0x FEFu,
0x F1Fu, 0x FEFu, 0x FFFu, 0x FEFu, 0x FDFu, 0x FEFu, 0x FFFu, 0x FEFu,
0x F9Fu, 0x FEFu, 0x FFFu, 0x FEFu, 0x FDFu, 0x FEFu, 0x FFFu, 0x FEFu,
0x C19u, 0x FEFu, 0x FFFu, 0x FEFu, 0x FDFu, 0x FEFu, 0x FFFu, 0x FEFu,
0x F9Fu, 0x FEFu, 0x FFFu, 0x FEFu, 0x FDFu, 0x FEFu, 0x FFFu, 0x FEFu,
0x F1Fu, 0x FEFu, 0x FFFu, 0x FEFu, 0x FDFu, 0x FEFu, 0x FFFu, 0x FEFu,
0x F9Fu, 0x FEFu, 0x FFFu, 0x FEFu, 0x FDFu, 0x FEFu, 0x FFFu, 0x FEFu,
0x E1Du, 0x FEFu, 0x FFFu, 0x FEFu, 0x FDFu, 0x FEFu, 0x FFFu, 0x FEFu,
0x F9Fu, 0x FEFu, 0x FFFu, 0x FEFu, 0x FDFu, 0x FEFu, 0x FFFu, 0x FEFu,
0x F1Fu, 0x FEFu, 0x FFFu, 0x FEFu, 0x FDFu, 0x FEFu, 0x FFFu, 0x FEFu,
0x F9Fu, 0x FEFu, 0x FFFu, 0x FEFu, 0x FDFu, 0x FEFu, 0x FFFu, 0x FEFu,