如何配置CRC表的计算

时间:2015-02-22 09:45:37

标签: c++ crc crc16

那里有很多CRC计算实例。使用预先计算的表进行位移和更高效的简单实现。但是在多项式旁边还有很多CRC参数会影响计算。您可以在此处评估这些参数:http://zorc.breitbandkatze.de/crc.html

这些参数是

  • CRC的初始值
  • 输入数据的反映
  • 输出数据的反映
  • CRC的最终XOR值

对于某些“标准”CRC算法,这些参数被很好地定义,如CRC-16(CCITT)。但是有些实现使用不同的参数。

我的实现必须与具有CCITT多项式的CRC16兼容(x 16 + x 12 + x 5 + 1)。但必须反映数据字节和最终CRC。我在计算方法中实现了这些反射。但这很费时间。为了获得最佳性能,必须将其从计算中删除。

如何在初始化方法中计算CRC的反射参数?

编辑:我该如何分别控制每个参数?我想了解Init函数如何实际工作以及如何实现所有参数。

typedef unsigned char uint8_t;
typedef unsigned short crc;

crc  crcTable[256];
#define WIDTH  (8 * sizeof(crc))
#define TOPBIT (1 << (WIDTH - 1))
#define POLYNOMIAL 0x1021

template<typename t>
t reflect(t v)
{
    t r = 0;

    for (int i = 0; i < (8 * sizeof v); ++i)
    {
        r <<= 1;
        r |= v&1;
        v >>= 1;
    }

    return r;
}

void Init()
{
    crc  remainder;

    for (int dividend = 0; dividend < 256; ++dividend)
    {
        remainder = dividend << (WIDTH - 8);

        for (uint8_t bit = 8; bit > 0; --bit)
        {
            if (remainder & TOPBIT)
                remainder = (remainder << 1) ^ POLYNOMIAL;
            else
                remainder = (remainder << 1);
        }

        crcTable[dividend] = remainder;
    }
}

crc Calculate(const uint8_t *message, int nBytes, crc wOldCRC)
{
    uint8_t data;
    crc remainder = wOldCRC;

    for (int byte = 0; byte < nBytes; ++byte)
    {
        data = reflect(message[byte]) ^ (remainder >> (WIDTH - 8));
        remainder = crcTable[data] ^ (remainder << 8);
    }

    return reflect(remainder);
}

int main()
{
    crc expected = 0x6f91;
    uint8_t pattern[] = "123456789";

    Init();
    crc result = Calculate(pattern, 9, 0xFFFF);

    if (result != expected)
    {
        // this output is not relevant to the question, despite C++ tag
        printf("CRC 0x%04x wrong, expected 0x%04x\n", result, expected);
    }
}

1 个答案:

答案 0 :(得分:4)

您只需反映多项式和操作,而不是反映进入的数据,CRC进入,CRC出现。在编写代码时,您只需要执行一次。反射多项式为0x8408

typedef unsigned char uint8_t;
typedef unsigned short crc;

crc  crcTable[256];
#define POLYNOMIAL 0x8408

void Init()
{
    crc  remainder;

    for (int dividend = 0; dividend < 256; ++dividend)
    {
        remainder = dividend;

        for (uint8_t bit = 8; bit > 0; --bit)
        {
            if (remainder & 1)
                remainder = (remainder >> 1) ^ POLYNOMIAL;
            else
                remainder = (remainder >> 1);
        }

        crcTable[dividend] = remainder;
    }
}

crc Calculate(const uint8_t *message, int nBytes, crc wOldCRC)
{
    uint8_t data;
    crc remainder = wOldCRC;

    for (int byte = 0; byte < nBytes; ++byte)
    {
        data = message[byte] ^ remainder;
        remainder = crcTable[data] ^ (remainder >> 8);
    }

    return remainder;
}

int main()
{
    crc expected = 0x6f91;
    uint8_t pattern[] = "123456789";

    Init();
    crc result = Calculate(pattern, 9, 0xFFFF);

    if (result != expected)
    {
        // this output is not relevant to the question, despite C++ tag
        printf("CRC 0x%04x wrong, expected 0x%04x\n", result, expected);
    }
}

对于一般情况,如果反映输入数据,则反映此答案中显示的多项式,输入底部的字节,检查低位以排除多项式,并向上移位。如果未反映输入数据,则按照问题中的代码执行,将多项式保留为原样,将字节输入顶部,检查高位,然后向下移动。

几乎在所有情况下,输出的反射与输入的反射相同。对于所有这些,不需要有点反向功能。如果未反映输入和输出,或者反映输入和输出,则将结果从移位寄存器中保留。只有72 CRCs catalogued at the RevEng site中的一个是与(CRC-12 / 3GPP)中的反映不同的反映。在这种情况下,您需要对输出进行反转,因为输入未反映,但输出为。

初始CRC只是移位寄存器的初始内容。在启动CRC时设置一次。最后的独占或者最后应用于移位寄存器的内容。如果你有一个一次计算一个CRC的函数,你需要应用最终的独占 - 或者在输入函数时,也应用最终的异或 - 或者用户看到的初始值,这样实际的初始值最后是移位寄存器。