无论是否可能,动态计算CRC



是否可以动态计算CRC(在流中(?

例如,我有1GB的数据,我想减少未检测到错误的可能性。我想在整个文件上实现一些东西(CRC或Hash(,(我已经为每个块实现了CRC,其中包含一些数据包(,

当我们将CRC放在整个文件上时,是否可以在收到第一个数据包后立即开始计算CRC,还是必须等待整个文件被接收,然后开始计算CRC?

是。CRC和我所知道的每一个散列都是可流式传输的。它们有一个小的、有限的状态,随着数据的输入而更新。对于CRC,状态就是CRC本身。

zlib中的CRC采用以下形式:

unsigned long crc32(unsigned long crc, const unsigned char *buf, unsigned len);

bufNULL时,返回初始CRC。所以它是这样使用的:

unsigned long crc = crc32(0, NULL, 0);    // initial CRC
for (...) {                               // some sort of loop
...                                   // generating a chunk of data
crc = crc32(crc, buf, len);           // update the CRC with the data
...                                   // this all gets repeated many times
}
...                                       // loop is done, crc has the CRC

最新更新